标题:求助:指针与结构,
只看楼主
wolfs
Rank: 1
来 自:成都
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-3-15
 问题点数:0 回复次数:9 
求助:指针与结构,
我有个程序,当输入  "1 Unto the hills I will left up my eyes (Line 1)"就会说出现问题需要关闭。我们对此引起的不便表示抱歉。是什么原因啊,我用的是 Borland C++ Builder Compiler V5.5编译器。源代码试这样的:
#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=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);
 }
}
搜索更多相关主题的帖子: line struct 指针 源代码 
2008-03-18 12:54
与谁争疯
Rank: 1
来 自:疯子国酋长
等 级:新手上路
帖 子:66
专家分:0
注 册:2008-3-18
得分:0 
用什么BCB啊,太垃圾啦
2008-03-18 13:09
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
你的指针是空没有指向...所以肯定会错..你先给指针地址吧
收到的鲜花
  • wolfs2008-03-18 13:36 送鲜花  1朵  

学习需要安静。。海盗要重新来过。。
2008-03-18 13:11
wolfs
Rank: 1
来 自:成都
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-3-15
得分:0 
做的  指针  链表,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]]

哎...我这扶不上墙的泥啊,只有自己爬上去了!
2008-03-18 13:14
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
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]]

学习需要安静。。海盗要重新来过。。
2008-03-18 13:40
wolfs
Rank: 1
来 自:成都
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-3-15
得分:0 
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]]

哎...我这扶不上墙的泥啊,只有自己爬上去了!
2008-03-18 14:55
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
我帮你改好了..指针是好东西..呵呵
#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;

}

学习需要安静。。海盗要重新来过。。
2008-03-18 14:59
xianshizhe111
Rank: 1
等 级:新手上路
帖 子:1451
专家分:0
注 册:2007-12-8
得分:0 
支持一下
2008-03-18 15:02
wolfs
Rank: 1
来 自:成都
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-3-15
得分:0 
你两兄弟,不管如何,先谢了,等下回赖在测,上课去了,呵呵呵

哎...我这扶不上墙的泥啊,只有自己爬上去了!
2008-03-18 15:18
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
你要小心空指针问题..而且指针是地址..其实即使你不用做参数..函数结束后空间还是会保留的..所以没必要用做参数..容易错..很难改正的

学习需要安静。。海盗要重新来过。。
2008-03-18 15:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-203985-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.111755 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved