题目要求:
1)该系统主要处理商场中的商品信息。
2)商品信息主要包括:商品编号、商品类型、价格、供应商等内容。
3)供应商信息主要包括:供应商编号、供应商名称、联系电话等内容。
4)完成以下的操作:实现商品信息、商品类型、供应商信息的添加、修
改、删除和查询。
5)可以利用文件进行信息的保存和读取。
为了完成上述的要求,我们可以采用面向对象的编程方式,首先设计商品类和供应商类,并分别定义其属性和方法。
商品类的属性包括:商品编号、商品类型、价格、供应商,其中供应商属性可以定义为另一个类的对象。而供应商类的属性包括:供应商编号、供应商名称、联系电话。
接下来,我们需要定义商品类和供应商类的初始化函数,以方便对其属性进行初始化。
在商品类中,我们还需要定义添加、修改、删除和查询商品信息的方法。在添加商品信息的方法中,我们可以通过传入商品的各个属性值,将商品加入商品列表中。在修改商品信息的方法中,我们可以根据商品编号或其他条件,找到需要修改的商品,然后进行相关属性的修改。删除商品信息的方法可以根据商品编号进行商品的删除操作。查询商品信息的方法可以根据商品编号或其他条件查询商品信息,将符合条件的商品信息返回。
在供应商类中,除了定义初始化、添加、修改、删除和查询供应商信息的方法外,我们还需要定义一个方法,即将指定商品与供应商进行关联的方法。
最后,我们可以提供文件读写的方法,实现将商品信息和供应商信息保存到文件中,以及从文件中读取并还原商品信息和供应商信息。
具体代码实现:
#include <iostream> #include <cstring> #include <fstream> using namespace std; class student { public: string id; string kind; string price; string people; public: student() { id = "0"; kind = "0"; price = "0"; people= "0"; } student(string i, string k, string p,string pe) { id = i; kind = k; price = p; people =pe; } void display() { cout << "商品编号为:" << id<<endl << "商品类别为:" << kind <<endl<< "商品价格为:" << price <<endl<<"商品供应商为:"<<people<< endl<<endl; } //写文件 void savefile(ofstream &x) { //写id { int length = id.length(); x.write((char *) & (length), sizeof(length)); x.write(id.c_str(), length); } //写kind { int length = kind.length(); x.write((char *) & (length), sizeof(length)); x.write(kind.c_str(), length); } //写price { int length = price.length(); x.write((char *) & (length), sizeof(length)); x.write(price.c_str(), length); } //写people { int length = people.length(); x.write((char *) & (length), sizeof(length)); x.write(people.c_str(), length); } } // 读文件 void readfile(ifstream &y) { // 读id { int length; y.read((char *)&length, sizeof(length)); char *p = new char[length + 1]; y.read(p, length); p[length] = '\0'; id = p; delete []p; } // 读kind { int length; y.read((char *)&length, sizeof(length)); char *p = new char[length + 1]; y.read(p, length); p[length] = '\0'; kind = p; delete []p; } // 读price { int length; y.read((char *)&length, sizeof(length)); char *p = new char[length + 1]; y.read(p, length); p[length] = '\0'; price = p; delete []p; } //读people { int length; y.read((char *)&length, sizeof(length)); char *p = new char[length + 1]; y.read(p, length); p[length] = '\0'; people = p; delete []p; } } }; class people { public: string id; string name; string number; public: people() { id = "0"; name = "0"; number = "0"; } people(string i, string n, string nu) { id = i; name = n; number = nu; } //写文件 void savefile(ofstream &x) { //写id { int length = id.length(); x.write((char *) & (length), sizeof(length)); x.write(id.c_str(), length); } //写name { int length = name.length(); x.write((char *) & (length), sizeof(length)); x.write(name.c_str(), length); } //写number { int length = number.length(); x.write((char *) & (length), sizeof(length)); x.write(number.c_str(), length); } } // 读文件 void readfile(ifstream &y) { // 读id { int length; y.read((char *)&length, sizeof(length)); char *p = new char[length + 1]; y.read(p, length); p[length] = '\0'; id = p; delete []p; } // 读name { int length; y.read((char *)&length, sizeof(length)); char *p = new char[length + 1]; y.read(p, length); p[length] = '\0'; name = p; delete []p; } // 读number { int length; y.read((char *)&length, sizeof(length)); char *p = new char[length + 1]; y.read(p, length); p[length] = '\0'; number = p; delete []p; } } void display() { cout << "供应商编号为:" << id << "供应商名称为:" << name << "供应商电话为:" << number << endl<<endl; } }; class Mstudent { private: student s[100]; int number=0; public: Mstudent() { number = 0; } void dislay() { // 先读取文件 readfile(); for (int i = 0; i < number; i++) { s[i].display(); } } void add(student x) { s[number] = x; number++; // 保存文件 savefile(); } void deletestudent(string id) { readfile(); int i,k=0,n; for(i=0;i<number;i++){ if(s[i].id==id){ k=1; n=i; } } if(k==0){ cout<<"输入错误,未找到此商品"<<endl; } if(k==1){ for(i=n;i<number;i++){ s[i]=s[i+1]; } number--; cout<<"已删除成功"<<endl; } savefile(); } void search(string id) { readfile(); int i,n; int k=0; for(i=0;i<number;i++){ if(s[i].id==id){ k=1; n=i; } } if(k==0){ cout<<"查无此人"<<endl; } if(k==1){ cout<<"此商品信息为" <<endl; s[n].display(); } savefile(); } // 保存所有商品到文件 void savefile() { ofstream ofs("person.txt", ios::out | ios::binary); ofs.write((char*)&number, sizeof(number)); for (int i = 0; i < number; i++) { s[i].savefile(ofs); } ofs.close(); } // 从文件读取所有商品 void readfile() { ifstream ifs("person.txt", ios::in | ios::binary); if (!ifs.is_open()) { return; } ifs.read((char*)&number, sizeof(number)); for (int i = 0; i < number; i++) { s[i].readfile(ifs); } ifs.close(); } }; class Mpeople { private: people s[100]; int number=0; public: Mpeople() { number = 0; } void dislay() { // 先读取文件 readfile(); for (int i = 0; i < number; i++) { s[i].display(); } } void add(people x) { s[number] = x; number++; // 保存文件 savefile(); } void deletepeople(string id) { readfile(); int i,k=0,n; for(i=0;i<number;i++){ if(s[i].id==id){ k=1; n=i; } } if(k==0){ cout<<"输入错误,未找到此供应商"<<endl; } if(k==1){ for(i=n;i<number;i++){ s[i]=s[i+1]; } number--; cout<<"已删除成功"<<endl; } savefile(); } void search(string id) { readfile(); int i,n; int k=0; for(i=0;i<number;i++){ if(s[i].id==id){ k=1; n=i; } } if(k==0){ cout<<"查无此人"<<endl; } if(k==1){ cout<<"此供应商信息为" <<endl; s[n].display(); } savefile(); } // 保存所有供应商到文件 void savefile() { ofstream ofs("person.txt", ios::out | ios::binary); ofs.write((char*)&number, sizeof(number)); for (int i = 0; i < number; i++) { s[i].savefile(ofs); } ofs.close(); } // 从文件读取所有供应商 void readfile() { ifstream ifs("person.txt", ios::in | ios::binary); if (!ifs.is_open()) { return; } ifs.read((char*)&number, sizeof(number)); for (int i = 0; i < number; i++) { s[i].readfile(ifs); } ifs.close(); } }; class shangpinmenu { public: static void start() { Mstudent a; for (;;) { cout << "1:商品信息添加:" << endl; cout << "2:商品信息删除:" << endl; cout << "3:商品信息查找:"<<endl; cout << "4:商品信息显示:"<<endl; cout<<"5:返回"<<endl; cout<<"请选择:"<<endl; string s; cin >> s; if (s == "1") { system("cls"); cout << "请输入" << endl; cout << "商品编号:"; string id; cin >> id; cout << "商品类别:"; string kind; cin >> kind; cout << "商品价格:"; string price; cin >> price; cout<<"供应商:"; string people; cin>>people; student l(id, kind, price,people); a.add(l); cout<<"添加成功"<<endl; } if(s=="2") { system("cls"); cout<<"请输入要删除的商品编号:"<<endl; string id; cin>>id; a.deletestudent(id); } if(s=="3") { system("cls"); cout<<"请输入要查询的商品编号:"<<endl; string id; cin>>id; a.search(id); } if(s=="4") { system("cls"); a.dislay(); } if (s == "5") { break; } cout<<"是否继续管理商品信息"<<endl<<"是(1)/否(0)"<<endl; int choice; cin>>choice; if(choice==0){ break; } system("cls"); } } }; class peoplemenu { public: static void start() { Mpeople a; for (;;) { cout << "1:供应商信息添加:" << endl; cout << "2:供应商信息删除:" << endl; cout << "3:供应商信息查找:"<<endl; cout << "4:供应商信息显示:"<<endl; cout<<"5:返回"<<endl; cout<<"请选择:"<<endl; string s; cin >> s; if (s == "1") { system("cls"); cout << "请输入" << endl; cout << "供应商编号:"; string id; cin >> id; cout << "供应商姓名:"; string name; cin >> name; cout << "供应商电话:"; string number; cin >> number; people p(id,name, number); a.add(p); cout<<"添加成功"<<endl; } if(s=="2") { system("cls"); cout<<"请输入要删除的供应商编号:"<<endl; string id; cin>>id; a.deletepeople(id) ; } if(s=="3") { system("cls"); cout<<"请输入要查询的供应商编号:"<<endl; string id; cin>>id; a.search(id); } if (s=="4") { system("cls"); a.dislay(); } if (s == "5") { break; } cout<<"是否继续管理供应商信息"<<endl<<"是(1)/否(0)"<<endl; int choice; cin>>choice; if(choice==0){ break; } system("cls"); } } }; class menu { public: static void start() { student a; people p; while (1) { system("cls"); cout << "主菜单" << endl; cout << "1:商品信息" << endl; cout << "2:供应商信息" << endl; cout << "3:退出系统"<< endl; cout << "请选择:"; string s; cin >> s; if (s == "1") { system("cls"); shangpinmenu::start(); } if (s == "2") { system("cls"); peoplemenu::start(); } if (s == "3") { exit(3); } } } }; int main() { menu::start(); }
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/36358.html