做的 指针 链表,del_line,add_line de 指针都是起零时储存作用。
如果输入的是其他的就没问题:如:1 From where does my help come? (Line 2)
就是输:1 Unto the hills I will left up my eyes (Line 1)要出问题,郁闷啊
[[it] 本帖最后由 wolfs 于 2008-3-18 13:30 编辑 [/it]]
----------------解决方案--------------------------------------------------------
head 你有地址吗?
我随便改了下,不改的话编译都不能通过..
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct line
{
char str[40];
struct line *next;
};
int get_choice (struct line *p);
struct line *add_line(struct line *p);
struct line *del_line(struct line *p);
int main ()
{
struct line *head;
int x;
head=(struct line *)malloc(sizeof(struct line));
strcpy(head->str,"diyichi");
head->next=NULL;
x=get_choice(head);
for (;x!=3;)
{
if (x==1)
head=add_line(head);
else
head=del_line(head);
x=get_choice(head);
}
}
struct line * del_line(struct line *p)
{
struct line *p_start,*p_last;
int i,number;
cout <<"\tWhich line number?";
cin >>number;
if (number==1)
p_start=p->next;
else
{
for (p_start=p,i=1;i<number;++i,p=p->next)
p_last=p;
p_last->next=p->next;
}
free(p);
return (p_start);
}
int get_choice(struct line *p)
{
int i,choice;
cout <<"\n...This is the start of your file so far...\n";
for (i=1;p!=NULL;++i,p=p->next)
cout <<i<<" "<<p->str<<endl;
cout <<"...This is the end";
cout <<"(Enter 1 to Insert,2 to Delete,and 3 to Quit)\n\t";
cin >>choice;
return choice;
}
struct line *add_line (struct line *p)
{
int number,i;
struct line *p_last, *p_new, *p_start;
char str_new[40];
p_start=p;
p_new=(struct line *)malloc(sizeof(struct line));
cout <<"First enter the line number and then its contens:\n";
cin >>number;
cin.getline(str_new,40);
for (i=1;i<number;++i,p=p->next)
p_last=p;
p_new->next=p;
strcpy(p_new->str,str_new);
if (i==1)
return (p_new);
else
{
p_last->next=p_new;
return (p_start);
}
}
[[it] 本帖最后由 sunkaidong 于 2008-3-18 13:43 编辑 [/it]]
----------------解决方案--------------------------------------------------------
head 的地址是malloc分配的啊
运行时输入:1 Unto the hills I will left up my eyes (Line 1) 程序就自动转入 del_line 了,然后就显示说程序错误,需要关闭; 程序原来是要在get_choice中输入 choice=2才会转入 del_line 的
输入其他的就没问题,想不通,是这句话有问题还是程序或编译器的问题
[[it] 本帖最后由 wolfs 于 2008-3-18 15:11 编辑 [/it]]
----------------解决方案--------------------------------------------------------
我帮你改好了..指针是好东西..呵呵
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct line
{
char str[40];
struct line *next;
};
int get_choice (struct line *p);
void add_line(struct line *p);
void del_line(struct line *p);
int get_choice ();
static int len=0;
struct line *head;
int main ()
{
int x;
head=(struct line *)malloc(sizeof(struct line));
head->next=NULL;
x=get_choice();
for (;x!=3;)
{
if (x==1)
add_line(head);
else
del_line(head);
x=get_choice(head);
}
return 1;
}
void del_line(struct line *p)
{
struct line *p_start,*p_last;
int i,number;
cout <<"\tWhich line number?";
cin >>number;
if (number==1)
p_start=p->next;
else
{
for (p_start=p,i=1;i<number;++i,p=p->next)
p_last=p;
p_last->next=p->next;
}
free(p);
len--;
head=p_start;
}
int get_choice()
{
int choice;
cout <<"\n...This is the start of your file so far...\n";
cout <<"(Enter 1 to Insert,2 to Delete,and 3 to Quit)\n\t";
cin >>choice;
return choice;
}
int get_choice(struct line *p)
{
int i,choice;
cout <<"\n...This is the start of your file so far...\n";
for (i=1;p->next!=NULL;p=p->next,i++)
cout <<i<<" "<<p->str<<endl;
cout <<i<<" "<<p->str<<endl;
cout <<"...This is the end"<<endl;
cout <<"(Enter 1 to Insert,2 to Delete,and 3 to Quit)\n\t";
cin >>choice;
return choice;
}
void add_line (struct line *p)
{
struct line *p_new, *p_start;
char str_new[40];
len++;
p_start=p;
p_new=(struct line *)malloc(sizeof(struct line));
cout <<"First enter the line number and then its contens:\n";
cout<<len<<":";
fflush(stdin);
cin.getline(str_new,40);
fflush(stdin);
for (;p->next!=NULL;p=p->next);
if(len==1)
{
p->next=NULL;
strcpy(p->str,str_new);
}
else
{
strcpy(p_new->str,str_new);
p->next=p_new;
p_new->next=NULL;
}
//return p_start;
}
----------------解决方案--------------------------------------------------------
支持一下
----------------解决方案--------------------------------------------------------
你两兄弟,不管如何,先谢了,等下回赖在测,上课去了,呵呵呵
----------------解决方案--------------------------------------------------------
你要小心空指针问题..而且指针是地址..其实即使你不用做参数..函数结束后空间还是会保留的..所以没必要用做参数..容易错..很难改正的
----------------解决方案--------------------------------------------------------
如果输入的是其他的就没问题:如:1 From where does my help come? (Line 2)
就是输:1 Unto the hills I will left up my eyes (Line 1)要出问题,郁闷啊
[[it] 本帖最后由 wolfs 于 2008-3-18 13:30 编辑 [/it]]
----------------解决方案--------------------------------------------------------
head 你有地址吗?
我随便改了下,不改的话编译都不能通过..
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct line
{
char str[40];
struct line *next;
};
int get_choice (struct line *p);
struct line *add_line(struct line *p);
struct line *del_line(struct line *p);
int main ()
{
struct line *head;
int x;
head=(struct line *)malloc(sizeof(struct line));
strcpy(head->str,"diyichi");
head->next=NULL;
x=get_choice(head);
for (;x!=3;)
{
if (x==1)
head=add_line(head);
else
head=del_line(head);
x=get_choice(head);
}
}
struct line * del_line(struct line *p)
{
struct line *p_start,*p_last;
int i,number;
cout <<"\tWhich line number?";
cin >>number;
if (number==1)
p_start=p->next;
else
{
for (p_start=p,i=1;i<number;++i,p=p->next)
p_last=p;
p_last->next=p->next;
}
free(p);
return (p_start);
}
int get_choice(struct line *p)
{
int i,choice;
cout <<"\n...This is the start of your file so far...\n";
for (i=1;p!=NULL;++i,p=p->next)
cout <<i<<" "<<p->str<<endl;
cout <<"...This is the end";
cout <<"(Enter 1 to Insert,2 to Delete,and 3 to Quit)\n\t";
cin >>choice;
return choice;
}
struct line *add_line (struct line *p)
{
int number,i;
struct line *p_last, *p_new, *p_start;
char str_new[40];
p_start=p;
p_new=(struct line *)malloc(sizeof(struct line));
cout <<"First enter the line number and then its contens:\n";
cin >>number;
cin.getline(str_new,40);
for (i=1;i<number;++i,p=p->next)
p_last=p;
p_new->next=p;
strcpy(p_new->str,str_new);
if (i==1)
return (p_new);
else
{
p_last->next=p_new;
return (p_start);
}
}
[[it] 本帖最后由 sunkaidong 于 2008-3-18 13:43 编辑 [/it]]
----------------解决方案--------------------------------------------------------
head 的地址是malloc分配的啊
运行时输入:1 Unto the hills I will left up my eyes (Line 1) 程序就自动转入 del_line 了,然后就显示说程序错误,需要关闭; 程序原来是要在get_choice中输入 choice=2才会转入 del_line 的
输入其他的就没问题,想不通,是这句话有问题还是程序或编译器的问题
[[it] 本帖最后由 wolfs 于 2008-3-18 15:11 编辑 [/it]]
----------------解决方案--------------------------------------------------------
我帮你改好了..指针是好东西..呵呵
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct line
{
char str[40];
struct line *next;
};
int get_choice (struct line *p);
void add_line(struct line *p);
void del_line(struct line *p);
int get_choice ();
static int len=0;
struct line *head;
int main ()
{
int x;
head=(struct line *)malloc(sizeof(struct line));
head->next=NULL;
x=get_choice();
for (;x!=3;)
{
if (x==1)
add_line(head);
else
del_line(head);
x=get_choice(head);
}
return 1;
}
void del_line(struct line *p)
{
struct line *p_start,*p_last;
int i,number;
cout <<"\tWhich line number?";
cin >>number;
if (number==1)
p_start=p->next;
else
{
for (p_start=p,i=1;i<number;++i,p=p->next)
p_last=p;
p_last->next=p->next;
}
free(p);
len--;
head=p_start;
}
int get_choice()
{
int choice;
cout <<"\n...This is the start of your file so far...\n";
cout <<"(Enter 1 to Insert,2 to Delete,and 3 to Quit)\n\t";
cin >>choice;
return choice;
}
int get_choice(struct line *p)
{
int i,choice;
cout <<"\n...This is the start of your file so far...\n";
for (i=1;p->next!=NULL;p=p->next,i++)
cout <<i<<" "<<p->str<<endl;
cout <<i<<" "<<p->str<<endl;
cout <<"...This is the end"<<endl;
cout <<"(Enter 1 to Insert,2 to Delete,and 3 to Quit)\n\t";
cin >>choice;
return choice;
}
void add_line (struct line *p)
{
struct line *p_new, *p_start;
char str_new[40];
len++;
p_start=p;
p_new=(struct line *)malloc(sizeof(struct line));
cout <<"First enter the line number and then its contens:\n";
cout<<len<<":";
fflush(stdin);
cin.getline(str_new,40);
fflush(stdin);
for (;p->next!=NULL;p=p->next);
if(len==1)
{
p->next=NULL;
strcpy(p->str,str_new);
}
else
{
strcpy(p_new->str,str_new);
p->next=p_new;
p_new->next=NULL;
}
//return p_start;
}
----------------解决方案--------------------------------------------------------
支持一下
----------------解决方案--------------------------------------------------------
你两兄弟,不管如何,先谢了,等下回赖在测,上课去了,呵呵呵
----------------解决方案--------------------------------------------------------
你要小心空指针问题..而且指针是地址..其实即使你不用做参数..函数结束后空间还是会保留的..所以没必要用做参数..容易错..很难改正的
----------------解决方案--------------------------------------------------------