【C 程序设计】(第四版)谭浩强 著第九章习题

本文详细解答了谭浩强《C程序设计》第四版中第九章的习题,使用Dev-C编译器进行编译和调试。内容涵盖C语言的基本语法和编程技巧,旨在帮助读者深入理解和应用所学知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编译器:Dev-c

/*第1题*/
#include <stdio.h>
struct date{
	int year;
	int month;
	int day;
};

int main(int argc, char const *argv[])
{
	struct date today;
	scanf("%d %d %d", &today.year, &today.month, &today.day);
	int total = 0, i;
	int b[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if (today.month == 1)
		printf("今天是今年的第 %d 天\n", today.day);
	else
	{
		if ( (today.year%4 == 0 && today.year %100 !=0) || today.year%400 == 0)
		{
			b[1] = 29;
			for (i = 0; i < today.month - 1; i++)
				total += b[i];
			printf("今天是第 %d 天\n", total + today.day);
		}else{
			for (i = 0; i < today.month - 1; i++)
				total += b[i];
			printf("今天是第 %d 天\n", total + today.day);
		}
	}
	return 0;
}

/*第2题*/
#include <stdio.h>
struct date{
	int year;
	int month;
	int day;
};

int days(struct date a);

int main(int argc, char const *argv[])
{
	struct date today;
	scanf("%d %d %d", &today.year, &today.month, &today.day);
	int x = days(today);
	printf("今天是今年的第 %d 天\n", x);
	return 0;
}

int days(struct date a)
{
	int total = 0, i;
	int b[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if (a.month == 1)
		printf("今天是今年的第 %d 天\n", a.day);
	else
	{
		if ( (a.year%4 == 0 && a.year %100 !=0) || a.year%400 == 0)
		{
			b[1] = 29;
			for (i = 0; i < a.month - 1; i++)
				total += b[i];
		}else{
			for (i = 0; i < a.month - 1; i++)
				total += b[i];
		}
	}
	return total;
}
/*第3、4题*/
#include <stdio.h>
struct Student{
	int num;
	char name[20];
	int score[3];
}stu[5];

int N = 5;

void input(struct Student stu[]);	//输入数据
void print(struct Student stu[]);	//打印数据

int main(int argc, char const *argv[])
{
	input(stu);
	print(stu);
	return 0;
}

void input(struct Student stu[])
{
	int i;
	printf("请依次输入各个学生的学号,姓名,三科成绩:\n");
	for (i = 0; i < 5; i++)
		scanf("%d %s %d %d %d", &stu[i].num, &stu[i].name, 
				&stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
}

void print(struct Student stu[])
{
	int i;
	for (i = 0; i < 5; i++)
	{
		printf("num   name  score1  score2  score3\n");
		printf("%-6d%-6s%-8d%-8d%-8d\n", stu[i].num, stu[i].name, 
				stu[i].score[0], stu[i].score[1], stu[i].score[2]);
	}
}
/*第5题*/
#include <stdio.h>
struct Student{
	int num;
	char name[20];
	int score[3];
	int average;
}stu[10];

int N = 10;

void input(struct Student stu[]);	//输入数据
void print(struct Student stu[]);	//打印平均成绩
void max(struct Student stu[]);		//输出最高同学的数据

int main(int argc, char const *argv[])
{
	input(stu);
	print(stu);
	max(stu);
	return 0;
}

void input(struct Student stu[])
{
	int i;
	printf("请依次输入各个学生的学号,姓名,三科成绩:\n");
	for (i = 0; i < N; i++)
	{
		scanf("%d %s %d %d %d", &stu[i].num, &stu[i].name, 
				&stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
		stu[i].average = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3;
	}
}

void print(struct Student stu[])
{
	int i;
	printf("num   name  average\n");
	for (i = 0; i < N; i++)
		printf("%-6d%-6s%-8d\n", stu[i].num, stu[i].name, stu[i].average);
}

void max(struct Student stu[])
{
	int i, m = 0;
	for (i = 0; i < N; i++)
		if (stu[i].average > stu[m].average)
			m = i;
	printf("\n成绩最好的同学:\n");
	printf("num   name  score1  score2  score3  average\n");
	printf("%-6d%-6s%-8d%-8d%-8d%-8d", stu[m].num, stu[m].name, stu[m].score[0],
			stu[m].score[1], stu[m].score[2], stu[m].average);
}

待添加…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值