C语言 DLUT 作业十
综合性作业
1、链表
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Person
{
long num;
char name[20];
float salary;
struct Person *next;
};
struct Person *create()
{
struct Person *head=NULL,*p1,*p2;
p1=p2=(struct Person*)malloc(sizeof(struct Person));
printf("Input num name salary:\n");
scanf("%lld%s%f",&p1->num,p1->name,&p1->salary);
while(p1->num!=0)
{
if(head==NULL)
head=p1;
else
{
p2->next=p1;
}
p2=p1;
p1=(struct Person*)malloc(sizeof(struct Person));
printf("Input num name salary:\n");
scanf("%lld%s%f",&p1->num,p1->name,&p1->salary);
}
p2->next=NULL;
return head;
}
void list(struct Person *head)
{
while(head)
{
printf("%d\t%s\t%f\t\n",head->num,head->name,head->salary);
head=head->next;
}
}
struct Person *search(struct Person *head,long inum)
{
while(head)
{
if(head->num==inum)
break;
head=head->next;
}
return head;
}
struct Person *add(struct Person *head)
{
printf("\n输入新增员工信息:\n");
struct Person *p,*q;
p=q=(struct Person *)malloc(sizeof(struct Person));
scanf("%lld%s%f",&p->num,p->name,&p->salary);
for(q=head;q->next!=NULL;)
{
q=q->next;
}
q->next=p;
p->next=NULL;
return head;
}
long delet(struct Person *head)
{
struct Person *p1=head,*p2;
char name1[20];
printf("\n请输入待删除的员工姓名:\n");
scanf("%s",name1);
while(strcmp(p1->name,name1)!=0)
{
p2=p1;
p1=p1->next;
}
if(p1->num==0)
{
return 0;
}
else
{
p2->next=p1->next;
return p1->num;
}
}
int main()
{
struct Person ptemp;
struct Person *head,*presult,*p1,*num,*q;
long inum;
char *cname;
float fsalary;
head=create();
list(head);
printf("Input search num:\n");
scanf("%d",&inum);
presult=search(head,inum);
if(presult)
printf("num=%d,name=%s,salary=%f",presult->num,presult->name,presult->salary);
else
printf("没有此人");
q=add(head);
list(q);
num=delet(head);
printf("删除的员工序号为:%ld\n",num);
printf("删除后的全部员工信息:\n");
list(head);
}
2、日期结构体
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
struct Date
{
int year;
int month;
int day;
};
int runnian(struct Date d)
{
if(d.year%400==0||(d.year%4==0&&d.year%100!=0))
return 1;
else
return 0;
}
int youxiao(struct Date d,int n)
{
int flag=1;
if(d.month==1||d.month==3||d.month==5||d.month==7||d.month==8||d.month==10||d.month==12)
{
if(d.day<=0||d.day>31)
{
flag=0;
}
}
else if(d.month==4||d.month==6||d.month==9||d.month==11)
{
if(d.day<=0||d.day>30)
{
flag=0;
}
}
else if(d.month==2)
{
if(n==1)
{
if(d.day<=0||d.day>29)
{
flag=0;
}
}
if(n==0)
{
if(d.day<=0||d.day>28)
{
flag=0;
}
}
}
else
{
flag=0;
}
if(flag!=0)
flag=1;
return flag;
}
int juli(struct Date d,int n)
{
int p,j;
if(n==0)
{
switch(d.month)
{
case 1:p=365-d.day;break;
case 2:p=334-d.day;break;
case 3:p=306-d.day;break;
case 4:p=276-d.day;break;
case 5:p=245-d.day;break;
case 6:p=215-d.day;break;
case 7:p=184-d.day;break;
case 8:p=153-d.day;break;
case 9:p=123-d.day;break;
case 10:p=92-d.day;break;
case 11:p=62-d.day;break;
case 12:p=31-d.day;break;
}
}
if(n==1)
{
switch(d.month)
{
case 1:p=366-d.day;break;
case 2:p=335-d.day;break;
case 3:p=306-d.day;break;
case 4:p=276-d.day;break;
case 5:p=245-d.day;break;
case 6:p=215-d.day;break;
case 7:p=184-d.day;break;
case 8:p=153-d.day;break;
case 9:p=123-d.day;break;
case 10:p=92-d.day;break;
case 11:p=62-d.day;break;
case 12:p=31-d.day;break;
}
}
for(j=d.year+1;j<=2020;j++)
{
if(j%400==0||(j%4==0&&j%100!=0))
p=p+366;
else
p=p+365;
}
return p;
}
int main()
{
struct Date d;
int i,n,p,x;
printf("输入1可自行输入日期,输入2可获取系统日期\n");
scanf("%d",&x);
if(x==1)
{
printf("输入日期(年 月 日):\n");
scanf("%d%d%d",&d.year,&d.month,&d.day);
n=runnian(d);
i=youxiao(d,n);
do
{
if(i==0)
{
printf("输入的日期有误,请重新输入\n");
scanf("%d%d%d",&d.year,&d.month,&d.day);
i=youxiao(d,n);
}
else
printf("日期有效\n");
}while(i!=1);
p=juli(d,n);
printf("距离2021.1.1还有%d天",p+1);
}
if(x==2)
{
time_t t;
struct tm * lt; time (&t);
lt = localtime (&t);
d.year=lt->tm_year+1900;
d.month=lt->tm_mon+1;
d.day=lt->tm_mday;
n=runnian(d);
p=juli(d,n);
printf("距离2021.1.1还有%d天",p+1);
}
return 0;
}
3、员工结构体
#include<stdio.h>
struct Date{int year,month,day;};
struct person{
int num;
char name[20];
struct Date birthday;
double salary;
};
void input_person(struct person *c,int n);
double output_average(struct person *c,int n);
int max(struct person *c,int n);
void output_person(struct person *c,int n);
int main()
{
int n,maxi;
printf("Input n:\n");
scanf("%d",&n);
struct person c[n+1];
input_person(c,n);
printf("平均工资为:\n%lf\n",output_average(c,n));
maxi=max(c,n);
printf("工资最高员工信息:\nnum=%d\tname=%s\tdate=%d/%d/%d\tsalary=%lf\n",c[maxi].num,c[maxi].name,c[maxi].birthday.year,c[maxi].birthday.month,c[maxi].birthday.day,c[maxi].salary);
printf("员工工资为:\n");
output_person(c,n);
}
void input_person(struct person *c,int n)
{
int i=0;
for(i=0;i<n;i++)
{
scanf("%d%s%d%d%d%lf",&c[i].num,c[i].name,&c[i].birthday.year,&c[i].birthday.month,&c[i].birthday.day,&c[i].salary);
}
}
double output_average(struct person *c,int n)
{
int i=0;
double sum=0,average=0;
for(i=0;i<n;i++)
{
sum+=c[i].salary;
}
average=sum/n;
return average;
}
int max(struct person *c,int n)
{
int i=0,max=0;
double zuida=c[0].salary;
for(i=0;i<n;i++)
{
if(zuida<c[i].salary)
max=i;
}
return max;
}
void output_person(struct person *c,int n)
{
int i,j;
struct person temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if((c+j)->salary<(c+j+1)->salary)
{
temp=*(c+j);
*(c+j)=*(c+j+1);
*(c+j+1)=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("num=%d\tname=%s\t%d/%d/%d\tsalary=%lf\n",c[i].num,c[i].name,c[i].birthday.year,c[i].birthday.month,c[i].birthday.day,c[i].salary);
}
}
C语言完结散花,感谢各位支持!!!