Hoshea_Z
雪藏尘.246
2021-06-10 21:35
采纳率: 100%
浏览 31

C++图书信息管理系统文件读取问题

#include <iostream>
#include <string>//使用string类 
#include <vector>//vector数组
#include <fstream>//文件操作 
#include<conio.h>//getch()函数
#include<algorithm>//库函数sort排序 
using namespace std;
class Book//图书基本信息类 
{
	public:
		string Id;//序列号 
		string Name;//书名 
		string Writer;//作者 
		string Type;//分类  
		string Publisher;//出版社 
		string Pubtime;//出版时间 
	    Book(){	};//构造函数 
	    ~Book(){};//析构函数 
};

class Price:public Book//价格类,继承图书类 
{
	public:
		double price;//价格 
		friend istream& operator>>(istream& in,Price &t)//重载>> 
		{in>>t.Id>>t.Name>>t.Writer>>t.Type>>t.Publisher>>t.Pubtime>>t.price;}
		friend ostream& operator>>(ostream& out,const Price &t)//重载<< 
		{out<<t.Id<<"\t"<<t.Name<<"\t"<<t.Writer<<"\t"<<t.Type<<"\t"<<t.Publisher<<"\t"<<t.Pubtime<<"\t"<<t.price<<endl;}
};

class Admin//管理类
{
	public:
		vector<Price>stu;//定义vector数组 
		void Add();//添加图书信息 
		void Show();//显示所有图书信息 
		void Change();//修改图书信息 
		void Delete();//删除图书信息 
		void Search();//查找图书信息 
		void ShowSort();//按价格排序 
		int  Get();//读取文件 
		void Keep();//保存信息 
  };

void Admin::Add()//添加图书信息 
{
	Price p;
	cout<<"Input Id:";cin>>p.Id;
	cout<<"Input Name:";cin>>p.Name;
	cout<<"Input Writer:";cin>>p.Writer;
	cout<<"Input Type:";cin>>p.Type;
	cout<<"Input Publisher:";cin>>p.Publisher;
	cout<<"Input Pubtime:";cin>>p.Pubtime;
	cout<<"Input Price:";cin>>p.price;
	stu.push_back(p);
	this->Keep();
	cout<<"图书信息添加成功!"<<endl;
}

void Admin::Show()//显示全部图书信息
{
	vector<Price>::iterator iter;//定义vector迭代器
	for(iter=this->stu.begin();iter!=this->stu.end();iter++) //遍历vector数组
	  {cout<<"Id:"<<iter->Id<<"Name:"<<iter->Name<<"Writer:"<<iter->Writer<<"Type:"<<iter->Type
	  <<"Publisher:"<<iter->Publisher<<"Pubtime:"<<iter->Pubtime<<"Price:"<<iter->price<<endl;//输出所有数据 
	   } 
 } 
 
void Admin::Change()//修改图书信息 
{
   if(stu.empty())//判断数组是否有图书信息
	{
		cout<<"暂无图书信息,无法修改"<<endl;
		return;  //返回主函数 
	}
	string _name;
	cout<<"Input Name: ";cin>>_name;
	vector<Price>::iterator iter; //定义Vector迭代器 
	for(iter=this->stu.begin();iter!=this->stu.end();)//遍历vector数组
	{
		if(iter->Name==_name)    //根据输入的Name找到成绩信息 
		{
			cout<<"Input Id:";cin>>iter->Id;
	        cout<<"Input Writer";cin>>iter->Writer;
	        cout<<"Input Type";cin>>iter->Type;
	        cout<<"Input Publisher";cin>>iter->Publisher;
	        cout<<"Input Pubtime";cin>>iter->Pubtime;
        	cout<<"Input Price";cin>>iter->price;
			this->Keep();  //调用keep函数将修改后的数据保存 
			cout<<"修改成功!"<<endl; 
		}
		else
			iter++; 
	}
 }
  
void Admin::Delete()//删除图书信息 
{
    if(stu.empty())//判断数组是否有图书信息
	{
		cout<<"暂无图书信息,无法修改"<<endl;
		return;  //返回主函数 
	}
    string _name;
	cout<<"Input Name: ";cin>>_name;
	vector<Price>::iterator iter; //定义Vector迭代器 
	for(iter=this->stu.begin();iter!=this->stu.end();)//遍历vector数组
	{
		if(iter->Name==_name)    //根据输入的Name找到成绩信息 
		{
			stu.erase(iter);//从vector数组中删除图书信息 
			this->Keep(); 
			cout<<"删除成功!"<<endl; 
		} 
	}	
}

