如何用C语言编写一个简单的计算器程序?
首先编写C代码以创建一个简单的计算器。然后,按照下面给出的算法编写一个C程序。
算法
Step 1: Declare variablesStep 2: Enter any operator at runtime
Step 3: Enter any two integer values at runtime
Step 4: Apply switch case to select the operator:
// 情况'+':结果= num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
case '/': result = num1 / num2;
break;
default: printf("\n Invalid Operator ");
Step 5: Print the result
示例
以下是使用Switch Case的计算器C程序-
#include <stdio.h>输出结果int main(){
char Operator;
float num1, num2, result = 0;
printf("\n Enter any one operator like +, -, *, / : ");
scanf("%c", &Operator);
printf("Enter the values of Operands num1 and num2 \n : ");
scanf("%f%f", &num1, &num2);
switch(Operator){
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
case '/': result = num1 / num2;
break;
default: printf("\n Invalid Operator ");
}
printf("The value = %f", result);
return 0;
}
执行以上程序后,将产生以下结果-
Enter any one operator: +Enter values of Operands num1 and num2:
23
45
The value = 68.000000
以上是 如何用C语言编写一个简单的计算器程序? 的全部内容, 来源链接: utcz.com/z/361894.html