当前位置:网站首页>【C语言】十进制数转换成二进制数
【C语言】十进制数转换成二进制数
2022-07-02 09:42:00 【每天默默学习】
前言
OK,今天讲一个简单的代码,这个代码对于程序猿来说是简单的,但是对于学习C语言的学生来说是比较复杂的,现在分享出来,和大家一起学习一下。
一、题目讲解
题目要求是:
- 把一个[0-1023]范围内的整数转换成无符号二进制数
- 超过数值或者输入的是非整数则正常退出,否则循环转换
我直接把运行界面分享给大家,大家先看界面,应该知道代码的逻辑关系,或者说功能了,然后再分享代码:
1:输入的数值超过范围
如图所示,输入234,输出11101010;如果我们输入3333,则退出,因为3333超过了1023
2:输入的是字母或者不是字符
比如,输入23,输出10111,输入222,输出11011110,如果输入dc34,则退出
二、分析
1.代码
代码如下(示例):
我们一起来学习一下代码:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void decToBin(int n)//输入一个数15
{
int result = 0;//结果
int k = 1;
int i = 0;
while(n)//除到0就停止
{
i = n % 2; //余数i=1 //i=1 //i=1 //i=1
result = k * i + result; //result=1 //result=11 //result=111 //result=1111
k = k * 10; //k=10 //k=100 //k=1000
n = n / 2; //n=7 //n=3 //n=1 //n=0退出
}
printf("我们得到的二进制数为:%d \n",result);//1111
}
int main() {
char s[6];
char *p;
do{
printf("请输入一个[0-1023]范围内的整数:") ;
gets(s);
p=s;
int Num=1,j=0,Num_T=0,Totle=0;
j=strlen(s);
while(*p!='\0') //输入一个整型数
{
if(*p<'9'&&*p>'0'){
for(int k=1;k<j;k++)
Num=Num*10;
// printf("Num=%d\n",Num);
Num_T=(*p-'0')*Num;
//printf("%c\n",*p);
Totle=Totle+Num_T;
// printf("Totle=%d\n",Totle);
Num=1;
j--;
p++;
}
else
exit(0);
}
if(Totle>=0&&Totle<1024)
decToBin(Totle);
else
exit(0);
}while(1);
}
2.重点算法
1:十进制转二进制直接代码:
while(n)//除到0就停止
{
i = n % 2; //余数i=1 //i=1 //i=1 //i=1
result = k * i + result; //result=1 //result=11 //result=111 //result=1111
k = k * 10; //k=10 //k=100 //k=1000
n = n / 2; //n=7 //n=3 //n=1 //n=0退出
}
2.把字符转换成十进制数,如果输入的不是0-9之间的字符自动退出:。
while(*p!='\0') //输入一个整型数
{
if(*p<'9'&&*p>'0'){
for(int k=1;k<j;k++)
Num=Num*10;
// printf("Num=%d\n",Num);
Num_T=(*p-'0')*Num;
//printf("%c\n",*p);
Totle=Totle+Num_T;
// printf("Totle=%d\n",Totle);
Num=1;
j--;
p++;
}
else
exit(0);
}
3.判断如果数值在0-1023,则转换,如果不在,这里判断的是数值超过1023,就退出
if(Totle>=0&&Totle<1024)
decToBin(Totle);
else
exit(0);
总结
这里对文章进行总结:
以上就是今天要讲的内容,本文仅仅简单介绍了该程序的功能和重点算法,希望大家学习是快乐的。
边栏推荐
- From scratch, develop a web office suite (3): mouse events
- Bedtools tutorial
- Implementation of address book (file version)
- Writing contract test cases based on hardhat
- XSS labs master shooting range environment construction and 1-6 problem solving ideas
- Applet link generation
- excel表格中选中单元格出现十字带阴影的选中效果
- Log4j2
- GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
- The position of the first underline selected by the vant tabs component is abnormal
猜你喜欢
Applet link generation
How does Premiere (PR) import the preset mogrt template?
Take you ten days to easily finish the finale of go micro services (distributed transactions)
预言机链上链下调研
Data analysis - Matplotlib sample code
How to Create a Beautiful Plots in R with Summary Statistics Labels
How to Easily Create Barplots with Error Bars in R
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
动态内存(进阶四)
自然语言处理系列(二)——使用RNN搭建字符级语言模型
随机推荐
可昇級合約的原理-DelegateCall
PX4 Position_ Control RC_ Remoter import
to_bytes与from_bytes简单示例
机械臂速成小指南(七):机械臂位姿的描述方法
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
K-Means Clustering Visualization in R: Step By Step Guide
Homer forecast motif
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
xss-labs-master靶场环境搭建与1-6关解题思路
How to Create a Beautiful Plots in R with Summary Statistics Labels
Implementation of address book (file version)
How to Add P-Values onto Horizontal GGPLOTS
Log4j2
Research on and off the Oracle chain
A sharp tool for exposing data inconsistencies -- a real-time verification system
预言机链上链下调研
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
From scratch, develop a web office suite (3): mouse events
Analyse de l'industrie
XSS labs master shooting range environment construction and 1-6 problem solving ideas