当前位置:网站首页>C language example 100 (IV)
C language example 100 (IV)
2022-04-22 10:32:00 【Let's hold hands again】
C Language examples (33-35)
33. Octal and binary conversion
33.1 Binary to octal
(1) Binary to octal The core yes : Convert binary to decimal first , Convert to octal in .
(2) Use pow There was a problem with the function : There are many. overloaded function “pow” The instance matches the parameter list . There are many. overloaded function “pow” The instance matches the parameter list :
function “pow(double _X, int _Y)”
function “pow(float _X, int _Y)”
function “pow(long double _X, int _Y)”
Parameter type is : (int, int)
That's because pow() The return value of the function is double type .
So here pow(2.0,i) Can not write pow(2,i)
(3) Octal numbers have only 0-7; Decimal numbers have only 0-9; Binary numbers only 0-1.
(1) Code
#if 1
/* Binary to octal */
#include <stdio.h>
#include <windows.h>
#include <math.h>
int convertBinarytoOctal(long long binaryNumber)
{
int octalNumber = 0;
int decimalNumber = 0;
int i = 0;
// Convert binary to decimal
while(binaryNumber != 0)
{
// It must be written here pow(2.0,i), because pow() The return type of is double
decimalNumber += (binaryNumber%10)*pow(2.0,i);// Get each bit in turn from the low position ( such as 11111%10 = 1)
++i;// Multiply sequentially 2^0、2^1、2^2、....
binaryNumber/=10;// Eliminate every digit from the low order (11111/10 = 1111)
}
i = 1;
// Convert decimal numbers to octal
while (decimalNumber != 0)
{
// ride i Because when decimal to octal , Short division , In reverse order . Multiply sequentially 1、10、100、1000...
// Get the remainder
octalNumber += (decimalNumber % 8) * i;
// Get divisor
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
int main()
{
long long binaryNumber;
printf(" Enter a binary number : ");
scanf("%lld", &binaryNumber);
printf(" Binary number %lld Convert to octal to %d", binaryNumber, convertBinarytoOctal(binaryNumber));
system("pause");
return 0;
}
#endif
// Statement
//33.1 Binary to octal
int convertBinarytoOctal(long long binaryNumber);
(2) Running results

33.2 Octal to binary
Octal to binary The core yes : First convert octal to decimal , Convert to binary .
(1) Code
#if 1
/*33.2 Octal to binary */
#include <stdio.h>
#include <windows.h>
#include <math.h>
long long convertOctalToBinary(int octalNumber)
{
int decimalNumber = 0, i = 0;
long long binaryNumber = 0;
// Octal to decimal
while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8.0,i);
++i;
octalNumber/=10;
}
// Convert decimal to binary
i = 1;
while (decimalNumber != 0)
{
binaryNumber += (decimalNumber % 2) * i;
decimalNumber /= 2;
i *= 10;
}
return binaryNumber;
}
int main()
{
int octalNumber;
printf(" Enter an octal number : ");
scanf("%d", &octalNumber);
printf(" Octal number %d Convert binary to %lld\n", octalNumber, convertOctalToBinary(octalNumber));
system("pause");
return 0;
}
#endif
// Statement
//33.2 Octal to binary
long long convertOctalToBinary(int octalNumber);
(2) Running results

34. Octal and decimal conversion
34.1 Convert decimal to octal
(1) Code
#include <stdio.h>
#include <math.h>
#include <windows.h>
int convertDecimalToOctal(int decimalNumber)
{
int octalNumber = 0, i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
int main()
{
int decimalNumber;
printf(" Enter a decimal number : ");
scanf("%d", &decimalNumber);
printf(" Decimal number %d Convert to octal to %d\n", decimalNumber, convertDecimalToOctal(decimalNumber));
system("pause");
return 0;
}
// Statement
//34.1 Convert decimal to octal
int convertDecimalToOctal(int decimalNumber);
(2) Running results

34.2 Binary to octal
(1) Code
#include <stdio.h>
#include <windows.h>
#include <math.h>
long long convertOctalToDecimal(int octalNumber)
{
int decimalNumber = 0, i = 0;
while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8.0,i);
++i;
octalNumber/=10;
}
i = 1;
return decimalNumber;
}
int main()
{
int octalNumber;
printf(" Enter an octal number : ");
scanf("%d", &octalNumber);
printf(" Octal number %d Convert to decimal to %lld\n", octalNumber, convertOctalToDecimal(octalNumber));
system("pause");
return 0;
}
// Statement
long long convertOctalToDecimal(int octalNumber);//34.2 Octal to decimal
(2) Running results

