结对编程个人项目分析

时间:2019-09-19
本文章向大家介绍结对编程个人项目分析,主要包括结对编程个人项目分析使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 项目需求

个人项目:中小学数学卷子自动生成程序

用户:

小学、初中和高中数学老师。

功能:

1、命令行输入用户名和密码,两者之间用空格隔开(程序预设小学、初中和高中各三个账号,具体见附表),如果用户名和密码都正确,将根据账户类型显示“当前选择为XX出题”,XX为小学、初中和高中三个选项中的一个。否则提示“请输入正确的用户名、密码”,重新输入用户名、密码;

2、登录后,系统提示“准备生成XX数学题目,请输入生成题目数量(输入-1将退出当前用户,重新登录):”,XX为小学、初中和高中三个选项中的一个,用户输入所需出的卷子的题目数量,系统默认将根据账号类型进行出题。每道题目的操作数在1-5个之间,操作数取值范围为1-100;

3、题目数量的有效输入范围是“10-30”(含10,30,或-1退出登录),程序根据输入的题目数量生成符合小学、初中和高中难度的题目的卷子(具体要求见附表)。同一个老师的卷子中的题目不能与以前的已生成的卷子中的题目重复(以指定文件夹下存在的文件为准,见5);

4、在登录状态下,如果用户需要切换类型选项,命令行输入“切换为XX”,XX为小学、初中和高中三个选项中的一个,输入项不符合要求时,程序控制台提示“请输入小学、初中和高中三个选项中的一个”;输入正确后,显示“”系统提示“准备生成XX数学题目,请输入生成题目数量”,用户输入所需出的卷子的题目数量,系统新设置的类型进行出题;

5、生成的题目将以“年-月-日-时-分-秒.txt”的形式保存,每个账号一个文件夹。每道题目有题号,每题之间空一行;

6、个人项目9月17日早上8点以前提交至各自负责的助教处。提交方式:工程文件打包,压缩包名为“几班+姓名.rar”。

附表-1:账户、密码

账户类型

账户

密码

备注

小学

张三1

123

张三2

123

张三3

123

初中

李四1

123

李四2

123

李四3

123

高中

王五1

123

王五2

123

王五3

123

附表-2:小学、初中、高中题目难度要求

小学

初中

高中

难度要求

+,-,*./

平方,开根号

sin,cos,tan

备注

只能有+,-,*./和()

题目中至少有一个平方或开根号的运算符

题目中至少有一个sin,cos或tan的运算符

 

代码分析

 优点

使用结构体对用户信息进行初始化,十分方便

struct Student
{
    char name[20];
    char degree[5];
    char password[20];    
};


Student stu[9] ={{"张三1","小学","123"},{"张三2","小学","123"},{"张三3","小学","123"},
                 {"李四1","初中","123"},{"李四2","初中","123"},{"李四3","初中","123"},
                 {"王五1","高中","123"},{"王五2","高中","123"},{"王五1","高中","123"}};

反例

class UserDB{
public:
    string name;
    string password;
    string grade;
};
UserDB user_db[9];
void load_db(){
    user_db[0].name="张三1";
    user_db[0].password="123";
    user_db[0].grade="小学";

    user_db[1].name="张三2";
    user_db[1].password="123";
    user_db[1].grade="小学";

    user_db[2].name="张三3";
    user_db[2].password="123";
    user_db[2].grade="小学";

    user_db[3].name="李四1";
    user_db[3].password="123";
    user_db[3].grade="初中";

    user_db[4].name="李四2";
    user_db[4].password="123";
    user_db[4].grade="初中";

    user_db[5].name="李四3";
    user_db[5].password="123";
    user_db[5].grade="初中";

    user_db[6].name="王五1";
    user_db[6].password="123";
    user_db[6].grade="高中";

    user_db[7].name="王五2";
    user_db[7].password="123";
    user_db[7].grade="高中";

    user_db[8].name="王五3";
    user_db[8].password="123";
    user_db[8].grade="高中";
}

使用strftime函数获取格式化日期信息

static string getCurrentTimeStr()
{
    time_t t = time(NULL);
    char ch[64] = {0};
    strftime(ch, sizeof(ch) - 1, "%Y-%m-%d-%H-%M-%S", localtime(&t));     //年-月-日-时-分-秒
    return ch;
}

  strftime是一种计算机函数,根据区域设置格式化本地时间/日期,函数的功能将时间格式化,或者说格式化一个时间字符串。我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。

  头文件:time.h

