| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 835 人关注过本帖
标题:求助:指针与结构,
取消只看楼主 加入收藏
wolfs
Rank: 1
来 自:成都
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-3-15
收藏
 问题点数:0 回复次数:3 
求助:指针与结构,
我有个程序,当输入  "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
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
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
wolfs
Rank: 1
来 自:成都
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-3-15
收藏
得分:0 
你两兄弟,不管如何,先谢了,等下回赖在测,上课去了,呵呵呵

哎...我这扶不上墙的泥啊,只有自己爬上去了!
2008-03-18 15:18
快速回复:求助:指针与结构,
数据加载中...
 
   



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

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