第二次作业结对编程

    本次结对编程是为了完成四则运算生成器,,针对这次结对编程的题目  

    我们决定支持最多 10 个运算符的应用程序,支持括号的运算,可以判断题目的对错。

    这次代码的难点在于不仅仅要保证题目不重复,还要完成多功能。

   需求分析:

  1. 测试加减乘除功能。
  2. 测试对于各种参数的支持。

对于这次结对编程我的感受与平时单独编程时候有了区别,不仅仅是我一个人的思路,在互动的过程中两个人之间要能够相互学习,相互讨论,发挥出1+1>2的能力。

代码规范,需求分析 (包括学习新技术),具体设计由我的搭档完成,具体编码,测试(自我测试,修改代码,提交修改),报告由我完成。

具体代码如下

#include "Computing.h"
using namespace std;

int main() {
Main m;

m.exec();
return 0;
}

/*
* Calculate.cpp
*
* Created on: Sep 8, 2017
* Author: moon
*/

#include "Calculate.h"
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
using namespace std;

Calculate::Calculate() {
this->result_true = 0;
this->result_user = 0;
this->type = 0;
this->x = 0;
this->y = 0;
}

Calculate::~Calculate() {
}

int Calculate::calculate(void) {
int ret;

switch (this->type) {
case ADD:
result_true = x + y;
if (result_user == result_true)
ret = 0;
else
ret = -1;
break;

case SUB:
result_true = x - y;
if (result_user == result_true)
ret = 0;
else
ret = -1;
break;

case MUL:
result_true = x * y;
if (result_user == result_true)
ret = 0;
else
ret = -1;
break;

case DIV:
result_true = x / y;
if ((result_user - result_true)<0.01||(result_true-result_user)<0.01)
ret = 0;
else
ret = -1;
break;

default:
cout << "该版本只支持四则运算!" << endl;
ret = -2;
break;
}
cout<<"正确答案:"<< setprecision(2)<<result_true;
if(ret==0)
cout<<"\t正确"<<endl<<endl;
else if(ret==-1)
cout<<"\t错误"<<endl<<endl;
return ret;
}

/*
* Main.cpp
*
* Created on: Sep 8, 2017
* Author: moon
*/

#include "Main.h"
#include <iostream>
using namespace std;

Main::Main() {
this->finished_num = 0;
this->grade = 0;
this->right_num = 0;
this->topic_num = 0;
this->wrong_num = 0;
}

Main::~Main() {

}

void Main::exec(void) {
int num;
float x;
float y;
char type;
float result;

cout << "请输入题目数量:";
cin >> this->topic_num;

num = this->topic_num;
while (num > 0) {
//system("clear");
show();
srand(time(0));
x = rand() % 100 + 1;
y = rand() % 100 + 1;
type = rand() % 4 + 1;
calculate.setX(x);
calculate.setY(y);
calculate.setType(type);
show_topic(type,x,y);
cin >> result;

calculate.setResultUser(result);
if (calculate.calculate() == 0)
this->right_num++;
else
this->wrong_num++;

this->finished_num++;
num--;
}
show_grade();
}

void Main::show(void) {
cout << "总题数:" << this->topic_num << "\t" << this->finished_num + 1 << "/"
<< this->topic_num << "(已做/总数)" << endl;
}

void Main::show_topic(char type,int x,int y) {
switch (type) {
case ADD:
cout << x << "+" << y << "=";
break;

case SUB:
cout << x << "-" << y << "=";
break;

case MUL:
cout << x << "*" << y << "=";
break;

case DIV:
cout << x << "/" << y << "=";
break;

}
}

void Main::show_grade(void)
{
this->grade=this->right_num*2;
cout<<"正确数量:"<<this->right_num<<"\t总数:"<<this->topic_num<<"\t得分:"<<this->grade<<endl;
}

 通过结对编程,能够从另一角度完成编写实现,同时进行改进。由于第一次尝试结对编程,在测试过程中难免有所分歧,搭档是一个有想法的人,但还会听取我的看法,相互讨论最终执行,在网络教程和其他同学的帮助下,终于完成了本次结对编程。由于搭档的建议下,代码中定义变量等等比较规范,使得在出现错误的时候我们不容易迷失方向,都能够看得懂且互相协作。最后,必要的注释可以让我们更快地理解整个程序。结对编程中我们两个人相互学习。发挥出1+1>2的能力。

猜你喜欢

转载自www.cnblogs.com/hydx123/p/10541829.html