【第一周】结对编程实现四则运算

coding.net地址:https://coding.net/u/Boxer_

ssh:git@git.coding.net:Boxer_/homework.git

---------------------------------------------------------------------------------------------------------------------------------------------

2016.9.8 0:29 update 完善了下用户友好性,另外邹老师的需求我想了一下,需要建立分数类,包含分子和分母,最好另外要重新定义运算符,用来支持分数计算,感觉一时半会完成不了,有些东西还需要学习。

杨老师的需求比较好满足,但也需要时间检验。

1.更新了下主函数(看起来很low,没有模块化,但涉及到输入输出的功能不太好模块化)

 1 int main()
 2 {
 3     char ope[4]={'+','-','*','/'};
 4     int sumsoc = 0;
 5     while(1)
 6     {
 7     srand(time(NULL));
 8     string s;
 9     int soc;
10     int n1,n2,n3,n4,op1,op2,op3;
11     n1 = num();
12     n2 = num();
13     n3 = num();
14     n4 = num();
15     op1 = fh();
16     op2 = fh();
17     op3 = fh();
18     soc = sqrt(n1+n2+n3+n4)*(op1+op2+op3);    
19     s = itos(n1)+ope[op1]+itos(n2)+ope[op2]+itos(n3)+ope[op3]+itos(4);
20     cout << s+" "+"="+" "+"?";
21     cout<<"	"<<"难度值:"<<" "<<soc<<endl; 
22      convert2RPN(s);
23      double res1 = calculateRPN(s);
24      double res2;
25      cin>>res2;
26      if(res1-res2<0.2) 
27      {
28          sumsoc += soc;
29          cout << "你答对了"<<"当前得分: "<<sumsoc<<endl<<endl; 
30      }
31      else
32      {
33          cout << "你答错了了"<<"答案是"<<res1<<"   当前得分:"<<sumsoc<<endl<<endl;
34           
35      }
36         
37     }

2,新的界面,增加积分系统,其中难度值的计算跟数的大小和运算符有关,对数目大小决定分数进行了开平方,保证数目大小和运算符占有合理权重,使难度计算更合理。

--------------------------------------------------------------------------------------------------------------------------

由于时间仓促,本程序距离完全体还有些距离,但完成度已经90%。剩下的就是优化下用户友好性之类的。

主要完成功能:1,实现了四则运算算式的随机产生。

       2,实现了将随机产生的四则综合运算算式计算出结果。

待完成功能:1,增加积分系统,生成的随机算式是有分值的,例如1+2+3+4这种运算式肯定和99*98*97*96这种运算式不一样,分值由两方面决定,一是数的大小,一方面是运算符。比如数又大乘除法又多那么分就高。

      2,增加点奖励,比如分数达到多少就说一句鼓励的话或出个图案。

以下是程序部分:

1,itos函数,将整形转换为String型。

1 string itos(int i) 
2 {
3      stringstream s;
4      s << i;
5      return s.str();
6  }

2,num函数,生成100以内随机数。

int num()
{

    return rand()%100;    
}

3,fh函数,生成4以内随机数,用来随机运算符。

int fh()
{
    return rand()%4;
}

4,convert2RPN函数,将字符型的算式有中缀表达式转换为后缀表达式,用来进行后续的计算,是非常重要的函数,承载着极大的工作量。

 

5,calculateRPN函数,用来计算后缀表达式。

float calculateRPN(const string &s) {
    stack<float> stk;
    for (size_t i = 0; i < s.length(); i++) {
        // 如果是数字,就和之前的数字组合起来
        if (isdigit(s.at(i))) {
            int e = atoi(&s.at(i));
            int t = e / 10;
            while (t > 0) {
                i++;
                t /= 10;
            }
            i++;
            stk.push(e);
        }
        else {
            float r = stk.top();
            stk.pop();
            float l = stk.top();
            stk.pop();
            float result;
            switch (s.at(i)) {
            case '+':
                result = l + r;
                break;
            case '-':
                result = l - r;
                break;
            case '*':
                result = l * r;
                break;
            case '/':
                result = l / r;
                break;
            }
            stk.push(result);
        }
    }
    return stk.top();
}  
 


6,主函数

int main()
{
    char ope[4]={'+','-','*','/'};
    
    srand(time(NULL));
    string s;
    s = itos(num())+ope[fh()]+itos(num())+ope[fh()]+itos(num())+ope[fh()]+itos(num());
    cout << s+" "+"=";
     convert2RPN(s);
    cout << calculateRPN(s) << endl;
    return 0;
}

程序结果截图,虽然比较简单,但是还是有些技术含量的,经过后面更新一定会更用户友好。

只做了一点微小的工作,恳请老师和同学批评,谢谢大家。

原文地址:https://www.cnblogs.com/Boxer1994/p/5847683.html