结对编程——四则运算

目录

一、实验目的

二、实验方法

三、实验内容

四、实验代码

五、实验结果

六、实验心得

 

一、实验目的

体验结对编程,两人合力完成一个编程(队友2152231)。

二、实验方法

两人使用一台机器,使用Visual Studio 2019进行编程,一个人输入代码,而另一个人审查他输入的每一行代码,在进行角色互换。

三、实验内容

编写一个四则运算程序(c/c++),限制100以内的数字,运算符1-2个,并检查答案是否正确。

四、实验代码

队员A(2152231)编写部分:

复制代码
 1 void DisplayFenshu(int a[][2],int w,int m) {
 2     if(w==1) {
 3         for(int q=0; q<m; q++) {
 4             if(a[q][0]==0)
 5                 cout<<"0"<<'\t';
 6             else
 7                 cout<<a[q][0]<<"/"<<a[q][1]<<'\t';
 8             if(q%5==4) {
 9                 cout<<endl;
10             }
11         }
12     } else {
13     };
14 }
15 void DealInt(int m,int a[]) {
16 
17     for(int p=0; p<m; p++) {
18         int i=(int)rand()%10;
19         int j=(int)rand()%10;
20         int k=(int)rand()%100/25;
21         switch(k) {
22             case 0:
23                 cout<<i<<"+"<<j<<"=";
24                 a[p]=i+j;
25                 break;
26             case 1:
27                 cout<<i<<"-"<<j<<"=";
28                 a[p]=i-j;
29                 break;
30             case 2:
31                 cout<<i<<"*"<<j<<"=";
32                 a[p]=i*j;
33                 break;
34             case 3:
35                 try {
36                     a[p]=i/j;
37                     cout<<i<<"/"<<j<<"=";
38                 } catch(...) {
39                     p--;
40                 }
41 
42         }
43 
44         if(p%5==4) {
45             cout<<endl;
46         } else {
47             cout<<'\t';
48         }
49     }
50 }
51 void DisplayInt(int a[],int w,int m) {
52     if(w==1) {
53         for(int q=0; q<m; q++) {
54             cout<<a[q]<<'\t';
55             if(q%5==4) {
56                 cout<<endl;
57             }
58         }
59     } else {
60     };
61 }
复制代码

队员B(1959137)编写部分:

复制代码
 1 #include<iostream>
 2 #include<stdlib.h>
 3 #include<conio.h>
 4 using namespace std;
 5 void DealFenshu(int m, int a[][2]) {
 6     for(int p=0; p<m; p++) {
 7         int i=(int)rand()%10;
 8         int j=(int)rand()%10;
 9         while(j==0||i>=j) {
10             i=(int)rand()%10;
11             j=(int)rand()%10;
12         }
13         int x=(int)rand()%10;
14         int y=(int)rand()%10;
15         while(y==0||x>=y) {
16             x=(int)rand()%10;
17             y=(int)rand()%10;
18         }
19         int k=(int)rand()%100/25;
20         switch(k) {
21             case 0:
22                 cout<<"("<<i<<"/"<<j<<")"<<"+"<<"("<<x<<"/"<<y<<")"<<"=";
23                 a[p][0]=i*y+x*j;
24                 a[p][1]=j*y;
25                 break;
26             case 1:
27                 cout<<"("<<i<<"/"<<j<<")"<<"-"<<"("<<x<<"/"<<y<<")"<<"=";
28                 a[p][0]=i*y-x*j;
29                 a[p][1]=j*y;
30                 break;
31             case 2:
32                 cout<<"("<<i<<"/"<<j<<")"<<"*"<<"("<<x<<"/"<<y<<")"<<"=";
33                 a[p][0]=i*x;
34                 a[p][1]=j*y;
35                 break;
36             case 3:
37                 a[p][0]=i*y;
38                 a[p][1]=j*x;
39                 cout<<"("<<i<<"/"<<j<<")"<<"/"<<"("<<x<<"/"<<y<<")"<<"=";
40         }
41         if(p%5==4) {
42             cout<<endl;
43         } else {
44             cout<<'\t';
45         }
46     }
47 
48 }
49 
50 
51 int main() {
52     int p;
53     do {
54         system("cls");
55         int a[1000],b[1000][2];
56         int m,n,w;
57         cout<<"请输入生成的四则运算题个数:";
58         cin>>m;
59         cout<<endl;
60         cout<<"请输入要生成的四则运算种类(输入1为整数,否则为真分数):";
61         cin>>n;
62         cout<<endl;
63         if(n==1) {
64             DealInt(m,a);
65             cout<<endl;
66         } else {
67             DealFenshu(m,b);
68             cout<<endl;
69         }
70         cout<<"是否输出答案(输入1则输出答案否则不输出答案)"<<endl;
71         cin>>w;
72         if(n==1) {
73             DisplayInt(a,w,m);
74         } else {
75             DisplayFenshu(b,w,m);
76         }
77         cout<<endl;
78         cout<<"是否继续生成运算题(输入1则生成否则不生成)"<<endl;
79         cin>>p;
80         cout<<endl;
81     } while(1==p);
82 
83 }
复制代码

五、实验结果

六、实验心得

  结对编程的好处就在于快速发现问题,节省时间和找debug的精力。由于身边有个观察员角色的存在,在编写代码时,一旦出现输入错误,就会有人及时的提醒。并且,在设计代码时,有个同伴可以一起讨论,融合两个人不同的见解和观点,我们往往可以得出更加准确且更加高效的设计思路。都为我们在完成代码后的debug过程省去了大量的时间。我们小组在结对编程过程中出现的最多的问题就是标点符号以及字母大小写问题的出现,编写代码过程中就会感觉到,身边有个观察员可以及时提醒错误,大大提高了效率。同时在意见发生分歧的时候,可以快速讨论,得到最合适的解决办法。
  同时两个人可以轮流编程,不会感觉到太疲惫,程序设计思路可以共享,互相为对方避免错误,同时结对编程也有个别不好的地方,在某名成员编程时,观察员可能出现了不一样的意见,立马就指出来,会打断驾驶员的思路,可能会导致工作量滞后。

posted @   葡萄吐不吐皮  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示