随机生成四则运算式2-NEW+PSP项目计划(补充没有真分数的情况)

PS:这是昨天编写的随机生成四则运算式2的代码:http://www.cnblogs.com/wsqJohn/p/5264448.html 做了一些改进。

补:在上一次的运行中并没有加入真分数参与的运算,创建一个子函数string create_num()来随机生成一个数,大大简便了代码的复杂度。


  1 // 按要求随机生成四则运算  王世强 2015/3/12 
  2 #include<fstream> 
  3 #include<iostream>
  4 #include<stdlib.h>
  5 #include<iomanip>
  6 #include<time.h>
  7 #include<cmath>  
  8 #include<string>  
  9 #include<stdio.h> 
 10 using namespace std;
 11 #define random() (rand()%100000) 
 12 
 13 //符号生成
 14 char create_symbol(int n)
 15 {
 16     int n1,j;    
 17     char symbol[1];
 18     if(n==0)
 19     {
 20         n1=2;
 21     }
 22     else if(n=1)
 23     {
 24         n1=4;
 25     }
 26     j=random()%n1;
 27     if(j==0) symbol[0]='+';
 28     else if(j==1) symbol[0]='-';
 29     else if(j==2) symbol[0]='*';
 30     else symbol[0]='/';
 31     return symbol[0];      
 32  } 
 33  //把数字转换成字符串型
 34  string int_string(int number)
 35  {
 36     char str[200];
 37     itoa(number,str,10);
 38     string str_=str;
 39     return str_; 
 40   } 
 41   //真分数合成一个字符串
 42  string combination1(string str1,string str2,char k) 
 43  {
 44      string equation;
 45      equation=str1+k+str2;
 46      return equation;
 47  } 
 48  //新生成一个数
 49 string create_num(int proper_fs,int range)
 50 { 
 51     int num,num1,num2,fs;
 52     string str_num,str_num1,str_num2;
 53     num=random()%range+1;
 54     str_num=int_string(num);
 55     if(proper_fs==1)
 56     {
 57         fs=random()%2;
 58         if(fs==0)//判断是否生成真分数  
 59         {    
 60             for(;;)
 61             {
 62                 num1=random()%range+1;
 63                 num2=random()%range+1;
 64                 if(num1<num2) break;
 65             }
 66             str_num1=int_string(num1);
 67             str_num2=int_string(num2);
 68             str_num=combination1(str_num1,str_num2,'/');
 69         }
 70     }
 71     return str_num; 
 72   } 
 73  //运算式转换成一个字符串
 74  string combination(string str1,string str2,char k) 
 75  {
 76      string equation;
 77      equation=str1+' '+k+' '+str2;
 78      return equation;
 79  }
 80  //主函数 
 81 int main()
 82 {
 83     srand((int)time(NULL));  //设置时间种子 ,使得程序每次运行的结果都不同 
 84     int num1,num2,num3,num4,count,n,change,amount,shuchu,range,j,repeat=0,bracket,proper_fs;
 85     string str_num1,str_num2,temp;
 86     cout<<"有无乘除法?1有,0没有:"<<endl;
 87     cin>>n;
 88     cout<<"是否有括号?1有,0没有:"<<endl;
 89     cin>>bracket; 
 90     cout<<"是否有真分数?1有,0没有:"<<endl;
 91     cin>>proper_fs; 
 92     cout<<"题目是否在文件中输出?1是,0不是:"<<endl;
 93     cin>>shuchu;
 94     cout<<"请输入数字范围:"<<endl;
 95     cin>>range; 
 96     cout<<"请输入出题数量:"<<endl;
 97     cin>>amount; 
 98     string Equation[amount];
 99     char symbol;
100     ofstream fout;
101     if(shuchu==1)
102     {
103         fout.open("fl.txt");
104         fout<<生成的<<amount<<"道四则运算题如下:"<<endl; 
105     }
106     else 
107     {
108         cout<<生成的<<amount<<"道四则运算题如下:"<<endl; 
109     }
110     for(int i=0;i<amount;i++)
111     {    
112         count=random()%9+2;
113         str_num1=create_num(proper_fs,range);
114         str_num2=create_num(proper_fs,range);
115         symbol=create_symbol(n);
116         Equation[i]=combination(str_num1,str_num2,symbol);
117         if(count>2)
118         {
119             for(count;count>2;count--)
120             {
121                 symbol=create_symbol(n);
122                 str_num1=Equation[i];
123                 if(bracket==1)
124                 {
125                     change=random()%2;
126                     if(change==0)
127                     {
128                        str_num1='('+str_num1+')';
129                      }
130                 } 
131                 symbol=create_symbol(n);
132                 str_num2=create_num(proper_fs,range);    
133                 change=random()%2;
134                 if(change==0)
135                 {
136                     temp=str_num1;
137                     str_num1=str_num2;
138                     str_num2=temp;
139                 }                
140                 Equation[i]=combination(str_num1,str_num2,symbol);
141             }
142         }
143         //判断是否重复
144          for(j=0;j<i;j++)
145          {
146              if(Equation[j]==Equation[i])
147              {
148                 i=i-1;
149                 repeat=1;
150                 break;
151              }
152          }
153          if(repeat!=1)//若不重复,则输出 
154          {
155              if(shuchu==1)
156             {
157                 fout<<Equation[i]<<"="<<endl;
158             }
159             else
160             {
161                 cout<<Equation[i]<<"="<<endl;
162             }
163          }
164        
165     }
166     if(shuchu==1)
167     {
168         fout.close();
169     }
170 }
 

实验结果截图:

   

文件输出时:

总结:原本昨天就把程序写完提交了,但是,想想还可以改进,就又忍不住修改了一下,争取做到更好!

 项目计划总结:

日期&&任务 听课 编写程序 阅读相关书籍 网上查找资料   日总计
周一 2 4 2 2 10
周二   3    1 4
周三          
周四 2 2     4
周五   5  1 1 7
周六    4     4
周日     2   2
周总计 4 18 5 4 31

 时间记录日志:

日期 开始时间 结束时间 中断时间 净时间 活动 备注
3/7 14:00 15:50 10 100 听课 软件工程上课
  16:00 18:20   140 编写程序 编写老师布置的作业
  19:00 21:00  10 110 阅读书籍 《构建之法》
  21:00 24:00 20 160 网上查找资料,编程 作业2,讨论问题
3/8 16:00 18:00  10 110 查资料,编写程序 作业2
  21:00 21:50   50 阅读书籍 《构建之法》
3/9            
3/10 14:00 15:50  10 100 听课 软件工程上课
  19:00 21:20  20 100 查资料,编写程序 休息,聊天,作业2
3/11 8:00 10:05  5 120 写博客 发表本周博客
  16:10 18:30  10 140 修改程序,写博客  测试程序,发表博客
  19:00 19:40   40 阅读书籍 《构建之法》
  19:45 21:45 10 110 写博客,修改程序 休息,聊天,发表博客
3/12 15:20 19:50  30 240 修改程序,写博客 休息,谈论问题,修改,发表博客
3/13 19:00 19:30   30 阅读书籍 计划阅读《梦断代码》

缺陷记录日志:

日期 编号 类型 引入阶段 排除阶段 修复时间 修复缺陷
3/11之前 1 ... ... ... ...  
  描述:之前的忘了统计,下次注意!
3/12 2 20 编码 编译 15min  
  描述:修改累赘部分,采用了调用函数来新生成一个数。
原文地址:https://www.cnblogs.com/wsqJohn/p/5269523.html