C Primer Plus(第五版)第14章 结构和其他数据形式

本文深入探讨了编程领域的基础知识、高级技术、开发工具、框架选择等核心内容,旨在帮助程序员从入门到精通。涵盖前端、后端、移动应用、游戏开发、大数据处理等多个方向,提供实战案例、最佳实践和最新趋势,为读者搭建起一个全面的编程知识体系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

14.1
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct month
{
char name[10];
char abbrev[4];
int days;
int monumb;
};
const struct month months[12]=
{
{"January", "Jan", 31, 1},
{"February", "Feb", 28, 2},
{"March", "Mar", 31, 3},
{"April", "Apr", 30, 4},
{"May", "May", 31, 5},
{"June", "Jun", 30, 6},
{"July", "Jul", 31, 7},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12}
};
#define size 50
int main(void)
{
int i,j,total;
_Bool flag;
char monthname[size];
puts("input a month name ,enter key for quit at the begin of a line.");
while(fgets(monthname,size,stdin)!=NULL&&monthname[0]!='\n')
{
flag=0;
total=0;
for(i=0;i<size;i++)
if(monthname[i]=='\n')
{
monthname[i]=0;
break;
}
for(i=0;i<12;i++)
if(strcmp(monthname,months[i].name)==0)
{
flag=1;
break;
}
if(flag==1)
{
for(j=0;j<i+1;j++)
total+=months[j].days;
printf("from %s to %s totally %d days\n",months[0].name,months[i].name,total);
}
else
puts("wrong input");
puts("input a month name ,enter key for quit at the begin of a line.");
}
return 0;
}

14.2
#include<stdio.h>
#include<string.h>
#include<ctype.h>
struct month
{
char name[10];
char abbrev[4];
int days;
int monumb;
};
const struct month months[12]=
{
{"January", "Jan", 31, 1},
{"February", "Feb", 28, 2},
{"March", "Mar", 31, 3},
{"April", "Apr", 30, 4},
{"May", "May", 31, 5},
{"June", "Jun", 30, 6},
{"July", "Jul", 31, 7},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12}
};
#define size 50
int main(void)
{
int i,j,total,date,year;
_Bool flag,wrong;
char monthname[size]={0};
puts("input the month,enter key for quit at the begin of a line.");
while(fgets(monthname,size,stdin)!=NULL&&monthname[0]!='\n')
{
flag=0;
total=0;
wrong=0;
for(i=0;i<size;i++)
if(monthname[i]=='\n')
{
monthname[i]=0;
break;
}
if(strlen(monthname)>=3)
{
for(i=0;i<12;i++)
if(strstr(months[i].name,monthname)!=NULL)
{
flag=1;
break;
}
if(flag==1)
for(j=0;j<i;j++)
total+=months[j].days;
else
{
wrong=1;
puts("wrong input");
}
}
else
{
if(monthname[1]==0)
if(monthname[0]>57||monthname[0]<48)
{
wrong=1;
puts("wrong input");
}
else
for(i=0;i<monthname[0]-49;i++)
total+=months[i].days;
else
if(monthname[1]>50||monthname[1]<48||monthname[0]!=49)
{
wrong=1;
puts("wrong input");
}
else
for(i=0;i<10+monthname[1]-49;i++)
total+=months[i].days;
}
if(wrong==0)
{
puts("input the date.");
while(scanf("%d",&date)!=1)
puts("wrong input,do it again.");
while(getchar()!='\n')
;
puts("input the year.");
while(scanf("%d",&year)!=1)
puts("wrong input,do it again.");
while(getchar()!='\n')
;
printf("frome the beginning of the year %d to %d-%s-%d is totally %d.\n",year,year,monthname,date,total+date);
}
puts("input the month,enter key for quit at the begin of a line.");
}
return 0;
}

