写一个c语言程序,实现二进制到十进制的转换
时间: 2024-05-29 17:10:47 浏览: 18
#include <stdio.h>
#include <math.h>
int main() {
long long binaryNumber;
int decimalNumber = 0, i = 0, remainder;
printf("请输入一个二进制数: ");
scanf("%lld", &binaryNumber);
// 将二进制数转换为十进制数
while (binaryNumber != 0) {
remainder = binaryNumber % 10;
binaryNumber /= 10;
decimalNumber += remainder * pow(2, i);
++i;
}
printf("转换后的十进制数为: %d", decimalNumber);
return 0;
}
相关问题
用C语言写一个二进制转换十进制的程序
下面是一个简单的C语言程序,用于将二进制数转换为十进制数:
```c
#include <stdio.h>
#include <math.h>
int binaryToDecimal(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, binaryToDecimal(n));
return 0;
}
int binaryToDecimal(long long n) {
int decimal = 0, i = 0, remainder;
while (n != 0) {
remainder = n % 10;
n /= 10;
decimal += remainder * pow(2, i);
++i;
}
return decimal;
}
```
在上面的程序中,我们首先从用户输入中获取一个二进制数。然后,我们调用一个名为`binaryToDecimal()`的函数来将二进制数转换为十进制数,并将其打印出来。
函数`binaryToDecimal()`使用while循环来遍历二进制数中的每个数字。我们通过使用`n%10`来获取二进制数的最后一位数字,并将其存储在`remainder`变量中。然后,我们将二进制数除以10,以便我们可以继续处理下一个数字。在每次循环中,我们使用`pow()`函数将二进制数的每一位数字乘以2的幂,以便将其转换为十进制数。最后,我们将所有数字相加,并将结果返回给调用函数。
用c语言栈写一个二进制转换十进制的程序
好的,以下是一个用 C 语言栈实现二进制转换为十进制的程序:
```c
#include <stdio.h>
#define MAX_SIZE 100
// 栈的结构体定义
struct Stack {
int top;
int arr[MAX_SIZE];
};
// 初始化栈
void initStack(struct Stack *s) {
s->top = -1;
}
// 判断栈是否已满
int isFull(struct Stack *s) {
return s->top == MAX_SIZE - 1;
}
// 判断栈是否为空
int isEmpty(struct Stack *s) {
return s->top == -1;
}
// 入栈操作
void push(struct Stack *s, int item) {
if (isFull(s)) {
printf("Stack is full\n");
return;
}
s->arr[++s->top] = item;
}
// 出栈操作
int pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
return -1;
}
return s->arr[s->top--];
}
// 二进制转换为十进制
int binaryToDecimal(char binary[]) {
struct Stack s;
initStack(&s);
int i = 0, decimal = 0, power = 1;
// 将二进制数的每一位依次入栈
while (binary[i] != '\0') {
push(&s, binary[i] - '0');
i++;
}
// 依次出栈并计算十进制数
while (!isEmpty(&s)) {
decimal += pop(&s) * power;
power *= 2;
}
return decimal;
}
int main() {
char binary[MAX_SIZE];
printf("Enter a binary number: ");
scanf("%s", binary);
int decimal = binaryToDecimal(binary);
printf("Decimal equivalent of %s is %d\n", binary, decimal);
return 0;
}
```
程序通过栈的数据结构来实现二进制转换为十进制,`push` 函数用来入栈,`pop` 函数用来出栈,`binaryToDecimal` 函数用来将二进制数转换为十进制数。在 `main` 函数中,读取用户输入的二进制数,并调用 `binaryToDecimal` 函数来计算十进制数的值,最后输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)