C Primer Plus 第十四章 编程练习 1-11题

第3题
#include
#define Len 40
#define Counts 100
struct Book
{
  char title[Len];
  char atuhor[Len];
  float value;
};
int main(void)
{
  int numbers = 0;
  struct Book books[Counts];
  do                                //完成输入
  {
    printf("请输入第%d本书的名称:",numbers+1);
    if(gets(books[numbers].title) == NULL || books[numbers].title[0] == '\0')
      break;
    printf("请输入第%d本书的作者:",numbers+1);
    if(gets(books[numbers].atuhor) == NULL || books[numbers].atuhor[0] == '\0')
      break;
    printf("请输入第%d本书的价格:",numbers+1);
    if(!scanf("%f",&(books[numbers].value)))
      break;
    getchar();
    ++numbers;
  }while(numbers <= Counts);

  for(int i = 0 ; i < numbers ; ++i)   //按输入顺序输出
    printf("第%d本书名字:%s,作者 %s,价格%.2f.\n",i+1,books[i].title,books[i].atuhor,books[i].value);
  
  for(int i = 0 ; i < numbers ; ++i)   //按标题字母输出
  {
    for(int j = i ; j < numbers ; ++j)
    {
      struct Book midBook;
      if(books[i].title[0] < books[j].title[0])
      {
        midBook = books[j];
        books[j] = books[i];
        books[i] = midBook;
      }
    }
  }
  for(int i = 0 ; i < numbers ; ++i)
    printf("第%d本书名字:%s,作者 %s,价格%.2f.\n",i+1,books[i].title,books[i].atuhor,books[i].value);
  
  for(int i = 0 ; i < numbers ; ++i)  //按价格输出
  {
    for(int j = i ; j < numbers ; ++j)
    {
      struct Book midBook;
      if(books[i].value < books[j].value)
      {
        midBook = books[j];
        books[j] = books[i];
        books[i] = midBook;
      }
    }
  }
  for(int i = 0 ; i < numbers ; ++i)
    printf("第%d本书名字:%s,作者 %s,价格%.2f.\n",i+1,books[i].title,books[i].atuhor,books[i].value);

  return 0;
}


第4题
#include
#define Len 40
#define Counts 5  //5个人
struct nameMan
{
  char firstName[Len];
  char lastName[Len];
  char midName[Len];
};
struct infoMan
{
  long numbers;
  struct nameMan name;
};
void showInfo(const struct infoMan* info , int len);
int main(void)
{
  struct infoMan mans[Counts];
  int i ;
  for(i = 0 ; i < Counts ; ++i)
  {
    printf("请输入第%d个人的姓:",i+1);
    if(gets(mans[i].name.firstName) == NULL && mans[i].name.firstName[0] == '\0')
      break;
    printf("请输入第%d个人的名:",i+1);
    if(gets(mans[i].name.lastName) == NULL && mans[i].name.lastName[0] == '\0')
      break;
    printf("请输入第%d个人的中间名:",i+1);
    if(gets(mans[i].name.midName) == NULL)
    {
      mans[i].name.midName[0] = '\0';
      break;
    }
    printf("请输入第%d个人的社会保障号:",i+1);
    if(!scanf("%ld",&(mans[i].numbers)))
      break;
    getchar();
  }  

  showInfo(mans,i);

  return 0;
}
void showInfo(const struct infoMan* info , int len)
{
  for(int i = 0 ; i < len ; ++i)
  {
    if((((info+i)->name).midName[0]) == '\0')
      printf("%s %s  -%ld\n",((info+i)->name).firstName,((info+i)->name).lastName,(info+i)->numbers);
    else
      printf("%s %s %c. -%ld\n",((info+i)->name).firstName,((info+i)->name).lastName,((info+i)->name).midName[0],(info+i)->numbers);
  }
}


第5题
#include
#define Len 40
#define size 3
#define CSIZE 4
struct name
{
  char firstName[Len];
  char lastName[Len];
};
struct student
{
  struct name names;
  float grade[size];
  float AVG;
};
int main(void)
{
  struct student classA[CSIZE];
  float AVGchinese = 0;
  float AVGmath = 0;
  float AVGenglish = 0;
  int i;
  for(i = 0 ; i < CSIZE ; ++i)
  {
    printf("请输入第%d个学生的姓:",i+1);
    if(gets(classA[i].names.firstName) == NULL)
      break;
    printf("请输入第%d个学生的名:",i+1);
    if(gets(classA[i].names.lastName) == NULL)
      break;
    printf("请输入第%d个学生的语文成绩:",i+1);
    if(!scanf("%f",&(classA[i].grade[0])))
      break;
    printf("请输入第%d个学生的数学成绩:",i+1);
    if(!scanf("%f",&(classA[i].grade[1])))
      break;
    printf("请输入第%d个学生的英语成绩:",i+1);
    if(!scanf("%f",&(classA[i].grade[2])))
      break;
    getchar();
    classA[i].AVG = (classA[i].grade[0] + classA[i].grade[1] + classA[i].grade[2]) / 3;
  }

  for(int j = 0 ; j < i ; ++j)
    printf("%s.%s 语文%.2f分,数学%.2f分,英语%.2f分,平均%.2f分\n",classA[j].names.firstName,classA[j].names.lastName,classA[j].grade[0],classA[j].grade[1],classA[j].grade[2],classA[j].AVG);

  for(int j = 0 ; j < i ; ++j)
  {
    AVGchinese += classA[j].grade[0];
    AVGmath += classA[j].grade[1];
    AVGenglish += classA[j].grade[2];
  }
  printf("语文平均%.2f分,数学平均%.2f分,英语平均%.2f分.",AVGchinese/i,AVGmath/i,AVGenglish/i);

  return 0;
}