14.3
#include<stdio.h>
#include<string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100
struct book {
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
int main(void)
{
struct book library[MAXBKS],temp;
int count=0;
int index,i,j;
printf("Please enter the book title.\n");
printf("Press [enter] at the start of a line to stop.\n");
while(count<MAXBKS&&gets(library[count].title)!=NULL&& library[count].title[0]!='\0')
{
printf("Now enter the author.\n");
gets(library[count].author);
printf("Now enter the value.\n");
scanf("%f", &library[count++].value);
while(getchar() != '\n')
;
if(count<MAXBKS)
printf("Enter the next title.\n");
}
if(count>0)
{
printf("Here is the list of your books:\n");
for(index=0;index<count;index++)
printf("%s by %s: $%.2f\n", library[index].title,library[index].author, library[index].value);
for(i=0;i<count-1;i++)
for(j=i+1;j<count;j++)
if(strcmp(library[i].title,library[j].title)>0)
{
temp=library[i];
library[i]=library[j];
library[j]=temp;
}
printf("Here is the list of your books(according the title):\n");
for(index=0;index<count;index++)
printf("%s by %s: $%.2f\n", library[index].title,library[index].author, library[index].value);
for(i=0;i<count-1;i++)
for(j=i+1; j<count;j++)
if(library[i].value>library[j].value)
{
temp=library[i];
library[i]=library[j];
library[j]=temp;
}
printf("Here is the list of your books(according the value):\n");
for(index=0;index<count;index++)
printf("%s by %s: $%.2f\n", library[index].title,library[index].author, library[index].value);
}
else
printf("No books? Too bad.\n");
return 0;
}

14.4
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define size 50
struct fullname
{
char lastname[size];
char middlename[size];
char firstname[size];
};
struct person
{
int num;
struct fullname name;
};
void show(struct person *,int );
int main(void)
{
struct person p[5]=
{
{302039823,{"Dribble","Mass","Flossie"}},
{123456425,{"sadfsafd","lsdkj","slkfdj"}},
{456732158,{"aslkfjf","sadfasdf","sadf"}},
{753184895,{"fasf","","thrht"}},
{951715586,{"safsdf","sadfasf","fsaf"}}
};
show(p,5);
return 0;
}
void show(struct person *p,int n)
{
int i;
for(i=0;i<n;i++)
{

printf("%s,%s",p[i].name.lastname,p[i].name.firstname);
if(strlen(p[i].name.middlename))
printf(" %c.",toupper(p[i].name.middlename[0]));
printf(" - %d\n",p[i].num);
}
}

14.4.b
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define size 50
struct fullname
{
char lastname[size];
char middlename[size];
char firstname[size];
};
struct person
{
int num;
struct fullname name;
};
void show(struct person);
int main(void)
{
struct person p[5]=
{
{302039823,{"Dribble","Mass","Flossie"}},
{123456425,{"sadfsafd","lsdkj","slkfdj"}},
{456732158,{"aslkfjf","sadfasdf","sadf"}},
{753184895,{"fasf","","thrht"}},
{951715586,{"safsdf","sadfasf","fsaf"}}
};
int i;
for(i=0;i<5;i++)
show(p[i]);
return 0;
}
void show(struct person p)
{
printf("%s,%s",p.name.lastname,p.name.firstname);
if(strlen(p.name.middlename))
printf(" %c.",toupper(p.name.middlename[0]));
printf(" - %d\n",p.num);
}

14.5
#include<stdio.h>
#define size 50
struct name
{
char last[size];
char first[size];
};
struct student
{
struct name n;
float grade[3];
float average;
};
#define CSIZE 4
void information(struct student *,int);
void average(struct student *,int);
void show(struct student *,int);
int main(void)
{
struct student stu[CSIZE];
information(stu,CSIZE);
average(stu,CSIZE);
show(stu,CSIZE);
return 0;
}
void information(struct student *p,int n)
{
int i,j;
for(i=0;i<n;i++)
{
puts("input the student firstname");
while(scanf("%s",p[i].n.first)==0)
puts("wrong,do it again");
while(getchar()!='\n')
;
puts("input the student lastname");
while(scanf("%s",p[i].n.last)==0)
puts("wrong,do it again");
while(getchar()!='\n')
;
puts("input the student 3 grade");
j=0;
while(j<3)
{
while(scanf("%f",&p[i].grade[j])==0)
puts("wrong,do it again");
j++;
}
}
}
void average(struct student *p,int n)
{
int i;
for(i=0;i<n;i++)
p[i].average=(p[i].grade[0]+p[i].grade[1]+p[i].grade[2])/3;
}
void show(struct student *p,int n)
{
int i;
for(i=0;i<n;i++)
printf("%s %s scores %g %g %g the average is %g\n",
p[i].n.last,p[i].n.first,p[i].grade[0],p[i].grade[1],p[i].grade[2],p[i].average);
}

14.6
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define size 50
struct stuf
{
int num;
char last[size];
char first[size];
int attend;
int hit;
int walk;
int run;
};
int main(void)
{
FILE*f;
int i,t,tattend,thit,twalk,trun;
struct stuf p[19]={0};
char c[size],temp[size];
puts("input the filename");
scanf("%s",temp);
if((f=fopen(temp,"r"))==NULL)
exit(EXIT_FAILURE);
while(fgets(c,size,f)!=NULL)
{
sscanf(c,"%d",&t);
if(strlen(p[t].first)==0)
sscanf(c,"%d %s %s %d %d %d %d",
&p[t].num,p[t].last,p[t].first,&p[t].attend,&p[t].hit,&p[t].walk,&p[t].run);
else
{
sscanf(c,"%*d %*s %*s %d %d %d %d",&tattend,&thit,&twalk,&trun);
p[t].attend+=tattend;
p[t].hit+=thit;
p[t].walk+=twalk;
p[t].run+=trun;
}
}
for(i=0;i<19;i++)
printf("the %d %s %s attend %d times %d hit %d walk %d run %g average.\n"
,p[i].num,p[i].last,p[i].first,p[i].attend,p[i].hit,p[i].walk,p[i].run,(double)(p[i].hit)/p[i].attend);
if(fclose(f)!=0)
exit(EXIT_FAILURE);
return 0;
}

14.7
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10
struct book
{
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
int main(void)
{
_Bool flag=0;
struct book library[MAXBKS],kong={0};
int count=0;
int index,filecount,d=0;
char delete[MAXBKS]={0};
FILE *pbooks;
int size=sizeof(struct book);
if ((pbooks=fopen("book.dat","r+b"))==NULL)
{
fputs("Can't open book.dat file\n",stderr);
exit(1);
}
while (count<MAXBKS&&fread(&library[count],size,1,pbooks)==1)
{
if (count==0)
puts("Current contents of book.dat:");
printf("%s by %s: $%.2f\n",library[count].title,library[count].author, library[count].value);
puts("input d for delete,m for mod");
filecount=getchar();
while(getchar()!='\n')
;
if(filecount=='d')
{
library[count]=kong;
delete[d++]=count+1;
}
else if(filecount=='m')
{
puts("input the title");
gets(library[count].title);
puts("input the author");
gets(library[count].author);
puts("input the value");
scanf("%f",library[count].value);
}
count++;
}
if((filecount=strlen(delete))!=0)
{
for(d=0;d<filecount;d++)
{
puts("Please add new book titles.");
puts("Press [enter] at the start of a line to stop.");
gets(library[delete[d]-1].title);
if(library[delete[d]-1].title[0]=='\0')
{
flag=1;
break;
}
puts("Now enter the author.");
gets(library[delete[d]-1].author);
puts("Now enter the value.");
scanf("%f", &library[delete[d]-1].value);
while (getchar() != '\n')
;
if(d<filecount-1)
puts("Enter the next title.");
}
}
filecount=count;
if(flag!=1)
{
if (count==MAXBKS)
{
fputs("The book.dat file is full.",stderr);
exit(2);
}
puts("Please add new book titles.");
puts("Press [enter] at the start of a line to stop.");
while (count<MAXBKS&&gets(library[count].title)!=NULL&& library[count].title[0]!='\0')
{
puts("Now enter the author.");
gets(library[count].author);
puts("Now enter the value.");
scanf("%f",&library[count++].value);
while (getchar()!='\n')
;
if(count<MAXBKS)
puts("Enter the next title.");
}
}
if(count>0)
{
puts("Here is the list of your books:");
for (index=0;index<count;index++)
printf("%s by %s: $%.2f\n",library[index].title,library[index].author,library[index].value);
fwrite(&library[filecount],size,count-filecount,pbooks);
}
else
puts("No books? Too bad.\n");
puts("Bye.\n");
fclose(pbooks);
return 0;
}

14.8
#include<stdio.h>
#include<stdlib.h>
#define size 50
struct plane
{
int num;
_Bool flag;
char first[size];
char last[size];
};
void menu(void);
char choose(void);
void show_number(struct plane*,int);
void show_list(struct plane*,int);
void show_alpha(struct plane*,int);
void assign(struct plane*,int);
void delete(struct plane*,int);
void read_record(struct plane*,int);
void write_record(struct plane*,int);
int main(void)
{
int i;
char c;
struct plane p[12]={0};
for(i=0;i<12;i++)
p[i].num=i+1;
read_record(p,12);
menu();
c=choose();
while(c!='f')
{
switch(c)
{
case 'a':
show_number(p,12);
break;
case 'b':
show_list(p,12);
break;
case 'c':
show_alpha(p,12);
break;
case 'd':
assign(p,12);
break;
case 'e':
delete(p,12);
break;
default:
break;
}
menu();
c=choose();
}
write_record(p,12);
return 0;
}
void menu(void)
{
puts("To choose a function,enter its letter label:");
puts("a) Show number of empty seats");
puts("b) Show list of empty seats");
puts("c) Show alphabetical list of seats");
puts("d) Assign a customer to a seat assignment");
puts("e) Delete a seat assignment");
puts("f) Quit");
}
char choose(void)
{
char c;
puts("input your choose");
c=getchar();
while(getchar()!='\n')
;
while(c>102||c<97)
{
puts("wrong,do it again");
c=getchar();
while(getchar()!='\n')
;
}
return c;
}
void show_number(struct plane *p,int n)
{
int i,j;
for(i=0,j=0;i<n;i++)
if(p[i].flag==0)
j++;
printf("there are totally %d empy seat.\n",j);
}
void show_list(struct plane *p,int n)
{
int i;
for(i=0;i<n;i++)
if(p[i].flag==0)
printf("the %d seat is empty.\n",p[i].num);
}
void show_alpha(struct plane *p,int n)
{
int i;
for(i=0;i<n;i++)
if(p[i].flag==0)
printf("the %c seat is empty.\n",p[i].num+96);
}
void assign(struct plane *p,int n)
{
int i;
puts("input seat num to order");
while(scanf("%d",&i)&&p[i-1].flag==1||i<0||i>n)
puts("the seat cannot ordered,input another one");
while(getchar()!='\n')
;
puts("input your firstname");
gets(p[i-1].first);
puts("input your lastname");
gets(p[i-1].last);
p[i-1].flag=1;
puts("you have orderd it");
}
void delete(struct plane *p,int n)
{
int i;
struct plane kong={0};
puts("input the number you want to delete");
while(scanf("%d",&i)&&i<0||i>n)
puts("the seat cannot to delete,input another one");
while(getchar()!='\n')
;
kong.num=p[i-1].num;
p[i-1]=kong;
puts("delete succeed");
}
void read_record(struct plane *p,int n)
{
FILE*f;
if((f=fopen("record","a+"))==NULL)
{
puts("can not open the record file.");
exit(EXIT_FAILURE);
}
fread(p,sizeof(struct plane),n,f);
if((fclose(f))!=0)
{
puts("cannot close the record file");
exit(EXIT_FAILURE);
}
}
void write_record(struct plane *p,int n)
{
FILE*f;
if((f=fopen("record","w"))==NULL)
{
puts("can not open the record file.");
exit(EXIT_FAILURE);
}
fwrite(p,sizeof(struct plane),n,f);
if((fclose(f))!=0)
{
puts("cannot close the record file");
exit(EXIT_FAILURE);
}
}

14.9
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define size 50
struct plane
{
int pn;
int num;
_Bool flag;
char first[size];
char last[size];
};
void second(struct plane *,int);
void topmenu(void);
char *topchoose(void);
void secondmenu(void);
char secondchoose(void);
void show_number(struct plane*,int);
void show_list(struct plane*,int);
void show_alpha(struct plane*,int);
void assign(struct plane*,int);
void delete(struct plane*,int);
void read_record(void *,int);
void write_record(void *,int);
int main(void)
{
int i,j;
char *c;
struct plane p[4][12]={0};
for(i=0;i<4;i++)
{
switch(i)
{
case 0:
for(j=0;j<12;j++)
p[i][j].pn=102;
break;
case 1:
for(j=0;j<12;j++)
p[i][j].pn=311;
break;
case 2:
for(j=0;j<12;j++)
p[i][j].pn=444;
break;
case 3:
for(j=0;j<12;j++)
p[i][j].pn=519;
break;
}
for(j=0;j<12;j++)
p[i][j].num=j+1;
}
read_record(p,12*4);
topmenu();
c=topchoose();
while(c[0]!='q')
{
sscanf(c,"%d",&i);
switch(i)
{
case 102:
second(p[0],12);
break;
case 311:
second(p[1],12);
break;
case 444:
second(p[2],12);
break;
case 519:
second(p[3],12);
break;
default:
break;
}
topmenu();
c=topchoose();
}
write_record(p,12*4);
return 0;
}
void second(struct plane *p,int n)
{
char c;
secondmenu();
c=secondchoose();
while(c!='f')
{
switch(c)
{
case 'a':
show_number(p,12);
break;
case 'b':
show_list(p,12);
break;
case 'c':
show_alpha(p,12);
break;
case 'd':
assign(p,12);
break;
case 'e':
delete(p,12);
break;
default:
break;
}
secondmenu();
c=secondchoose();
}
}
void topmenu(void)
{
puts("To choose a airplane,enter its number label:");
puts("102) for 102 airplane");
puts("311) for 311 airplane");
puts("444) for 444 airplane");
puts("519) for 519 airplane");
puts("q) for quit");
}
char* topchoose(void)
{
static char tmp[50];
int i;
puts("input your choose");
scanf("%s",tmp);
while(getchar()!='\n')
;
while(tmp[0]!='q'&&sscanf(tmp,"%d",&i)&&i!=102&&i!=311&&i!=444&&i!=519)
{
puts("wrong,do it again");
scanf("%s",tmp);
while(getchar()!='\n')
;
}
return tmp;
}
void secondmenu(void)
{
puts("To choose a function,enter its letter label:");
puts("a) Show number of empty seats");
puts("b) Show list of empty seats");
puts("c) Show alphabetical list of seats");
puts("d) Assign a customer to a seat assignment");
puts("e) Delete a seat assignment");
puts("f) topmenu");
}
char secondchoose(void)
{
char c;
puts("input your choose");
c=getchar();
while(getchar()!='\n')
;
while(c>102||c<97)
{
puts("wrong,do it again");
c=getchar();
while(getchar()!='\n')
;
}
return c;
}
void show_number(struct plane *p,int n)
{
int i,j;
for(i=0,j=0;i<n;i++)
if(p[i].flag==0)
j++;
printf("there are totally %d empy seat in %d airplane.\n",j,p[0].pn);
}
void show_list(struct plane *p,int n)
{
int i;
for(i=0;i<n;i++)
if(p[i].flag==0)
printf("the num %d seat is empty in %d airplane\n",p[i].num,p[i].pn);
}
void show_alpha(struct plane *p,int n)
{
int i;
for(i=0;i<n;i++)
if(p[i].flag==0)
printf("the %c seat is empty in %d airplane\n",p[i].num+96,p[i].pn);
}
void assign(struct plane *p,int n)
{
int i;
puts("input seat num to order");
while(scanf("%d",&i)&&p[i-1].flag==1||i<0||i>n)
puts("the seat cannot ordered,input another one");
while(getchar()!='\n')
;
puts("input your firstname");
gets(p[i-1].first);
puts("input your lastname");
gets(p[i-1].last);
p[i-1].flag=1;
printf("you have orderd number %d seat in %d airplane.\n",i,p[i-1].pn);
}
void delete(struct plane *p,int n)
{
int i;
struct plane kong={0};
puts("input the number you want to delete");
while(scanf("%d",&i)&&i<0||i>n||p[i-1].flag==0)
puts("the seat cannot to delete,input another one");
while(getchar()!='\n')
;
kong.num=p[i-1].num;
kong.pn=p[i-1].pn;
p[i-1]=kong;
printf("delete number %d seat in %d airplane succeed.\n",i,p[i-1].pn);
}
void read_record(void *p,int n)
{
FILE*f;
if((f=fopen("record","a+"))==NULL)
{
puts("can not open the record file.");
exit(EXIT_FAILURE);
}
fread(p,sizeof(struct plane),n,f);
if((fclose(f))!=0)
{
puts("cannot close the record file");
exit(EXIT_FAILURE);
}
}
void write_record(void *p,int n)
{
FILE*f;
if((f=fopen("record","w"))==NULL)
{
puts("can not open the record file.");
exit(EXIT_FAILURE);
}
fwrite(p,sizeof(struct plane),n,f);
if((fclose(f))!=0)
{
puts("cannot close the record file");
exit(EXIT_FAILURE);
}
}

14.10
#include<stdio.h>
int main(void)
{
char c;
void (*p[3])(void);
puts("input a letter");
while((c=getchar())!=EOF)
{
switch(c)
{
case 'a':
(*p[0])();
break;
case 'b':
(*p[1])();
break;
case 'c':
(*p[2])();
break;
}
}
return 0;
}

14.11
#include<stdio.h>
#include<math.h>
void transform(double *,double *,int ,double (*)(double));
double fuck(double);
double shit(double);
int main(void)
{
int i;
double s[100],d[100];
double (*p)(double);
for(i=0;i<100;i++)
s[i]=i;
p=sin;
transform(s,d,100,p);
for(i=0;i<100;i++)
printf("%f %f\n",s[i],d[i]);
p=cos;
transform(s,d,100,p);
for(i=0;i<100;i++)
printf("%f %f\n",s[i],d[i]);
p=fuck;
transform(s,d,100,p);
for(i=0;i<100;i++)
printf("%f %f\n",s[i],d[i]);
p=shit;
transform(s,d,100,p);
for(i=0;i<100;i++)
printf("%f %f\n",s[i],d[i]);
return 0;
}
void transform(double *s,double *d,int n,double (*p)(double))
{
int i;
for(i=0;i<n;i++)
d[i]=p(s[i]);
}
double fuck(double x)
{
return x-1;
}
double shit(double x)
{
return x+1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值