懂c++的朋友请帮忙啊。。。

来源:百度知道 编辑:UC知道 时间:2024/05/09 14:31:46
#include <iostream>
#include <string>
using namespace std;
class book
{
public:
int num;
float price;
book *next;
};
book*head=NULL;
bool check(string str)
{
for (int i=0;i<str.length();i++)
{
if ((str[i]>'9'||str[i]<'0')&&(str[i]!='.'))
{
return false;
}
return true;
}
}
book*creat()
{
book*p1,*p2;
string str;
p1=new book; //这里给p1分配个堆中空间有什么作用?
head=p1;
p2=p1; //这里p2=p1又是什么作用?
cout<<"请输入图书的编号,以0结束"<<endl;
cin>>str;
while(!check(str))
{
cout<<"输入错误,请从新输入"<<endl;
cin>>str;
}
p1->num=atoi(str.c_str());
if (p1->num!=0)
{
cout<<"请输入图书的价格"<<endl;
cin>>str;
while(!check(str))
{
cout<

book*p1,*p2
这里只是申明了指针 ,但是没有分配实际的空间,
在第一次使用的时候必须用new分配实际空间

p2=p1; //这里为什么p2又要指向p1?
p1=new book; //为什么又要申请内存给p1
这里是添加一本新书吧,所以要新分配一个空间,并连接在p2后面

这个其实就是一个链表的问题,你仔细看看链表,如果你的C/C++教材上没有动态链表,那你就买本数据结构,上面就有对链表,二杈树,双向链表等等的操作方法与讲解