c语言编写加、减、乘、除、阶乘、正弦、余弦、开平方、指数、对数运算,十进制数、二进制数、八进制数、十六进制之间的相互转换。要求以菜单形式选择需要进行的运算。
时间: 2023-10-30 13:05:39 浏览: 80
以下是C语言编写的加、减、乘、除、阶乘、正弦、余弦、开平方、指数、对数运算,以及十进制数、二进制数、八进制数、十六进制之间的相互转换的示例代码。你可以根据需要进行修改和完善。
```c
#include <stdio.h>
#include <math.h>
void display_menu();
double add(double x, double y);
double subtract(double x, double y);
double multiply(double x, double y);
double divide(double x, double y);
int factorial(int n);
double sine(double x);
double cosine(double x);
double square_root(double x);
double exponent(double x);
double logarithm(double x);
void decimal_to_binary(int n);
void decimal_to_octal(int n);
void decimal_to_hexadecimal(int n);
int main()
{
int choice;
double x, y, result;
int n;
do {
display_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%lf %lf", &x, &y);
result = add(x, y);
printf("Result: %lf\n", result);
break;
case 2:
printf("Enter two numbers: ");
scanf("%lf %lf", &x, &y);
result = subtract(x, y);
printf("Result: %lf\n", result);
break;
case 3:
printf("Enter two numbers: ");
scanf("%lf %lf", &x, &y);
result = multiply(x, y);
printf("Result: %lf\n", result);
break;
case 4:
printf("Enter two numbers: ");
scanf("%lf %lf", &x, &y);
if (y == 0) {
printf("Error: Division by zero!\n");
} else {
result = divide(x, y);
printf("Result: %lf\n", result);
}
break;
case 5:
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: Factorial of negative number does not exist!\n");
} else {
result = factorial(n);
printf("Result: %lf\n", result);
}
break;
case 6:
printf("Enter an angle in degrees: ");
scanf("%lf", &x);
result = sine(x);
printf("Result: %lf\n", result);
break;
case 7:
printf("Enter an angle in degrees: ");
scanf("%lf", &x);
result = cosine(x);
printf("Result: %lf\n", result);
break;
case 8:
printf("Enter a number: ");
scanf("%lf", &x);
if (x < 0) {
printf("Error: Square root of negative number does not exist!\n");
} else {
result = square_root(x);
printf("Result: %lf\n", result);
}
break;
case 9:
printf("Enter a number: ");
scanf("%lf", &x);
result = exponent(x);
printf("Result: %lf\n", result);
break;
case 10:
printf("Enter a number: ");
scanf("%lf", &x);
if (x <= 0) {
printf("Error: Logarithm of non-positive number does not exist!\n");
} else {
result = logarithm(x);
printf("Result: %lf\n", result);
}
break;
case 11:
printf("Enter a decimal number: ");
scanf("%d", &n);
decimal_to_binary(n);
break;
case 12:
printf("Enter a decimal number: ");
scanf("%d", &n);
decimal_to_octal(n);
break;
case 13:
printf("Enter a decimal number: ");
scanf("%d", &n);
decimal_to_hexadecimal(n);
break;
case 14:
printf("Exiting the program...\n");
break;
default:
printf("Invalid choice!\n");
break;
}
printf("\n");
} while (choice != 14);
return 0;
}
void display_menu()
{
printf("Math Operations Menu\n");
printf("--------------------\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. Factorial\n");
printf("6. Sine\n");
printf("7. Cosine\n");
printf("8. Square Root\n");
printf("9. Exponent\n");
printf("10. Logarithm\n");
printf("11. Decimal to Binary\n");
printf("12. Decimal to Octal\n");
printf("13. Decimal to Hexadecimal\n");
printf("14. Exit\n");
}
double add(double x, double y)
{
return x + y;
}
double subtract(double x, double y)
{
return x - y;
}
double multiply(double x, double y)
{
return x * y;
}
double divide(double x, double y)
{
return x / y;
}
int factorial(int n)
{
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
double sine(double x)
{
double radians = x * M_PI / 180;
return sin(radians);
}
double cosine(double x)
{
double radians = x * M_PI / 180;
return cos(radians);
}
double square_root(double x)
{
return sqrt(x);
}
double exponent(double x)
{
return exp(x);
}
double logarithm(double x)
{
return log(x);
}
void decimal_to_binary(int n)
{
int binary[32];
int i = 0;
while (n > 0) {
binary[i] = n % 2;
n /= 2;
i++;
}
printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}
void decimal_to_octal(int n)
{
int octal[32];
int i = 0;
while (n > 0) {
octal[i] = n % 8;
n /= 8;
i++;
}
printf("Octal: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", octal[j]);
}
printf("\n");
}
void decimal_to_hexadecimal(int n)
{
char hexadecimal[32];
int i = 0;
while (n > 0) {
int remainder = n % 16;
if (remainder < 10) {
hexadecimal[i] = remainder + '0';
} else {
hexadecimal[i] = remainder - 10 + 'A';
}
n /= 16;
i++;
}
printf("Hexadecimal: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", hexadecimal[j]);
}
printf("\n");
}
```
阅读全文