读书人

C++链表有关问题

发布时间: 2012-06-12 14:21:25 作者: rapoo

C++链表问题
#include<iostream>
#include<cstdlib>
using namespace std;




class Mytoy
{
double counts;
static double sum;
public:
int the_number_of_toy;
double the_price_of_toy;
char serial_number[5];
Mytoy();
~Mytoy();
void data();
void count();
void show();
int show_the_number_of_toy(){return the_number_of_toy;}
double show_the_price_of_toy(){return the_price_of_toy;}
Mytoy* next;
};

double Mytoy::sum=0;


void fun_add(Mytoy* head);

Mytoy* fun_search_n(Mytoy* head,int the_number_of_toy);
Mytoy* fun_search_p(Mytoy* head,double the_price_of_toy);

void fun_divide(Mytoy* before,Mytoy* & divide);


void fun_change(Mytoy* & change);



int main()
{
int the_count_number_of_toys;

cout<<"Enter the number of toys ";
cin>>the_count_number_of_toys;
cout<<endl;
Mytoy* toy_array;
toy_array=new Mytoy[the_count_number_of_toys];
Mytoy* head;

head=toy_array;//指向链表的头

for(int i=0;i<the_count_number_of_toys;i++)
{
cout<<"Now you enter the data of toy--"<<i+1<<endl;
toy_array[i].data();
toy_array[i].count();
cout<<endl;
}
toy_array[i].show();
cout<<endl;

cout<<" The list is"<<endl;
cout<<"The serial_number "<<"The number "<<"The price "<<endl;
for(i=0;i<the_count_number_of_toys;i++)
{

cout<<" "<<toy_array[i].serial_number <<" "<<toy_array[i].show_the_number_of_toy() <<" "<<toy_array[i].show_the_price_of_toy() <<endl;
}
cout<<endl;

cout<<"Here are some function hope you do some works ."<<endl;

cout<<endl;

char choice;
cout<<"Here are some function hope you do some works ."<<endl;
cout<<"------------------------------------------------------"<<endl;
cout<<" C "<<" | "<<" change the price or number "<<endl;
cout<<" A "<<" | "<<" add the toy "<<endl;
cout<<" D "<<" | "<<" delete the toy "<<endl;
cout<<" S "<<" | "<<" search the toy "<<endl;
cout<<"------------------------------------------------------"<<endl;
cout<<"which your choice : ";
cin>>choice;
cout<<endl;

char ch;
do
{


switch(choice)
{
case 'C': char C_information;
cout<<"The toy's information enter(serial_number(n) or price(p)) whitch do you remember "<<endl;
cin>>C_information;
switch(C_information)
{
case 'n': int C_number;
cout<<"Enter the number :";
cin>>C_number;
Mytoy* change1;
change1=fun_search_n(head,C_number);
fun_change( change1);
head=toy_array;
break;

case 'p': double C_price;
cout<<"Enter the price :";
cin>>C_price;
Mytoy* change2;
change2=fun_search_p(head,C_price);
fun_change( change2);
head=toy_array;
break;

default:break;
}break;
case 'A':fun_add(head);


head=toy_array;
break;
case 'D':char D_information;
cout<<"The toy's information enter(serial_number(n) or price(p)) whitch do you remember "<<endl;
cin>>D_information;
switch(D_information)
{
case 'n': int D_number;
cout<<"Enter the number :";
cin>>D_number;
Mytoy* change3;
change3=fun_search_n(head,D_number);
fun_divide(head,change3);
head=toy_array;
break;

case 'p': double D_price;
cout<<"Enter the price :";
cin>>D_price;
Mytoy* change4;
change4=fun_search_p(head,D_price);
fun_divide(head,change4);
head=toy_array;
break;

default:break;
}

break;
case 'S':char S_information;
cout<<"The toy's you want to search it's information enter(serial_number(n) or price(p)) whitch do you remember "<<endl;
cin>>S_information;
switch(S_information)
{
case 'n': int S_number;
cout<<"Enter the number :";
cin>>S_number;
Mytoy* S_n;
S_n=fun_search_n(head,S_number);
S_n->count();
S_n->show ();
head=toy_array;
break;

case 'p': double S_price;
cout<<"Enter the price :";
Mytoy* S_p;
S_p=fun_search_p(head,S_price);
S_p->count();
S_p->show ();
head=toy_array;
break;

default:break;
}
break;
default:break;
}
cout<<"If you want to end the program press(E or e)"<<endl;
cout<<"Your choice is :";
cin>>ch;

cout<<"Here are some function hope you do some works ."<<endl;
cout<<"------------------------------------------------------"<<endl;
cout<<" C "<<" | "<<" change the price or number "<<endl;
cout<<" A "<<" | "<<" add the toy "<<endl;
cout<<" D "<<" | "<<" delete the toy "<<endl;
cout<<" S "<<" | "<<" search the toy "<<endl;
cout<<"------------------------------------------------------"<<endl;
cout<<"which your choice : ";
cin>>choice;
cout<<endl;

}while(ch!='E'&&ch!='e');



delete [] toy_array;

return 0;
}




