软件工程综合实践第二次作业
1759134 张长锋and
1659117 石韫嘉
————————
3.11我们对要做的工作程序进行了一段时间的讨论
我们队完成程序的时间估计:两个小时左右
基本的程序思想:初始想法是一个存放符号的数组array[15],随机间隔存放空格,数字,空格,符号,空格,数字,空格,符号,空格,数字,空格,其中空格用来让左右括号来替换
后来想到4则运算取2种,加起来也就16种,直接switch全部枚举出来
主要考虑和存在的问题:
首先除号后面的运算结果或者数字不能为0 2+3÷0 3÷(2-2)
括号也不能忘记括号的位置要随机
注意程序语言的规范
答案还在0到100之间
4个运算符取两个 总共16种取法 switch16次
第一次完成的代码:
#include<bits/stdc++.h>
using namespace std;
void main(){
cout<<"exercise of four operations"<<endl;
int n=0;
int answer;
int user_answer;
int right_answer=0;
int a=1+rand()%100;
int b=1+rand()%100;
int c=1+rand()%100;
int operation=1+rand()%16;
while(n<300){
switch(operation){
case'1':
cout<<a<<"+"<<b<<"+"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a+b+c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
case'2':
cout<<a<<"+"<<b<<"-"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a+b-c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
if(answer>=0){
n++;}
continue;
case'3':
cout<<a<<"+"<<b<<"*"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a+b*c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
case'4':
cout<<a<<"+"<<b<<"/"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a+b/c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
case'5':
cout<<a<<"-"<<b<<"+"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a-b+c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
if(answer>=0){
n++;}
continue;
case'6':
cout<<a<<"-"<<b<<"-"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a-b-c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
if(answer>=0){
n++;}
continue;
case'7':
cout<<a<<"-"<<b<<"*"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a-b*c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
if(answer>=0){
n++;}
continue;
case'8':
cout<<a<<"-"<<b<<"/"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a-b/c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
if(answer>=0){
n++;}
continue;
case'9':
cout<<a<<"*"<<b<<"+"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a*b+c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
case'10':
cout<<a<<"*"<<b<<"-"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a*b-c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
if(answer>=0){
n++;}
continue;
case'11':
cout<<a<<"*"<<b<<"*"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a*b*c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
case'12':
cout<<a<<"*"<<b<<"/"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a*b/c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
case'13':
cout<<a<<"/"<<b<<"+"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a/b+c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
case'14':
cout<<a<<"/"<<b<<"-"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a/b-c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
if(answer>=0){
n++;}
continue;
case'15':
cout<<a<<"/"<<b<<"*"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a/b*c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
case'16':
cout<<a<<"/"<<b<<"/"<<c;
cout<<"your answer:"
cin>>user_answer;
answer=a/b/c;
if(user_answer==answer){
cout<<"yours is right"<<endl;
right_answer++;
}
else
cout<<"yours is wrong";
n++;
continue;
}
}
cout<<"right answers:"<<right_answer<<endl;
}
运行后的程序截图
枚举出16种情况后,我们对程序做出了测试发现根本跑不起来,程序的代码主要由我的搭档完成。对于出现的这种情况我们有一定的预见,但还是感到很失望。
我们重新进行了系统化的分析:
循环函数,因此想到用for()函数
随机产生两个数,就想到用rand()函数。
注: 头文件#include<stdlib.h>
我在调试程序的过程中,随机数一直不变,通过查资料我了解到,要想让随机数改变,需要提供一个种子,相同的种子会产生相同的随机数,因此必须在rand()函数前加上 srand(time(NULL));语句,且头文件中加上#include<time.h>,这样就能保证两个随机数能够该变了。
随机缠上四则运算要想每道题+ ,-,*,/都是随机的,我就想到了用分支函数,通过用if 语句,我感觉有点麻烦,然后我就想到了switch()函数,且通过随机产生0,1,2,3四个数来进行分支,比较方便一点
进行的代码如下:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int x,y,z,t;
srand(time(NULL));
cout<<"小学二年级四则运算测试题"<<endl;
for(int i=0;i<100;i++)
{
x=rand()%100;
y=rand()%100;
z=rand()%4;
switch(z)
{
case 0:
cout<<x<<"+"<<y<<"="<<endl;
break;
case 1:
if(x<y)
{
t=x;
x=y;
y=t;
}
cout<<x<<"-"<<y<<"="<<endl;
break;
case 2:
cout<<x<<"*"<<y<<"="<<endl;
break;
case 3:
if(y!=0)
{
cout<<x<<"/"<<y<<"="<<endl;
}
else
{
i=i-1;
}
break;
default:
cout<<"超出测试范围"<<endl;
break;
}
}
cout<<"恭喜你已经完成了100道数学测试题"<<endl;
return 0;
}
以上代码有参考百度,是在3.14这天进行的
关于结对编程:
并没有想象中的那么糟糕,当然也有很多不愉快的地方,对于编程能力并不是特别强的我们,每一次的交流讨论都是一次学习和进步,和自己一个人编程还是有很大区别的,可以更快的解决遇到的问题,并且知识的碰撞让我们学的更快。
在代码遇到问题的时候,我们还求助了班里的大神,无形之中也加深了自己和对方的交流。通过本次的结对编程,我们的收货还是很大的。