当前位置:网站首页>【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

Power Spectral Density Estimates Using FFT---MATLAB

How does Premiere (PR) import the preset mogrt template?

Seriation in R: How to Optimally Order Objects in a Data Matrice

The computer screen is black for no reason, and the brightness cannot be adjusted.
![[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl](/img/6a/111da81436659c7502648907ec1367.jpg)
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl

PyTorch nn.RNN 参数全解析

Seriation in R: How to Optimally Order Objects in a Data Matrice

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
随机推荐
可昇級合約的原理-DelegateCall
QT meter custom control
K-Means Clustering Visualization in R: Step By Step Guide
How to Create a Beautiful Plots in R with Summary Statistics Labels
PyTorch nn.RNN 参数全解析
Depth filter of SvO2 series
[multithreading] the main thread waits for the sub thread to finish executing, and records the way to execute and obtain the execution result (with annotated code and no pit)
Uniapp uni list item @click, uniapp uni list item jump with parameters
PX4 Position_ Control RC_ Remoter import
ESP32音频框架 ESP-ADF 添加按键外设流程代码跟踪
Time format display
Develop scalable contracts based on hardhat and openzeppelin (II)
vant tabs组件选中第一个下划线位置异常
How does Premiere (PR) import the preset mogrt template?
Easyexcel and Lombok annotations and commonly used swagger annotations
基于Hardhat编写合约测试用例
YYGH-9-预约下单
【2022 ACTF-wp】
MySQL stored procedure cursor traversal result set
How to Create a Beautiful Plots in R with Summary Statistics Labels