35. Binary and decimal conversion
35.1 Binary to decimal
(1) Code
#include <stdio.h>
#include <windows.h>
#include <math.h>
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
decimalNumber += remainder*pow(2.0,i);
n /= 10;
++i;
}
return decimalNumber;
}
int main()
{
long long n;
printf(" Enter a binary number : ");
scanf("%lld", &n);
printf(" Binary number %lld Convert to decimal to %d\n", n, convertBinaryToDecimal(n));
system("pause");
return 0;
}
// Statement
//35.1 Binary to decimal
int convertBinaryToDecimal(long long n);
(2) Running results

35.2 Convert decimal to binary
(1) Code
#include <stdio.h>
#include <windows.h>
#include <math.h>
#define _CRT_SECURE_NO_WARNINGS 0
long long convertDecimalToBinary(int n)
{
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
printf("Step %d: %d/2, remainder = %d, merchant = %d\n", step++, n, remainder, n/2);
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
int main()
{
int n;
printf(" Enter a decimal number : ");
scanf("%d", &n);
printf(" Decimal number %d Convert to binary %lld\n", n, convertDecimalToBinary(n));
system("pause");
return 0;
}
// Statement
//35.2 Convert decimal to binary
long long convertDecimalToBinary(int n);
(2) Running results

版权声明
本文为[Let's hold hands again]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221023364236.html
边栏推荐
- 「笔记」某电信公司转型 SRE 运维体系交流
- 001 MySQL command
- Secure remote access + secure file transfer + terminal simulation + Remote Management - Shanghai daoning united with Van Dyke to bring reliable and easy to configure software to IT industry personnel
- cesium地图右击菜单(cesium篇.72)
- Symfony3.4 数据库反向生成entity 已解决
- Cobbler cobbler cobbler
- TIANTI 22 analog l3-2 puzzle a punch in reward (30 points)
- [SV] assign force difference
- Oracle帐户被锁了解锁方法
- 【leetcode】二叉树遍历迭代法的统一模板
猜你喜欢

matlab2009安装教程

Use of navigationview

OneFlow学习笔记:从Functor到OpExprInterpreter

Dynamic visualization of data based on pyqt5
![[leetcode] the first mock exam of two tree traversal iteration method.](/img/2a/4fb0de0f001738729a0051c14a1e5d.jpg)
[leetcode] the first mock exam of two tree traversal iteration method.

Secure remote access + secure file transfer + terminal simulation + Remote Management - Shanghai daoning united with Van Dyke to bring reliable and easy to configure software to IT industry personnel

OneFlow學習筆記:從Functor到OpExprInterpreter

Grpc experience of multilingual communication foundation

Introduction and basic use of numpy Library

被滥用的“架构师”!
随机推荐
PCIE XDMA IP核介绍(附列表)-明德扬科教(mdy-edu.com)
基于PyQt5实现数据动态可视化
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
Pytorch semantic segmentation total convolution network
Oracle account is locked. Unlocking method
Use of navigationview
Slime 2022 展望:把 Istio 的复杂性塞入智能的黑盒
批量更新软件和安全优化(以openssh为例) —— 筑梦之路
Addition, deletion, modification and query of MySQL advanced table
"Translation" 10 suggestions for people who hate yaml to write yaml
OneFlow學習筆記:從Functor到OpExprInterpreter
MySQL进阶之表的增删改查
MySQL数据库常识之储存引擎
Cobbler cobbler cobbler
[SV] assign force difference
分享一款投屏映射软件
Google Earth engine (GEE) -- aggregate grid population data
被滥用的“架构师”!
async 函数
Share some unknown and super easy-to-use API functions in sklearn module