设有一个单位的人员工资有如下信息:name、department、 base pay、allowance、total。现从键盘输入一组人员工资数据并将它们存储到名为paydata的文件中;再从paydata取出工资数据并给每个人的base pay增加100元,增加后将工资数据显示于屏幕(每行1人)。请编写能够完成上述工作的程序。用C语言的链表实现
/******************花了我一个多小时,对链表还不是太熟悉,有错误的话请各位指正!***************************/
#include "stdlib.h"
#include "stdio.h"
#define NUM 3
#define LEN sizeof(struct paycount)
struct paycount
{char name[20];
char department[20];
float basepay;
float allowance;
float total;
struct paycount *next;
};
int n;
struct paycount *creat()
{ struct paycount *head;
struct paycount *p1,*p2;
int i;
char ss1[10],ss2[10];;
n=-1;
head=0;
for(i=0;i
if (n==-1){
p2=p1=(struct paycount *)malloc(LEN);
}
else{
p2=p1;
p1=(struct paycount *)malloc(LEN);
}
printf("name:/n");
scanf("%s",p1->name);
printf("department:/n");
scanf("%s",p1->department);
printf("basepay:/n");
scanf("%s",ss1);
printf("allowance:/n");
scanf("%s",ss2);
p1->basepay=atof(ss1);
p1->allowance=atof(ss2);
p1->total=p1->basepay+p1->allowance;
n=n+1;
if (n==0) head=p1;
else p2->next=p1;
}
p1->next=NULL;
return(head);
}
void prin(head)
struct paycount *head;
{struct paycount *p;
p=head;
while (p!=NULL)
{printf("%s %s %6.1f %6.1f %6.1f/n",p->name,p->department,p->basepay+100,p->allowance,p->total+100);
p=p->next;
}
}
int main() {struct paycount *head; FILE *fp; head=creat(); prin(head); fp=fopen("c://paydata.txt","wb"); while(head!=NULL){ fwrite(head,2,LEN,fp); head=head->next; } fclose(fp); system("pause"); }