求求哪位大佬可以幫我看看我自己寫的這個代碼,不知道哪里的問題,錄入圖書資訊的功能實作不了,也不能讀取檔案資訊,其它功能無法測驗,也不知道有什么問題,知識太少了,弄了好久都沒弄好,想求助大佬幫看看,馬上就要ddl了,或者用陣列或者用類幫忙寫一份代碼也行啊(別太難否則我看不懂)謝謝啦啊
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <string.h>
#include <fstream>
using namespace std;
#define BOOK 100
//圖書資訊
struct Book
{int book_no; //圖書編號
char name[30]; //書名
char author[20]; // 作者
char press[20]; // 出版社
char date[15]; //出版日期:YYYY-MM-DD
char kind[15]; //圖書類別
double price; //圖書單價
int total; //藏書數量
};
//圖書類
class book
{
private:
Book bo_array[BOOK]; //圖書陣列
int booknum; //當前圖書記錄
public:
book();
~book();
void menu();
int get_booknum(); //獲取當前圖書記錄
void input();
void search(); //查找
void update();
void show();
void delete_();
void load_file();
void save_file();
};
book::book()
{
booknum=0;
}
book::~book()
{
}
int book::get_booknum()
{
return booknum;
}
void book::menu()
{
int choice;
system("cls");
cout<<" 圖書管理系統"<<endl;
cout<<"--------------------------------------------"<<endl;
cout<<"\t\t1.錄入圖書資訊"<<endl;
cout<<"\t\t2.瀏覽圖書資訊"<<endl;
cout<<"\t\t3.查詢圖書資訊"<<endl;
cout<<"\t\t4.修改圖書資訊"<<endl;
cout<<"\t\t5.洗掉圖書資訊"<<endl;
cout<<"\t\t6.退出系統"<<endl;
cout<<"請選擇(1-6):";
cin>>choice;
if(choice>=0&&choice<7)
{
switch(choice)
{
case 1:
input();
break;
case 2:
show();
break;
case 3:
search();
break;
case 4:
update();
break;
case 5:
delete_();
break;
case 6:
save_file();
cout<<"感謝您的使用,系統已退出,再見!"<<endl;
system("pause");
exit(0);
}
}
else
{
cout<<"輸入有誤!請重新輸入!"<<endl;
}
}
int main()
{
book book1;
while(1)
{
book1.menu();
}
return 0;
}
void book::input()
{
Book boo;
get_booknum();
while(1)
{
system("cls");
cout<<"下面請輸入(添加)圖書資訊:"<<endl;
cout<<"請輸入圖書編號、書名、作者、出版社、出版日期(輸入0時退出):"<<endl;
cin>>boo.book_no;
if(boo.book_no==0)
return;
cin>>boo.book_no>>boo.name>>boo.author>>boo.press>>boo.date;
cout<<"請輸入圖書類別、圖書價格(單位:元)、藏書數量:"<<endl;
cout<<"(類別:科學技術、語言文學、政治經濟、歷史地理、意識形態、藝術)"<<endl;
cin>>boo.kind>>boo.price>>boo.total;
bo_array[booknum]=boo;
booknum++;
}
}
void book::load_file()
{
Book boo;
ifstream infile;
get_booknum();
infile.open("bookfile.txt",ios::in);
if(!infile)
{
cout<<"無資料檔案bookfile.txt!";
Sleep(3000);
booknum=0;
return;
}
while(!infile.eof())
{
infile>>boo.book_no>>boo.name>>boo.author>>boo.press>>boo.date>>boo.kind>>boo.price>>boo.total;
if(infile)
{
bo_array[booknum]=boo;
booknum++;
}
else
break;
}
infile.close();
return;
}
void book::save_file()
{
ofstream outfile;
get_booknum();
outfile.open("bookfile.txt",ios::out);
if(!outfile)
{
cout<<"無法將資料保存到檔案bookfile.txt中!"<<endl;
Sleep(3000);
return;
}
for(int i=0;i<get_booknum();i++)
{
outfile<<bo_array[i].book_no<<" "<<bo_array[i].name<<" "<<bo_array[i].author<<" "<<bo_array[i].press<<" "<<bo_array[i].date<<" ";
outfile<<bo_array[i].kind<<" "<<bo_array[i].price<<" "<<bo_array[i].total<<endl;
}
outfile.close();
cout<<"已經將"<<booknum<<"個資料保存到檔案bookfile.txt中"<<endl;
Sleep(1000);
return;
}
void book::search()
{
Book boo;
int bo_no;
bool judge=0;
cout<<"請輸入要查詢的圖書編號:"<<endl;
cin>>bo_no;
cout<<"查詢結果:"<<endl;
for(int i=0;i<get_booknum();i++)
{
if(bo_array[i].book_no==bo_no)
{
judge=1;
cout<<"圖書編號 書名 作者 出版社 出版日期 圖書類別 圖書價格 藏書數量 "<<endl;
cout<<"------------------------------------------------------------------------------------------------------"<<endl;
cout<<setw(10)<<bo_array[i].book_no<<setw(15)<<bo_array[i].name<<setw(15)<<bo_array[i].author<<setw(20)<<bo_array[i].press;
cout<<setw(15)<<bo_array[i].date<<setw(10)<<bo_array[i].kind<<setw(8)<<bo_array[i].price<<setw(10)<<bo_array[i].total<<endl;
}
}
if(judge==0)
{
cout<<"查詢不到該編號!"<<endl;
}
system("pause");
system("cls");
}
void book::delete_()
{
bool judge=0;
int bo_no;
char sel;
cout<<"請輸入要洗掉的圖書編號:"<<endl;
cin>>bo_no;
for(int i=0;i<get_booknum();i++)
{
if(bo_array[i].book_no==bo_no)
{
judge=1;
cout<<"圖書編號 書名 作者 出版社 出版日期 圖書類別 圖書價格 藏書數量 "<<endl;
cout<<"------------------------------------------------------------------------------------------------------"<<endl;
cout<<setw(10)<<bo_array[i].book_no<<setw(15)<<bo_array[i].name<<setw(15)<<bo_array[i].author<<setw(20)<<bo_array[i].press;
cout<<setw(15)<<bo_array[i].date<<setw(10)<<bo_array[i].kind<<setw(8)<<bo_array[i].price<<setw(10)<<bo_array[i].total<<endl;
cout<<"是否確認洗掉該條記錄?(Y/N)"<<endl;
while(1)
{
cin>>sel;
if(sel=='Y'||sel=='y'||sel=='N'||sel=='n')
{
break;
}
else
{
cout<<"輸入有誤,請重新輸入!"<<endl;
}
}
if(sel=='Y'||sel=='y')
{
for(int j=i;j<get_booknum();j++)
{
bo_array[j].book_no=bo_array[j+1].book_no;
for(int k=0;k<30;k++)
{
bo_array[j].name[k]=bo_array[j].name[k+1];
bo_array[j].author[k]=bo_array[j].author[k+1];
}
}
}
if(sel=='N'||sel=='n')
{
menu();
}
}
}
if(judge==1)
{
cout<<"洗掉成功!"<<endl;
booknum--;
}
if(judge==0)
{
cout<<"查詢不到該編號!"<<endl;
}
system("pause");
system("cls");
}
void book::update()
{
bool judge=0;
int bo_no;
char sel;
cout<<"請輸入要修改的圖書編號:"<<endl;
cin>>bo_no;
for(int i=0;i<get_booknum();i++)
{
if(bo_array[i].book_no==bo_no)
{
judge=1;
cout<<"圖書編號 書名 作者 出版社 出版日期 圖書類別 圖書價格 藏書數量 "<<endl;
cout<<"------------------------------------------------------------------------------------------------------"<<endl;
cout<<setw(10)<<bo_array[i].book_no<<setw(15)<<bo_array[i].name<<setw(15)<<bo_array[i].author<<setw(20)<<bo_array[i].press;
cout<<setw(15)<<bo_array[i].date<<setw(10)<<bo_array[i].kind<<setw(8)<<bo_array[i].price<<setw(10)<<bo_array[i].total<<endl;
cout<<"是否確認修改該圖書資訊?(Y/N)"<<endl;
while(1)
{
cin>>sel;
if(sel=='Y'||sel=='y'||sel=='N'||sel=='n')
{
break;
}
else
{
cout<<"輸入有誤,請重新輸入!"<<endl;
}
}
if(sel=='Y'||sel=='y')
{
judge=1;
cout<<"圖書編號:";
cin>>bo_array[i].book_no;
cout<<"圖書書名:";
cin>>bo_array[i].name;
cout<<"圖書作者:";
cin>>bo_array[i].author;
cout<<"出版社:";
cin>>bo_array[i].press;
cout<<"出版日期:";
cin>>bo_array[i].date;
cout<<"修改成功!"<<endl;
}
if(sel=='N'||sel=='n')
{
menu();
}
}
}
if(judge==0)
{
cout<<"查詢不到該編號!"<<endl;
}
}
void book::show()
{
cout<<"圖書編號 書名 作者 出版社 出版日期 圖書類別 圖書價格 藏書數量 "<<endl;
cout<<"------------------------------------------------------------------------------------------------------"<<endl;
for(int i=0;i<booknum;i++)
{
cout<<setw(10)<<bo_array[i].book_no;
cout<<setw(15)<<bo_array[i].name;
cout<<setw(15)<<bo_array[i].author;
cout<<setw(20)<<bo_array[i].press;
cout<<setw(15)<<bo_array[i].date;
cout<<setw(10)<<bo_array[i].kind;
cout<<setw(8)<<bo_array[i].price;
cout<<setw(10)<<bo_array[i].total;
cout<<endl;
if((i+1)%10==0)
system("pause");
}
}
uj5u.com熱心網友回復:
delete函式定義結尾不要用“-”,bool型函式雖然true是不為1,false是0,但使用時不要用0或1,還要用false和true,你改一下,不行咱們再研究研究。uj5u.com熱心網友回復:
這個代碼錄入不了資訊,在線求助uj5u.com熱心網友回復:
把你的char[]改為string,否則就要用copy轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/9532.html
標籤:C++ 語言
下一篇:c++
