1.重写复习题3,但用月份名的拼写代替月份号(别忘了可以使用strcmp)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define LEN 20
#define SIZE 12
typedef struct moon{
char fullname[LEN];
char abbname[LEN];
int AmountOfDays;
int NumberOfMoon;
}MOON;
enum moons{January=1,February,April,March,May,June,July,August,September,October,November,December};
const char *FullName[SIZE]={"January","February","April","March","May","June","July","August","September","October","November","December"};
const char *AbbName[SIZE]={"JAN","FEB","APR","MAR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
const int amountofday[SIZE]={31,28,31,30,31,30,31,31,30,31,30,31}; //题目要求年份,假设为平年
int main(void)
{
int i;
MOON date[SIZE];
char input[LEN];
bool isfound=false;
//初始化
for(i=0;i<SIZE;i++)
{
strcpy(date[i].fullname,FullName[i]);
strcpy(date[i].abbname,AbbName[i]);
date[i].AmountOfDays=amountofday[i];
date[i].NumberOfMoon=i+1;
}
printf("Please enter a full name of moon,I can't you some information.\n");
printf("Enter Ctrl+Z or a empty line to quit.\n");
while((gets(input) != NULL) && (input[0] != '\0'))
{
for(i=0,isfound=false;i<SIZE;i++)
{
if(strcmp(FullName[i],input) == 0)
{
isfound=true;
break;
}
}
if(isfound)
{
printf("Well,its full name is \"%s\"\n",date[i].fullname);
printf("Its abbreviation is \"%s\"\n",date[i].abbname);
printf("There %d days in this moon\n",date[i].AmountOfDays);
printf("The number of this moons is %d\n",date[i].NumberOfMoon);
}
else
{
printf("Check your spell.try it again.\n");
}
printf("Please enter a full name of moon,I can't you some information.\n");
printf("Enter Ctrl+Z or a empty line to quit.\n");
}
return 0;
}
2.编写一个程序。请求用户键入日,月,年。月份可以是月份号、月份名、或者月份缩写。然后程序返回一年中到给定日子的总天数。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
typedef struct Data{
int year;
int month;
int day;
}date;
void endline(void);
int maxday(date someday);
bool isleap(date someday);
int calculat(date someday);
int main(void)
{
int ch;
date someday;
printf("Enter a data like year-month-day.\n");
printf("Now enter year first.\n");
while( (scanf("%d",&someday.year)) ==1 && someday.year != 0 )
{
endline();
printf("Now enter month.\n");
while( (scanf("%d",&someday.month) != 1) || (someday.month>12) || (someday.month<1))
{
printf("Error to enter month.please enter again!\n");
endline();
}
printf("Now enter day.\n");
while( (scanf("%d",&someday.day)) != 1 || (someday.day<1) || (someday.day>maxday(someday)) )
{
printf("Error to enter day.please enter again!\n");
endline();
}
printf("There is %d days from begining of year to the entering date.\n",calculat(someday));
printf("Enter the next date.\n");
printf("Now enter year first.\n");
}
}
void endline(void)
{
int ch;
while((ch=getchar()) != '\n')
continue;
}
bool isleap(date someday)
{
bool is_leap=false;
if(someday.year%400==0)
is_leap=true;
else if(someday.year%4==0)
is_leap=true;
return is_leap;
}
int maxday(date someday)
{
int max[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isleap(someday))
max[1]=29;
return max[someday.month-1];
}
int calculat(date someday)
{
int i;
int total=0;
int rt=0;
int max[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isleap(someday))
max[1]=29;
for(i=1;i<someday.month;i++)
{
total=total+max[i-1];
}
rt=someday.day+total;
}
3.修改程序14.2中的节目列表程序,使得它首先按照输入的顺序输出图书的描述,然后按照标题的字母升序输出图书的描述,最后按照value值得升序输出图书的描述。
#include <stdio.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100
void acending_title(struct book library[],int count);
void acending_value(struct book library[],int count);
struct book{
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
int main(void)
{
struct book library[MAXBKS];
int count=0;
int index;
int ch;
printf("Now 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') //gets遇到EOF或者读取错误返回NULL。 .title[0]='\0'表示空行按enter自动补空字符。
{
printf("Now enter the author.\n");
gets(library[count].author);
printf("Now enter the value.\n");
scanf("%f",&library[count++].value); //配合下面的语句,如果scanf输入123abc.那么scanf会拿到123.而abc和回车会被下面的语句拿到
while((ch=getchar()) != '\n')
continue;
if(count<MAXBKS)
printf("Enter the next tile.\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);
}
printf("acending order of title\n");
acending_title(library,count);
printf("acending order of value\n");
acending_value(library,count);
}
else
printf("No books? too bad.\n");
return 0;
}
void acending_title(struct book library[],int count)
{
struct book temp;
int i,j;
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;
}
}
}
for(i=0;i<count;i++)
{
printf("%s by %s: $%.2f\n",library[i].title,library[i].author,library[i].value);
}
}
void acending_value(struct book library[],int count)
{
struct book temp;
int i,j;
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;
}
}
}
for(i=0;i<count;i++)
{
printf("%s by %s: $%.2f\n",library[i].title,library[i].author,library[i].value);
}
}
4.编写一个程序。按照下列要求,创建一个含有两个成员的模板:
a.第一个成员是社会保障号;第二个成员是一个含有三个成员的结构。它的第一个成员是名字,第二个成员是名和姓中间的名字,最后一个成员是姓。创建并初始化一个含有5个此类结构的素组,程序以下列形式输出数据:
Dribble,Flossie M.- 302039823
名和姓中间的名字只输出它的第一个字母,后面加一个句号。如果姓名中间的名字为空,那么它的第一个字母和句点都不会输出。写一个函数实现输出,把结构数组传递给函数
#include <stdio.h>
#include <string.h>
#define LEN 20
#define SIZE 1
struct Name{
char firstname[LEN];
char midname[LEN];
char familyname[LEN];
};
struct Identity{
int num;
struct Name name;
};
void endline(void);
void input(struct Identity arr[],int size);
void output(struct Identity arr[],int size);
int main(void)
{
struct Identity arr[SIZE];
input(arr,SIZE);
printf("\n");
output(arr,SIZE);
}
void endline(void)
{
int ch;
while ((ch=getchar())!='\n')
continue;
}
void input(struct Identity arr[],int size)
{
int i,j;
for(i=0;i<size;i++)
{
puts("Enter you first name.");
gets(arr[i].name.firstname);
puts("Now enter your midname.empty line means non.");
gets(arr[i].name.midname);
puts("Now enter your family name.");
gets(arr[i].name.familyname);
puts("Now enter your social security number");
scanf("%d",&arr[i].num);
endline();
}
}
void output(struct Identity arr[],int size)
{
int i,j;
for(i=0;i<size;i++)
{
if(arr[i].name.midname[0] != '\0')
printf("%s, %s %c. - %d",arr[i].name.firstname,arr[i].name.familyname,arr[i].name.midname[0],arr[i].num);
else
printf("%s, %s - %d",arr[i].name.firstname,arr[i].name.familyname,arr[i].num);
printf("\n");
}
}
b.修改a部分,传递结构的值而不是结构地址。
#include <stdio.h>
#include <string.h>
#define LEN 20
#define SIZE 1
struct Name{
char firstname[LEN];
char midname[LEN];
char familyname[LEN];
};
struct Identity{
int num;
struct Name name;
};
void endline(void);
void input(struct Identity arr[],int size);
void output(int num,char *firstname,char *midname,char *familyname);
int main(void)
{
int i;
struct Identity arr[SIZE];
input(arr,SIZE);
printf("\n");
for(i=0;i<SIZE;i++)
{
output(arr[i].num,arr[i].name.firstname,arr[i].name.midname,arr[i].name.familyname);
}
}
void endline(void)
{
int ch;
while ((ch=getchar())!='\n')
continue;
}
void input(struct Identity arr[],int size)
{
int i,j;
for(i=0;i<size;i++)
{
puts("Enter you first name.");
gets(arr[i].name.firstname);
puts("Now enter your midname.empty line means non.");
gets(arr[i].name.midname);
puts("Now enter your family name.");
gets(arr[i].name.familyname);
puts("Now enter your social security number");
scanf("%d",&arr[i].num);
endline();
}
}
void output(int num,char *firstname,char *midname,char *familyname)
{
if(midname[0] != '\0')
printf("%s, %s %c. - %d",firstname,familyname,midname[0],num);
else
printf("%s, %s - %d",firstname,familyname,num);
printf("\n");
}
5.写一个程序,满足下列要求:
a.外部定义一个name结构模板,它含有2个成员:一个字符串用于存放名字,另一个字符串用于存放姓氏。
b.外部定一个student结构模板,它含有3个成员:一个name结构,一个存放3个浮点分数的grade数组,以及一个存放这3个分数的平均分的变量。
c.使main()函数声明一个具有CSIZE(CSIZE=4)个student结构的数组,并随意初始化这些结构的名字部分。使用函数来执行d,e,f以及g部分所描述的任务。
d.请求用户输入学生姓名和分数,以交互地获取每个学生的成绩。并将分数放入相应的结构的grade数组成员中。您可以自主的选择在main()或在函数中实现这个循环。
e.为每个结构计算平均分,并把这个值赋给各个合适的成员。
f.输出每个结构中的信息.
g.输出结构的每个数值成员的班级平均分。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define LEN 20
#define SIZE 3
#define CSIZE 4
struct name{
char familyname[LEN];
char firstname[LEN];
};
struct student{
struct name somebody;
float grade[SIZE];
float average;
};
struct test{
int num;
};
void getmark(struct student arr[],int csize,int size);
float getaverage(struct student arr[],int csize,int size);
void classaverage(struct student arr[],int csize,int size);
void endline(void);
int main(void)
{
//定义一个student结构体数组,并且初始化它的第一个成员。
struct student Class[CSIZE]={
{
{"wang","yaowen"}
},
{
{"ke","yongjia"}
},
{
{"zhu","minghou"}
},
{
{"jiang","zhongping"}
},
};
getmark(Class,CSIZE,SIZE);
getaverage(Class,CSIZE,SIZE);
classaverage(Class,CSIZE,SIZE);
}
void endline(void)
{
int ch;
while ((ch=getchar())!='\n')
continue;
}
void getmark(struct student arr[],int csize,int size)
{
char temp[LEN];
int count=0;
bool is_found=false;
int i,j;
puts("Please enter students name to enter his mark.");
while(count<csize)
{
gets(temp);
for(i=0;i<csize;i++)
{
if( strcmp(arr[i].somebody.firstname,temp) == 0)
{
is_found=true;
break;
}
}
if(is_found)
{
printf("Find the student.Now enter 3 number for mark\n");
for(j=0;j<size;j++)
{
scanf("%f",&(arr[i].grade[j]));
}
endline();
printf("Enter anther student's name.\n");
count++;
}
else
{
printf("Cant't find the student.\n");
printf("Please enter the right name.\n");
}
}
}
float getaverage(struct student arr[],int csize,int size)
{
int i,j;
float total;
for(i=0;i<csize;i++)
{
for(j=0,total=0;j<size;j++)
{
total+=arr[i].grade[j];
}
arr[i].average=total/size;
printf("%s average mark is %.2f\n",arr[i].somebody.firstname,arr[i].average);
}
printf("\n");
}
void classaverage(struct student arr[],int csize,int size)
{
float t_subject,a_subject;
int i,j;
for(i=0,a_subject=0;i<3;i++)
{
for(j=0,t_subject=0;j<csize;j++)
{
t_subject+=arr[j].grade[i];
}
a_subject=t_subject/csize;
printf("Average of subject %d is %.2f\n",i+1,a_subject);
}
}
6.一个文本文件中存放在和一个棒球队的信息。每一行的数据都是这样排列的:
4 Jessie Joybat 5 2 1 1
第一项是球员号码,为了方便,范围是0到18.第二项是球员的名,第三项是姓。姓和名都是单个的单词。下一项是光放统计的球员上场次数,紧跟着的击中数、走垒数和跑点数。文件可能包括超过一场比赛的数据,因此同一个球员可能有多于一行的数据,而且在不同的行之间可能有别的球员的数据。写一个程序,把这些数据存储到一个结构数组中。结构中必须含有姓、名、上场次数、击中数、走垒数、和跑点数、以及击球平均成功率。可以使用球员号码作为数组索引。程序应该读到文件末尾,并且应该保存每个球员的累计总和
这个棒球运动中统计方法是相关的。例如,一次走垒和触垒中的失误并不会记作上场次数,但是这可能产生一个跑点数。可是,该程序所做的只是处理数据文件,而不必关心数据的实际意义。
要实现这些功能,最简单的办法是把结构的内容初始化为0值,将文件数据读入临时变量中,然后把他们加到相应结构的内容中。程序读完文件后,应该计算每个球员的击球平均成功率,并把它保存到相应的结构的内容中。计算击球平均成功率是用球员的累积击中次数除以上场累计次数:这是个浮点计算。然后程序要显示每个球员的累计数据,并且对整个时期显示一行综合统计数据。
/*
本程序需要在程序目录下新建立一个iii.txt文件夹
并输入拷贝以下信息
0 wang yaowen 5 2 1 1
1 ke yongjia 6 3 2 2
0 wang yaowen 5 2 1 1
2 zhu minghou 9 5 3 2
3 jiang zhongping 7 4 2 2
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define NSIZE 20
#define PLSIZE 3
#define COLS 50
#define ROWS 5
typedef struct player{
char LName[NSIZE];
char FName[NSIZE];
int time;
int goal;
int walk;
int run;
float average;
}PL;
void initialize(PL * players,int plsize);
void gettemp(char (*temp)[COLS],int rows,FILE *fp);
void inputstruct(PL * players,char (*temp)[COLS]);
void outputstruct(PL * players,int plsize);
int main(void)
{
FILE *fp;
char temp[ROWS][COLS];
char test[NSIZE];
PL players[PLSIZE];
if( (fp=fopen("iii.txt","r"))==NULL )
{
fprintf(stderr,"Can't open file \"iii.txt\".");
exit(1);
}
initialize(players,PLSIZE);
gettemp(temp,ROWS,fp);//临时二维数组从文件中读取数据
inputstruct(players,temp);
outputstruct(players,PLSIZE);
}
void initialize(PL * players,int plsize)
{
int i;
for(i=0;i<plsize;i++)
{
players[i].time=0;
players[i].goal=0;
players[i].walk=0;
players[i].run=0;
}
}
void gettemp(char (*temp)[COLS],int rows,FILE *fp)
{
int i=0;
while((fgets(temp[i],COLS,fp) != NULL) && i<rows)
puts(temp[i++]);
}
void inputstruct(PL * players,char (*temp)[COLS])
{
char index_char[3];
int i,j,x,y;
char ch;
int status;//判断标志,1表示空格,0表示非空格
int count_space; //空格计数
int count_char;//字符计数
int index;
char lname[NSIZE];
char fname[NSIZE];
int times;
int goals;
int walks;
int runs;
for(i=0;i<ROWS&&temp[i][0]!=EOF;i++)
{
for(j=0,status=0,count_space=0,count_char=0,x=0,y=0;j<COLS&&temp[i][j] != '\0';j++)
{
ch=temp[i][j];
if(ch==' ')
{
status=1;
count_space++;
}
else
{
status=0;
}
if(count_space==0) //count_space==0表示第一个空格之前
{
count_char++;
}
if(count_space==1)
{
if(ch!=' ')
fname[x++]=ch;
}
if(count_space==2)
{
if(ch!=' ')
lname[y++]=ch;
}
if(count_space==3 && status==0)
{
times=ch-48;
}
if(count_space==4 && status==0)
{
goals=ch-48;
}
if(count_space==5 && status==0)
{
walks=ch-48;
}
if(count_space==6 && isalnum(ch))
{
runs=ch-48;
}
}
strncpy(index_char,temp[i],count_char);
index_char[count_char]='\0';
index=atoi(index_char); //计算出下标
// printf("index=%d\n",index);
fname[x]='\0';
// printf("%s\n",fname);
lname[y]='\0';
/* printf("%s\n",lname);
printf("times=%d\n",times);
printf("goals=%d\n",goals);
printf("walks=%d\n",walks);
printf("runs=%d\n",runs);*/
strcpy(players[index].FName,fname);
strcpy(players[index].LName,lname);
players[index].time+=times;
players[index].goal+=goals;
players[index].walk+=walks;
players[index].run+=runs;
players[index].average=(float)players[index].goal/(float)players[index].time;
/* printf("players[%d].goal=%d,players[%d].time=%d\n",index,players[index].goal,index,players[index].time);
printf("players[%d].average=%.2f\n",index,players[index].average);*/
}
}
void outputstruct(PL * players,int plsize)
{
int i;
printf("\n");
for(i=0;i<plsize;i++)
{
printf("\n");
printf("%s%s:\nplaying times:%d\ngoal=%d\nwalk=%d\nrun=%d\nrate=%.2f\n"
,players[i].FName,players[i].LName,players[i].time,players[i].goal,players[i].walk,players[i].run,players[i].average);
}
printf("\n");
}
7.修改程序清单14.14,在从文件中读出每个记录并且显示它时,允许用户选择删除该记录。要能改变现有文件的内容。必须使用"r+b"模式,而不是"a+b"模式。注意文件指针的定位,以便追加的记录不会覆盖已有的记录,最简单的方法是对存储在程序内存中的数据做所有的改变,然后再把最后的信息集中写入文件中。
暂时没想到思路
8.巨人航空公司的集群由作为容量为12的飞机组成。它每天飞行一个航班。按照下面的功能,写一个座位预定程序。
a.程序使用一个含有12个结构的数组,每个结构要包含一个用于标示作为座位的编号、一个标示作为是否已分配出去的标记、座位预定人的姓和座位预订人的名。
b.程序显示下面的菜单:
To choose a function,enter its letters label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d)Assign a customer to aseat assignment.
e)Delete a seat assignment
f)Quit
c.程序应能执行菜单所给出的功能。选择d)和e)需要额外的输入,每个选项都应允许用户终止输入。
d.程序执行完一个特定的功能之后,程序再次显示菜单,除非选择了f)
e.每次运行程序都把数据保存到一个文件中。当程序再次运行时,首先从文件中载入数据。如果有的话。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
const int LEN=20;
const int ROWS=4;
const int SIZE=18;
typedef struct seat{
char fn[LEN]; //姓
char ln[LEN]; //名
int seatnum; //座位编号
bool available; //座位是否可用
}SEAT;
const int STRSIZE=sizeof(SEAT);
void TopMenu();
void SecondMenuChoice(SEAT data[],int size,FILE *fp,const char *filename);
void Initialize(SEAT data[],int size,FILE *fp,const char *filename); //初始化结构数组
void SecondMenu(void); //显示菜单
void endline(void); //功能菜单
int ShowEmptySeatsAmount(SEAT data[],int size); //显示空座数量
void ShowListOfEmptySeat(SEAT data[],int size); //显示空座号码列表
void AlphabeticalList(SEAT data[],int size); //已预订座位顾客的姓名列表
void AssignSeat(SEAT data[],int size,FILE * fp,const char *filename); //顾客预订座位的操作
void DeleteAssign(SEAT data[],int size,FILE * fp,const char *filename); //顾客取消预订座位的操作
int main(void)
{
SEAT data[ROWS][SIZE];
char filenames[ROWS][10]={"102.txt","311.txt","444.txt","519.txt"};
char filename[10];
FILE *fp[ROWS];
int choice;
TopMenu();
scanf("%d",&choice);
endline();
while(choice!=5)
{
switch(choice)
{
case 1: strcpy(filename,filenames[0]); SecondMenuChoice(data[0],SIZE,fp[0],filename); break;
case 2: strcpy(filename,filenames[1]); SecondMenuChoice(data[1],SIZE,fp[1],filename); break;
case 3: strcpy(filename,filenames[2]); SecondMenuChoice(data[2],SIZE,fp[2],filename); break;
case 4: strcpy(filename,filenames[3]); SecondMenuChoice(data[3],SIZE,fp[3],filename); break;
default: printf("Error.\n");
}
TopMenu();
scanf("%d",&choice);
endline();
}
}
void TopMenu()
{
printf("\n");
printf("To choose a Flight\n");
printf("1) Flight 102\n");
printf("2) Flight 311\n");
printf("3) Flight 444\n");
printf("4) Flight 519\n");
printf("5) Quit\n");
}
void SecondMenuChoice(SEAT data[],int size,FILE *fp,const char *filename)
{
/* SEAT data[SIZE];
FILE *fp;*/
char choice;
Initialize(data,SIZE,fp,filename); //初始化
SecondMenu();
scanf("%c",&choice);
endline();
while(choice!='f')
{
switch(choice)
{
case 'a': ShowEmptySeatsAmount(data,SIZE);break;
case 'b': ShowListOfEmptySeat(data,SIZE);break;
case 'c': AlphabeticalList(data,SIZE);break;
case 'd': AssignSeat(data,SIZE,fp,filename);break;
case 'e': DeleteAssign(data,SIZE,fp,filename);break;
case 'f': break;
default:
printf("Please enter the right character.\n");
}
if(choice=='f')
break;
SecondMenu();
scanf("%c",&choice);
endline();
}
}
void Initialize(SEAT data[],int size,FILE *fp,const char *filename)
{
int i;
fp=fopen(filename,"a+b");
fseek(fp,0L,SEEK_END);
if(ftell(fp)==0) //空文件手动初始化数组。并且写入文件
{
printf("It is a empty file.\n");
rewind(fp);
for(i=0;i<size;i++) //初始化结构数组
{
data[i].fn[0]='\0';
data[i].ln[0]='\0';
data[i].seatnum=i;
data[i].available=true;
}
i=0;
while( (i<size) && (fwrite(&data[i],STRSIZE,1,fp)==1 ) ) //把初始化后结构数组写入文件
{
i++;
}
fclose(fp);
}
else //不是空文件则从文件中导入数据到结构数组
{
printf("Now load data from file %s ......",filename);
rewind(fp);
i=0;
while( (i<size) && (fread(&data[i],STRSIZE,1,fp)==1 ) ) //写入文件
{
i++;
}
printf("%s ",data[i].fn);
fclose(fp);
}
}
void SecondMenu(void)
{
printf("\n");
printf("To choose a function,enter its letters label:\n");
printf("a) Show number of empty seats\n");
printf("b) Show list of empty seats\n");
printf("c) Show alphabetical list of seats\n");
printf("d) Assign a customer to a seat assignment\n");
printf("e) Delete a seat assignment\n");
printf("f) return previous menu\n");
}
void endline(void)
{
int ch;
while((ch=getchar()) != '\n')
continue;
}
int ShowEmptySeatsAmount(SEAT data[],int size)
{
int total;
int i;
for(total=0,i=0;i<size;i++)
{
if(data[i].available==true)
total++;
}
printf("There are %d empty seats.\n",total);
return total;
}
void ShowListOfEmptySeat(SEAT data[],int size)
{
int i;
if(ShowEmptySeatsAmount(data,size)==0)
printf("All of seats are resevation.\n");
else
{
for(i=0;i<size;i++)
{
if(data[i].available==true)
{
printf("Num:%3d\n",data[i].seatnum);
}
}
}
}
/*
typedef struct seat{
char fn[LEN]; //姓
char ln[LEN]; //名
int seatnum; //座位编号
bool available; //座位是否可用
}SEAT;
*/
void AlphabeticalList(SEAT data[],int size) //应该是按照已经预定了座位的顾客姓名字母顺序打印列表。
{
int rows=ShowEmptySeatsAmount(data,size);
if(rows==SIZE)
{
printf("No comsters right now.\n");
}
else
{
// printf("here!\n");
char customer[rows][LEN];
char temp[LEN]="\0";
int i,j;
for(i=0;i<rows;i++)
{
customer[i][0]='\0';
}
for(i=0,j=0;i<size;i++) //遍历结构数组,找到已经预定的座位,把姓名写入customer
{
if(data[i].available==false)
{
strcpy(customer[j],data[i].fn);
j++;
}
}
for(i=0;i<rows-1;i++) //按字母排序
{
for(j=i+1;j<rows;j++)
{
if(strcmp(customer[i],customer[j])>0)
{
strcpy(temp,customer[i]);
strcpy(customer[i],customer[j]);
strcpy(customer[j],temp);
}
}
}
for(i=0;i<rows;i++)
{
puts(customer[i]);
}
}
}
/*
typedef struct seat{
char fn[LEN]; //姓
char ln[LEN]; //名
int seatnum; //座位编号
bool available; //座位是否可用
}SEAT;
*/
void AssignSeat(SEAT data[],int size,FILE * fp,const char * filename)
{
ShowListOfEmptySeat(data,size);
int choice;
int i;
printf("Enter the seat number which you want to reserve.\n");
scanf("%d",&choice);
endline();
while(data[choice].available==false)
{
printf("I'm sorry.you choose a unavailabel seats.\n");
printf("Enter another seat number again.\n");
scanf("%d",&choice);
endline();
}
puts("Enter your family name now.");
gets(data[choice].fn);
puts("Enter your last name now.");
gets(data[choice].ln);
data[choice].available=false;
puts("Thanks for your comstom.Have a good journey.");
/* *fp=fopen(filename,"a+b");
rewind(fp);
fseek(*fp,(long)(choice*sizeof(SEAT)),SEEK_SET);
fwrite(&data[choice],sizeof(SEAT),1,*fp);
fclose(*fp);*/
fp=fopen(filename,"w+b");
i=0;
while(i<size && (fwrite(&data[i],sizeof(SEAT),1,fp)) )
i++;
fclose(fp);
}
void DeleteAssign(SEAT data[],int size,FILE * fp,const char *filename)
{
int choice;
int i;
puts("Enter your assigning seat number.");
scanf("%d",&choice);
endline();
while(data[choice].available==true)
{
puts("I can't find your assignment.");
puts("Please try again.");
scanf("%d",&choice);
endline();
}
data[choice].available=true;
data[choice].fn[0]='\0';
data[choice].ln[0]='\0';
/* *fp=fopen(filename,"a+b");
rewind(fp);
fseek(*fp,(long)(choice*sizeof(SEAT)),SEEK_SET);
fwrite(&data[choice],sizeof(SEAT),1,*fp);
fclose(*fp);*/
fp=fopen(filename,"w+b");
i=0;
while(i<size && (fwrite(&data[i],sizeof(SEAT),1,fp)) )
i++;
fclose(fp);
puts("Delete assignment have done.");
}
11.编写一个transform()函数,他接受4个参数:包含double类型数据的源数组组名,double类型的目标数组组名,表示数组元素个数的int变量个数的int变量以及一个函数名。transform()函数把指定的函数作用语源数组的每个元素,并将返回值放入到目标数组中。例如:
transform(source,target,100,sin);
这个函数调用把sin(source[0]赋给target[0]),等等,共有100个元素。在一个程序中测试该函数,并且调用4次transform(),分别使用math.h函数库中的两个函数以及自己设计的两个合适的函数作为参数。
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define SIZE 10
typedef double (*FCT)(double source,double target);
double add(double source,double target);
double equal(double source,double target);
void transform(double *source,double *target,int size,FCT a);
int times=0;
int main(void)
{
FCT function[4]={add,equal,fmod,pow};
double source[SIZE];
double target[SIZE];
int i;
for(i=0;i<SIZE;i++) //初始化
{
source[i]=(1+i)*10.0;
target[i]=2;
}
for(i=0;i<4;i++) //调用4次函数
{
transform(source,target,SIZE,function[i]);
}
}
double add(double source,double target)
{
return target=source+1.0;
}
double equal(double source,double target)
{
return target=source;
}
void transform(double *source,double *target,int size,FCT a)
{
int j;
for(j=0;j<size;j++)
{
target[j]=a(source[j],target[j]);
}
times++;
printf("Call function %d times:\n",times);
for(j=0;j<size;j++)
{
printf("%.1f ",source[j]);
}
printf("\n");
for(j=0;j<size;j++)
{
printf("%.1f ",target[j]);
}
printf("\n");
}