超市商品管理系统,供大家参考,具体内容如下
一、问题描述及功能要求
1.提供商品系统的添加、删除、编辑、显示等功能。
2.同类系统多数使用结构体数组来操作数据,本系统使用链表结构操作数据,提高了数据处理的效率。
二、代码实现 带有注释
废话不说,直接代码,欢迎指正。
大家CV可能有不兼容的情况,可以滴滴,尽可能解决问题地回复。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
#include <iostream>
#include <string.h>
#include <fstream>
#include <conio.h>//用getch();
using namespace std;
//以下是类的设计
class commodity
{
public:
char name[20];
char Id[20];
int buy;//进货价;
int sale;//卖出价;
int amount;//数量;
int sum;//利润;
commodity * Next;
void Input()
{
cout<<"\t\t请输入商品的名称:"; cin>>name;
cout<<"\t\t请输入商品的编号:"; cin>>Id;
cout<<"\t\t请输入进货价:"; cin>>buy;
cout<<"\t\t请输入售出价:"; cin>>sale;
cout<<"\t\t请输入商品数量:"; cin>>amount;
sum=(sale-buy)*amount;
}
void ReadFile(istream & in)
{
in>>name>>Id>>sale>>buy>>sum;
}
void Show()
{
cout<<"商品名"<<name<<endl<<"编号:"<<Id<<endl<<"进货价"<<buy<<"售出价"<<sale<<"商品数量:"<<
amount<<"预计总利润:"<<sum<<endl<<endl<<endl;
}
};
//以下是对象或对象数组的定义
//﹌﹌﹌﹌﹌﹌﹌﹌﹌Commoditymassage类﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
class Commoditymassage
{
public:
Commoditymassage();
~Commoditymassage();
void ShowMenu();
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Swap(commodity *,commodity *);
void Sort();
int ListCount();
void Display()
{
for(commodity * p=Head->Next;p!=End;p=p->Next)
p->Show();
cout<<"输入任意字符!继续……";
getch();
}
void AddItem()
{
End->Input();
End->Next=new commodity;
End=End->Next;
cout<<"添加成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
private:
commodity * Head,* End;
ifstream in;
ofstream out;
commodity *FindItem(char * name)
{
for(commodity * p=Head;p->Next!=End;p=p->Next)//匹配成功则返回上一个指针,不成功就返回空
if(!strcmp(p->Next->name,name))return p;
return NULL;
}
commodity *FindID(char * Id)
{
for(commodity * p=Head;p->Next!=End;p=p->Next)//匹配成功则返回上一个指针,不成功就返回空
if(!strcmp(p->Next->Id,Id))return p;
return NULL;
}
};
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌构造函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
Commoditymassage::Commoditymassage()
{
Head=new commodity;
Head->Next=new commodity;
End=Head->Next;
in.open("sort.txt");
if(!in)
cout<<"无商品信息。请先输入。"<<endl;
else
{
while(!in.eof())
{
End->ReadFile(in);
if(End->name[0]=='\0')break;
End->Next=new commodity;
End=End->Next;
}
in.close();
cout<<"\t\t读取商品信息成功!"<<endl;
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌析构函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
Commoditymassage::~Commoditymassage()
{
Save();
for(commodity * temp;Head->Next!=End;)
{
temp=Head->Next;
Head->Next=Head->Next->Next;
delete temp;
}
delete Head,End;
}
//以下是主函数
int main()
{
int x,i=0;
bool quit=false;
cout<<"\t\t**************************"<<endl;
for(i=0;i<3;i++)
cout<<"\t\t*\t\t\t\t\t\t *"<<endl;
cout<<"\t\t*****【 欢迎进入超市商品管理系统 】*****"<<endl;
for(i=0;i<3;i++)
cout<<"\t\t◎\t\t\t\t\t\t ◎"<<endl;
cout<<"\t\t**************************\n"<<endl;;
Commoditymassage Grade;
cout<<"按任意键开始……";
getch();
while(!quit)
{
Grade.ShowMenu();
cin>>x;
switch(x)
{
case 0:quit=true;break;
case 1:Grade.AddItem();break;
case 2:Grade.Display();break;
case 3:Grade.Sort();break;
case 4:Grade.Find();break;
case 5:Grade.RemoveItem();break;
case 6:Grade.ModifyItem();break;
}
}
return 0;
}
void Commoditymassage::ShowMenu()
{
cout<<" 超 市 商 品 管 理 系 统 "<<endl;
cout<<" ┌────-────┐"<<endl;
cout<<" │ 1.增加超市商品 │"<<endl;
cout<<" │ 2.显示超市商品 │"<<endl;
cout<<" │ 3.排序统计商品 │"<<endl;
cout<<" │ 4.查找超市商品 │"<<endl;
cout<<" │ 5.删除超市商品 │"<<endl;
cout<<" │ 6.修改超市商品 │"<<endl;
cout<<" │ 0.安全退出系统 │"<<endl;
cout<<" └────────-┘"<<endl;
cout<<"\n\t\t\n\t\t请选择:";
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌查找函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Commoditymassage::Find()
{
char name[20] ,Id[10];
int x;
commodity * p=NULL;
cout<<"\n\t\t*********************************\n";
cout<<"\t\t※ 1.按商品的名称查找\n\t\t※ 2.按商品编号查找";
cout<<"\n\t\t*********************************\n请选择:";
cin>>x;
switch(x)
{
case 1:{cout<<"\t\t请输入要查找的商品的名称:";cin>>name;
if(p=FindItem(name))
{
p->Next->Show();
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"\t\t没有找到该名称的商品!"<<'\n'<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}break;
case 2:
{
cout<<"\t\t请输入要查找的商品的编号:";cin>>Id;
if(p=FindID(Id))
{
p->Next->Show();
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"\t\t没有找到该编号的商品!"<<'\n'<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}break;
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌修改商品信息﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Commoditymassage::ModifyItem() //修改商品信息
{
char name[20];
commodity * p=NULL;
cout<<"\t\t请输入要修改的商品的名称:";cin>>name;
if(p=FindItem(name))
{
cout<<"\t\t已找到商品的信息,请输入新的信息!"<<endl;
p->Next->Input();
cout<<"修改成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"\t\t没有找到!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌删除信息﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Commoditymassage::RemoveItem() // 删除信息
{
char name[20];
commodity * p=NULL,*temp=NULL;
cout<<"\t\t请输入要删除的商品的名称:"<<endl;cin>>name;
if(p=FindItem(name))
{
temp=p->Next;
p->Next=p->Next->Next;
delete temp;
cout<<"\t\t删除成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"\t\t没有找到!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Commoditymassage::Swap(commodity *p1, commodity *p2)//交换两个combox变量的数据域
{
commodity *temp=new commodity;
strcpy(temp->name,p1->name);
strcpy(temp->Id,p1->Id);
temp->sale=p1->sale;
temp->buy=p1->buy;
temp->sum=p1->sum;
strcpy(p1->name,p2->name);
strcpy(p1->Id,p2->Id);
p1->sale=p2->sale;
p1->buy=p2->buy;
p1->sum=p2->sum;
strcpy(p2->name,temp->name);
strcpy(p2->Id,temp->Id);
p2->sale=temp->sale;
p2->buy=temp->buy;
p2->sum=temp->sum;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
int Commoditymassage::ListCount()//统计当前链表的记录总数,返回一个整数
{
if(! Head)
return 0;
int n=0;
for(commodity * p=Head->Next;p!=End;p=p->Next)
{
n++;
}
return n;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Commoditymassage::Sort()//对当前链表进行排序
{
cout <<"Sorting..."<<endl;
commodity *p=NULL,*p1=NULL,*k=NULL;
int n=Commoditymassage::ListCount();
if(n<2)
return;
for(p=Head->Next;p!=End;p=p->Next)
for(k=p->Next;k!=End;k=k->Next)
{
if(p->sum>k->sum)
{
Commoditymassage::Swap(p,k);
}
}
cout <<"排序完成!"<<endl;
getch();
return;
}
//﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌保存函数﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌
void Commoditymassage::Save()
{
out.open("sort.txt");
for(commodity *p=Head->Next;p!=End;p=p->Next)
out<<p->name<<"\t"<<p->Id<<"\t"<<p->sum<<'\n';
out.close();
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_46525584/article/details/106967429








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。