我需要你写一个使用c语言的计算器,功能应该有菜单,可以实现二进制、十进制的加减乘除,在运算结束后可以选择继续或者是返回菜单,可以进行简单错误判断,如除以0判断等
时间: 2023-11-28 22:47:53 浏览: 75
好的,我可以给你写一个简单的计算器程序。这个程序可以进行二进制和十进制的加减乘除运算,同时还具有错误判断及返回菜单的功能。
首先,我们需要编写一个菜单函数,用于展示用户可以进行的操作:
```c
void show_menu() {
printf("\n");
printf("1. Binary addition\n");
printf("2. Binary subtraction\n");
printf("3. Binary multiplication\n");
printf("4. Binary division\n");
printf("5. Decimal addition\n");
printf("6. Decimal subtraction\n");
printf("7. Decimal multiplication\n");
printf("8. Decimal division\n");
printf("9. Exit\n");
printf("\n");
}
```
接下来编写二进制加法的函数:
```c
void binary_addition() {
char binary1[100], binary2[100];
int decimal1 = 0, decimal2 = 0, sum = 0;
printf("Enter first binary number: ");
scanf("%s", binary1);
printf("Enter second binary number: ");
scanf("%s", binary2);
decimal1 = binary_to_decimal(binary1);
decimal2 = binary_to_decimal(binary2);
sum = decimal1 + decimal2;
printf("Sum of the two binary numbers is: %d\n", decimal_to_binary(sum));
}
```
这个函数首先要求用户输入两个二进制数,然后将它们转换为十进制数,计算它们的和,并将和转换为二进制数进行输出。在这里,我们需要编写两个辅助函数 `binary_to_decimal` 和 `decimal_to_binary`。下面是这两个函数的代码:
```c
int binary_to_decimal(char binary[]) {
int decimal = 0, base = 1, i = 0;
for (i = strlen(binary) - 1; i >= 0; i--) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2;
}
return decimal;
}
int decimal_to_binary(int decimal) {
int binary = 0, base = 1;
while (decimal > 0) {
binary += (decimal % 2) * base;
decimal /= 2;
base *= 10;
}
return binary;
}
```
接下来依次编写二进制减法、乘法和除法的函数:
```c
void binary_subtraction() {
char binary1[100], binary2[100];
int decimal1 = 0, decimal2 = 0, diff = 0;
printf("Enter first binary number: ");
scanf("%s", binary1);
printf("Enter second binary number: ");
scanf("%s", binary2);
decimal1 = binary_to_decimal(binary1);
decimal2 = binary_to_decimal(binary2);
diff = decimal1 - decimal2;
printf("Difference of the two binary numbers is: %d\n", decimal_to_binary(diff));
}
void binary_multiplication() {
char binary1[100], binary2[100];
int decimal1 = 0, decimal2 = 0, product = 0;
printf("Enter first binary number: ");
scanf("%s", binary1);
printf("Enter second binary number: ");
scanf("%s", binary2);
decimal1 = binary_to_decimal(binary1);
decimal2 = binary_to_decimal(binary2);
product = decimal1 * decimal2;
printf("Product of the two binary numbers is: %d\n", decimal_to_binary(product));
}
void binary_division() {
char binary1[100], binary2[100];
int decimal1 = 0, decimal2 = 0, quotient = 0;
printf("Enter first binary number: ");
scanf("%s", binary1);
printf("Enter second binary number: ");
scanf("%s", binary2);
decimal1 = binary_to_decimal(binary1);
decimal2 = binary_to_decimal(binary2);
if (decimal2 == 0) {
printf("Error: Division by zero\n");
return;
}
quotient = decimal1 / decimal2;
printf("Quotient of the two binary numbers is: %d\n", decimal_to_binary(quotient));
}
```
接下来编写十进制加减乘除的函数,与二进制的运算类似:
```c
void decimal_addition() {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Sum of the two numbers is: %.2f\n", num1 + num2);
}
void decimal_subtraction() {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Difference of the two numbers is: %.2f\n", num1 - num2);
}
void decimal_multiplication() {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Product of the two numbers is: %.2f\n", num1 * num2);
}
void decimal_division() {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
if (num2 == 0) {
printf("Error: Division by zero\n");
return;
}
printf("Quotient of the two numbers is: %.2f\n", num1 / num2);
}
```
最后,我们把所有的函数整合起来,加上菜单及错误判断的代码:
```c
#include <stdio.h>
#include <string.h>
int binary_to_decimal(char binary[]) {
int decimal = 0, base = 1, i = 0;
for (i = strlen(binary) - 1; i >= 0; i--) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2;
}
return decimal;
}
int decimal_to_binary(int decimal) {
int binary = 0, base = 1;
while (decimal > 0) {
binary += (decimal % 2) * base;
decimal /= 2;
base *= 10;
}
return binary;
}
void show_menu() {
printf("\n");
printf("1. Binary addition\n");
printf("2. Binary subtraction\n");
printf("3. Binary multiplication\n");
printf("4. Binary division\n");
printf("5. Decimal addition\n");
printf("6. Decimal subtraction\n");
printf("7. Decimal multiplication\n");
printf("8. Decimal division\n");
printf("9. Exit\n");
printf("\n");
}
void binary_addition() {
char binary1[100], binary2[100];
int decimal1 = 0, decimal2 = 0, sum = 0;
printf("Enter first binary number: ");
scanf("%s", binary1);
printf("Enter second binary number: ");
scanf("%s", binary2);
decimal1 = binary_to_decimal(binary1);
decimal2 = binary_to_decimal(binary2);
sum = decimal1 + decimal2;
printf("Sum of the two binary numbers is: %d\n", decimal_to_binary(sum));
}
void binary_subtraction() {
char binary1[100], binary2[100];
int decimal1 = 0, decimal2 = 0, diff = 0;
printf("Enter first binary number: ");
scanf("%s", binary1);
printf("Enter second binary number: ");
scanf("%s", binary2);
decimal1 = binary_to_decimal(binary1);
decimal2 = binary_to_decimal(binary2);
diff = decimal1 - decimal2;
printf("Difference of the two binary numbers is: %d\n", decimal_to_binary(diff));
}
void binary_multiplication() {
char binary1[100], binary2[100];
int decimal1 = 0, decimal2 = 0, product = 0;
printf("Enter first binary number: ");
scanf("%s", binary1);
printf("Enter second binary number: ");
scanf("%s", binary2);
decimal1 = binary_to_decimal(binary1);
decimal2 = binary_to_decimal(binary2);
product = decimal1 * decimal2;
printf("Product of the two binary numbers is: %d\n", decimal_to_binary(product));
}
void binary_division() {
char binary1[100], binary2[100];
int decimal1 = 0, decimal2 = 0, quotient = 0;
printf("Enter first binary number: ");
scanf("%s", binary1);
printf("Enter second binary number: ");
scanf("%s", binary2);
decimal1 = binary_to_decimal(binary1);
decimal2 = binary_to_decimal(binary2);
if (decimal2 == 0) {
printf("Error: Division by zero\n");
return;
}
quotient = decimal1 / decimal2;
printf("Quotient of the two binary numbers is: %d\n", decimal_to_binary(quotient));
}
void decimal_addition() {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Sum of the two numbers is: %.2f\n", num1 + num2);
}
void decimal_subtraction() {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Difference of the two numbers is: %.2f\n", num1 - num2);
}
void decimal_multiplication() {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Product of the two numbers is: %.2f\n", num1 * num2);
}
void decimal_division() {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
if (num2 == 0) {
printf("Error: Division by zero\n");
return;
}
printf("Quotient of the two numbers is: %.2f\n", num1 / num2);
}
int main() {
int choice = 0;
while (1) {
show_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
binary_addition();
break;
case 2:
binary_subtraction();
break;
case 3:
binary_multiplication();
break;
case 4:
binary_division();
break;
case 5:
decimal_addition();
break;
case 6:
decimal_subtraction();
break;
case 7:
decimal_multiplication();
break;
case 8:
decimal_division();
break;
case 9:
return 0;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
```
此时,你可以编译并运行此程序,以体验完整的计算器功能。
阅读全文