博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
软件工程综合实践第二次作业
阅读量:5072 次
发布时间:2019-06-12

本文共 5234 字,大约阅读时间需要 17 分钟。

软件工程综合实践第二次作业

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这天进行的  

  

关于结对编程:

并没有想象中的那么糟糕,当然也有很多不愉快的地方,对于编程能力并不是特别强的我们,每一次的交流讨论都是一次学习和进步,和自己一个人编程还是有很大区别的,可以更快的解决遇到的问题,并且知识的碰撞让我们学的更快。

 

在代码遇到问题的时候,我们还求助了班里的大神,无形之中也加深了自己和对方的交流。通过本次的结对编程,我们的收货还是很大的。

转载于:https://www.cnblogs.com/cy1024/p/10535325.html

你可能感兴趣的文章
洛谷 [P1948] 电话线
查看>>
MM主要的表和主要字段
查看>>
数据验证随想
查看>>
k8s etcd 集群配置安装
查看>>
SQL反模式学习笔记16 使用随机数排序
查看>>
MySQL_第一部基础入门
查看>>
XML 树结构,语法规则,元素,属性,验证及其解析
查看>>
hdu 4850 Wow! Such String!(欧拉回路)
查看>>
rlwrap(在sqlplus下使用上下键)
查看>>
重新初始化VS2010
查看>>
TCP的ACK确认系列 — 延迟确认
查看>>
uboot的FIT功能
查看>>
ZkApi的方法跨域访问ZkResource的静态资源文件出现的问题
查看>>
ZendStudio快捷键 注释的快捷键
查看>>
Headfirst设计模式的C++实现——迭代器(Iterator)
查看>>
Unity图形功能的硬件要求 Hardware Requirements for Unity's Graphics Features
查看>>
CellClick点击一行,左边编辑数据绑定
查看>>
iOS学习笔记
查看>>
easyui的combobox/combogrid检索
查看>>
GRASP软件设计的模式和原则
查看>>