• 小型项目:基于菜单的计算机系统


    /*
    calc.cpp实现了计算机系统。开始时,他显示一个菜单,以选择计算器的类型。如果用户选择了
    标准型计算器,则出现标准型计算器的菜单,而如果用户选择了科学性计算器,则出现科学计算器
    的菜单,最后一个的选项是退出程序。
    在标准计算器中,选项有加、减、乘、除等,而在科学计算器中,选项有求幂、阶乘、开方等。
    在本程序中,定义了新旧计算器的宏开关,新计算器接受输入值,而旧计算器的操作数则是前次操
    作的结果。
    本项目没有考虑异常处理,所以输入输出时不要输入非数字字符串。
    数据范围还不够大,将在往后进一步改进。
    */
    //包含预处理宏和头文件的文件
    #include<iostream>
    using namespace std;
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<stdio.h>
    #define new_cal 1
    #define old_cal 0
    //stand_calc 类定义了标准计算器的函数
    class stand_calc
    {
        /*标准计算器函数的原型。这些函数都是静态的,所以调用他们时,
        只使用类名即可,没有必要创建该类的实例。*/
    public:
        static double addition(double,double);
        static double substract(double,double);
        static double multiplication(double,double);
        static double division(double,double*);
        static double modulus(double*,double*);
    };
    //science_calc类定义了科学型计算器的函数
    class science_calc
    {
    public:
        static double square(double);
        static double cube(double);
        static double power(double,double);
        static double sq_root(double);
        static long int fact(double);
        static double sin_func(double);
        static double cos_func(double);
        static double tan_func(double);
    };
    double stand_calc::addition(double a, double b)
    {
        return (a+b);
    }
    double stand_calc::substract(double a, double b)
    {
        return (a-b);
    }
    double stand_calc::multiplication(double a, double b)
    {
        return (a*b);
    }
    /*除法函数将执行被除数和除数的相除,该函数有两个参数,其中一个是变量副本,
    另一个是指针。之所以如此,是因为如果除数是0,则该函数将显示再次输入除数的
    信息。使用指针后,再次输入的除数值也会在主函数中被更新。*/
    double stand_calc::division(double a, double*b)
    {
        while(*b==0)
        {
            cout<<"
    Cannot divide by zero"<<endl;
            cout<<"
    Enter the second number again:";
            cin>>*b;
        }
        return (a/(*b));
    }
    /*求余函数将执行被除数和除数的相除,并返回除法的余数。和除法操作类似,它的
    除数也不能为0。由于不能对双精度数求余,所以得先转换成整数。*/
    double stand_calc::modulus(double* a, double*b)
    {
        while(*b==0)
        {
            cout<<"
    Cannot divide by zero"<<endl;
            cout<<"
    Enter the second number again:";
            cin>>*b;
        }
        int x=(int)*a;
        int y=(int)*b;
        if(*a-x>0||*b-y>0)
        {
            cout<<"
    Converting decimal number into an integer to perform modulus";
        }
        *a=x;
        *b=y;
        return ((x)%(y));
    }
    
    //science_calcl类的函数声明由此开始
    double science_calc::square(double x)
    {
        return (pow(x,2));
    }
    double science_calc::cube(double x)
    {
        return (pow(x,3));
    }
    double science_calc::power(double x,double y)
    {
        return (pow(x,y));
    }
    double science_calc::sq_root(double x)
    {
        return (sqrt(x));
    }
    long int science_calc::fact(double x)
    {
        int n=(int)x;
        long int f=1;
        while(n>1)
        {
            f*=n;
            n--;
        }
        return f;
    }
    double science_calc::sin_func(double x)
    {
        return (sin(x));
    }
    double science_calc::cos_func(double x)
    {
        return (cos(x));
    }
    double science_calc::tan_func(double x)
    {
        return (tan(x));
    }
    
    int main()
    {
        double num1,num2,num3,temp;
        int choice1=0,choice2,flag;
        //主菜单的循环,程序从此开始,他将显示选择计算器类型的菜单
        do
        {
    //   clrsrc();//清除屏幕
            system("cls");
            cout<<"===============Type of Calculators===============";
            cout<<"
    1	Standard Calculator
    2	Scientific Calculator
    3	Quit"<<endl;
            cin>>choice1;
            flag=new_cal;
            //根据主菜单中的输入选项,执行相应操作
            switch(choice1)
            {
            case 1:
                do
                {
                    //  clrscr();
                    system("cls");
                      cout<<"================Standard Calculator================";
                      cout<<"
    1	Addition
    2	Substraction
    3	Multification
    4	Division
    5	Modulus
    6	Return to Previous Menu
    7	Quit";
                      //只有在旧计算器模式时,选项8才会显示,此时一个操作数已经存储在计算机的内存中
                    if(flag==old_cal)
                    {
                        cout<<"
    8	Clear Memory";
                    }
                    cout<<"
    Choose the type of calculator:"<<endl;
                    cin>>choice2;
                    switch(choice2)
                    {
                    case 1:
                      // 如果是新计算机模式,则接受第一个操作数,否则第一个操作数由上一次计算结果提供
                      if(flag==new_cal)
                      {
                          cout<<"Enter first number:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    First num is"<<num1<<endl;
                      }
                      cout<<"Enter second number:";
                      cin>>num2;
                      num3=stand_calc::addition(num1,num2);
                      cout<<"
    Addition of"<<num1<<" and"<<num2<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                    case 2:
                      // 如果是新计算机模式,则接受第一个操作数,否则第一个操作数由上一次计算结果提供
                      if(flag==new_cal)
                      {
                          cout<<"Enter first number:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    First num is"<<num1<<endl;
                      }
                      cout<<"Enter second number:";
                      cin>>num2;
                      num3=stand_calc::substract(num1,num2);
                      cout<<"
    Substraction of"<<num1<<" from"<<num2<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 3:
                      // 如果是新计算机模式,则接受第一个操作数,否则第一个操作数由上一次计算结果提供
                      if(flag==new_cal)
                      {
                          cout<<"Enter first number:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    First num is"<<num1<<endl;
                      }
                      cout<<"Enter second number:";
                      cin>>num2;
                      num3=stand_calc::multiplication(num1,num2);
                      cout<<"
    Multification of"<<num1<<" and"<<num2<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 4:
                      // 如果是新计算机模式,则接受第一个操作数,否则第一个操作数由上一次计算结果提供
                      if(flag==new_cal)
                      {
                          cout<<"Enter first number:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    First num is"<<num1<<endl;
                      }
                      cout<<"Enter second number:";
                      cin>>num2;
                      num3=stand_calc::division(num1,&num2);
                      cout<<"
    Division of"<<num1<<" by"<<num2<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 5:
                      // 如果是新计算机模式,则接受第一个操作数,否则第一个操作数由上一次计算结果提供
                      if(flag==new_cal)
                      {
                          cout<<"Enter first number:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    First num is"<<num1<<endl;
                      }
                      cout<<"Enter second number:";
                      cin>>num2;
                      num3=stand_calc::modulus(&num1,&num2);
                      cout<<"
    Modulus of"<<num1<<" by"<<num2<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 6:
                           cout<<"
    Returning to previous menu.";
                           cout<<"
    press any key to continue......";
                           getch();
                           break;
                       case 7:
                        cout<<"
    Quitting......";
                        cout<<"
    press any key to continue......";
                        getch();
                        exit(0);
                       case 8:
                        //如果是新计算机模式,则8是非法选项,否则8选项用以重启一个新的计算
                        if(flag==new_cal)
                        {
                            cout<<"
    Invalid choice.";
                            cout<<"
    press any key to continue......";
                            getch();
                        }
                        else
                        {
                            temp=0;
                            flag=new_cal;
                        }
                        break;
                       default:
                        cout<<"
    Invalid choice.";
                        cout<<"
    press any key to continue......";
                        getch();
                        break;
                     }
                    }while(choice2!=6);
                    break;
                    case 2:
                   do
                   {
                //      clrscr();
                     system("cls");
                      cout<<"================Scientific Calculator================";
                      cout<<"
    1	Square
    2	Cube
    3	Power
    4	Factorial
    5	Sin
    6	Cos
    7	Tan
    8	Return to previous menu
    9	Quit";
                      //只有在旧计算器模式时,选项8才会显示,此时一个操作数已经存储在计算机的内存中
                    if(flag==old_cal)
                    {
                        cout<<"
    10	Clear Memory";
                    }
                    cout<<"
    Choose the type of calculator:"<<endl;
                    cin>>choice2;
                    switch(choice2)
                    {
                    case 1:
                      if(flag==new_cal)
                      {
                          cout<<"Enter number to find square:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    Number is"<<num1<<endl;
                      }
                      num3=science_calc::square(num1);
                      cout<<"
    Square of"<<num1<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                    case 2:
                      if(flag==new_cal)
                      {
                          cout<<"Enter number to find cube:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    Number is"<<num1<<endl;
                      }
                      num3=science_calc::cube(num1);
                      cout<<"
    Cube of"<<num1<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                case 3:
                    if(flag==new_cal)
                      {
                          cout<<"Enter first number for base to find power:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    First number is"<<num1<<endl;
                      }
                      cout<<"Enter second number for base to fine power:";
                      cin>>num2;
                      num3=science_calc::power(num1,num2);
                      cout<<"
    Power of"<<num1<<"to the power "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 4:
                      if(flag==new_cal)
                      {
                          cout<<"Enter number to find factorial:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    Number to find factorial is"<<num1<<endl;
                      }
                      num3=science_calc::fact(num1);
                      cout<<"
    Factorial of"<<num1<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 5:
                      if(flag==new_cal)
                      {
                          cout<<"Enter number to find sin value:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    Number for sin value is"<<num1<<endl;
                      }
                      num3=science_calc::sin_func(num1);
                      cout<<"
    Sin value of"<<num1<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 6:
                        if(flag==new_cal)
                      {
                          cout<<"Enter number to find cos value:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    Number for cos value is"<<num1<<endl;
                      }
                      num3=science_calc::cos_func(num1);
                      cout<<"
    Cos value of"<<num1<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 7:
                        if(flag==new_cal)
                      {
                          cout<<"Enter number to find tan value:";
                          cin>>num1;
                      }
                      else
                      {
                          num1=temp;
                          cout<<"
    Number for tan value is"<<num1<<endl;
                      }
                      num3=science_calc::tan_func(num1);
                      cout<<"
    Tan value of"<<num1<<" is "<<num3;
                      cout<<"
    Press any key to continue......"<<endl;
                      getch();
                      temp=num3;
                      flag=old_cal;
                      break;
                       case 8:
                           cout<<"
    Returning to previous menu.";
                           cout<<"
    press any key to continue......";
                           getch();
                           break;
                       case 9:
                        cout<<"
    Quitting......";
                        cout<<"
    press any key to continue......";
                        getch();
                        exit(0);
                       case 10:
                        if(flag==new_cal)
                        {
                            cout<<"
    Invalid choice.";
                            cout<<"
    press any key to continue......";
                            getch();
                        }
                        else
                        {
                            temp=0;
                            flag=new_cal;
                        }
                        break;
                       default:
                        cout<<"
    Invalid choice.";
                        cout<<"
    press any key to continue......";
                        getch();
                        break;
                    }
                }while(choice2!=8);
                break;
                  case 3:
                    cout<<"
    Quitting......";
                    cout<<"
    press any key to continue......";
                    getch();
                    break;
            default:
                cout<<"
    Invalid choice.";
                cout<<"
    press any key to continue......";
                getch();
                break;
            }
        }while(choice1!=3);
    
        return 0;
    }

    执行效果图如下(可以自己尝试):



    除了system("cls")清除屏幕的应用,我觉得突出的一点就是新旧计算模式。

    该程序主要是函数的书写以及循环与条件的层层嵌入。

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    03-JS事件循环-宏任务与微任务
    10-Promise.all和Promise.race
    nodeJS-04
    nodeJS-03
    01-npm / npm install的过程 -nodemon 自动重启
    nodeJS-02
    nodeJS-01
    01-git
    比较运算符,in,instanceof——第十二级
    TCP(Transmission Control Protocol 传输控制协议)
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965758.html
Copyright © 2020-2023  润新知