你帮我助软件开发-PSP

 

 

设计思路

        实现需求只需要一个简单的单向链表,通过添加、删除、查找、遍历等基本操作即可。

 

 设计实现

        用Struct记录物品的信息,name、owner、tel分别记录物品名称、提供者、提供者联系方式。add、del、show、search分别实现添加、删除、遍历、查找功能。为了兼顾用户操作的便捷性和性能,采用每次操作前询问操作类型,支持一次添加多种物品、单个物品的删除,通过物品名称查找,对应物品必须提供完整的信息才可删除。

        实现过程中以外耗费时间的是发现要维护历史数据......由于还没学会数据库,因此只能采用粗暴的文本文档读入历史物品清单/输出操作后的物品清单+命令行进行新操作【捂脸】

 

改进思路

        基础功能比较朴素,主要进行了一些使用体验方面的小完善(甚至不能称之为改进...),比如设置清单输出时的对齐格式,增加一些误输入的处理(添加物品数量为0、输入格式不规范的提醒、3次输入错误后自动退出等)。此外实在写这篇总结时想到的,将原本查找物品必须名称完全一致修改成现有物品名称包含输入名称即可(显然这也是有漏洞的,比如“酱油”包含“油”,但实际上是完全不同的物品)。

 

代码

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<iomanip>
  7 
  8 using namespace std;
  9 
 10 typedef struct node{
 11     char name[22];
 12     char owner[22];
 13     char tel[22];
 14     struct node *next;
 15 }item; 
 16 
 17 item *head,*end;
 18 
 19 int request(){
 20     cout<<"请输入命令:"<<endl;
 21     cout<<"添加物品请输入1"<<endl;
 22     cout<<"删除物品请输入2"<<endl;
 23     cout<<"显示当前物品列表请输入3"<<endl;
 24     cout<<"查找所需物品请输入4"<<endl;
 25     cout<<"操作完毕请输入0"<<endl;
 26     int x;
 27     cin>>x;
 28     return x;
 29 }
 30 
 31 //初始化物品列表 
 32 void init(){
 33     head=new item;
 34     end=head;
 35     end->next=NULL;
 36     char name[22],owner[22],tel[22];
 37     item *p;
 38     p=new item;
 39     while (cin>> p->name){
 40         cin>> p->owner >> p->tel;
 41         end->next=p;
 42         end=p;
 43         p=new item;
 44     }
 45     end->next=NULL; 
 46 }
 47 
 48 //显示物品列表
 49 void show(){
 50     if (head->next==NULL){
 51         cout<<"当前没有物品"<<endl;
 52         return; 
 53     }
 54     item *p;
 55     for (p=head->next;p!=NULL;p=p->next){
 56         cout<< setw(21) << left << p->name << setw(21) << left << p->owner << setw(21) << left << p->tel << endl; 
 57     }
 58 } 
 59 
 60 //查找物品
 61 void search(){
 62     item *p,*pre;
 63     char name[20],owner[20],tel[20];
 64     cout<<"请输入您要查找的物品名称"<<endl;
 65     cin>>name; 
 66     pre=head;
 67     bool flag=false;
 68     for (p=head->next;p!=NULL;p=p->next){
 69         if (strstr(p->name,name)){ 70             cout<< setw(21) << left << p->name << setw(21) << left << p->owner << setw(21) << left << p->tel << endl; 
 71             flag=true;
 72         }
 73         pre=p; 
 74     }
 75     if (!flag) cout<<"不存在您要查找的物品"<<endl; 
 76 } 
 77 
 78 //添加物品 
 79 void add(){
 80     item *p;
 81     int x;
 82     cout<<"请输入您要添加的物品数量"<<endl;
 83     cin>>x; 
 84     if (x==0){
 85         cout<<"不允许添加0件物品,请检查输入是否错误" <<endl; 
 86         return;
 87     } 
 88     cout<<"请输入您要添加的物品名称、提供者、电话号码"<<endl;
 89     for (int i=1;i<=x;i++){    
 90         p=new item;
 91         cin>> p->name >> p->owner >> p->tel;
 92         end->next=p;
 93         end=p;
 94     }
 95     end->next=NULL;
 96 }
 97 
 98 //删除物品
 99 void del(){
100     item *p,*pre;
101     char name[20],owner[20],tel[20];
102     cout<<"请输入您要删除的物品名称、提供者、电话号码"<<endl;
103     cin>>name>>owner>>tel; 
104     pre=head;
105     bool flag=false;
106     for (p=head->next;p!=NULL;p=p->next){
107         if (strcmp(p->name,name)==0 && strcmp(p->owner,owner)==0 && strcmp(p->tel,tel)==0){
108             pre->next=p->next;
109             delete p;
110             flag=true;
111         }
112         pre=p; 
113     }
114     if (!flag) cout<<"您要删除的物品不存在,请检查是否输入错误";
115 } 
116 
117 int main(){
118     freopen("物品清单.txt","r",stdin);
119     init();
120     fclose(stdin);
121     freopen("CON","r",stdin);cin.clear();
122     bool flag=true;
123     int num=0;
124     while (flag){
125         cout<< "*********************************************************" <<endl;
126         switch (request()){
127             case 1:
128                 add();
129                 continue;
130             case 2:
131                 del();
132                 continue;
133             case 3:
134                 show();
135                 continue;
136             case 4:
137                 search();
138                 continue;
139             case 0:
140                 flag=false;
141                 continue;
142             default:
143                 cout<<"输入不符合规范,请重新输入"<<endl; 
144                 cin.sync();
145                 num++;
146         }    
147         if (num==3) break;
148     }
149     
150     freopen("物品清单.txt","w",stdout);
151     show();
152     fclose(stdout);
153     
154     return 0;
155 } 

 

未实现的改进

        显然上述代码只是一个很基础的暴力实现,在用户交互方面做得比较差(,继续学习ing)。预想中的改进有如下几项:

        1.可视化方面。物品清单的显示不够清晰,尤其在文本文档输出后就没有命令行操作的对齐格式了;

        2.错误输入排查方面。对用户操作失误的控制和排查不够完善;

        3.模糊搜索功能。现有的代码只能查找到名称包含输入名称的物品,这在真正应用中显然是会出现很多疏漏的。但要真正实现比较精准的模糊搜索恐怕比较困难......;

        4.使用体验方面。现有的操作比较繁琐,不能进行连续操作,添加物品时也需要预先统计添加的物品总数,删除物品也只支持单次删除。

 

总结

  虽然代码的功能比较简单,但预先的设计仍然不够完善,没有充分考虑维护历史数据、可视化等方面的细节,而且对C++的字符串处理依然不够熟悉【泪】。此外,在之前的学习经历中只需关注正确输出答案,而不存在用户交互的需求,这是之后需要学习和加强的方面。(以及好几年没好好码代码了真的退步好多啊TAT)

 

posted @ 2022-10-05 10:39  Aliar11  阅读(48)  评论(0)    收藏  举报