大一学struct时并没有学好,现在果然要了命。这11道题摆弄了整整三天,终于完成了,但还是磕磕绊绊,唉……
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define NAMELEN 40
#define CSIZE 4
enum monthName
{
January,Feburary,March,April,May,June,July,August,September,October,November,December
};
struct flight
{
int num;
int flag;
char first[NAMELEN];
char last[NAMELEN];
};
struct plane
{
int flightNum;
struct flight list[NAMELEN];
};
struct MONTH
{
char month[NAMELEN];
char shoMon[3];
int numMon;
int numDay;
};
struct book
{
char title[NAMELEN];
char author[NAMELEN];
float value;
};
struct name
{
char first[NAMELEN];
char middle[NAMELEN];
char last[NAMELEN];
};
struct student
{
struct name people;
float grade[3];
float average;
};
struct securityNum
{
int num;
struct name people;
};
struct gameInfo
{
int num;
char playerLast[NAMELEN];
char playerFirst[NAMELEN];
int onTime;
int hitTime;
int goTime;
int runTime;
float hitRate;
};
int main(void) {
//1.a
/*
struct MONTH temp;
printf("enter the month:\n");
gets(temp.month);
printf("enter the short of month:\n");
gets(temp.shoMon);
printf("enter the position of the month:\n");
scanf_s("%d", &temp.numMon);
printf("enter the number of days in this month:");
scanf_s("%d", &temp.numDay);
printf("the month is %s\n",temp.month);
printf("the abbraviate of this month is:%s\n", temp.shoMon);
printf("this month has %d days and it's in the %dth\n", temp.numDay, temp.numMon);
*/
//1.b
/*
struct MONTH MonInfo[12];
int Days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
char ShoMon[][12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
printf("Success!");
for (enum Month loop = January; loop <= December; loop++)
{
MonInfo[loop].numDay = Days[loop];
strcpy(MonInfo[loop].shoMon, ShoMon[loop]);;
MonInfo[loop].numMon = loop + 1;;
switch (loop)
{
case January:
strcpy(MonInfo[loop].month, "January");
break;
case Feburary:
strcpy(MonInfo[loop].month, "Feburary");
break;
//省略其他月份
default:
break;
}
printf("the month is %s\n", MonInfo[loop].month);
printf("the abbraviate of this month is:%s\n", MonInfo[loop].shoMon);
printf("this month has %d days and it's in the %dth\n", MonInfo[loop].numDay, MonInfo[loop].numMon);
printf("\n**********************\n");
}
*/
//2
/*
struct tm t;
char monName[12][NAMELEN] = { "January","Feburary","March","April"
,"May","June","July","August","September","October","November","December" };
char str[80];
char* TOUPPERMy(char *temp, int n);
long temp;
time_t timeNum;
double days;
char timeCh[NAMELEN];
t.tm_sec = 0;
t.tm_min = 0;
t.tm_hour = 0;
t.tm_wday = 0;
t.tm_yday = 0;
t.tm_isdst = 0;
//存入日期信息
printf("Please enter the year:\n");
scanf("%d", &t.tm_year);
getchar();
t.tm_year = t.tm_year - 1900;//去掉起始点
printf("Please enter the month:\n");
scanf("%d", &t.tm_mon);
gets(timeCh);
if (timeCh[0] != '\n'&&timeCh[0] != '\0')
{
for (int i = 0; i < 12; i++)
{
if (strstr(TOUPPERMy(monName[i], NAMELEN), TOUPPERMy(timeCh, NAMELEN))!=NULL)
{//将所有输入都转化为大写然后判断,其实原始数据仅转化一遍就可以了。
t.tm_mon = i+1;//为了匹配朋份数,因此在0的基础上加1
break;
}
}
}
t.tm_mon = t.tm_mon - 1;//上文月份以0开始,此处统一改为0开始
printf("Please enter the date:\n");
scanf("%d", &t.tm_mday);
//将日期信息放入字符串以打印
strcpy(str, asctime(&t));
printf("您所输入的日期为:%s\n", str);
//将所获日期转化为秒以计算
temp = mktime(&t);//获得从1970年1月1日0时0分0秒以来过去的时间
days = ((temp - time(NULL))/3600)/24.0;
printf("今日之%s时间 :%lf", days>0?"后":"前",days>0?days:-days);//人性化表达,第二个参数为了给出days的绝对值。math.h中的绝对值返回只int
*/
//3
/*
int n;
struct book library[NAMELEN];
int index;
int count = 0;
void showBook(struct book *temp, int n, int choice);
printf("please enter the book title:\n");
printf("press [enter] at the top of the line to stop.\n");
while (count < NAMELEN&&gets(library[count].title) != NULL&&library[count].title[0] != '\n'&&library[count].title[0] != '\0')
{
printf("enter author:\n");
gets(library[count].author);
printf("enter the value:\n");
scanf("%f", &library[count].value);
while (getchar() != '\n')
{
continue;
}
count++;
if (count < NAMELEN)
{
printf("enter the next title:\n");
}
}
if (count>0)
{
showBook(library, count, 1);
showBook(library, count, 2);
showBook(library, count, 3);
}
else
{
printf("No book!");
}
*/
//4
//a
/*struct securityNum list[5] = { 100,{ "h"," ","rn" },101,{ "ji"," ","bi" },102,{ "u","ng","ei" },
103,{ "bai","y","ie" },104,{ "z","r","bo" } };
void showInfo(const struct securityNum *temp, int n);
showInfo(list, 5);
*/
//4
//b
/*
struct securityNum list[5] = { 100,{"h"," ","rn"},101,{"ji"," ","bi"},102,{"u","ng","ei"},
103,{"bai","y","ie"},104,{"z","r","bo"} };
void showInfo2(struct securityNum temp[],int n);
showInfo2(list, 5);
*/
//5
/*
struct student list[CSIZE];
void GetInfo(struct student *list, int n);
void GetAverage(struct student *list, int n);
void ShowInfo(struct student *list, int n);
float ClassAverage(struct student *list, int n);
GetInfo(list, CSIZE);
printf("%f",list[0].average);
GetAverage(list, CSIZE);
ShowInfo(list, CSIZE);
printf("the average grade of the class is:%f\n",ClassAverage(list, CSIZE));
*/
//6
/*
FILE *fp;
struct gameInfo list[18];
int retNum;
char absPath[80] = "d:\\CWorkSpace\\Chapter14_02\\Debug\\data1.txt";
struct gameInfo temp;
void giveInfo(struct gameInfo *dest, struct gameInfo *source);
int count;
if ((fp = fopen(absPath,"r")) == NULL)
{
printf("Error in open file:%s", absPath);
exit(1);
}
retNum = fscanf(fp, "%d %s %s %d %d %d %d ",
&temp.num, &temp.playerFirst, &temp.playerLast, &temp.onTime, &temp.hitTime, &temp.goTime, &temp.runTime);
while (retNum > 0)
{
retNum = fscanf(fp, "%d %s %s %d %d %d %d ",//注意序号存在count中
&temp.num, &temp.playerFirst, &temp.playerLast, &temp.onTime, &temp.hitTime, &temp.goTime, &temp.runTime);
count = temp.num;
giveInfo(&list[count-1],&temp);
}
for (int i = 0; i < 18; i++)
{
printf("%d %s %s %d %d %d %d\n",//注意序号存在count中
list[i].num, list[i].playerFirst, list[i].playerLast, list[i].onTime, list[i].hitTime, list[i].goTime, list[i].runTime);
list[i].hitRate = (float)list[i].hitTime / list[i].onTime;
printf("the %dth player has the hit rate of %f\n", i, list[i].hitRate);
}
fclose(fp);
*/
//7//
/*
struct book library[NAMELEN];
int modifyNum;
int count = 0;
int index, filecount;
FILE *pbook;
int size = sizeof(struct book);
char choice[NAMELEN];
if ((pbook = fopen("d:\\CWorkSpace\\Chapter14_02\\Debug\\book.txt", "r+b")) == NULL)
{
printf("cannot open file!");
exit(1);
}
rewind(pbook);
while (count < NAMELEN&&fread(&library[count], size, 1, pbook) == 1&&library[count].title[0]!=EOF)//给最后留出的那个空加上结束符强制结束(其实并没有删去)
{
if (count==0)
{
printf("current book list:\n");
}
printf("%dth. %s by %s:$%.2f\n", count + 1, library[count].title, library[count].author, library[count].value);
count++;
}
filecount = count;
if (count==NAMELEN)
{
fputs("the book list is full!\n", stderr);
exit(2);
}
printf("Please enter your order:add, modify or delete(Press q to quit)\n");
fgets(choice, NAMELEN, stdin);
while (choice[0] != 'q')
{
if (tolower(choice[0]) == 'a')
{//选择添加
puts("please add new book titles.");
puts("press [enter] at the start of a line to stop!");
while (count < NAMELEN&&gets(library[count].title) != NULL&&library[count].title[0] != '\0')
{
puts("please enter author:");
gets(library[count].author);
puts("please enter value:");
scanf("%f", &library[count].value);
count++;
while (getchar() != '\n')
{
continue;
}
if (count < NAMELEN)
{
puts("enter the next title:");
}
}
fwrite(&library[filecount], size, count - filecount, pbook);
}
else if (tolower(choice[0]) == 'm')
{//选择修改
printf("which one you want to change:\n");
scanf("%d", &modifyNum);
modifyNum = modifyNum - 1;//封面上的序号大1,需要减回来
while (getchar() != '\n')
{
continue;
}
puts("please add new book titles.");
gets(library[modifyNum].title);
puts("please enter author:");
gets(library[modifyNum].author);
puts("please enter value:");
scanf("%f", &library[modifyNum].value);
fseek(pbook, 0, SEEK_SET);
fwrite(&library, size, count, pbook);
}
else if (tolower(choice[0]) == 'd')
{//选择删除
printf("which one you want to change:\n");
scanf("%d", &modifyNum);//scanf后头一定要跟一个eatline,要不总有问题
modifyNum--;
while (getchar() != '\n')
{
continue;
}
for (int i = modifyNum; i < count - 1; i++)
{
library[i] = library[i + 1];
}
library[count-1].title[0] = EOF;
fseek(pbook, 0, SEEK_SET);
fwrite(&library, size, count, pbook);
count--;
}
else
{
printf("no option!");
}
if (count > 0)
{
puts("here's the list of the books:\n");
for (int index = 0; index < count; index++)
{
printf("%dth. %s by %s:$%.2f\n", index + 1, library[index].title, library[index].author, library[index].value);
}
}
else {
puts("no books?Too bad!\n");
}
printf("Please enter your order:add, modify or delete(Press q to quit)\n");
fgets(choice, NAMELEN, stdin);
}
puts("Bye!\n");
fclose(pbook);
*/
//8
/*
struct flight list[NAMELEN];
char absPath[80] = "d:\\CWorkSpace\\Chapter14_02\\Debug\\bookseats1.txt";
FILE *bookSeats;
char choice[NAMELEN];
int sum;
int NumOfEmpty(struct flight *list, int n);
void ListOfEmpty(struct flight *list, int n);
void TakeSeat(struct flight list[],int n);
void AlphabeticalList(struct flight list[], int n);
void DeleteSeat(struct flight list[], int n);
int count = 0;
int size;
if ((bookSeats = fopen(absPath, "r+b")) == NULL)
{
printf("Error in open file:%s", absPath);
exit(1);
}
size = sizeof(struct flight);
while (count < NAMELEN&&fread(&list[count], size, 1, bookSeats) == 1)
{
count++;
}
printf("Menu:\na)number of empty seats;\nb)list of empty seats;\nc)alphabetical list of seats;\nd)assign a customer;\n");
printf("e)delete a seat assignment; \nf)quit.\n");
fgets(choice,NAMELEN,stdin);
while (choice[0]!='f')
{
switch (choice[0])
{
case 'a':
sum = NumOfEmpty(list, NAMELEN);
printf("there are %d seats left!", sum);
break;
case 'b':
ListOfEmpty(list, NAMELEN);
break;
case 'c':
AlphabeticalList(list, NAMELEN);
break;
case 'd':
TakeSeat(list, NAMELEN);
fseek(bookSeats, 0, SEEK_SET);
fwrite(list, size, NAMELEN, bookSeats);
printf("Your order is taken! Thank you!");
break;
case 'e':
printf("taken seats:\n");
for (int i = 0; i < NAMELEN; i++)
{
if (list[i].flag == 1)
{
printf("No.%4d ", list[i].num + 1);
}
}
DeleteSeat(list, NAMELEN);
fseek(bookSeats, 0, SEEK_SET);
fwrite(list, size, NAMELEN, bookSeats);
printf("Your order is taken! Thank you!");
ListOfEmpty(list, NAMELEN);
break;
default:
puts("Sorry, no such an option!\n");
break;
}
printf("Menu:\na)number of empty seats;\nb)list of empty seats;\nc)alphabetical list of seats;\nd)assign a customer;\n");
printf("e)delete a seat assignment; \nf)quit.\n");
fgets(choice, NAMELEN, stdin);
}
fclose(bookSeats);
*/
//9
/*
struct plane planeList[4];//等于就是一共四架次飞机
char absPath[80] = "d:\\CWorkSpace\\Chapter14_02\\Debug\\fourplane1.txt";
FILE *bookSeats;
char choice[NAMELEN];
int planeNum;
int sum;
int NumOfEmpty(struct flight *list, int n);
void ListOfEmpty(struct flight *list, int n);
void TakeSeat(struct flight list[],int n);
void AlphabeticalList(struct flight list[], int n);
void DeleteSeat(struct flight list[], int n);
void AssignFlight(struct flight *list);
int count = 0;
int chooseFlight;
int size;
if ((bookSeats = fopen(absPath, "r+b")) == NULL)
{
printf("Error in open file:%s", absPath);
exit(1);
}
size = sizeof(struct plane);
while (count < 4&&fread(&planeList[count], size, 1, bookSeats) == 1)
{
count++;
}
printf("please choose your flight:102,311,444,519,press 0 to quit\n");
scanf("%d", &planeNum);
while (getchar() != '\n')
{
continue;
}
while (planeNum != 0)
{
switch (planeNum)
{
case 102:chooseFlight = 0;
case 311:chooseFlight = 1;
case 444:chooseFlight = 2;
case 519:chooseFlight = 3;
default:
printf("no such option!");
break;
}
AssignFlight(&planeList[chooseFlight]);
printf("please choose your flight:102,311,444,519,press 0 to quit\n");
scanf("%d", &planeNum);
while (getchar() != '\n')
{
continue;
}
}
fseek(bookSeats, 0, SEEK_SET);
fwrite(planeList, size, 4, bookSeats);
printf("Your order is taken! Thank you!");
fclose(bookSeats);
*/
//10
struct flight list[NAMELEN];
char absPath[80] = "d:\\CWorkSpace\\Chapter14_02\\Debug\\bookseats1.txt";
FILE *bookSeats;
char choice[NAMELEN];
int sum;
int NumOfEmpty(struct flight *list, int n);
void ListOfEmpty(struct flight *list, int n);
void TakeSeat(struct flight list[], int n);
void AlphabeticalList(struct flight list[], int n);
void DeleteSeat(struct flight list[], int n);
void (*pf[4])(struct flight list[], int n);
int count = 0;
int size;
pf[0] = ListOfEmpty;//给指向函数的指针赋值!
pf[1] = AlphabeticalList;
pf[2] = TakeSeat;
pf[3] = DeleteSeat;
if ((bookSeats = fopen(absPath, "r+b")) == NULL)
{
printf("Error in open file:%s", absPath);
exit(1);
}
size = sizeof(struct flight);
while (count < NAMELEN&&fread(&list[count], size, 1, bookSeats) == 1)
{
count++;
}
printf("Menu:\na)number of empty seats;\nb)list of empty seats;\nc)alphabetical list of seats;\nd)assign a customer;\n");
printf("e)delete a seat assignment; \nf)quit.\n");
fgets(choice, NAMELEN, stdin);
while (choice[0] != 'f')
{
switch (choice[0])
{
case 'a':
sum = NumOfEmpty(list, NAMELEN);
printf("there are %d seats left!", sum);
break;
case 'b':
pf[0](list, NAMELEN);
break;
case 'c':
pf[1](list, NAMELEN);
break;
case 'd':
pf[2](list, NAMELEN);
fseek(bookSeats, 0, SEEK_SET);
fwrite(list, size, NAMELEN, bookSeats);
printf("Your order is taken! Thank you!");
break;
case 'e':
printf("taken seats:\n");
for (int i = 0; i < NAMELEN; i++)
{
if (list[i].flag == 1)
{
printf("No.%4d ", list[i].num + 1);
}
}
pf[3](list, NAMELEN);
fseek(bookSeats, 0, SEEK_SET);
fwrite(list, size, NAMELEN, bookSeats);
printf("Your order is taken! Thank you!");
ListOfEmpty(list, NAMELEN);
break;
default:
puts("Sorry, no such an option!\n");
break;
}
printf("Menu:\na)number of empty seats;\nb)list of empty seats;\nc)alphabetical list of seats;\nd)assign a customer;\n");
printf("e)delete a seat assignment; \nf)quit.\n");
fgets(choice, NAMELEN, stdin);
}
fclose(bookSeats);
//11
/*
void transform(double source[], double dest[], int n, double (*pf)(double x));
double source[100];
double dest[100];
double(*pf)(double);
double squreMy(double x);
pf = sin;
for (int i = 0; i < 100; i++)
{
*(source + i) = i/10.0;
}
transform(source, dest, 100, pf);
for (int i = 0; i < 100; i++)
{
printf("%16lf", dest[i]);
}
printf("\n********\n");
pf = cos;
for (int i = 0; i < 100; i++)
{
*(source + i) = i / 10.0;
}
transform(source, dest, 100, pf);
for (int i = 0; i < 100; i++)
{
printf("%16lf", dest[i]);
}
printf("\n********\n");
pf = squreMy;
for (int i = 0; i < 100; i++)
{
*(source + i) = i / 10.0;
}
transform(source, dest, 100, pf);
for (int i = 0; i < 100; i++)
{
printf("%16lf", dest[i]);
}
*/
return 0;
}
double squreMy(double x) {
x *= x;
return x;
}
void transform(double source[], double dest[], int n, double (*pf)(double x)) {
for (int i = 0; i < n; i++)
{
dest[i] = (*pf)(source[i]);
}
}
void AssignFlight(struct flight *list) {
char choice[NAMELEN];
int sum;
int size;
size = sizeof(struct flight);
printf("Menu:\na)number of empty seats;\nb)list of empty seats;\nc)alphabetical list of seats;\nd)assign a customer;\n");
printf("e)delete a seat assignment; \nf)back to the main menu\ng)quit.\n");
fgets(choice, NAMELEN, stdin);
while (choice[0] != 'f')
{
switch (choice[0])
{
case 'a':
sum = NumOfEmpty(list, NAMELEN);
printf("there are %d seats left!", sum);
break;
case 'b':
ListOfEmpty(list, NAMELEN);
break;
case 'c':
AlphabeticalList(list, NAMELEN);
break;
case 'd':
TakeSeat(list, NAMELEN);
break;
case 'e':
printf("taken seats:\n");
for (int i = 0; i < NAMELEN; i++)
{
if (list[i].flag == 1)
{
printf("No.%4d ", list[i].num + 1);
}
}
DeleteSeat(list, NAMELEN);
ListOfEmpty(list, NAMELEN);
break;
case 'g':
printf("Bye!");
exit(1);
default:
puts("Sorry, no such an option!\n");
break;
}
printf("Menu:\na)number of empty seats;\nb)list of empty seats;\nc)alphabetical list of seats;\nd)assign a customer;\n");
printf("e)delete a seat assignment; \nf)quit.\n");
fgets(choice, NAMELEN, stdin);
}
}
void DeleteSeat(struct flight list[], int n) {
int choice;
char first[NAMELEN];
char last[NAMELEN];
printf("which seat you want to cancel:\n");
scanf("%d", &choice);
choice--;
printf("please enter your name:\n");
scanf("%s %s", first,last);
printf("1.%s -- %s, 2.%s -- %s", first, list[choice].first,last, list[choice].last);
if (strcmp(first,list[choice].first)==0&&strcmp(last,list[choice].last)==0)
{
list[choice].flag = 0;
}
}
void TakeSeat(struct flight list[], int n) {
int choice;
void ListOfEmpty(struct flight *list, int n);
ListOfEmpty(list, n);
printf("which one would you like to take?");
scanf_s("%d", &choice);
while (getchar() != '\n')
{
continue;
}
choice--;
printf("please enter your name:\n");
scanf("%s %s", &list[choice].first, &list[choice].last);
while (getchar() != '\n')
{
continue;
}
list[choice].flag = 1;
list[choice].num = choice;
}
int NumOfEmpty(struct flight *list, int n) {
int sum = 0;
for (int i = 0; i < NAMELEN; i++)
{
if (list[i].flag != 1)
{
sum++;
}
}
return sum;
}
void ListOfEmpty(struct flight *list, int n) {
int sum;
int NumOfEmpty(struct flight *list, int n);
sum = NumOfEmpty(list, NAMELEN);
printf("there are %d seats left!\n", sum);
printf("empty seats:\n");
for (int i = 0; i < NAMELEN; i++)
{
if (list[i].flag != 1)
{
printf("No.%4d,", i + 1);
}
}
}
void AlphabeticalList(struct flight list[],int n) {
struct flight temp;
for (int i = 0; i < n; i++)
{
temp = list[i];
for (int j = i; j < n; j++)
{
if (strcmp(temp.first,list[j].first)>0)
{
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
}
printf("Taken seats are:\n");
for (int i = 0; i < n; i++)
{
if (list[i].flag==1)
{
printf("%8s %8s at %6dth seat\n", list[i].first, list[i].last, list[i].num+1);
}
}
}
void giveInfo(struct gameInfo *dest, struct gameInfo *source) {
if ((*dest).num != (*source).num)
{
*dest = *source;//如果不等于,证明什么也没有,直接复制
}
else {
(*dest).goTime += (*source).goTime;
(*dest).hitTime += (*source).hitTime;
(*dest).onTime += (*source).onTime;
(*dest).runTime += (*source).runTime;
}
}
float ClassAverage(struct student *list, int n) {
float sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += list[i].average;
}
return sum/n;
}
void ShowInfo(struct student *list, int n) {
printf("student info:\n");
for (int i = 0; i < n; i++)
{
printf("student:%s %s %s, subject 1:%f,subject 2:%f,subject 3:%f, average grade:%f\n",
list[i].people.first, list[i].people.middle, list[i].people.last, list[i].grade[0], list[i].grade[1], list[i].grade[2],
list[i].average);
}
}
void GetAverage(struct student *list, int n) {
for (int i = 0; i < n; i++)
{
list[i].average = (list[i].grade[0] + list[i].grade[1] + list[i].grade[2]) / 3;
}
}
void GetInfo(struct student *list, int n) {
for (int i = 0; i < n; i++)
{
printf("please enter the name of this studnet:\n");
scanf("%s %s %s", list[i].people.first, list[i].people.middle, list[i].people.last);
printf("please enter three grades:\n");
scanf_s("%f %f %f", &list[i].grade[0], &list[i].grade[1], &list[i].grade[2]);
list[i].average = (list[i].grade[0] + list[i].grade[1] + list[i].grade[2]) / 3;
}
}
void showInfo(const struct securityNum *temp, int n) {
//printf("info is :%d,name is %s, %s, %s",temp[0].num,temp[0].people.first, temp[0].people.middle, temp[0].people.last);
for (int i = 0; i < n; i++)
{
if (strcmp(temp[i].people.middle, " ") != 0)
{
//printf("\nsuccess\n");
printf("%s %s %c.- %d\n", temp[i].people.first, temp[i].people.last, temp[i].people.middle[0], temp[i].num);
}
else
{
printf("%s %s - %d\n", temp[i].people.first, temp[i].people.last, temp[i].num);
}
}
}
void showInfo2(struct securityNum temp[],int n) {
//printf("info is :%d,name is %s, %s, %s",temp[0].num,temp[0].people.first, temp[0].people.middle, temp[0].people.last);
for (int i = 0; i < n; i++)
{
if (strcmp(temp[i].people.middle," ")!=0)
{
//printf("\nsuccess\n");
printf("%s %s %c.- %d\n", temp[i].people.first, temp[i].people.last,temp[i].people.middle[0],temp[i].num);
}
else
{
printf("%s %s - %d\n", temp[i].people.first, temp[i].people.last,temp[i].num);
}
}
}
void showBook(struct book *temp,int n,int choice) {
struct book medium;
if (choice == 1)//按录入顺序
{
printf("\nhere's the list of the book:\n");
for (int i = 0; i < n; i++)
{
printf("%s by %s $%f\n", temp[i].title, temp[i].author, temp[i].value);
}
}
else if (choice == 2)//按字母顺序
{
printf("\nhere's the list of the book:(alphibetic version)\n");
for (int i = 0; i < n; i++)
{
medium = temp[i];
for (int j = i; j < n; j++)
{
if (strcmp(medium.title,temp[j].title)>0)
{
medium = temp[j];
temp[j] = temp[i];
temp[i] = medium;
}
}
}
for (int i = 0; i < n; i++)
{
printf("%s by %s $%f\n", temp[i].title, temp[i].author, temp[i].value);
}
}
else if (choice == 3)
{
printf("\nhere's the list of the book:(alphibetic version)\n");
for (int i = 0; i < n; i++)
{
medium = temp[i];
for (int j = i; j < n; j++)
{
if (medium.value > temp[j].value)
{
medium = temp[j];
temp[j] = temp[i];
temp[i] = medium;
}
}
}
for (int i = 0; i < n; i++)
{
printf("%s by %s $%f\n", temp[i].title, temp[i].author, temp[i].value);
}
}
}
char* TOUPPERMy(char *temp,int n) {
//将数组转化为大写
int i = 0;
while (i < n&&*(temp + i) != '\0')
{
*(temp + i) = toupper(*(temp + i));
i++;
}
return temp;
}
void eatLine(void) {
while (getchar()!='\n')
{
continue;
}
}
int ChOrNot(char *temp) {
int flag = 0;//默认为字符
if (temp[0]<=9||temp[0]>=0)
{//证明为数字
flag = 1;
}
else if (tolower(temp[0]) <= 'z' || tolower(temp[0]) >= 'a')
{//证明为字母
flag = 0;
}
else
{//证明为符号
flag = -1;
}
return flag;
}