[求助贴]用链表、结构体和流和输入输出文件流的知识知识来编个程序

maifuming 2011-05-11 06:05:25
呼。。。本人C++语言基础还很菜,参考书本写了个程序,却出错,程序我调试了一天还是调试不出哪出错,细看了程序,可能本人基础还不够好吧,找不出解决方法。。。没办法,只好来求助哪位大神能否帮助下找出哪里出错。。。
这程序我是在VC2010下调试的。。。先谢各位了。。。呵呵。。。


//BookList.h文件,Book和BookList类的声明
#include "stdafx.h"
#ifndef BOOKLIST_H
#define BOOKLIST_H
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int NUM=100; //图书最多不超过100本。
class Book
{
public:
char booknum[6];
char bookname[12];
char author[8];
double price;
Book *next;
Book *head;
};
class BookList
{
private:
Book book[NUM];
Book *head;
public:
BookList(){
head=NULL;
};
void CreatBook(int n);
void Pri2File();
void Find_high_30();
void Serch();
void Pri_b_list();
~BookList()
{
Book *p;
while(head)
{
p=head;
head=head->next;
delete p;
}
}
};
#endif

// BookList.cpp BookList的相关成员函数的定义

#include "stdafx.h"
#include "BookList.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
using namespace std;
void BookList :: CreatBook(int n)//按输入顺序建立链表
{
char booknum[6],bookname[12],author[8];
double price;
Book *tail,*newnode;
int j=0;
cout<<"请输入图书书号、书名、作者、价格:"<<endl;
while(j<n)
{
newnode=new Book;
cin>>booknum>>bookname>>author>>price;
strcpy(newnode->booknum,booknum);
strcpy(newnode->bookname,bookname);
strcpy(newnode->author,author);
newnode->price=price;
newnode->next=NULL;
if(head->next==NULL)
head->next=newnode;
else
tail->next=newnode;//让尾结点的指针指向新增结点
tail=newnode;//新增结点成为尾结点 *|* 被赋指针,即赋指向;被赋结点,即使指向
j++;
}
}
void BookList :: Pri2File()
{
Book *current;
current=head->next;
ofstream bookfile("ST1.dat");
if(!bookfile)
{
cout<<"不能打开bookfile!"<<endl;
}
bookfile.setf(ios::showpoint);
bookfile.setf(ios::fixed);
bookfile.precision(2);
while(current!=NULL)
{
bookfile<<current->booknum<<" "<<current->bookname<<" "<<current->author<<" "<<current->price<<endl;
current=current->next;
}
bookfile.close();
cout<<"已将图书信息保存到ST1.DAT文件中。"<<endl<<endl;
}
void BookList :: Find_high_30()
{
char booknum[6];
char bookname[12];
char author[8];
double price;
ifstream bookfile("ST1.dat");
if(!bookfile)
{
cout<<"不能打开bookfile!"<<endl;
}
cout<<"价格高于30元的图书信息如下:"<<endl;
bookfile.setf(ios::showpoint);
bookfile.setf(ios::fixed);
bookfile.precision(2);
while(bookfile)
{
bookfile>>booknum>>bookname>>author>>price;

if(price>30)
{
cout<<booknum<<" "<<bookname<<" "<<author<<" "<<price<<endl;
price=NULL;
}

}
bookfile.close();
}
void BookList::Serch()
{
int flag=0;
char temp_num[6];
char booknum[6];
char bookname[12];
char author[8];
double price;
ifstream bookfile("ST1.dat");
if(!bookfile)
{
cout<<"不能打开bookfile!"<<endl;
}
cout<<"请输入你想查询的图书书号:"<<endl;
cin>>temp_num;
while(bookfile)
{
bookfile>>booknum>>bookname>>author>>price;
if(strcmp(temp_num,booknum)==NULL)
{
cout<<"你查找的图书信息如下:"<<endl;
cout<<booknum<<" "<<bookname<<" "<<author<<" "<<price<<endl;
flag=1;
}
}
if(flag==0)
cout<<"你所查找的图书不存在!"<<endl;
bookfile.close();
}
void BookList :: Pri_b_list()
{
Book *current;
current=head->next;
cout<<"图书信息:"<<endl;
cout<<"书号、书名、作者、价格:"<<endl;
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
while(current!=NULL)
{
cout<<current->booknum<<" "<<current->bookname<<" "<<current->author<<" "<<current->price<<endl;
current=current->next;
}
cout<<endl;
}


//BookList_main.cpp 主函数
#include "stdafx.h"
#include "BookList.h"
int _tmain(int argc, _TCHAR* argv[])
{
BookList book;
int n;
cout<<"请输入图书本数(小于100本):";
cin>>n;
book.CreatBook(n);
book.Pri2File();
book.Pri_b_list();
book.Find_high_30();
book.Serch();
return 0;
}
...全文
98 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
maifuming 2011-05-11
  • 打赏
  • 举报
回复
如何是好呢?。。。哎,没人能帮下吖。。。麻烦下啦。。。。
maifuming 2011-05-11
  • 打赏
  • 举报
回复
咳。。。没人能帮忙吗?。。。
jjajun 2011-05-11
  • 打赏
  • 举报
回复
一般都是链表操作时,指针指错了

64,686

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