首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

c语言链表求教,该怎么解决

2012-06-12 
c语言链表求教#includestdio.h#includestdlib.h#includestring.htypedef struct bookinformation{ch

c语言链表求教
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct bookinformation{
char name[100];
char writer[100];
char press[100];
int publicationdata;
float price;
int lentdata;
int number;
int returndata;
char sort[100];
char copy[100];
struct bookinformation * NEXT;
}Book;

typedef struct student{
int number;//学号
char name[20];
char bookname[100];
int lentdata;
int num;
int returndata;
struct student * NEXT;
}Std;
int n1,n2;
Book * addbook()
{
Book *head, *p1,*p2;
char name[100], writer[100],press[100];
int publicationdata,lentdata,returndata,number,size=sizeof(Book);
float price;
char sort[100],copy[100];
FILE *fp;
head=(Book*)malloc(size);
if((fp=fopen("tushu.txt","r"))==NULL)
{
printf("File open error!\n");
exit(0);
}
n1=0;
p2=p1=(Book*)malloc(size);
if((fscanf(fp,"%d %s %s %s %s %s %d %f %d %d",&number,name,writer,press,sort,copy,&publicationdata,&price,&lentdata,&returndata))!=EOF)
{
p1->number=number;
strcpy(p1->name,name);
strcpy(p1->writer,writer);
strcpy(p1->press,press);
strcpy(p1->sort,sort);
strcpy(p1->copy,copy);
p1->publicationdata=publicationdata;
p1->price=price;
p1->lentdata=lentdata;
p1->returndata=returndata;

}
head->NEXT=NULL;
while(!feof(fp))
{
n1++;
if(n1==1)
head->NEXT=p1;
else
p2->NEXT=p1;
p2=p1;
p1=(Book*)malloc(size);
if((fscanf(fp,"%d %s %s %s %s %s %d %f %d %d",&number,name,writer,press,sort,copy,&publicationdata,&price,&lentdata,&returndata))!=EOF)
{
p1->number=number;
strcpy(p1->name,name);
strcpy(p1->writer,writer);
strcpy(p1->press,press);
strcpy(p1->sort,sort);
strcpy(p1->copy,copy);
p1->publicationdata=publicationdata;
p1->price=price;
p1->lentdata=lentdata;
p1->returndata=returndata;

}
}
p2->NEXT=NULL;
if(fclose(fp))
{
printf("Can not close the file!\n");
exit(0);
}
return head;
}
void Write(Book *head)
{
Book *p=head->NEXT;
FILE *fp;
if((fp=fopen("tushu2.txt","w"))==NULL)
{
printf("File open error!\n");
exit(0);
}
while(p!=NULL)
{
fprintf(fp,"%d %s %s %s %s %s %d %lf",p->number,p->name,p->writer,p->press,p->sort,p->copy,p->publicationdata,p->price);
fprintf(fp," %d %d\n",p->lentdata,p->returndata);
p=p->NEXT;
};
if(fclose(fp)){
printf("Can not close the file!\n");
exit(0);
}
}

Std * addstd()
{
int lentdata,returndata,num,size=sizeof(Std);
int number;
char name[20];
Std *head1,*q1,*q2;
FILE *fp;
head1=(Std*)malloc(size);
if((fp=fopen("xuesheng1.txt","r"))==NULL)
{
printf("File open error!\n");
exit(0);
}
n2=0;
q2=q1=(Std*)malloc(size);
if((fscanf(fp,"%d %s",&number,name))!=EOF)
{
q1->number=number;
strcpy(q1->name,name);
}
head1->NEXT=NULL;
while(!feof(fp))
{
n2++;
if(n2==1)
head1->NEXT=q1;
else
q2->NEXT=q1;
q2=q1;
q1=(Std*)malloc(size);
if((fscanf(fp,"%d %s",&number,name))!=EOF)
{
q1->number=number;
strcpy(q1->name,name);
}
};
if(fclose(fp)){
printf("Can not close the file!\n");


exit(0);
}
return head1;

}

void Write1(Std *head1)
{
Std *p=head1->NEXT;
FILE *fp;

  if((fp=fopen("xuesheng333.txt","w"))==NULL)
{
printf("File open error!\n");
exit(0);
}
while(p!=NULL)
{
fprintf(fp,"%d %s\n",p->number,p->name);
p=p->NEXT;
};
if(fclose(fp))
{
printf("Can not close the file!\n");
exit(0);
}
}  
int main()
{
Book *p;
Std *q;
q=addstd();
p=addbook();
  Write(p);
Write1(q);//这里有错,请大家帮忙看看,Write1函数有什么问题,这两个函数为觉得一模一样啊,write就没问题
return 0;
}

[解决办法]
先单步调试
[解决办法]
自己先单步调试。看看是什么地方有问题
[解决办法]
VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习汇编以及C和汇编的对应关系。
从汇编的角度理解和学习C语言的指针,原本看似复杂的东西就会变得非常简单!
指针即地址。“地址又是啥?”“只能从汇编语言和计算机组成原理的角度去解释了。”

提醒:
“学习用汇编语言写程序”

“VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习C和汇编的对应关系。”
不是一回事!

不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。
并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。
任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!

热点排行