Mytoy::Mytoy():the_number_of_toy(0),the_price_of_toy(0),counts(0)
{}

void Mytoy::data()
{
cout<<"This toy's sreiol number is ";
cin>>serial_number;
cout<<serial_number<<endl;
cout<<"This toy's number is ";
cin>>the_number_of_toy;
cout<<endl;
do{
try{
cout<<"This toy's price is ";
cin>>the_price_of_toy;

if(!(the_price_of_toy>=0))
throw the_price_of_toy;
}
catch(double e)
{
cout<<"Again the toy's price is ";
cin>>the_price_of_toy;
}
}while(the_price_of_toy<0);


}


void Mytoy::count()
{
counts=the_number_of_toy*the_price_of_toy;
cout<<"The counts is "<<counts<<endl;
sum=sum+counts;
}


void Mytoy::show()
{
cout.setf(ios::fixed);


cout.setf(ios::showpoint);
cout.precision(3);
cout<<"The result is "<<sum<<"$"<<endl;
}


Mytoy::~Mytoy ()
{}




void fun_change(Mytoy* & change)
{
cout<<endl;
cout<<"Now you can change the information of this "<<endl;
cout<<"choice the char to change data"<<endl;
cout<<"the_number_of_toy"<<" | "<<"n"<<endl;
cout<<"the_price_of_toy"<<" | "<<"p"<<endl;
cout<<"the serial_number[5]"<<" | "<<"c"<<endl;
cout<<"all to change "<<" | "<<"&"<<endl;
cout<<endl;
char ch;
cout<<"Your choice ";
cin>>ch;
switch(ch)
{
case 'n':cout<<"The number change into :";
cin>>change->the_number_of_toy;
change->count();break;

case 'p':cout<<"The price into :";
cin>>change->the_price_of_toy;
change->count();break;

case 'c':cout<<"The serial_number into :";
cin>>change->serial_number;
change->count();break;

case '&':cout<<"The number change into :";
cin>>change->the_number_of_toy;
cout<<"The price into :";
cin>>change->the_price_of_toy;
"The serial_number into :";
cin>>change->serial_number;
change->count();break;
default: break;
}

}




Mytoy* fun_search_n(Mytoy* head,int the_number_of_toy)
{
Mytoy* here=head;

if(here==NULL)
{return NULL;}
else
{
while(here->the_number_of_toy!=the_number_of_toy&&here->next!=NULL)
{
here=here->next;
}

if(here->the_number_of_toy ==the_number_of_toy)
{
return here;
}
else
{
return NULL;
}


}
}



Mytoy* fun_search_p(Mytoy* head,double the_price_of_toy)
{
Mytoy* here=head;

if(here==NULL)
{return NULL;}
else
{
while(here->the_price_of_toy!=the_price_of_toy&&here->next!=NULL)
{
here=here->next;
}

if(here->the_price_of_toy ==the_price_of_toy)
{
return here;
}
else
{
return NULL;
}


}
}



void fun_add(Mytoy* head)
{
Mytoy* temp;
temp=new Mytoy;
cout<<"the new toy's number :";
cin>>temp->the_number_of_toy;
cout<<endl;

cout<<"the new toy's price :";
cin>>temp->the_price_of_toy;
cout<<endl;

cout<<"the new toy's serial_number :";
cin>>temp->serial_number;
cout<<endl;

temp->count();
temp->next =head;
head=temp;
temp->show();


}


void fun_divide(Mytoy* before,Mytoy* & divide)
{
before->next=divide;
before->next =divide->next ;
delete divide;
}





查找链表总是不对,求帮助。


[解决办法]
here->the_price_of_toy!=the_price_of_toy
浮点数一般不能用= 或者!=.
这么写
#define EQUAL_DOUBLE(a,b) ((a-b<1e-8)&&(a-b>-1e-8))

while(!EQUAL(here->the_price_of_toy,the_price_of_toy)&&here->next!=NULL)


{
here=here->next;
}

[解决办法]

C/C++ code
楼主你的 查找 价格的是函数是这样的,根据next指针向往下查找,但你在 构造链表的时候 根本就没 把每个结点的next指针连接起来,你用数组来保存了数据,根本不是用链表,所以这里查找的时候,要用 数组下标去循环。。。Mytoy* fun_search_p(Mytoy* head,double the_price_of_toy){    Mytoy* here=head;    if(here==NULL)    {return NULL;}    else    {        while(here->the_price_of_toy!=the_price_of_toy&&here->next!=NULL)        {            here=here->next;        }                if(here->the_price_of_toy ==the_price_of_toy)        {            return here;        }        else        {            return NULL;        }                    }} 

读书人网 >C语言

热点推荐