第8题
#include
#include
#include
#define Len 40
#define SeatCount 12
#define DatFile "DatFile.txt"
struct PlaneSeat
{
  int Codes;
  bool IsSaled;
  char FName[Len];
  char LName[Len];
};
void ShowMenu(void);  
void ShowList(void); //A
void ShowEmptySeat(void);  //B
void ShowEmptySeatByCodes(void);// C
void SaleSeat(void);// D
void DeleteSeat(void);  //E
int main(void)
{
  if(fopen(DatFile,"r") == NULL)  //create datfile if it's not exist.
  {
    struct PlaneSeat NewPlane[SeatCount];
    for(int i = 0 ; i < SeatCount ; ++i)
    {
      NewPlane[i].Codes = (i+1);
      NewPlane[i].IsSaled = false;
      strcpy(NewPlane[i].FName,"None");  //This is no one
      strcpy(NewPlane[i].LName,"None");
    }
    int BufferSize = sizeof(struct PlaneSeat);
    FILE* SeatFile;
    SeatFile = fopen(DatFile,"w");
    for(int i = 0 ; i < SeatCount ; ++i)  
      fwrite(&(NewPlane[i]),BufferSize,1,SeatFile);
    fclose(SeatFile);
  }

  bool on_off = true;
  while(on_off)
  {
    char ch;
    ShowMenu();
    scanf("%c",&ch);
    getchar();
    switch(ch)
    {
      case 'a':ShowList();
               break;
      case 'b':ShowEmptySeat();
               break;
      case 'c':ShowEmptySeatByCodes();
               break;
      case 'd':SaleSeat();
               break;
      case 'e':DeleteSeat();  
               break;
      case 'f':on_off = false;
               break;
      default:on_off = false;
               break;
    }
  }
  return 0;
}
void ShowMenu(void)
{
  printf("~~~~~~~~~~~\n");
  printf("%s\n","To Choose A Funtion.");
  printf("%s\n","A:Show Number Of Empty Seat.");
  printf("%s\n","B:Show Lits Of Empyt.");
  printf("%s\n","C:Show Alphabetical Lits  Of Empty Seat.");
  printf("%s\n","D:Assign A Customer To A Seat Assignment..");
  printf("%s\n","E:Delete A Seat Assingment.");
  printf("%s\n","F.Quit.");
}
  
void ShowList(void)
{
  struct PlaneSeat NewPlane[SeatCount];
  int BufferSize = sizeof(struct PlaneSeat);
  FILE* SeatFile;
  SeatFile = fopen(DatFile,"r");
  for(int i = 0 ; i < SeatCount ; ++i)
  {
    fread(&(NewPlane[i]),BufferSize,1,SeatFile);
    printf("%2d %2d %40s %40s\n",NewPlane[i].Codes,NewPlane[i].IsSaled,NewPlane[i].FName,NewPlane[i].LName);
  }
  fclose(SeatFile);
}

void ShowEmptySeat(void)
{
  struct PlaneSeat NewPlane[SeatCount];
  int BufferSize = sizeof(struct PlaneSeat);
  FILE* SeatFile;
  SeatFile = fopen(DatFile,"r");
  for(int i = 0 ; i < SeatCount ; ++i)
  {
    fread(&(NewPlane[i]),BufferSize,1,SeatFile);
    if(NewPlane[i].IsSaled == false)
      printf("%d\n",NewPlane[i].Codes);
  }
  fclose(SeatFile);
}

void ShowEmptySeatByCodes(void)
{
  struct PlaneSeat NewPlane[SeatCount];
  int BufferSize = sizeof(struct PlaneSeat);
  FILE* SeatFile;
  SeatFile = fopen(DatFile,"r");
  for(int i = 0 ; i < SeatCount ; ++i)
    fread(&(NewPlane[i]),BufferSize,1,SeatFile);
  fclose(SeatFile);

  for(int i = 0 ; i < SeatCount ; ++i)   //sort
  {
    for(int j = i ; j 


第11题
#include
#include
#include
#include
#define Ns 100
#define Len 10
double up1(double i);   //给元素-1
double down1(double i);  //给元素+1
void transform(double* source , double* tar , int Counts , double (*pt)(double i));
void showInfo(double* tar , int Counts);
int main(void)
{
  srand((unsigned)time(NULL));
  double sourceArray[Ns];
  double targetArray[Ns];
  for(int i = 0 ; i < Ns ; ++i)
    sourceArray[i] = rand()%100 + 0.1;  //随机生产数字  
  showInfo(sourceArray,Ns);

  transform(sourceArray,targetArray,Ns,up1);
  transform(sourceArray,targetArray,Ns,down1);
  transform(sourceArray,targetArray,Ns,sqrt);
  transform(sourceArray,targetArray,Ns,cbrt);

  return 0;
}
double up1(double i)
{
  return ++i;
}
  
double down1(double i)
{
  return --i;
}  

void transform(double* source , double* tar , int Counts , double (*pt)(double i))
{
  for(int i = 0 ; i < Counts ; ++i)
    *(tar+i) = (*pt)(*(source+i));
  showInfo(tar,Counts);
}

void showInfo(double* tar , int Counts)
{
  printf("~~~~~~~~~~~~~~我是华丽的分割线~~~~~~~~~~~~~~\n");
  for(int i = 1 ; i <= Counts ; ++i)
  {
    printf("%6.2lf ",*(tar+(i-1)));
    if(i%Len == 0)
      printf("\n");
  }
}


你可能感兴趣的:(c语言)