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

maifuming maifuming 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
呼。。。本人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;
}
...全文

相关推荐

c语言中的链表-用结构体和指针构造链表

文章主要介绍在c语言中用结构体和指针构造链表的方法,感兴趣的朋友可以看看。

weixin_38696090

C++课程设计学生管理系统源码详解:链表+文件流实现增删改查排序

C++编程学习中,数据结构与文件操作是绕不开的核心技能。链表作为一种基础且重要的动态数据结构,其增删改查操作直接关系到程序的效率与稳定性;而文件流则解决了数据持久化的问题,让程序重启后仍能恢复现场。两者的结合,天然适合信息管理类应用场景,例如学生成绩管理、员工信息登记等。理解链表节点如何分配与释放、文件流如何读写结构化数据,是掌握C++工程实践的关键一步。本文以一个功能完整、可直接编译运行的学生管理系统为例,从整体设计到模块拆分,逐步讲解如何用链表组织学生数据、用文件流实现保存与加载,并覆盖排序、统计、菜单

脑袋被门夹得好痛

C++ 类 链表 文件流操作

该小系统是用c++类 链表和文件流来写的。。。没有用结构体,内附源代码。对在链表 文件流方面薄弱的同学很有学习的价值

wanglei230513

C语言题库数组,指针,结构体,链表习题

整理的一些关于C语言的练习题,题目答案都有,需要的可以下载看一下

花开莫淡忘

C语言结构体链表的排序方法汇总

C语言结构体链表的排序方法汇总 ========================== 功能:选择排序(由小到大) 返回:指向链表表头的指针 ========================== */ /* 选择排序的基本思想就是反复从还未排好序的那些节点中, 选出键值(就是用它排序的字段,我们取学号num为键值)最小的节点, 依次重新组合成一个链表。 我认为写链表这类程序,关键是理解: head存储的是第一个节点的地址,head->next存储的是第二个节点的地址; 任意一个节点p的地址,只能通过它前一个节点的next来求得。

l003479164
评论 3
收藏 已收藏
分享
返回顶部
maifuming
C++ 语言 65,214 成员 250,514 内容