void Admin::Search()//查找图书信息 
{cout<<"1.按书名查找;"<<endl;
 cout<<"2.按作者查找;"<<endl;
 int n;
 cout<<"Choose:";cin>>n;
 switch(n)
 {case 1:
    {
	string _name;
	cout<<"Input Name: ";cin>>_name;
	vector<Price>::iterator iter; //定义Vector迭代器 
	for(iter=this->stu.begin();iter!=this->stu.end();)//遍历vector数组
	{
		if(iter->Name==_name)    //根据输入的Name找到成绩信息 
		{
			cout<<"Id:"<<iter->Id<<"Name:"<<iter->Name<<"Writer:"<<iter->Writer<<"Type:"<<iter->Type
	        <<"Publisher:"<<iter->Publisher<<"Pubtime:"<<iter->Pubtime<<"Price:"<<iter->price<<endl;//输出所有数据 
	    }
    }
   }
  break;
  case 2:
  	{
    string _writer;
	cout<<"Input Writer: ";cin>>_writer;
	vector<Price>::iterator iter; //定义Vector迭代器 
	for(iter=this->stu.begin();iter!=this->stu.end();)//遍历vector数组
	{
		if(iter->Writer==_writer)    //根据输入的Name找到成绩信息 
		{
			cout<<"Id:"<<iter->Id<<"Name:"<<iter->Name<<"Writer:"<<iter->Writer<<"Type:"<<iter->Type
	        <<"Publisher:"<<iter->Publisher<<"Pubtime:"<<iter->Pubtime<<"Price:"<<iter->price<<endl;//输出所有数据 
	    }
    }
  break;
 }
}
}

bool operator < (Price &A, Price &B)//定义排序规则
{return  A.price>B.price; } 

void Admin::ShowSort()//按价格排序图书信息 
{
	sort(stu.begin(),stu.end());//调用库函数sort
	vector<Price>::iterator iter;//定义vector迭代器
	for(iter=this->stu.begin();iter!=this->stu.end();iter++) //遍历vector数组
	  {cout<<"Id:"<<iter->Id<<"Name:"<<iter->Name<<"Writer:"<<iter->Writer<<"Type:"<<iter->Type
	  <<"Publisher:"<<iter->Publisher<<"Pubtime:"<<iter->Pubtime<<"Price:"<<iter->price<<endl;//输出所有数据 
	   } 
}

int  Admin::Get()//获取文件 
{
	fstream myfile("ohu.txt");//打开文件
	int row=0;
	if(myfile)//判断文件是否存在,如果存在
	{
		Price p;
		if(!(myfile>>p.Id>>p.Name>>p.Writer>>p.Type>>p.Publisher>>p.Pubtime>>p.price)) //第一次读取为空
		  {cout<<"文件为空"<<endl;
		   return 1; 
		  } 
		stu.push_back(p);
		row++;
		while(myfile>>p.Id>>p.Name>>p.Writer>>p.Type>>p.Publisher>>p.Pubtime>>p.price)
		{row++;
		 stu.push_back(p);
		 return row;
		}
	}
	else 
	{ofstream outfile;//若没有则创建
	 outfile.open("ohu.txt");
	 if(!outfile.is_open()) 
	   {cout<<"文件未创建!";
	    return -1; 
	   }
	 else cout<<"文件为空!"<<endl;
	      outfile.close(); //关闭文件 
	} 
	return 2;
}

void Admin::Keep()//保存 
{
 ofstream outfile("ohu.txt");
 vector<Price>::iterator iter;//vector保存磁盘文件 
	for(iter=this->stu.begin();iter!=this->stu.end();iter++) //遍历vector数组
	   {outfile<<iter->Id<<"\t"<<iter->Name<<"\t"<<iter->Writer<<"\t"<<iter->Type<<"\t"
	    <<iter->Publisher<<"\t"<<iter->Pubtime<<"\t"<<iter->price<<endl;
	   }
 outfile.close(); //关闭文件
}

int main()
{
	int n;
	Admin a;//定义对象 
	a.Get();//从文件中读取数据 
	do{
		cout<<endl<<" 图书信息管理系统  "<<endl;
		cout<<"1、添加图书信息   " <<endl;  
		cout<<"2、显示图书信息   " <<endl; 
		cout<<"3、修改图书信息   " <<endl; 
		cout<<"4、删除图书信息   " <<endl; 
		cout<<"5、按价格图书信息" <<endl; 
		cout<<"6、查询图书信息" <<endl; 
		cout<<"7、保存并退出   " <<endl; 
		cout<<"请选择 " ;cin>>n;
		switch(n)
		{
			case 1: a.Add();getch();break;
			case 2: a.Show();getch();break;
			case 3: a.Change();getch();break;
			case 4: a.Delete();getch();break;
			case 5: a.ShowSort();getch();break;
			case 6: a.Search(); getch();break;
			case 7: a.Keep();exit(0);
		 } 
	}while(1);
	return 0;
} 

请问这个图书管理系统编译运行之后只能读出图书目录文件里的前两个是怎么回事啊,求助各位大佬

请问这个图书管理系统编译运行之后只能读出图书目录文件里的前两个是怎么回事啊,求助各位大佬

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • technologist_37
    CSDN专家-link 2021-06-10 21:39
    已采纳

    while(myfile>>p.Id>>p.Name>>p.Writer>>p.Type>>p.Publisher>>p.Pubtime>>p.price)

    {

    row++;

    stu.push_back(p);

    return row;

    }

    循环里面直接return row了。读了一行就结束了,再加上上面的代码读了第一行,所以就两行啊

    把return row放到while外面就行了,whilie结束函数才return啊

    点赞 1 评论

相关推荐