基于C++的计算机系统

项目来源:

项目来源于软件开发与创新课程的结对编程,对100以内的四则运算进行计算、练习和出题。

项目分析:

本系统使用C++语言编程,运用Dev C++进行开发,拥有计算题目、自动出题和进行练习三个功能,可以对结果100以内的四则运算进行灵活运用。自动出题使用随机函数可自由选择系统写出多少道拥有两个运算符的四则运算,计算题目则可计算任意个运算符的四则运算,进行练习写出答案后系统会自动判别对错。

编程中遇到的问题:

1.对于结果有小数点的四则运算,系统给出的答案往往是经过四舍五入的整数,结果并不精准,容易造成误导。

2. 在计算时,输入的“+”、“-”、“*”、“/”都被默认为“+”导致结果出错

 

3.在使用进行练习功能时,输入答案后会跳转到主页面无法判断输入结果正确与否且如果结果错误,不会输出正确结果。

 

改进方案:

1.定义数据时将int改为double,结果输出时可以保留小数点后两位,使结果更加精准。

 

2.写代码时将双等号写成了赋值号单等号,导致运算符无法正常使用,改为双等号后已恢复正常。

 

3.在计算函数中加入

double an=jieguo(a1,a2,a3,x,y);

            if(i==an){

                  cout<<"你做对了"<<endl;

            }else{

                  cout<<"你做错了,结果是:"<<an<<endl;

            }

就可以实现对输入结果的对错判断且当结果错误时显示出正确答案。

 

代码清单:

#include <iostream>

#include <stdlib.h>

#include <string.h>

#include <time.h>

using namespace std;

int menu(){

      int m;

      cout<<"欢迎来到计算器系统"<<endl;

      cout<<"1.计算题目"<<endl;

      cout<<"2.自动出题"<<endl;

      cout<<"3.进行练习"<<endl;

      cout<<"4.退出程序"<<endl;

      cin>>m;

      system("cls");

      return m;

}

double jieguo(double a1,double a2,double a3,int x,int y){

            double an;

      if(x==0&&y==2||x==0&&y==3||x==1&&y==2||x==1&&y==3){

            if(y==2){

                  an=a2*a3;

                  if(x==0){

                       an=an+a1;

                  }else if(x==1){

                       an=a1-an;

                  }

            }else if(y==3){

                  an=a2/a3;

                  if(x==0){

                       an=an+a1;

                  }else if(x==1){

                       an=a1-an;

                  }

            }

      }else{

            if(x==0&&y==0){

                  an=a1+a2+a3;

            }else if(x==0&&y==1){

                  an=a1+a2-a3;

            }else if(x==1&&y==0){

                  an=a1-a2+a3;

            }else if(x==1&&y==1){

                  an=a1-a2-a3;

            }else if(x==1&&y==0){

                  an=a1-a2+a3;

            }else if(x==2&&y==0){

                  an=a1*a2+a3;

            }else if(x==2&&y==1){

                  an=a1*a2-a3;

            }else if(x==2&&y==2){

                  an=a1*a2*a3;

            }else if(x==2&&y==3){

                  an=a1*a2/a3;

            }else if(x==3&&y==0){

                  an=a1/a2+a3;

            }else if(x==3&&y==1){

                  an=a1/a2-a3;

            }else if(x==3&&y==2){

                  an=a1/a2*a3;

            }else if(x==3&&y==3){

                  an=a1/a2/a3;

            }

      }

      return an;

}

int panduan(double a1,double a2,double a3,int x,int y){

      int i=0;

      double an=jieguo(a1,a2,a3,x,y);

      if(an>100){

            i=1;

      }

      return i;

}

void jisuan(){

      double a1,a2,a3;

      int x,y;

      char p,q;

      cout<<"请输入算式"<<endl;

      cin>>a1>>p>>a2>>q>>a3;

      if(p=='+'){

            x=0;

      }else if(p=='-'){

            x=1;

      }else if(p=='*'){

            x=2;

      }else if(p=='/'){

            x=3;

      }

      if(q=='+'){

            y=0;

      }else if(q=='-'){

            y=1;

      }else if(q=='*'){

            y=2;

      }else if(q=='/'){

            y=3;

      }

      double an=jieguo(a1,a2,a3,x,y);

      cout<<"结果为:"<<an<<endl;

}

void chuti(){

      int i,a1,a2,a3,x,y;

      cout<<"请输入要出的题数"<<endl;

      cin>>i;

      srand((unsigned)time(NULL));

      for(int j=0;j<i;j++){

            a1=rand()%99+1;

            a2=rand()%99+1;

            a3=rand()%99+1;

            x=rand()%4;

            y=rand()%4;

            cout<<a1;

            if(x==0){

                  cout<<"+";

            }else if(x==1){

                  cout<<"-";

            }else if(x==2){

                  cout<<"x";

            }else if(x==3){

                  cout<<"÷";

            }

            cout<<a2;

            if(y==0){

                  cout<<"+";

            }else if(y==1){

                  cout<<"-";

            }else if(y==2){

                  cout<<"x";

            }else if(y==3){

                  cout<<"÷";

            }

            cout<<a3<<"="<<endl;

      }

}

void lianxi(){

      int a1,a2,a3,x,y;

      double i;

      x:

            srand((unsigned)time(NULL));

            a1=rand()%99+1;

            a2=rand()%99+1;

            a3=rand()%99+1;

            x=rand()%4;

            y=rand()%4;

            if(panduan(a1,a2,a3,x,y)){

                  goto x;

            }

            cout<<a1;

            if(x==0){

                  cout<<"+";

            }else if(x==1){

                  cout<<"-";

            }else if(x==2){

                  cout<<"x";

            }else if(x==3){

                  cout<<"÷";

            }

            cout<<a2;

            if(y==0){

                  cout<<"+";

            }else if(y==1){

                  cout<<"-";

            }else if(y==2){

                  cout<<"x";

            }else if(y==3){

                  cout<<"÷";

            }

            cout<<a3<<"=?"<<endl;

            cout<<"请输入答案:";

            cin>>i;

            double an=jieguo(a1,a2,a3,x,y);

            if(i==an){

                  cout<<"你做对了"<<endl;

            }else{

                  cout<<"你做错了,结果是:"<<an<<endl;

            }

}

int main(){

      int n=1;

      while(n){

            switch(menu()){

                  case 1:jisuan();break;

                  case 2:chuti();break;

                  case 3:lianxi();break;

                  case 4:cout<<"退出成功";n=0;break;

                  default:cout<<"输入错误,请重新输入"<<endl;

            }

      }

      return 0;

}

心得体会:

在进行结对编程的过程中,能够增强思想的碰撞,从题目本身发散出更多的功能,在编程的过程中,能够互相找到对方编写的错误,极大地加快了编程的速度,相较于一个人进行编程,感到十分顺心。在编写这个计算机系统中,我们遇到了不少的困难,但是很快通过讨论和网络就轻松地解决了,一次愉快的体验。

posted @ 2021-04-21 22:26  胖胖xx  阅读(273)  评论(0编辑  收藏  举报