| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 545 人关注过本帖
标题:哪位大哥heip me 啊!!
只看楼主 加入收藏
tuqige
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-12-14
收藏
 问题点数:0 回复次数:0 
哪位大哥heip me 啊!!

以下是本人编写的程序,在文件读取这快总是编不成功,哪位C语言高手帮帮我啊,小弟在此不甚感激!!
#include "stdlib.h"
#include "stdio.h"
struct salary
{ char name[20];
long num ;
int age ;
char sex ;
float jbgz,sj,kk,sjgz;

struct salary*next ;
} *head=NULL, *this, *new;

void new_record(void)
{ char numstr[20];
new=(struct salary*) malloc( sizeof(struct salary) );
if(head==NULL) head=new ;
else
{ this=head ;
while(this->next!=NULL) this=this->next ;
this->next=new ;
}
this=new ;
printf("\nenter name:");
gets(this->name);
printf("enter number:");
gets(numstr); this->num=atoi(numstr);
printf("enter age:");
gets(numstr); this->age=atoi(numstr);
printf("enter sex:");
gets(numstr); this->sex=numstr[0];
printf("enter jbgz:");
gets(numstr); this->jbgz=atof(numstr);
printf("enter sj:");
gets(numstr); this->sj=atof(numstr);
printf("enter kk:");
gets(numstr); this->kk=atof(numstr);
this->sjgz=(this->jbgz)-(this->sj)-(this->kk);
this->next=NULL ;
}
void search_record(void)
{ char n[20];
int i=0;
printf("enter the name: ");
gets(n);
this=head;
while(strcmp(this->name,n)&&this!=NULL)
this=this->next;
printf("%8s%16s%8s%8s%8s%16s\n","record","name","num","age","sex","sjgz");
printf("%8d",++i);
printf("%16s",this->name);
printf("%8ld",this->num);
printf("%8d",this->age);
printf("%8c",this->sex);
printf("%16.2f\n",this->sjgz);
}
void average(void)
{float sum,ave;
int i;
sum=0;
if(head==NULL)
{ printf("empty list\n"); }
else
{
this=head;
for(i=1;this->next!=NULL;i++)
{ sum+=this->sjgz;
this=this->next;
}
ave=sum/i;
printf("the workers average sjgz:%f\n",ave);
}
}
void listall(void)
{ int i=0 ;
if(head==NULL) { printf("enmpty list.\n"); return ; }
this=head ;
printf("%8s%16s%8s%8s%8s%16s\n","record","name","num","age","sex","sjgz");
do
{ printf("%8d",++i);
printf("%16s",this->name);
printf("%8ld",this->num);
printf("%8d",this->age);
printf("%8c",this->sex);
printf("%16.2f\n",this->sjgz);
this=this->next ;
} while(this!=NULL);
}
void p_record(void)
{int i=0;
float t;
if(head=NULL)
{ printf("empty list\n");
return;}
else
{
for(;head->next!=NULL;head++)
{
this=head+1;
for(;this!=NULL;this++)
{
if(head->sjgz<this->sjgz)
{ t=head->sjgz;
head->sjgz=this->sjgz;
this->sjgz=t;}
this=this->next;}
}
}
this=head;
printf("%8s%16s%8s%8s%8s%16s\n","record","name","num","age","sex","sjgz");
do
{ printf("%8d",++i);
printf("%16s",this->name);
printf("%8ld",this->num);
printf("%8d",this->age);
printf("%8c",this->sex);
printf("%16.2f\n",this->sjgz);
this=this->next ;
} while(this!=NULL);
}
void keep_file(void)
{ FILE *fp;
char filename[20];
printf("enter file name\n");
gets(filename);
if( (fp=fopen(filename,"wb"))==NULL)
{ printf(" empty list\n" );
return;}
this=head;
while(this!=NULL)
{ fwrite(this,sizeof(struct salary),1,fp);
this=this->next;}
fclose(fp);
printf("save success\n");
}
struct salary *open_file()
{ FILE *fp;
char filename[20];
struct salary *p,*q,*h=NULL;
printf(" enter file name\n");
gets(filename);
fp=fopen(filename,"rb");
p=(struct salary *)malloc(sizeof(struct salary));
p=h;
while(!feof(fp))
{
fread(this,sizeof(struct salary),1,fp);
p->next=(struct salary *)malloc(sizeof(struct salary));
q=p;p=p->next; }
q->next=NULL;
fclose(fp);
printf("open success\n");
return h;
}
main()
{
char ch ; int flag=1 ;
while(flag)
{ printf("--------------------------\n");
printf("| O) open file |\n");
printf("| K) keep to file\n");
printf("| E) enter new record |\n");
printf("| S) search a record |\n");
printf("| L) list all record |\n");
printf("| p) get all record ||\n");
printf("| A) view the average salary |\n");
printf("| Q) QUIT |\n");
printf("-------------------------\n");
printf("select command ?"); ch=getch(); printf("%c\n\n",ch);
switch(ch)
{ case 'e' :
case 'E' : new_record(); break ;
case 's' :
case 'S' : search_record();break;
case 'a' :
case 'A' : average();break;
case 'l' :
case 'L' : listall(); break;
case 'P' :
case 'p' : p_record();break;
case 'o' :
case 'O' : head=open_file();break;
case 'k' :
case 'K' : keep_file();break;
case 'q' :
case 'Q' : flag=0 ; break ;
default : break ;
}
}
}



搜索更多相关主题的帖子: heip 
2006-12-14 16:20
快速回复:哪位大哥heip me 啊!!
数据加载中...
 
   



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

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