|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
跪求帮忙分析自己程序。
这个题就是把一个txt文件的东西按照%后面的指令按照单链表进行操作,我几乎完成了,但是最后sort的时候总是出错,我也不知道为什么不行,编译可以通过,但是运行的时候就不行了。
第一个分割线下是我的代码。
第二个分割线是txt文件内的数据。
马上交作业了,碰了一个水货TA,也解决不了,真心没办法了,求贴吧内大神们帮忙,C++的程序
。。。。。。。。。。。。 分割线。。。。。。。。。。。。
#include <iostream>
#include <string.h>
#include <fstream>
#include <stdlib.h>
using namespace std;
class linklist
{
private:
struct node
{
string name;
int age;
node *link;
}*p;
public:
linklist();
void append(string Name, int Age);
void add_as_first(char Name[30], int Age);
void addafter(int c, char Name[30], int Age);
void del(string Name, int Age);
void sear(string Name);
void display();
void sortasc();
int count();
~linklist();
void temp();
};
linklist II;
ifstream infile;
void linklist::temp(){
node *a;
a = new node;
a->name = "dfs";
a->age =44;
p=a;
p=p->link;
cout<<p->name<<endl<<p->age<<endl;
}
linklist::linklist()
{
p =NULL;
}
void linklist:: display()
{
node *q;
cout<< endl;
for (q=p; q != NULL; q =q->link)
cout<< endl<<q->name <<" " <<q->age;
}
void linklist::append(string Name, int Age)
{
node *q, *t;
q = new node; t = new node;
if (p== NULL)
{
p= new node;
q->name=Name; ///////////////
q->age = Age;
q->link= NULL;
p=q;
}
else
{
q= p;
while (q->link != NULL)
q= q->link;
t= new node;
t->name= Name;
t->age= Age;
t->link=NULL;
q->link= t;
}
}
void linklist::add_as_first(char Name[30], int Age)
{
node *q;
q= new node;
q->name= Name;
q->age= Age;
q->link= p;
p=q;
}
void linklist::addafter(int c, char Name[30], int Age)
{
node *q,*t;
int i;
for(i=0,q=p;i<c;i++)
{
q= q->link;
if(q==NULL)
{
cout<<"\n There are less than"<< c<<"elements.";
return;
}
}
t= new node;
t->name[30]=Name[30];
t->age= Age;
t->link= q->link;
q->link=t;
}
int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;
return c;
}
void linklist::del(string Name, int Age)
{
node *q,*r;
q= p;
if (q ->name==Name && q->age== Age)
{
p=q->link;
delete q;
return;
}
r= q;
while (q!= NULL)
{
if (q->name == Name && q->age== Age)
{
r->link= q->link;
delete q;
return;
}
r= q;
q= q->link;
}
cout<<"\nElenment"<<Name<<" not found.";
}
void linklist::sear(string Name)
{
node *q;
for(q=p; q!= NULL;q=q ->link)
{
if (q->name == Name)
{
cout<<endl<<q->name<<" "<<q->age;
}
}
}
void linklist::sortasc()
{
int len = II.count();
node *q = p ;
for (int i=0; i< len; i++)
{
for(int j=0; j< len-i; j++,q= q->link)
{
if(q->age > q->link->age)
{
int tem;
tem = q->age;
q->age = q->link->age;
q->link->age = tem;
string ten;
ten = q->name;
q->name= q->link->name;
q->link->name = ten;
}
}
}
}
linklist::~linklist()
{
node *q;
if (p==NULL)
return;
while(p != NULL)
{
q= p->link;
delete p;
p=q;
}
}
int main()
{
infile.open("a3.txt");
if (infile.fail())
{
cout<<"\n File is failed to open!"<<endl;
}
char *a; a = new char;
infile.getline(a,30);
cout<<"the first instruction is: "<<a<<endl;
cout<<"Do you want to play? (Y/N)";
char huifu;
cin>>huifu;
if (huifu =='n'||huifu=='N')
{
exit(1);
}
else
{
for (int i=0; i<11; i++)
{
string name; int age;
infile>>name>>age;
II.append(name,age);
}
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %VISIT"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu1;
cin>>huifu1;
if (huifu1 =='n'||huifu1=='N')
{
exit(1);
}
else
{
II.display();
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %DELETE"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu2;
cin>>huifu2;
if (huifu2 =='n'||huifu2=='N')
{
exit(1);
}
else
{
II.del("MARK",29);
II.del("DAVID",21);
II.del("DAVID",18);
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %VISIT"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu3;
cin>>huifu3;
if (huifu3 =='n'||huifu3=='N')
{
exit(1);
}
else
{
II.display();
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %SEARCH"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu4;
cin>>huifu4;
if (huifu4 =='n'||huifu4=='N')
{
exit(1);
}
else
{
II.sear("JONE");
II.sear("DAVID");
II.sear("LARRY");
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %INSERT"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu5;
cin>>huifu5;
if (huifu5 =='n'||huifu5=='N')
{
exit(1);
}
else
{
II.append("LARRY",13);
II.append("GARY",15);
II.append("GARY",42);
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %VISIT"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu6;
cin>>huifu6;
if (huifu6 =='n'||huifu6=='N')
{
exit(1);
}
else
{
II.display();
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %INSERT"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu7;
cin>>huifu7;
if (huifu7 =='n'||huifu7=='N')
{
exit(1);
}
else
{
II.append("TERRY",23);
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %DELETE"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu8;
cin>>huifu8;
if (huifu8 =='n'||huifu8=='N')
{
exit(1);
}
else
{
II.del("GARFIED",29);
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %SEARCH"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu9;
cin>>huifu9;
if (huifu9 =='n'||huifu9=='N')
{
exit(1);
}
else
{
II.sear("PHIL");
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %SORTASC"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu10;
cin>>huifu10;
if (huifu10 =='n'||huifu10=='N')
{
exit(1);
}
else
{
II.sortasc();
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %VISIT"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu11;
cin>>huifu11;
if (huifu11 =='n'||huifu11=='N')
{
exit(1);
}
else
{
II.display();
}
infile.getline(a,30);
cout<<endl<<"Next instruction is; %end"<<endl;
cout<<"Do you want to play?(Y/N)";
char huifu12;
cin>>huifu12;
exit(1);
infile.close();
return 0;
}
。。。。。。。a3.txt。。。。。。。。分割线。。。。。。。。。。。
%INSERT
MARK 29
DAVID 21
JOHN 44
JOHN 51
LARRY 39
MARK 21
DAVID 18
JOHN 28
MARK 35
DONALD 41
PHIL 26
%VISIT
%DELETE
MARK
DAVID
%VISIT
%SEARCH
JONE
DAVID
LARRY
%INSERT
LARRY 13
GARY 15
GARY 42
%VISIT
%INSERT
TERRY 23
%DELETE
GARFIELD 29
%SEARCH
PHIL
%VISIT
%SORTASC
%VISIT
%END |
|