zoukankan      html  css  js  c++  java
  • 四则运算3

    //信1301-1班   队员:武于微 张鹏宇
    #include<iostream>
    #include<sstream>
    #include<string>
    #include<ctime>
    #include<time.h>
    #include<fstream>
    #define size 10000
    using namespace std;
    class Calculator
    {
        public:
            Calculator() 
            {
                STop=0;
                NTop=0;
                SignTemp=NULL;
                NumTemp=0;
            }
            string num_str(int i);    // int string
            int Operator(int m);
            void compute();    //计算函数
            double Run(int &i,int &length);
            char cal[100][100];
        private:
            char Sign[100],SignTemp; 
            double Num[100],NumTemp;  
            int STop,NTop;
    };
    string num_str(int i)    //将数字转换为字符串
    {
        stringstream ss;
        ss<<i;
        return ss.str();
    }
    
    int Operator(int m)    //产生运算符
    {
        int a,b;
        char oper;
        a=rand()%4;    //产生随机整数用于除四取余后进行四种运算符号和四种数的选择
        b=rand()%2;
        if(m==2)    //无乘除法情况
        {
            switch(b)    //用于加减运算两种符选择
            {
            case 0:
                oper='+';
                break;
            case 1:
                oper='-';
                break;
            default:
                break;
            }
        }
        if(m==1)    //有乘除法情况
        {
            switch(a)          //用于四则运算四种符号的选择
            {
            case 0:
                oper='+';
                break;
            case 1:
                oper='-';
                break;
            case 2:
                oper='*';
                break;
            case 3:
                oper='/';
                break;    
            default:
                break;
            }
        }
        return oper;
    }
    
    void Calculator::compute()    //计算
    {
        switch (Sign[STop-1]) 
        {
            case '+': 
                Num[NTop-2]+=Num[NTop-1];//运算后将结果存入
                STop--;
                NTop--;
                break;
            case '-': 
                Num[NTop-2]-=Num[NTop-1];
                STop--;
                NTop--;
                break;
            case '*': 
                Num[NTop-2]*=Num[NTop-1];
                STop--;
                NTop--;
                break;
            case '/':    //判断除数是否为0,0的话结束
                if (Num[STop-1]==0) 
                {
                    cout<<"data error!!"<<endl; 
                    exit(0);
                }
                else
                {
                    Num[NTop-2]/=Num[NTop-1];
                    STop--;
                    NTop--;
                    break; 
                }    //其中STop--;NTop--;实现的是将数据弹出栈
            case ')': 
                STop=STop-2;
                break;    //发现')'对上个运算符进行判断
        }
    }
    
    double Calculator::Run(int &i,int &length)    //运行函数
    {
        char temp;
        int p=0;
        while (p<=length)    //读入数据
        { 
            temp=cal[i][p]; 
            if (temp>='0'&&temp<='9') 
            {
                NumTemp=NumTemp*10+double(temp-48); 
            }
            else 
            {
                Sign[STop]=SignTemp; 
                SignTemp=temp;
                STop++;    //运算符入栈
                if (Sign[STop-1]!=')' && temp!='(')     //数字入栈
                {
                    Num[NTop]=NumTemp; 
                    NTop++; 
                    NumTemp=0;
                }   
                switch (temp)    //进行有关运算优先级别的考虑 
                {
                case '+': 
                case '-':
                    while(Sign[STop-1]!='(' && STop-1>0) 
                        compute();
                    break;    //"+""-"的时候考虑"("
                case '*':
                case '/':
                    while( Sign[STop-1]!='(' && Sign[STop-1]!='+' && Sign[STop-1]!='-'&& STop-1>0 ) 
                        compute();
                    break;
                case '(':
                    break;    //继续读取
                case ')':
                    while(Sign[STop-1]!='(') compute();
                    break;
                default: 
                    while(STop-1>0) 
                        compute();
                    break;    //结束
                }
            }
            p++;
        }
        return Num[0];
    }
    
    int main()    //满足最多十个数值运算
    {
    
        int num1,num2,jishu,number,value,print,m,kuohao,negative,i,j,n=0,kuo;
        char oper1;
        string s_num1,s_num2,s_connect[size],t;
        srand((unsigned)time(NULL));    //通过系统时间初始化随机数种子,若不设置则每次产生随机数相同
        cout<<"请输入运算题目数量:"<<endl;  //定制运算题目数量
        cin>>number;
        cout<<"请输入数值产生范围:"<<endl;    //选择数值产生范围
        cin>>value;
        cout<<"请选择运算题目打印方式: 1.dos界面打印 2.文件打印"<<endl;  //定制运算题目打印方式
        cin>>print;
        cout<<"请选择运算是否有乘除法:1.有  2.没有"<<endl;  //乘除法选择
        cin>>m;
        cout<<"请选择运算是否有括号:1.有  2.没有"<<endl;  //乘除法选择
        cin>>kuohao;
        cout<<"请选择加减法运算是否有负数:1.有  2.没有"<<endl;  //乘除法选择
        cin>>negative;
        cout<<"请计算并输出结果:"<<endl;
        ofstream outfile;    //定义ofstream类即输出文件流类对象outfile
        if(print==2)    //文件打开
        {
            outfile.open("out.txt",ios::out);
            if(!outfile)    //打开错误,报错
            {
                cerr<<"open error!"<<endl;
                exit(0);
            }
        }
        for(i=0;i<number;i++)
        {    
            num1=rand()%value+1;
            num2=rand()%value+1;
            oper1=Operator(m);
            s_num1=num_str(num1);
            s_num2=num_str(num2);
            s_connect[i]=s_num1+oper1+s_num2;
            jishu=rand()%4+2;
            if(jishu>2)    //对随机生成运算式里数值个数判断
            {
                for(j=jishu;j>2;j--)     //两个数值以上运算式生成
                {
                    s_num1=s_connect[i];
                    oper1=Operator(m);
                    num2=rand()%value+1;
                    s_num2=num_str(num2);
                    if(kuohao==1)    //随机产生括号
                    {
                        kuo=rand()%2;
                        if(kuo==0)
                        {
                            s_num1='('+s_num1+')';
                        }
                    }
                    kuo=rand()%2;
                    if(kuo==0)
                    {
                        t=s_num1;
                        s_num1=s_num2;
                        s_num2=t;
                    }
                    s_connect[i]=s_num1+oper1+s_num2;
                }
            }
            Calculator Ca;
            int L;
            double b;
            L=s_connect[i].length();
            int k;
            for(k=0;k<L;k++)    //字符串转变成char类型
            {
                Ca.cal[i][k]=s_connect[i][k];
            }
            double a=Ca.Run(i,L);    //运算结果判断
            if(negative==2&&a<0)
            {
                s_connect[i]="";
                i--;
            }
            if(negative==1||(negative==2&&a>0))
            {
                if(print==2)    //文件输出
                {
                    cout<<s_connect[i]<<"=";
                    cin>>b;
                    if(b==a)
                    {
                        cout<<"恭喜你,回答正确!"<<endl;
                        n++;
                    }
                    else
                    {
                        cout<<"很遗憾回答错误,继续努力!"<<"---"<<"正确答案为:"<<a<<endl;
                    }
                    outfile<<s_connect[i]<<"="<<b<<endl;
                    if(b==a)
                    {
                        outfile<<"恭喜你,回答正确!"<<endl;
                        n++;
                    }
                    else
                    {
                        outfile<<"很遗憾回答错误,继续努力!"<<"---"<<"正确答案为:"<<a<<endl;
                    }
                }
                if(print==1)
                {
                    cout<<s_connect[i]<<"=";    //dos界面输出
                    cin>>b;
                    if(b==a)
                    {
                        cout<<"恭喜你,回答正确!"<<endl;
                        n++;
                    }
                    else
                    {
                        cout<<"很遗憾回答错误,继续努力!"<<"---"<<"正确答案为:"<<a<<endl;
                    }
                }
            }
        }
        if(print==2)
        {
            outfile<<endl<<"本次答题共答对"<<n<<"道题!"<<endl<<endl<<endl<<endl;
        }
        else
        {
            cout<<endl<<"本次答题共答对"<<n<<"道题!"<<endl<<endl<<endl<<endl;
        }
        if(print==2)
        {
            outfile.close();    //关闭文件
        }
        return 0;
    }

    输出到文件:

    思路:

    定义Calculator类,类中的函数包括:
    1,string num_str(int i)——将int整形的算式变为string型
    利用stringstream函数
    2,int Operator(int m)——随机产生运算符
    用rand函数随机产生0-3的数
    3,void compute();——计算函数
    符号的选择,优先级
    关于正负:若出现负数,则之前数组置空,重新生成
    4,double Run(int &i,int &length);——计算并输出
    char型的运算,出栈入栈

    main函数:
    随机产生两个操作数,操作符,整体转化为string型s_connect[i]
    随机产生个数,根据个数,利用for循环,将之前s_connect[i]赋值给s_num1,并产生新的符号,变量,三者结合为新的s_connect[i]
    定义变量kuohao,若kuohao=1,用rand随机赋值,随机添加括号
    将string的算式转换为char型数组,赋值给Calculator类中的变量cal,调用Run()进行运算,结果判断是否为负数
    将用户输入的结果和Run()运算结果比较,根据情况输出判断结果并计数

    项目计划总结:

    日期&&任务

    听课

    编写程序

    阅读相关书籍

    网上查找资料

      日总计

    周一

    2

    2

    1

    1

    6

    周二

     

    3

     

    1

    4

    周三

     

    1

    1

    6

    周四

    2

    4

     

     

    6

    周五

     

    6

    1

    1

    8

    周六

     

    4

     

     

    4

    周日

     

     

    2

     

    2

    周总计

    4

    23

    5

    4

    36

     时间记录日志:

    日期

    开始时间

    结束时间

    中断时间

    净时间

    活动

    备注

    3/14

    14:00

    15:50

    10

    100

    听课

    软件工程上课

     

    16:00

    18:20

    20

    120

    编写程序

    合作讨论并编写四则运算3

     

    19:00

    21:00

    10

    110

    编程,网上查资料

    合作编写四则运算3,讨论问题

     

    21:00

    22:00

     

    60

    阅读书籍

    《构建之法》

    3/15

    19:00

    22:30

    30

    180

    查资料,编写程序

    合作编写四则运算3

    3/16

    18: 00

    22:10

    10

    240

    编写程序

    合作编写四则运算3 

    3/17

    14:00

    15:50

    10

    100

    听课

    软件工程上课

     

    19:00

    22:30

    20

    160

    查资料,编写程序

    合作编写四则运算3,改程序错误

    3/18

    14:00

    17:30

    10

    200

    编写修改程序

    合作编写程序,查改程序错误

     

    18:30

    22:00

    10

    200

    修改程序

    修改并测试程序

     

    22:20

    23:30

     

    70

    阅读书籍

    《构建之法》

    3/19

    15:20

    19:50

    30

    240

    修改程序,写博客

    测试修改程序,发表博客

    3/20

    19:00

    21:00

    20

    100

    阅读书籍

    《构建之法》

    缺陷记录日志:

    日期

    编号

    类型

    引入阶段

    排除阶段

    修复时间

    修复缺陷

    3/15

    1

     

    讨论思路

    查阅资料

    30min

     

     

    描述:对于产生的一个运算式用了string型不会计算,后采用产生char型运算式后再用栈的方法进行计算

    3/16-3/17

    2

     

    编码

    编译

    10hour

     

     

    描述:随机生成的运算式转变为char型并和括号的组合出现问题

    3/18-3/19

    3

     

    编码

    调试

    30min

     

     

    描述:修复括号随机产生,加减结果是否出现负数解决

    结对编程照片

    结对的小伙伴博客:http://www.cnblogs.com/fooreveryu/

  • 相关阅读:
    配置navigation bar外观
    加急审核
    UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
    打开某个好友的聊天界面
    ALAssets的两种用法
    更改appstore开发商名字
    回调函数的使用
    相应字体所占的位置大小
    IOS的变量前加extern和static字段
    iOS开发代码规范(通用)
  • 原文地址:https://www.cnblogs.com/wuyw/p/5295821.html
Copyright © 2011-2022 走看看