C Primer Plus 第十四章 编程练习 1-11题
来源:互联网 发布:酒神淘宝店地址 编辑:程序博客网 时间:2024/06/06 07:22
#include<stdio.h>第4题
#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;
}
第5题#include<stdio.h>
#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);
}
}
第8题#include<stdio.h>
#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;
}
第11题#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#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 <SeatCount ; ++j)
{
if(NewPlane[i].Codes < NewPlane[j].Codes)
{
struct PlaneSeat M = NewPlane[i];
NewPlane[i] = NewPlane[j];
NewPlane[j] = M;
}
}
}
for(int i = 0 ; i < SeatCount ; ++i)
{
if(NewPlane[i].IsSaled == false);
printf("%d\n",NewPlane[i].Codes);
}
}
void SaleSeat(void)
{
struct PlaneSeat NewPlane[SeatCount];
int BufferSize = sizeof(struct PlaneSeat);
FILE* SeatFile;
int codes;
SeatFile = fopen(DatFile,"r"); //get data
for(int i = 0 ; i < SeatCount ; ++i)
fread(&(NewPlane[i]),BufferSize,1,SeatFile);
fclose(SeatFile);
printf("Please Which Seat Do You Want:"); //sale
scanf("%d",&codes);
--codes;
NewPlane[codes].IsSaled = true;
printf("What's Your First Name:");
scanf("%s",NewPlane[codes].FName);
printf("What's Your Last Name:");
scanf("%s",NewPlane[codes].LName);
getchar();
SeatFile = fopen(DatFile,"w+");
for(int i = 0 ; i < SeatCount ; ++i)
fwrite(&(NewPlane[i]),BufferSize,1,SeatFile);
fclose(SeatFile);
}
void DeleteSeat(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);
int codes;
printf("Please Which Seat Do You Want To Delete:");
scanf("%d",&codes);
getchar();
--codes;
NewPlane[codes].IsSaled = false;
strcpy(NewPlane[codes].FName,"None");
strcpy(NewPlane[codes].LName,"None");
SeatFile = fopen(DatFile,"w+");
for(int i = 0 ; i < SeatCount ; ++i)
fwrite(&(NewPlane[i]),BufferSize,1,SeatFile);
fclose(SeatFile);
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#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 Primer Plus 第十四章 编程练习 1-11题
- C primer plus(第五版)编程练习第十四章
- C Primer Plus 第十章 编程练习 1-11题
- C Primer Plus 第七章 编程练习 7-11题
- C Primer Plus 第二章 编程练习 1-7题
- C Primer Plus 第四章 编程练习 1-7题
- C Primer Plus 第五章 编程练习 1-8题
- C Primer Plus 第六章 编程练习 1-8题
- C Primer Plus 第七章 编程练习 1-6题
- C Primer Plus 第八章 编程练习 1-8题
- C Primer Plus 第九章 编程练习 1-9题
- C Primer Plus 第十三章 编程练习 1-13题
- C Primer Plus 第十一章 编程练习 1-15题
- C Primer Plus 第十二章 编程练习 1-8题
- C Primer Plus 第十六章 编程练习 1-7题
- C++ Primer Plus第六版 第十四章 编程练习答案
- C Primer Plus第二章编程练习
- c++primer plus第五章编程练习
- Leetcode 494. Target Sum Add to List
- python 核心编程 第九章
- python 调用 C/C++ 编程
- C Primer Plus 第十一章 编程练习 1-15题
- C Primer Plus 第十二章 编程练习 1-8题
- C Primer Plus 第十四章 编程练习 1-11题
- C Primer Plus 第十六章 编程练习 1-7题
- C++类成员变量是为什么选择private?
- maven项目管理
- Essential Qt 第一章 Hello Qt
- 冒泡排序实现及优化(Java)
- Essential Qt 第二章 界面布局
- Essential Qt 第三章 信号与槽
- 王道计算机网络--1.2 计算机网络体系结构与参考模型