当前位置:网站首页>[C language] convert decimal numbers to binary numbers
[C language] convert decimal numbers to binary numbers
2022-07-02 12:06:00 【Study silently every day】
Preface
OK, Today, let's talk about a simple code , This code is simple for program apes , But for learning C Language is more complicated for students , Share it now , Learn with you .
One 、 Topic explanation
The title requires :
- Put one [0-1023] Integers in the range are converted to unsigned binary numbers
- If it exceeds the value or the input is a non integer, it will exit normally , Otherwise, loop conversion
I will directly share the operation interface with you , Let's first look at the interface , You should know the logical relationship of the code , Or function , Then share the code :
1: The value entered is out of range
As shown in the figure , Input 234, Output 11101010; If we type 3333, The exit , because 3333 More than the 1023
2: The input is a letter or not a character
such as , Input 23, Output 10111, Input 222, Output 11011110, If input dc34, The exit
Two 、 analysis
1. Code
The code is as follows ( Example ):
Let's learn the code together :
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void decToBin(int n)// Enter a number 15
{
int result = 0;// result
int k = 1;
int i = 0;
while(n)// Divide to 0 Just stop
{
i = n % 2; // remainder 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 sign out
}
printf(" The binary number we get is :%d \n",result);//1111
}
int main() {
char s[6];
char *p;
do{
printf(" Please enter a [0-1023] Range of integers :") ;
gets(s);
p=s;
int Num=1,j=0,Num_T=0,Totle=0;
j=strlen(s);
while(*p!='\0') // Enter an integer number
{
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. Key algorithms
1: Decimal to binary direct code :
while(n)// Divide to 0 Just stop
{
i = n % 2; // remainder 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 sign out
}
2. Convert characters to decimal numbers , If the input is not 0-9 Characters between automatically exit :.
while(*p!='\0') // Enter an integer number
{
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. Judge if the value is 0-1023, Conversion , If not , What we judge here is that the value exceeds 1023, Quit
if(Totle>=0&&Totle<1024)
decToBin(Totle);
else
exit(0);
summary
Here is a summary of the article :
That's what we're going to talk about today , This paper simply introduces the function and key algorithm of the program , I hope everyone is happy to learn .
边栏推荐
- Filtre de profondeur de la série svo2
- uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
- xss-labs-master靶场环境搭建与1-6关解题思路
- Repeat, tile and repeat in pytorch_ The difference between interleave
- PyTorch nn.RNN 参数全解析
- Natural language processing series (III) -- LSTM
- YYGH-10-微信支付
- Leetcode209 长度最小的子数组
- Time format display
- File operation (detailed!)
猜你喜欢
随机推荐
HOW TO ADD P-VALUES TO GGPLOT FACETS
php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
【工控老马】西门子PLC Siemens PLC TCP协议详解
Codeforces 771-div2 C (trouble, permutation is not very good)
数据分析 - matplotlib示例代码
SVO2系列之深度濾波DepthFilter
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Log4j2
测试左移和右移
Uniapp uni list item @click, uniapp uni list item jump with parameters
SSH automatically disconnects (pretends to be dead) after a period of no operation
Some problems encountered in introducing lvgl into esp32 Arduino
XSS labs master shooting range environment construction and 1-6 problem solving ideas
How does Premiere (PR) import the preset mogrt template?
Flesh-dect (media 2021) -- a viewpoint of material decomposition
Deep understanding of NN in pytorch Embedding
行业的分析
小程序链接生成
Leetcode14 最长公共前缀
进入前六!博云在中国云管理软件市场销量排行持续上升