软件工程实践第一次作业

最近找来了同学写的一段小代码,想在学习同学编码的同时对其进行小小的改进。

本次学习修改的程序是模拟购书平台。

一、语言类型

 

面向过程,C++(并未涉及C++中的类)

二、流程分析

(黑线部分为已存在流程,红色部分为我认为缺少的流程)

三、优缺点:

 

优点:1.每个行为均用函数封装,简洁明了。

            2.有一定的异常处理。

缺点:1.有些代码相近,可以使用带参函数来简化。

   2.操作流程上用户不友好,不符合用户的使用流程,有些中断不合理。

   3.异常处理不够全面。

 

四、修改示例:

 

1.管理员登录代码(原):

 1 void logo1()
 2 {   
 3      char *password = new char [20];
 4      string name;
 5      cout << "name:"<<endl;
 6      cin>>name;
 7      cout <<"secret:"<<endl;
 8      char test;int i=0;
 9     while((test=getch())!='\r')
10      {
11          if(test==8)//ASC2码8为退格
12          {
13          if(i>0)
14          {
15              cout << test << " " << test;
16              password[i--]='\0';
17          }
18          }
19          else
20          {
21              if(i<20)
22              cout << "*";
23              password[i]=test;
24              i++;
25          }
26      }
27      password[i]='\0'; //
28      ifstream fin;
29      fin.open("admis.txt",ios_base::in);
30      if(fin.fail())
31      {
32          cout << "File open failed";
33      }
34      user c;int f1;
35    while(fin>>c.name>>c.password)
36      {
37          if(c.name==name&&c.password==password)
38          {
39              cout << "log in successfully";
40              f1=1;//登录成功标志
41              interface3();
42              int m;
43              cin>>m;
44              switch(m)
45              {
46              case 1:function1();break;
47              case 2:function2();break;
48              }
49          }
50      }
51      if(f1==0)//f1==0登录失败
52      {   
53          cout<<endl;
54          system("cls");
55          cout << "Incorrect user name or password!\n";
56          cout<<"Please enter again\n ";
57          cout<<endl;
58          logo1();
59      }
60 }

a)此处将 f1==0 作为登录失败的判断条件,在定义f1的时候并未说明f1的值,f1的值未知。

   此处改为int f1=0;登录成功置1。

b)当账号密码错误时,就反复要求输入,没有返回键,也不符合正常登录流程。

修改:

 1 static int errorCount=0;
 2 void logo1()
 3 {   
 4      char *password = new char [20];
 5      string name;
 6      cout << "name:"<<endl;
 7      cin>>name;
 8      cout <<"secret:"<<endl;
 9      char test;int i=0;
10     while((test=getch())!='\r')
11      {
12          if(test==8)//ASC2码8为退格
13          {
14          if(i>0)
15          {
16              cout << test << " " << test;
17              password[i--]='\0';
18          }
19          }
20          else
21          {
22              if(i<20)
23              cout << "*";
24              password[i]=test;
25              i++;
26          }
27      }
28      password[i]='\0'; //
29      ifstream fin;
30      fin.open("admis.txt",ios_base::in);
31      if(fin.fail())
32      {
33          cout << "File open failed";
34      }
35      user c;int f1=0;/*未设置f1=0*/
36    while(fin>>c.name>>c.password)
37      {
38          if(c.name==name&&c.password==password)
39          {
40              cout << "log in successfully";
41              f1=1;//登录成功标志
42              interface3();
43              int m;
44              cin>>m;
45              switch(m)
46              {
47              case 1:function1();break;
48              case 2:function2();break;
49              }
50          }
51      }
52    if(errorCount>1){errorCount=0;system("cls");cout<<"Too many input errors!You are forced to quit!\n";exit(0);}
53      if(f1==0)//f1==0登录失败
54      {   
55          errorCount++;
56          cout<<endl;
57          system("cls");
58          cout << "Incorrect user name or password!\n";
59          cout<<"Please enter again\n ";
60          cout<<endl;
61          logo1();
62      }
63 }

修改:

1.新增全局变量:errorCount

用来记录输入错误的次数。

1  if(errorCount>1){errorCount=0;system("cls");cout<<"Too many input errors!You are forced to quit!\n";exit(0);}

当输入错误次数过多,出现错误提示,并退出程序。

修改:

在每次函数运行前,执行system("cls"),以清除屏幕上过多的冗余。

 

五、总结

 

在实现一个简单任务时,我们太急于功能的实现,而在细节和异常处理上太过放松,并且有可能会和实际的用户的操作脱离。

posted on 2019-03-03 23:02  littleWhite_Q  阅读(159)  评论(0编辑  收藏  举报