size_t strftime(
char *strDest,
size_t maxsize,
const char *format,
const  struct tm *timeptr
);

出题逻辑完整,能随机生成任意形式的题目

1.cos(7)+32+cos(tan(17))*cos(cos(tan(20)))

2.cos(76)+5-32+sin(cos(66))+cos(sin(cos(96)))

3.√(cos(5))*cos(tan(47))/√(cos(cos(tan(81))))

4.tan(25)+tan(41)+tan(75)

5.√(73)*89/53/tan(63)-√(tan(93))

6.tan(sin(cos(tan(18))))/sin(cos(tan(95)))

7.sin(tan(sin(tan(82))))

8.cos(47)-cos(63)

9.√(sin(sin(57)))/sin(19)

10.√(sin(sin(cos(tan(87)))))/sin(cos(tan(46)))

11.(cos(64))/77/sin(cos(1))^2 -(cos(sin(cos(77)))^2 )

12.sin(tan(sin(tan(5))))

13.35*(58)*73/tan(40)/tan(27)

14.√(sin(sin(tan(51))))

15.(√(sin(cos(32))))*sin(cos(35))-sin(cos(sin(cos(1))))^2 

16.√(tan(sin(tan(29))))-sin(tan(95))

17.tan(sin(cos(tan(2))))

18.57/cos(23)*cos(82)

19.√(sin(tan(sin(tan(14)))))/sin(tan(33))

20.tan(tan(56))

21.(6)/tan(39)-tan(54)

22.cos(tan(100))+55*cos(tan(70))-cos(tan(cos(tan(60))))

23.38*56/tan(75)*tan(53)

24.(tan(61))-sin(tan(29)^2 )-(sin(tan(54)))^2 

25.(35)/tan(22)^2 /(tan(37))^2 

26.(59)+sin(cos(tan(62)^2 ))/(sin(cos(tan(97))))^2 

27.17*(37)-64-sin(19)/sin(92)

28.49-(11)+cos(tan(36))*cos(tan(77))

29.tan(sin(tan(47)))

30.sin(93)+sin(55)/sin(74)
View Code

缺点

出题函数长达500行,将小学、初中、高中题目完全分开,导致了大量功能相似的代码行

  1 void Chuti(Student stu,char *degree0,int num)
  2 {
  3     int num0=1;
  4     cout<<degree0<<"出题成功"<<endl;
  5     srand((unsigned)time(NULL));
  6     ofstream outfile;
  7     const char *filename1=getCurrentTimeStr().c_str();
  8     char filename[100]="";
  9     strcat(filename,filename1);
 10     strcat(filename,".txt");
 11     char path[100]="";
 12     char *xiegang="\\\\";
 13     const char *file=getFile();
 14     strcat(path,file);
 15     strcat(path,stu.name);
 16     char path0[100]="";
 17     
 18     strcat(path,xiegang);
 19     strcat(path,degree0);
 20     strcpy(path0,path);
 21     strcat(path,xiegang);
 22     strcat(path,filename);
 23     outfile.open(path);
 24     if(!outfile)
 25     {
 26         char command[100]="mkdir ";
 27         strcat(command,stu.name);
 28         strcat(command,xiegang);
 29         strcat(command,degree0);
 30         system(command);
 31         outfile.open(path);
 32     }
 33     if(strcmp(degree0,"小学")==0)
 34     {
 35         while(num0<=num)
 36         {
 37             outfile<<num0<<'.';
 38             int cznum=rand()%5+1;
 39             bool kuohao=rand()%2;
 40             bool shoukuohao=0;
 41             
 42             if (cznum<3)
 43             {
 44                 kuohao=0;
 45             }
 46             int count=0;
 47             int kuohaouse=0;
 48             int cznum0=cznum-1;
 49             while(cznum--)
 50             {
 51                 bool choose0=rand()%2;
 52                 bool choose1=rand()%2;
 53                 int temp=rand()%100+1;
 54                 int jjcc=rand()%4+1;
 55                 if(kuohao&&cznum>=2&&choose0&&!kuohaouse)
 56                 {
 57                     outfile<<"(";
 58                     kuohaouse=1;
 59                     if(cznum0==cznum)
 60                     {
 61                         shoukuohao=1;
 62                     }
 63                 }
 64                 outfile<<temp;
 65                 count++;
 66                 if(kuohao&&kuohaouse&&choose1&&count>=2)
 67                 {
 68                     outfile<<")";
 69                     kuohao=0;
 70                     kuohaouse=0;
 71                 }
 72                 if(kuohao&&kuohaouse&&cznum==0)
 73                 {
 74                     outfile<<")";
 75                     kuohaouse=0;
 76                 }
 77                 if(shoukuohao&&kuohaouse&&cznum==1)
 78                 {
 79                     outfile<<")";
 80                     kuohaouse=0;
 81                 }
 82                 if(cznum>0)
 83                 {
 84                     if(jjcc==1) outfile<<"+";
 85                     else if(jjcc==2) outfile<<"-";
 86                     else if(jjcc==3) outfile<<"*";
 87                     else if(jjcc==4) outfile<<"/";
 88                 }
 89             }
 90             num0++;
 91             outfile<<endl<<endl;
 92         }
 93     } 
 94     if(strcmp(degree0,"初中")==0)
 95     {
 96         while(num0<=num)
 97         {
 98         //    bool kuohao=rand()%2+1;
 99             
100             bool pingfang=rand()%2;
101             bool genhao=rand()%2; 
102             while(!(pingfang||genhao))
103             {
104                 pingfang=rand()%2;
105                 genhao=rand()%2; 
106             }
107             outfile<<num0<<'.';
108             int cznum=rand()%5+1;
109             bool kuohao=rand()%2;
110             bool shoukuohao=0;
111             
112             bool pingfangout=rand()%2;
113             bool genhaoout=rand()%2;
114             
115             bool pingfanguse=0;
116             bool genhaouse=0;
117             
118             int numkh=0;
119             if (cznum<3)
120             {
121                 kuohao=0;
122             }
123             int count0=0;
124             int count1=0; 
125             int count=0;
126             int kuohaouse=0;
127             int cznum0=cznum-1;
128             while(cznum--)
129             {
130                 
131                 bool choose0=rand()%2;
132                 bool choose1=rand()%2;
133                 int temp=rand()%100+1;
134                 int jjcc=rand()%4+1;
135                 if(pingfang&&pingfangout&&!pingfanguse)
136                 {
137                     outfile<<"(";
138                     numkh++;
139                     pingfanguse=1;
140                 }
141                 
142                 if(genhao&&genhaoout&&!genhaouse)
143                 {
144                     outfile<<"√(";
145                     numkh++;
146                     genhaouse=1;
147                 }
148                 
149                 if(cznum==0||cznum==1)
150                 {
151                     if(pingfang==1)
152                     {
153                         outfile<<"(";
154                         numkh++;
155                         pingfanguse=1;    
156                     }
157                     if(genhao==1)
158                     {
159                         outfile<<"√(";
160                         numkh++;
161                         genhaouse=1;
162                     }
163                 }
164                 
165                 
166                 
167                 if(kuohao&&cznum>=2&&choose0&&!kuohaouse)
168                 {
169                     outfile<<"(";
170                     numkh++;
171                     kuohaouse=1;
172                     if(cznum0==cznum)
173                     {
174                         shoukuohao=1;
175                     }
176                 }
177                 outfile<<temp;
178                 count++;
179                 count0++;
180                 count1++;
181                 
182                 if(genhao&&genhaouse&&cznum==0&&numkh>0)
183                 {
184                     outfile<<")";
185                     numkh--;
186                     genhaouse=0;
187                 }
188                 if(genhao&&genhaoout&&genhaouse&&count1>=1&&numkh>0)
189                 {
190                     outfile<<")";
191                     numkh--;
192                     genhaouse=0;
193                     count1=0;
194                 }
195                 
196                 if(pingfanguse&&cznum==0&&numkh>0)
197                 {
198                     outfile<<")^2 ";
199                     numkh--;
200                     pingfanguse=0;
201                 }
202                 if(pingfang&&pingfangout&&pingfanguse&&count0>=1&&numkh>0)
203                 {
204                     outfile<<")^2 ";
205                     numkh--;
206                     pingfanguse=0;
207                     count0=0;
208                 }
209                 
210                 if(kuohao&&kuohaouse&&choose1&&count>=2&&numkh>0)
211                 {
212                     outfile<<")";
213                     numkh--;
214                     kuohaouse=0;
215                     count=0;
216                 }
217                 if(kuohao&&kuohaouse&&cznum==0&&numkh>0)
218                 {
219                     outfile<<")";
220                     numkh--;
221                     kuohaouse=0;
222                 }
223                 if(shoukuohao&&kuohaouse&&cznum==1&&numkh>0)
224                 {
225                     outfile<<")";
226                     numkh--;
227                     kuohaouse=0;
228                 }
229                 
230                 while(numkh>0)
231                 {
232                     numkh--;
233                     outfile<<")";
234                     
235                 } 
236                 
237                 if(cznum>0)
238                 {
239                     if(jjcc==1) outfile<<"+";
240                     else if(jjcc==2) outfile<<"-";
241                     else if(jjcc==3) outfile<<"*";
242                     else if(jjcc==4) outfile<<"/";
243                 }
244                 
245             }
246             num0++;
247             outfile<<endl<<endl;
248         }
249     } 
250     if(strcmp(degree0,"高中")==0)
251     {
252         while(num0<=num)
253         {
254             bool pingfang=rand()%2;
255             bool genhao=rand()%2; 
256             bool sincal=rand()%2;
257             bool coscal=rand()%2;
258             bool tancal=rand()%2; 
259             while(!(sincal||coscal||tancal))
260             {
261                 sincal=rand()%2;
262                 coscal=rand()%2;
263                 tancal=rand()%2; 
264             }
265             int numkh=0;
266             outfile<<num0<<'.';
267             int cznum=rand()%5+1;
268             bool kuohao=rand()%2;
269             bool shoukuohao=0;
270             
271             bool pingfangout=rand()%2;
272             bool genhaoout=rand()%2;
273             
274             bool pingfanguse=0;
275             bool genhaouse=0;
276             
277             bool sinout=rand()%2;
278             bool cosout=rand()%2;
279             bool tanout=rand()%2;
280             
281             bool sinuse=0;
282             bool cosuse=0;
283             bool tanuse=0;
284             
285             if (cznum<3)
286             {
287                 kuohao=0;
288             }
289             int count0=0;
290             int count1=0; 
291             int count=0;
292             int count2=0;
293             int count3=0;
294             int count4=0;
295             int kuohaouse=0;
296             int cznum0=cznum-1;
297             while(cznum--)
298             {
299                 
300                 bool choose0=rand()%2;
301                 bool choose1=rand()%2;
302                 int temp=rand()%100+1;
303                 int jjcc=rand()%4+1;
304                 if(pingfang&&pingfangout&&!pingfanguse)
305                 {
306                     outfile<<"(";
307                     numkh++;
308                     pingfanguse=1;
309                 }
310                 
311                 if(genhao&&genhaoout&&!genhaouse)
312                 {
313                     outfile<<"√(";
314                     numkh++;
315                     genhaouse=1;
316                 }
317                 
318                 
319                 if(sincal&&sinout&&!sinuse)
320                 {
321                     outfile<<"sin(";
322                     numkh++;
323                     sinuse=1;
324                 }
325                 if(coscal&&cosout&&!cosuse)
326                 {
327                     outfile<<"cos(";
328                     numkh++;
329                     cosuse=1;
330                 }
331                 if(tancal&&tanout&&!tanuse)
332                 {
333                     outfile<<"tan(";
334                     numkh++;
335                     tanuse=1;
336                 }
337                 
338                 if(cznum==0||cznum==1)
339                 {
340                     if(sincal==1)
341                     {
342                         outfile<<"sin(";
343                         numkh++;
344                         sinuse=1;    
345                     }
346                     if(coscal==1)
347                     {
348                         outfile<<"cos(";
349                         numkh++;
350                         cosuse=1;    
351                     }
352                     if(tancal==1)
353                     {
354                         outfile<<"tan(";
355                         numkh++;
356                         tanuse=1;    
357                     }
358                 }
359                 
360                 if(kuohao&&cznum>=2&&choose0&&!kuohaouse)
361                 {
362                     outfile<<"(";
363                     numkh++;
364                     kuohaouse=1;
365                     if(cznum0==cznum)
366                     {
367                         shoukuohao=1;
368                     }
369                 }
370                 outfile<<temp;
371                 
372                 if(sinuse&&cznum==0&&numkh>0)
373                 {
374                     outfile<<")";
375                     numkh--;
376                     sinuse=0;
377                 }
378                 if(sincal&&sinout&&sinuse&&count2>=2&&numkh>0)
379                 {
380                     outfile<<")";
381                     numkh--;
382                     sinuse=0;
383                     count2=0;
384                 }
385                 if(sincal&&sinout&&genhaouse&&count2==1&&numkh>0)
386                 {
387                     outfile<<")";
388                     numkh--;
389                     sinuse=0;
390                 }
391                 
392                 if(cosuse&&cznum==0&&numkh>0)
393                 {
394                     outfile<<")";
395                     numkh--;
396                     cosuse=0;
397                 }
398                 if(coscal&&cosout&&cosuse&&count3>=2&&numkh>0)
399                 {
400                     outfile<<")";
401                     numkh--;
402                     cosuse=0;
403                     count2=0;
404                 }
405                 if(coscal&&cosout&&genhaouse&&count3==1&&numkh>0)
406                 {
407                     outfile<<")";
408                     numkh--;
409                     cosuse=0;
410                 }
411                 
412                 if(tanuse&&cznum==0&&numkh>0)
413                 {
414                     outfile<<")";
415                     numkh--;
416                     tanuse=0;
417                 }
418                 if(tancal&&tanout&&tanuse&&count4>=2&&numkh>0)
419                 {
420                     outfile<<")";
421                     numkh--;
422                     tanuse=0;
423                     count2=0;
424                 }
425                 if(tancal&&tanout&&genhaouse&&count4==1&&numkh>0)
426                 {
427                     outfile<<")";
428                     numkh--;
429                     tanuse=0;
430                 }
431                 
432                 
433                 if(genhaouse&&cznum==0&&numkh>0)
434                 {
435                     outfile<<")";
436                     numkh--;
437                     genhaouse=0;
438                 }
439                 if(genhao&&genhaoout&&genhaouse&&count1>=2&&numkh>0)
440                 {
441                     outfile<<")";
442                     numkh--;
443                     genhaouse=0;
444                     count1=0;
445                 }
446                 if(genhao&&genhaoout&&genhaouse&&count1==1&&numkh>0)
447                 {
448                     outfile<<")";
449                     numkh--;
450                     genhaouse=0;
451                 }
452                 
453                 
454                 if(pingfanguse&&cznum==0&&numkh>0)
455                 {
456                     outfile<<")^2 ";
457                     numkh--;
458                     pingfanguse=0;
459                 }
460                 if(pingfang&&pingfangout&&pingfanguse&&count0>=2&&numkh>0)
461                 {
462                     outfile<<")^2 ";
463                     numkh--;
464                     pingfanguse=0;
465                     count0=0;
466                 }
467                 if(pingfang&&pingfangout&&pingfanguse&&count0==1&&numkh>0)
468                 {
469                     outfile<<")^2 ";
470                     numkh--;
471                     pingfanguse=0;
472                 }
473                 
474                 
475                 if(kuohao&&kuohaouse&&choose1&&count>=2&&numkh>0)
476                 {
477                     outfile<<")";
478                     numkh--;
479                     kuohao=0;
480                     kuohaouse=0;
481                 }
482                 if(kuohao&&kuohaouse&&cznum==0&&numkh>0)
483                 {
484                     outfile<<")";
485                     numkh--;
486                     kuohaouse=0;
487                 }
488                 if(shoukuohao&&kuohaouse&&cznum==1&&numkh>0)
489                 {
490                     outfile<<")";
491                     numkh--;
492                     kuohaouse=0;
493                 }
494                 count0++;
495                 count1++;
496                 count++;
497                 count2++;
498                 count3++;
499                 count4++;
500                 while(numkh>0)
501                 {
502                     numkh--;
503                     outfile<<")";
504                 }
505                 if(cznum>0)
506                 {
507                     if(jjcc==1) outfile<<"+";
508                     else if(jjcc==2) outfile<<"-";
509                     else if(jjcc==3) outfile<<"*";
510                     else if(jjcc==4) outfile<<"/";
511                 }
512             }
513             num0++;
514             outfile<<endl<<endl;
515         }
516     } 
517 }
View Code

如果能将这些相似功能的代码整合成一个模块,多次调用(如+、-、*、/、括号、幂运算的添加)代码量至少能缩短1/3还便于维护

总结

  通过这次结对编程对对方代码的分析,收获良多。和其他人合作,能让你观察到其他人的思考过程。你还可以看到他们写代码的方式。你能学会他们使用的工具。对于很多人来说,找到其他开发者一起合作,是一件奢侈的事情。但是所有人都可以用一种另外的方式学习别人的经验和技巧:仔细研读其他人写的代码。阅读高质量代码,就像是阅读一本好的小说。

原文地址:https://www.cnblogs.com/sorvon/p/11552847.html