用c语言编写一个计算器且可以开根号和三角函数的运算
时间: 2023-10-08 14:03:52 浏览: 98
好的,以下是一个简单的用C语言编写的计算器,包括开根号和三角函数的运算:
```c
#include <stdio.h>
#include <math.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter operator (+, -, *, /, sqrt, sin, cos, tan): ");
scanf("%c", &operator);
switch (operator) {
case '+':
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 + num2;
printf("Result: %lf", result);
break;
case '-':
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 - num2;
printf("Result: %lf", result);
break;
case '*':
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 * num2;
printf("Result: %lf", result);
break;
case '/':
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 / num2;
printf("Result: %lf", result);
break;
case 's':
printf("Enter a number: ");
scanf("%lf", &num1);
result = sqrt(num1);
printf("Result: %lf", result);
break;
case 'c':
printf("Enter a number: ");
scanf("%lf", &num1);
result = cos(num1);
printf("Result: %lf", result);
break;
case 't':
printf("Enter a number: ");
scanf("%lf", &num1);
result = tan(num1);
printf("Result: %lf", result);
break;
default:
printf("Error! Invalid operator");
break;
}
return 0;
}
```
该计算器可以接受用户输入的运算符,包括加减乘除、平方根、正弦、余弦和正切,并输出相应的结果。
阅读全文