📄 cpp1.cpp
字号:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
struct textbook
{
int id;
char textbookname[20];
char zhuanye[20];
int number;
};
void Append(fstream);
void Change(fstream);
void Inquire(fstream);
void CreateTxt(fstream);
void main()
{
int choice;
fstream iof("d:\\textbookFile.dat",ios::binary|ios::in|ios::out);
if(! iof)
{cerr<<"文件不能打开"<<endl;
return;
}
while(1)
{
cout<<"************教材管理系统************"<<endl;
cout<<"***(023210班) 薛晓川,姚曼曼,李萍,廖笛***"<<endl;
cout<<" ********请键入操作选择********* "<<endl;
cout<<" ******1.录入教材信息******* "<<endl;
cout<<" ******2.修改教材信息******* "<<endl;
cout<<" ******3.查询浏览信息******* "<<endl;
cout<<" ******4.打印教材信息******* "<<endl;
cout<<" ******0.退出信息系统******* "<<endl;
cin>>choice;
switch(choice)
{
case 1: Append(iof);break;
case 2: Change(iof);break;
case 3: Inquire(iof);break;
case 4: CreateTxt(iof);break;
case 0: cout<<"退出系统\n";return;
default : cout<<"输入错误,请再输入\n";
}
}
iof.close();
}
void Append(fstream f)
{
int choice;
textbook book;
int key;
f.seekp(0,ios::end); //读指针移到文件末尾
long posEnd=f.tellp(); //记录文件尾位置
cout<<"************录入教材信息************\n";
while(1)
{
cout<<"请键入操作选择\n"
<<"1:录入信息"
<<"0:退出\n";
cin>>choice;
switch(choice)
{
case 1: //追加新记录
{
cout<<"编号,教材名,专业,数量:\n编号: ";
cin>>book.id;
cout<<"教材名: ";
cin>>book.textbookname;
cout<<"专业: ";
cin>>book.zhuanye;
cout<<"数量: ";
cin>>book.number;
f.write((char *)& book,sizeof(textbook));
break;
};
case 0:return;
}
}
}
void Change(fstream f) //修改操作
{
int choice;
textbook book;
int key;
int ch,m;
int id;
char textbookname[20];
char zhuanye[20];
int number;
f.seekp(0,ios::end); //读指针移到文件末尾
long posEnd=f.tellp(); //记录文件尾位置
cout<<"************修改教材信息************\n";
while(1)
{
cout<<"请键入操作选择\n"
<<"1:修改教材信息\t"
<<"0:退出\n";
cin>>choice;
switch(choice)
{
case 1:
{
{f.seekp(0,ios::beg); //文件头开始检索
cout<<"请输入要修改的教材的编号:";
cin>>key;
do
{
f.read((char *)& book,sizeof(textbook));
}while(book.id!=key&&f.tellp()!=posEnd);
cout<<"编号"<<'\t'<<"教材名"<<'\t'<<"专业"
<<'\t'<<"数量"<<endl;
if(book.id==key)
{
cout<<book.id<<'\t'<<book.textbookname
<<'\t'<<book.zhuanye<<'\t'
<<book.number<<endl;
cout<<"****你确定要修改吗?*****"<<endl;
cout<<"1.修改"<<'\t'<<"0.退出"<<endl;
cin>>ch;
switch(ch)
{
case 1:
{ cout<<"**请选择修改选项**"<<endl;
cout<<"1.修改编号"<<'\t'<<"2.修改书名"<<'\t'<<"3.修改专业"
<<'\t'<<"4.修改数量"<<'\t'<<"0.退出"<<endl;
cin>>m;
switch(m)
{
case 1:
{
cout<<"编号改为:";
cin>>id;
book.id=id;
cout<<"修改成功!!"<<endl;goto f1;
}
case 2:
{
cout<<"书名改为:";
cin>>textbookname;
strcpy(book.textbookname,textbookname);
cout<<"修改成功!!"<<endl;goto f1;
}
case 3:
{
cout<<"专业改为:";
cin>>zhuanye;
strcpy(book.zhuanye,zhuanye);
cout<<"修改成功!!"<<endl;goto f1;
}
case 4:
{
cout<<"数量改为:";
cin>>number;
book.number=number;
cout<<"修改成功!!"<<endl;goto f1;
}
case 0:
return;
}
}
case 0: return;
}
f1: f.seekp(-long(sizeof(textbook)),ios::cur); //指针复位
f.write((char*)&book,sizeof(textbook)); //写入文件
}
else
cout<<"编号输入错误\n";
break;
}
}
case 0: return;
}
}
}
void Inquire(fstream f) //查询记录
{
int choice;
textbook book;
int key;
char name[20];
f.seekp(0,ios::end); //读指针移到文件末尾
long posEnd=f.tellp(); //记录文件尾位置
cout<<"************查询教材信息************\n";
while(1)
{
cout<<"请键入操作选择\n"
<<"1:按编号查询\t"
<<"2:浏览\t"
<<"3.统计现有教材数量\t"
<<"0:退出\n";
cin>>choice;
switch(choice)
{
case 1: //按编号检索
{
{f.seekp(0,ios::beg);
cout<<"编号:";
cin>>key;
do
{
f.read((char *)&book,sizeof(textbook));
}while(book.id!=key&&f.tellp()!=posEnd);
if(book.id==key)
{cout<<"编号"<<'\t'<<"教材名"<<'\t'<<"专业"
<<'\t'<<"数量"<<endl;
cout<<book.id<<'\t'<<book.textbookname
<<'\t'<<book.zhuanye<<'\t'
<<book.number<<endl;
}
else
{cout<<"编号输入错误\n";
continue;
}
break;
}
case 2: //浏览文件
{
f.seekg(0,ios::beg);
cout<<"编号"<<'\t'<<"教材名"<<'\t'<<"专业"
<<'\t'<<"数量"<<endl;
do //输出所有记
{ f.read((char*)&book,sizeof(textbook));
cout<<book.id<<'\t'<<book.textbookname
<<'\t'<<book.zhuanye<<'\t'
<<book.number<<endl;
}while(book.id!=key&&f.tellp()!=posEnd);
break;
}
case 3:
{
int number=0;
f.seekg(0,ios::beg);
cout<<"编号"<<'\t'<<"教材名"<<'\t'<<"专业"
<<'\t'<<"数量"<<endl;
do
{ f.read((char*)&book,sizeof(textbook));
cout<<book.id<<'\t'<<book.textbookname
<<'\t'<<book.zhuanye<<'\t'
<<book.number<<endl;
number+=book.number;
}while(book.id!=key&&f.tellp()!=posEnd);
cout<<"现有教材数量为:"<<number<<endl;
break;
}
case 0: return;
}
}
}
}
void CreateTxt(fstream f) //建立文本文件,可打印
{
fstream ftxt("d:\\textbookFile.txt",ios::out); //写方式找开文本文件
textbook book;
f.seekg(0,ios::end);
long posEnd=f.tellg(); //记录二进制文件尾位置
f.seekg(0,ios::beg); //移动读指针到文件头
cout<<"************建立文本文件************\n";
do
{
f.read((char*)&book,sizeof(textbook));
ftxt<<book.id<<'\t'<<book.textbookname<<'\t'<<book.zhuanye
<<'\t'<<book.number<<endl;
}while(f.tellg()!=posEnd);
ftxt.close();
cout<<"文本文件已建立,要浏览文件吗?(Y/N)\n";
char answer,s[200];
cin>>answer;
if(answer='Y'||answer=='y')
{
ftxt.open("d:\\textbook.txt",ios::in); //重用流打开文件
while(!ftxt.eof()) //按行显示文本文件
{
ftxt.getline(s,200);
cout<<s<<endl;
}
}
ftxt.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -