《C Primer Plus》(第6版)编程练习——第14章

第1题

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int days(char* st);

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}
};

int main(void)
{
	char input[20];
	int daytotal;

	printf("Enter the name of a month: ");
	while (scanf("%s", input) == 1 && input[0] != 'q')
	{
		daytotal = days(input);
		if (daytotal > 0)
			printf("There are %d days through %s.\n", daytotal, input);
		else
			printf("%s is not valid input.\n", input);		
		printf("Next month (q to quit): ");
	}
	puts("Bye.");

	return 0;
}

int days(char* st)
{
	int total = 0;
	int mon_num = 0;
	int i;

	for (i = 0; st[i] != '\0'; i++)
	{
		if (i == 0)
			st[i] = toupper(st[i]);
		else
			st[i] = tolower(st[i]);
	}
	for (i = 0; i < 12; i++)
	{
		if (strcmp(st, months[i].name) == 0)
		{
			mon_num = months[i].monumb;
			break;
		}
	}
	if (mon_num == 0)
		return -1;
	for (i = 0; i < mon_num; i++)
		total += months[i].days;

	return total;
}

第2题

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int days(int, int, int);
void eatline(void);

struct month {
	char name[10];
	char abbrev[4];
	int days;
	int monumb;
};

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}
};

int main(void)
{
	int year;
	int month;            // 数字形式的月份号
	int day;
	int daytotal;
	char st_month[10];    // 字符串形式的月份名或月份名缩写
	int i;

	printf("Enter the year: ");
	while (scanf("%d", &year) == 1)
	{
		eatline();
		month = 0;
		printf("Enter the month: ");
		while (scanf("%d", &month) != 1)   // 数字亦可以字符串的形式接收,因此优先以整型接收
		{
			scanf("%s", st_month);
			// 统一格式
			st_month[0] = toupper(st_month[0]);
			for (i = 1; st_month[i] != '\0'; i++)
				st_month[i] = tolower(st_month[i]);
			// 确认月份号
			for (i = 0; i < 12; i++)
			{
				if (strcmp(st_month, months[i].name) == 0 || strcmp(st_month, months[i].abbrev) == 0)
				{
					month = months[i].monumb;
					break;
				}
			}
			// 判断月份是否成功输入
			if (month == 0)
			{
				printf("%s is not valid input. Enter again.\n", st_month);
				printf("Enter the month: ");
				continue;
			}
			else
				break;
		}
		eatline();
		printf("Enter the day: ");
		scanf("%d", &day);
		daytotal = days(year, month, day);
		printf("There are %d days through %d/%d (d/m) in %d.\n", daytotal, day, month, year);
		printf("Enter next year (q to quit): ");
	}
	puts("Bye.");

	return 0;
}

int days(int y, int m, int d)
{
	int total = 0;
	int i;
	
	if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)  // 判断是否闰年
		months[1].days = 29;
	for (i = 0; i < m - 1; i++)
		total += months[i].days;
	total += d;
	months[1].days = 28;  // 恢复非闰年的数据

	return total;
}

void eatline(void)
{
	while (getchar() != '\n')
		continue;
}

第3题

#include <stdio.h>
#include <string.h>
char* s_gets(char* st, int n);
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100

struct book {
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

void sortt(struct book* bk[], int n);
void sortv(struct book* bk[], int n);

int main(void)
{
	struct book library[MAXBKS];
	struct book* pbk[MAXBKS];  // 用指针数组的优点,一是不用改变原结构数组,二是更省存储空间
	int count = 0;
	int index;

	printf("Please enter the book title.\n");
	printf("Press [enter] at the start of a line to stop.\n");
	while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')
	{
		printf("Now enter the author.\n");
		s_gets(library[count].author, MAXAUTL);
		printf("Now enter the value.\n");
		scanf("%f", &library[count].value);
		pbk[count] = &library[count];
		count++;
		while (getchar() != '\n')
			continue;
		if (count < MAXBKS)
			printf("Enter the next title.\n");
	}

	if (count > 0)
	{
		printf("\nHere 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);

		sortt(pbk, count);
		printf("\nHere is the list of your books sorted by title:\n");
		for (index = 0; index < count; index++)
			printf("%s by %s: $%.2f\n", pbk[index]->title, pbk[index]->author, pbk[index]->value);

		sortv(pbk, count);
		printf("\nHere is the list of your books sorted by value:\n");
		for (index = 0; index < count; index++)
			printf("%s by %s: $%.2f\n", pbk[index]->title, pbk[index]->author, pbk[index]->value);
	}
	else
		printf("No books? Too bad.\n");

	return 0;
}

void sortt(struct book* bk[], int n)
{
	struct book* temp;
	int i, j;

	for (i = 0; i < n - 1; i++)
		for (j = i + 1; j < n; j++)
			if (strcmp(bk[i]->title, bk[j]->title) > 0)
			{
				temp = bk[i];
				bk[i] = bk[j];
				bk[j] = temp;
			}
}

void sortv(struct book* bk[], int n)
{
	struct book* temp;
	int i, j;

	for (i = 0; i < n - 1; i++)
		for (j = i + 1; j < n; j++)
			if (bk[i]->value > bk[j]->value)
			{
				temp = bk[i];
				bk[i] = bk[j];
				bk[j] = temp;
			}
}

char* s_gets(char* st, int n)
{
	char* ret_val;
	char* find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');
		if (find)
			*find = '\0';
		else
			while (getchar() != '\n')
				continue;
	}

	return ret_val;
}

第4题

// 4a 方式一:传递结构的地址
#include <stdio.h>
#include <string.h>
#define LEN 20

struct s_insurance {
	unsigned long id;
	struct {
		char first[LEN];
		char middle[LEN];
		char last[LEN];
	};
};
void a_showinfo(const struct s_insurance st[], int n);

int main(void)
{
	struct s_insurance person[5] = {
		{ 302039823, { "Dribble", "Mack", "Flossie"} },
		{ 302039824, { "Ima", "", "Pundit"} },
		{ 302039825, { "Prince", "Nikoli", "Buvan"} },
		{ 302039826, { "Stephen", "", "Prata"} },
		{ 302039827, { "Filmore", "", "Walletz"} }
	};

	a_showinfo(person, 5);
	
	return 0;
}

void a_showinfo(const struct s_insurance st[], int n)
{
	int i;

	for (i = 0; i < n; i++)
	{
		printf("%s, %s ", st[i].first, st[i].last);
		if (strcmp(st[i].middle, "") != 0)
			printf("%c. ", st[i].middle[0]);
		printf("-- %lu\n", st[i].id);
	}
}
// 4b 方式二:传递结构的值
#include <stdio.h>
#include <string.h>
#define LEN 20

struct s_insurance {
	unsigned long id;
	struct {
		char first[LEN];
		char middle[LEN];
		char last[LEN];
	};
};
void b_showinfo(const struct s_insurance st);

int main(void)
{
	struct s_insurance person[5] = {
		{ 302039823, { "Dribble", "Mack", "Flossie"} },
		{ 302039824, { "Ima", "", "Pundit"} },
		{ 302039825, { "Prince", "Nikoli", "Buvan"} },
		{ 302039826, { "Stephen", "", "Prata"} },
		{ 302039827, { "Filmore", "", "Walletz"} }
	};
	int index;

	for (index = 0; index < 5; index++)
		b_showinfo(person[index]);
		
	return 0;
}

void b_showinfo(const struct s_insurance st)
{
	printf("%s, %s ", st.first, st.last);
	if (strcmp(st.middle, "") != 0)
		printf("%c. ", st.middle[0]);
	printf("-- %lu\n", st.id);
}

第5题

#include <stdio.h>
#include <string.h>
#define LEN 14
#define SCORES 3
#define CSIZE 4

struct name {
	char first[LEN];
	char last[LEN];
};

struct student {
	struct name handle;
	float grade[SCORES];
	double mean;
};

void get_scores(struct student st[], int n);
void find_means(struct student st[], int n);
void show_class(const struct student st[], int n);
void show_ave(const struct student st[], int n);

int main(void)
{
	struct student person[CSIZE] = {
		{ "Flip", "Snide"},
		{ "Clare", "Voyans"},
		{ "Bingo", "Higgs"},
		{ "Fawn", "Hunter"}
	};

	get_scores(person, CSIZE);
	find_means(person, CSIZE);
	show_class(person, CSIZE);
	show_ave(person, CSIZE);

	return 0;
}

void get_scores(struct student st[], int n)
{
	int i, j;

	for (i = 0; i < n; i++)
	{
		printf("Please enter %d scores for %s %s:\n", SCORES, st[i].handle.first, st[i].handle.last);
		for (j = 0; j < SCORES; j++)
		{
			while (scanf("%f", &st[i].grade[j]) != 1)
			{
				scanf("%*s");         // 处理非数值输入
				puts("Please use numeric input.");
			}
		}
	}
}

void find_means(struct student st[], int n)
{
	int i, j;
	float sum;

	for (i = 0; i < n; i++)
	{
		for (sum = 0, j = 0; j < SCORES; j++)
			sum += st[i].grade[j];
		st[i].mean = sum / SCORES;
	}
}

void show_class(const struct student st[], int n)
{
	int i, j;
	char wholename[2 * LEN];

	for (i = 0; i < n; i++)
	{
		// 存储姓名,方便格式打印
		strcpy(wholename, st[i].handle.first);
		strcat(wholename, " ");
		strcat(wholename, st[i].handle.last);
		// 打印
		printf("%*s: ", 2 * LEN - 1, wholename);
		for (j = 0; j < SCORES; j++)
			printf("%6.1f ", st[i].grade[j]);
		printf(" Average = %5.2f\n", st[i].mean);
	}
}

void show_ave(const struct student st[], int n)
{
	int i, j;
	double total;

	printf("\n%*s ", 2 * LEN, "QUIZ AVERAGES");
	for (j = 0; j < SCORES; j++)
	{
		for (total = 0, i = 0; i < n; i++)
			total += st[i].grade[j];
		printf("%6.1f ", total / n);
	}
}

第6题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 14
#define NUM 19

struct player {
	unsigned id;
	char fname[LEN];
	char lname[LEN];	
	unsigned times;     // 上场次数
	unsigned hits;      // 击中数
	unsigned running;   // 走垒数
	unsigned rbi;       // 打点
	double baverage;    // 安打率
};

int main(void)
{
	struct player person[NUM];
	struct player temp;
	int index;
	int count[NUM] = { 0 };
	FILE* fp;
	char wholename[2 * LEN];

	// 初始化
	for (index = 0; index < NUM; index++)
	{
		person[index].id = index;
		person[index].times = 0;
		person[index].hits = 0;
		person[index].running = 0;
		person[index].rbi = 0;
		person[index].baverage = 0;
	}
	// 打开文件
	if ((fp = fopen("softball.txt", "r")) == NULL)
	{
		fputs("Can't open softball.txt\n", stderr);
		exit(EXIT_FAILURE);
	}
	// 读取数据并存储在结构数组中
	rewind(fp);
	printf("Reading data from softball.txt...");
	while (fscanf(fp, "%u", &temp.id) == 1)
	{
		if (count[temp.id] == 0)
			fscanf(fp, "%s%s", person[temp.id].fname, person[temp.id].lname);
		else
			fscanf(fp, "%*s%*s");
		fscanf(fp, "%u %u %u %u", &temp.times, &temp.hits, &temp.running, &temp.rbi);
		person[temp.id].times += temp.times;
		person[temp.id].hits += temp.hits;
		person[temp.id].running += temp.running;
		person[temp.id].rbi += temp.rbi;
		count[temp.id]++;
	}
	printf("finished!\n");
	// 计算安打率
	for (index = 0; index < NUM; index++)
		if (count[index] > 0 && person[index].times > 0)
			person[index].baverage = 1.0 * person[index].hits / person[index].times;
	// 显示结果
	printf("The cumulative figures for the players are as follows:\n");
	printf("\nNo. %-*s Times Hits Running RBI Batting-average\n", 2 * LEN - 1, "Name");
	for (index = 0; index < NUM; index++)
	{
		strcpy(wholename, person[index].fname);
		strcat(wholename, " ");
		strcat(wholename, person[index].lname);
		printf("%-2u  %-*s %-5u %-4u %-7u %-3u %-15.2g\n", 
			person[index].id, 2 * LEN - 1, wholename, person[index].times, 
			person[index].hits, person[index].running, person[index].rbi, person[index].baverage);
	}
	// 关闭文件
	if (fclose(fp) != 0)
		fprintf(stderr, "Error closing softball.txt\n");

	return 0;
}

第7题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ava实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),可运行高分资源 Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值