📄 5_2-t.cpp
字号:
/*第2题 物品库存管理--源代码及关键源代码注解如下:*/
// file operations
#include<iostream.h>
#include<process.h> //exit(1)
#include<fstream.h> //file operations
#include<string.h>
#include<dos.h>
int lgwg,num,i;
char pause;
class Brand
{
private:
int Brand_code;
int quantity;
float brnd_price;
char brnd_name[20];
public:
Brand()
{
brnd_name[0]=NULL;
}
void getdata();
void showdata();
char *rtnname() {return brnd_name;}
float rtnprice() {return brnd_price;}
};
class Item
{
private:
char Item_name[10];
int Item_code;
char colour[10];
int quantity;
Brand brnd[9];
//Private Member functions used internally
void getdata();
void showdata();
public:
void add();
void remove();
void search();
void showab();
};
// GET Brand DATA
// gets data about the Brand
// it is temperary storage
void Brand::getdata()
{
switch(lgwg)
{
case 1:
cout<<"\n\nEnter Brand Name ";
cin.ignore();
cin.getline(brnd_name,20);
cout<<"\nEnter Brand Code ";
cin>>Brand_code;
cout<<"\nEnter Quantity ";
cin>>quantity;
cout<<"\nEnter Price";
cin>>brnd_price;
break;
case 2:
cout<<"\n\n请输入商标名称: ";
cin.ignore();
cin.getline(brnd_name,20);
cout<<"\n请输入商标编号: ";
cin>>Brand_code;
cout<<"\n请输入物品数量: ";
cin>>quantity;
cout<<"\n请输入物品价格: ";
cin>>brnd_price;
}
}
// SHOW Brand DATA
// shows the data of the Brand
void Brand::showdata()
{
switch(lgwg)
{
case 1:
cout<<"\n\nName : "; cout<<brnd_name;
cout<<"\n\nCode : "; cout<<Brand_code;
cout<<"\n\nQuantity : "; cout<<quantity;
cout<<"\n\nPrice : "; cout<<brnd_price<<endl;
break;
case 2:
cout<<"\n\n商标名称 : "; cout<<brnd_name;
cout<<"\n\n商标编号 : "; cout<<Brand_code;
cout<<"\n\n物品数量 : "; cout<<quantity;
cout<<"\n\n物品价格 : "; cout<<brnd_price<<endl;
}
}
// GET Item DATA
// gets data about the Item it is temporary storage
void Item::getdata()
{
switch(lgwg)
{
case 1:
cout<<"\nEnter Item Name ";
cin.ignore();
cin.getline(Item_name,10);
cout<<"\nEnter Item Code ";
cin>>Item_code;
cout<<"\nEnter Colour ";
cin.ignore();
cin.getline(colour,10);
cout<<"\nEnter Item Quantity ";
cin>>quantity;
cout<<"\nEnter Brand Quantity (Less than nine) ";
cin>>num;
for(i=0;i<num;i++)
{
cout<<"\n\nEnter Brand "<<i+1;
brnd[i].getdata();
}
i=0;
break;
case 2:
cout<<"\n请输入物品名称: ";
cin.ignore();
cin.getline(Item_name,10);
cout<<"\n请输入物品编号: ";
cin>>Item_code;
cout<<"\n请输入物品颜色: ";
cin.ignore();
cin.getline(colour,10);
cout<<"\n请输入物品数量: ";
cin>>quantity;
cout<<"\n请输入商标数量(九个以内): ";
cin>>num;
for(i=0;i<num;i++)
{
cout<<"\n\n请输入第"<<i+1<<"个商标数据: ";
brnd[i].getdata();
}
i=0;
}
}
// SHOW Item DATA
// shows data about the Item
void Item::showdata()
{
switch(lgwg)
{
case 1:
cout<<"\n\n\t\tItem Data";
cout<<"\n\nItem Name : ";
cout<<Item_name;
//Write a string to stdout
cout<<"\n\nItem Code : ";
cout<<Item_code;
cout<<"\n\nColour : ";
cout<<colour;
cout<<"\n\nQuantity : ";
cout<<quantity<<endl;
cout<<"\n\nPress Enter to continue.";cin.get(pause);
//clrscr();
while(*brnd[i].rtnname())
{
cout<<"\n\nBrand "<<i+1<<endl; brnd[i].showdata();
i++;
cout<<"\n\nPress Enter to continue.";cin.get(pause);
//clrscr();
}
i=0;
break;
case 2:
cout<<"\n\n\t\t物品数据";
cout<<"\n\n物品名称 : ";
cout<<Item_name;
//Write a string to stdout
cout<<"\n\n物品编码 : ";
cout<<Item_code;
cout<<"\n\n物品颜色 : ";
cout<<colour;
cout<<"\n\n物品数量 : ";
cout<<quantity<<endl;
cout<<"\n\n按回车继续";cin.get(pause);
//clrscr();
while(*brnd[i].rtnname())
{
cout<<"\n\n第"<<i+1<<"个商标\n"; brnd[i].showdata();
i++;
cout<<"\n\n按回车继续";cin.get(pause);
//clrscr();
}
i=0;
}
}
// Add Item
// this function first gets data into temp storage then saves it on the hard disk
void Item::add()
{
//clrscr();
ofstream file( "TIS.txt", ios::out |ios::ate ); //open file for input
if(!file) //if file could not be opened
{
if(lgwg==1) cout<<"Error Could Not Open File.\n\nPress Enter to continue.";
else cout<<"错误!无法打开文件。\n\n按回车继续";
cin.ignore();cin.get(pause);
exit(1);
}
Item p1;
p1.getdata(); //Get Data From User For Temp. Storage
// write data to hard disk
file.write(reinterpret_cast <const char *> (&p1),sizeof(Item) );
}
// TRAVERSE
// this function shows the list of all the Brands
void Item::showab()
{
system("cls");//clrscr();
ifstream file( "TIS.txt" ,ios::in); //open file for output
if(!file) //if file could not be opened
{
if(lgwg==1) cout<<"Error Could Not Open File.\n\nPress Enter to continue.";
else cout<<"错误!无法打开文件。\n\n按回车继续";
cin.ignore();cin.get(pause);
exit(1);
}
Item p1;
file.read(reinterpret_cast<char *>(&p1),sizeof(Item));
//Store Data In Object
cin.ignore();
while(!file.eof()) //Untill Contacts Present
{
//clrscr();
p1.showdata() ; //Display on Screen
file.read(reinterpret_cast<char *> (&p1),sizeof(Item)); //Read Next Contact
}
}
// SEARCH
void Item::search()
{
node1:
//clrscr();
char name1[10]={NULL};
int code=-1;
int option;
float prc=-1;
system("cls");
if(lgwg==1) cout<<"\n(1)Search By Name\n(2)Search By Code\n(3)Search By Price\n(4)Back ";
else cout<<"\n(1)按名称搜索\n(2)按编码搜索\n(3)按价格搜索\n(4)返回 ";
cin>>option;
cin.ignore();
switch(option)
{
case 1:
if(lgwg==1) cout<<"\nEnter Item name ";
else cout<<"\n请输入物品名称 :";
cin.getline(name1,10);
break;
case 2:
if(lgwg==1) cout<<"\nEnter Item Code ";
else cout<<"\n请输入编码 : ";
cin>>code;
cin.ignore();
break;
case 3:
if(lgwg==1) cout<<"\nEnter The Price ";
else cout<<"\n请输入价格 : ";
cin>>prc;
break;
case 4:
system("cls");
return;
default:
goto node1;
}
fstream file( "TIS.txt" ,ios::in); //Open File For Output/Read
if(!file) //If File Could Not Be Opened
{
if(lgwg==1) cout<<"Error Could Not Open File.\n\nPress Enter to continue.";
else cout<<"错误!无法打开文件。\n\n按回车继续";
cin.ignore();cin.get(pause);
exit(1);
}
char flag='a'; //Flag To Check If Found
Item p1;
file.read(reinterpret_cast<char *>(&p1),sizeof(Item));
//Read Data To Object
while(!file.eof()) //Untill There Are Contacts
{
if((strcmp(p1.Item_name,name1)==0)||(p1.Item_code==code)) //Compare
{
//clrscr();
p1.showdata();
// if(lgwg==1) cout<<"\n\nPress Enter to continue.";
// else cout<<"\n\n按回车继续";
// cin.get(pause);
flag='z'; //Set Flag
break; //Break Loop
}
for(i=0;*p1.brnd[i].rtnname()&&i<9;i++)
{
if(p1.brnd[i].rtnprice()==prc)
{
cout<<"\n\n"<<p1.Item_name<<endl;
p1.brnd[i].showdata();
if(lgwg==1) cout<<"\n\nPress Enter to continue.";
else cout<<"\n\n按回车继续";
cin.ignore();cin.get(pause);
flag='z';
}
}
i=0;
file.read(reinterpret_cast<char *> (&p1),sizeof(Item));
//Read Next Contact
}
if(flag !='z') //If Not Found
{
if(lgwg==1) cout<<"Item Not Found.\n\nPress Enter to continue.";
else cout<<"未找到匹配档案。\n\n按回车继续";
cin.ignore();cin.get(pause);
}
goto node1;
}
// REMOVE Item
// remove is a bit different first all the data except the Item to be deleted is saved in an other file called delete...
// then the original file is washed and all the data is copied back again
void Item::remove()
{
node2:
//clrscr();
char name1[10]={NULL};
int code;
int option;
system("cls");
if(lgwg==1) cout<<"\n(1)Delete By Name\n(2)Delete By Code\n(3)Back ";
else cout<<"\n(1)按姓名删除\n(2)按代码删除\n(3)返回 ";
cin>>option;
switch(option)
{
case 1:
if(lgwg==1) cout<<"\nEnter Item name ";
else cout<<"\n请输入物品名称 :";
cin.ignore();
cin.getline(name1,10);
code=0;
break;
case 2:
if(lgwg==1) cout<<"\nEnter Item Code ";
else cout<<"\n请输入编码 :";
cin>>code;
cin.ignore();
break;
case 3:
system("cls");
return;
default:
goto node2;
}
ifstream file( "TIS.txt" ,ios::in );
ofstream temp( "delete.txt", ios::trunc);
if(!file||!temp)
{
if(lgwg==1) cout<<"Error Could Not Open File.\n\nPress Enter to continue.";
else cout<<"错误!无法打开文件。\n\n按回车继续";
cin.ignore();cin.get(pause);
exit(1);
}
file.seekg(0);
temp.seekp(0);
Item p1;
file.read(reinterpret_cast<char *> (&p1),sizeof(Item));
pause='a';
while(!file.eof())
{
if((strcmp(name1,p1.Item_name)==0)||(code==p1.Item_code))
{
file.read(reinterpret_cast<char *> (&p1),sizeof(Item));
pause='z';
}
else
{
temp.write(reinterpret_cast<char *> (&p1),sizeof(Item));
file.read(reinterpret_cast<char *> (&p1),sizeof(Item));
}
}
file.close();
temp.close();
ifstream temp1("delete.txt",ios::in);
ofstream file1("TIS.txt", ios::trunc);
temp1.seekg(0);
file1.seekp(0);
temp1.read(reinterpret_cast<char *> (&p1),sizeof(Item));
while(!temp1.eof())
{
file1.write(reinterpret_cast<char *> (&p1),sizeof(Item) );
temp1.read(reinterpret_cast<char *> (&p1),sizeof(Item) );
}
temp1.close();
file1.close();
if(pause=='z')
{
if(lgwg==1) cout<<"Deleted.\n\nPress Enter to continue.";
else cout<<"已删除。\n\n按回车继续";
cin.get(pause);
}
else
{
if(lgwg==1) cout<<"Item Not Found.\n\nPress Enter to continue.";
else cout<<"未找到匹配档案。\n\n按回车继续";
cin.get(pause);
}
goto node2;
}
void welcome()
{
system("cls");
cout<<"\n\n\n\t\t Please Choice The Language(请选择语言):\n\t\t (1)English(英文)\t (2)Chinese(中文)\n\t\t\t\t\t";
cin>>lgwg;
while(lgwg!=1&&lgwg!=2)
{
cout<<"\t\tBad Command.Please Choice Again.(无效!情重新输入)\n\t\t\t\t\t";
cin>>lgwg;
}
system("cls");
switch(lgwg)
{
case 1:
cout<<"\t\t*************************************************\n";
cout<<"\t\t* *\n";
cout<<"\t\t* Welcome to The Inventory System (TIS) Ver.1 *\n";
cout<<"\t\t* *\n";
cout<<"\t\t* Projected by: Fan Jingyu *\n";
cout<<"\t\t* *\n";
cout<<"\t\t* Class:03042101 Code:03304578 *\n";
cout<<"\t\t* *\n";
cout<<"\t\t*************************************************\n\n";
break;
case 2:
cout<<"\t\t*************************************************\n";
cout<<"\t\t* *\n";
cout<<"\t\t* 欢迎使用物品库存管理程序 第一版 *\n";
cout<<"\t\t* *\n";
cout<<"\t\t* 设计者: 樊劲宇 *\n";
cout<<"\t\t* *\n";
cout<<"\t\t* 班级:03042101 学号:03304578 *\n";
cout<<"\t\t* *\n";
cout<<"\t\t*************************************************\n\n";
}
if(lgwg==1) cout<<"\n\n\t\t\tPress Enter to continue.";
else cout<<"\n\n\t\t\t\t按回车继续";
cin.ignore();cin.get(pause);
}
// MAIN FUNCTION
void main()
{
Item m;
int choice;
welcome();
while(1)
{
//clrscr();
system("cls");
if(lgwg==1)
{
cout<<"\n\n(1)Add Item\n\n(2)Show All Data\n\n(3)Search Item\n\n(4)Remove Item\n\n(5)Choice Language\n\n(6)Exit TIS";
cout<<"\n\nEnter Choice ";
}
else
{
cout<<"\n\n(1)添加物品\n\n(2)显示所有数据\n\n(3)搜索物品\n\n(4)删除物品\n\n(5)语言选择\n\n(6)退出程序";
cout<<"\n\n请输入选择项: ";
}
cin>>choice;
switch(choice)
{
case 1:
m.add();
break;
case 2:
m.showab();
break;
case 3:
m.search();
break;
case 4:
m.remove();
break;
case 5:
welcome();
break;
case 6:
exit(1);
default:
{
if(lgwg==1) cout<<"Bad Command.Please Choice Again.\n";
else cout<<"无效!情重新输入\n";
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -