C 语言基本计算器程序


2023 年 1 月 8 日, Learn eTutorial
2039

计算器是一个能够执行基本数学运算(如加法、减法、乘法和除法)的程序

什么是计算器以及计算器如何工作?

在这个 C 语言程序中,我们帮助你制作一个简单的计算器,用户必须输入数字并选择操作,包括

  • 加法
  • 减法
  • 乘法
  • 除法。

然后计算器将获取输入并执行用户选择的数学运算,并显示结果。

我们如何使用 C 语言程序实现计算器?

在从用户那里获取操作数和运算符的输入后,我们使用 switch case 语句根据用户输入选择任何操作,如 '+'、'- '、'*' 和 '/ '。然后控制将切换到该特定情况。

我们还为浮点数使用了精度运算符,因为我们只需要显示一定位数的十进制数字。为了使计算器不退出并继续运行,使用了 do-while 循环。然后询问用户是否想进行更多操作。

如果用户选择 'y' 或 'Y',则可以执行下一个操作,否则退出计算器。操作将在 do 中执行,包括用户选择的下一个操作,直到 while 检查用户输入选择 NO 为止。

注意: switch case 需要一个默认情况,如果没有任何其他情况与 switch case 匹配,则会采用该情况。

算法

步骤 1: 将头文件库导入 C 程序,以使用 printfscanf 等内置函数。

步骤 2:n1n2result 声明为 double 类型。

步骤 3: 定义一个 void 类型的函数 readOperands(),以读取操作数到 n1n2

步骤 4: 使用 main() 函数开始程序执行。

步骤 5:opernxt 声明为字符类型,将 tmp 声明为整数类型。

步骤 6: 开始一个 do-while 循环,从 6 到 10 执行这些步骤,直到用户想要退出(即用户输入的选项不是 'y' || 'Y')。

步骤 7:tmp 重置为 0,接受用户输入的运算符 oper,表示他要执行的操作。

步骤 8: 使用 switch case 操作来选择用户输入的运算,并调用函数 readOperands(),然后根据选择的运算符在该 switch 语句中执行操作。default 情况会将 tmp 设置为 1 表示不正确的运算符输入,并打印“Entered operation is not available”。

步骤 9: 如果 tmp 为 0,则使用浮点精度运算符打印 result。[以指定所需的小数位数]。

步骤 10: 询问用户“Do you want to continue? (y/n) ”并将输入读取到 nxtwhile 检查 nxt == 'y' || nxt == 'Y' 是否为 true,然后转到步骤 6,继续操作;但如果检查结果为 false,则停止执行。


为了制作一个计算器程序,我们在 C 语言编程中使用了以下概念,我们建议您参考这些概念以更好地理解

C 语言源代码

                                          #include <stdio.h>

double n1, n2, result; // Global variables

//user defined function to read operands
void readOperands() {
  n1 = n2 = result = 0; // reset variables to 0
  printf("Enter two operands: ");
  scanf(" %lf %lf", & n1, & n2);
}

void main() {
  int tmp;                       // tmp is used to check operator inputed is correct or not
  char oper, nxt;            /* oper is an operator to be selected, nxt is the next choice of user */
  printf("Simulation of a Basic Calculator\n");
  do {
    tmp =0;
    printf("\nEnter an operator (+, -, *, /): ");       // read operation
    scanf(" %c", & oper);
    switch (oper) 
    {
      case '+':
         readOperands();
         result = n1 + n2; // switch case 1 for addition
         break;
      case '-':
         readOperands();
         result = n1 - n2; // case 2 for subtraction
         break;
      case '*':
         readOperands();
         result = n1 * n2; // case 3 for multiplication
         break;
      case '/':
         readOperands();
         result = n1 / n2; // case 4 for division
         break;
     default:
         tmp = 1;
         printf("\nEntered operation is not available\n\n");    // default switch case for incorrect operator input
    }
    if (tmp == 0)
      printf("\n%5.2f %c %5.2f= %5.2f\n\n", n1, oper, n2, result);   // prints the output of the c program
    printf("Do you want to continue? (y/n) "); //    asking user for more operation on calculator
    scanf(" %c", & nxt);
  } while (nxt == 'y'||nxt == 'Y');
}
                                      

输出

Simulation of a Basic Calculator

Enter an operator (+, -, *, /): +
Enter two operands: 25 56

25.00 + 56.00= 81.00

Do you want to continue? (y/n) y

Enter an operator (+, -, *, /): *
Enter two operands: 15 15

15.00 * 15.00= 225.00

Do you want to continue? (y/n) y

Enter an operator (+, -, *, /): -
Enter two operands: 23.5 12.3

23.50 - 12.30= 11.20

Do you want to continue? (y/n) y

Enter an operator (+, -, *, /): /
Enter two operands: 125 12

125.00 / 12.00= 10.42

Do you want to continue? (y/n) y

Enter an operator (+, -, *, /): %

Entered operation is not available

Do you want to continue? (y/n) n