结构体练习

输入年月日,计算这一天是这一年的第几天

结构体定义

typedef struct Date
{
	int Year;
	int Month;
	int Day;
}Date;

求总共天数的函数实现

int All_Days(Date* pdate)
{
	assert(pdate != nullptr);
	int i = 0, sum = 0, num = 0;
	for (i = 1; i < pdate->Month; ++i)
	{
		switch (i)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			num = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			num = 30;
			break;
		default:
		{

			if (Is_Leap_Year(pdate))
			{
				num = 29;
			}
			else
			{
				num = 28;
			}
			break;
		}
		}
		sum += num;
	}
	return (sum += pdate->Day);
}

这里进行求和的方法多种多样,也可以把每个月的天数写成数组形式来进行访问。

判断是否为闰年

bool Is_Leap_Year(Date* pdate)
{
	return ((pdate->Year % 4 == 0 && pdate->Year % 100 != 0) || (pdate->Year % 400 == 0));
}

输入示例

主函数

int main(void)
{
	Date date;
	int days = 0;
	printf("Please input year, month and day:\n");
	scanf_s("%d %d %d", &date.Year, &date.Month, &date.Day);
	days = All_Days(&date);
	printf("This day is the Day %3d.\n", days);
	return 0;
}

运行结果:
平年结果
在这里插入图片描述
闰年结果
在这里插入图片描述

打印一个学生的成绩数组

结构体定义

typedef struct Student
{
	int num;
	char name[20];
	float score[3];
}Student;

输入函数

void Input(Student* pstud)
{
	assert(pstud != nullptr);
	int i = 0, j = 0;
	Student* p = pstud;
	for (i = 0; i < 5; ++i)
	{
		printf("Please input NO.%d student information:\n", i + 1);
		printf("Num: ");
		scanf_s("%d", &p[i].num);
		printf("Name: ");
		scanf_s("%s", p[i].name, 20);
		printf("Score: ");
		for (j = 0; j < 3; ++j)
			scanf_s("%f", &p[i].score[j]);
		printf("\n");
	}
	printf("\n");
}

输出函数

void Print(Student* pstud)
{
	assert(pstud != nullptr);
	int i = 0, j = 0;
	Student* p = pstud;
	for (printf("Print Result:\n\n"), i = 0; i < 5; ++i)
	{
		printf("Num   :%3d\n", p[i].num);
		printf("Name  : %-10s\n", p[i].name, 20);
		for (j = 0; j < 3; ++j)
		{
			printf("Score%d:%5.2f\n", i + 1, p[i].score[j]);
		}
		printf("\n");
	}
}

输出示例

主函数

int main(void)
{
	Student Stud[5];
	Input(Stud);
	Print(Stud);
	
	printf("\tProgram Exit!\n");
	return 0;
}

运行结果

输入
在这里插入图片描述

输出

Print Result:

Num   :  1
Name  : Srh
Score1:100.00
Score1:99.00
Score1:86.00

Num   :  2
Name  : zzc
Score2:87.00
Score2:96.00
Score2:100.00

Num   :  3
Name  : sauron
Score3:100.00
Score3:90.00
Score3:75.00

Num   :  4
Name  : adfee
Score4:77.00
Score4:96.00
Score4:85.00

Num   :  5
Name  : liu
Score5:100.00
Score5:100.00
Score5:100.00

        Program Exit!
### C语言结构体练习题目 #### 题目一:学生信息管理系统 创建一个程序来管理学生的个人信息。每个学生的信息应包括学号、姓名、性别和三门课程的成绩。 ```c #include <stdio.h> #include <string.h> // 定学生结构体类型并简化名称为STUDENT typedef struct { int id; char name[50]; char gender; float scores[3]; } STUDENT; int main() { // 创建两个学生对象,并初始化其成员变量 STUDENT s1 = {1, "Alice", 'F', {90.5f, 87.0f, 92.0f}}; STUDENT s2 = {2, "Bob", 'M', {85.0f, 88.5f, 91.0f}}; // 打印第一个学生的信息 printf("Student ID:%d\nName:%s\nGender:%c\nScores:[%.1f, %.1f, %.1f]\n", s1.id, s1.name, s1.gender, s1.scores[0], s1.scores[1], s1.scores[2]); // 修改第二个学生的某项成绩 s2.scores[1] += 5.0f; // 再次打印修改后的第二位同学的成绩 printf("\nAfter updating the second subject's score of Student 2:\n"); printf("New Scores:[%.1f, %.1f, %.1f]", s2.scores[0], s2.scores[1], s2.scores[2]); return 0; } ``` 此段代码展示了如何定`struct`以及通过`.`操作符访问其中的数据字段[^2]。 #### 题目二:图书库存记录系统 设计一个简单的图书库房管理系统,每本书籍应该具有书名、作者名字、出版年份三个属性;另外还需要支持增加新书籍到列表里去的功能。 ```c #include <stdio.h> #include <stdlib.h> #define MAX_BOOKS 100 // 使用typedef关键字让后续声明更简洁 typedef struct BookRecord { char title[100]; char author[50]; unsigned short yearPublished; } BOOK_RECORD; void addBook(BOOK_RECORD books[], size_t *count); int main(void) { BOOK_RECORD library[MAX_BOOKS]; size_t bookCount = 0; while (bookCount < MAX_BOOKS && getchar() != '\n') { addBook(library, &bookCount); } for (size_t i = 0; i < bookCount; ++i) { printf("%zu: Title=%s Author=%s Year=%hu\n", i + 1, library[i].title, library[i].author, library[i].yearPublished); } } void addBook(BOOK_RECORD books[], size_t *count){ puts("Enter new book details:"); scanf_s("%99s%49s%hu",books[*count].title,sizeof(books[*count].title), books[*count].author,sizeof(books[*count].author),&books[*count].yearPublished); (*count)++; } ``` 上述示例演示了怎样利用数组存储多个相同类型的结构实例,并提供了向该集合添加元素的方法[^3]。 #### 题目三:员工工资计算工具 编写一段可以用来帮助公司人事部门快速统计每位职员月度薪资总额的小型应用程序。假设每一位职工都有基本月薪、奖金比例两项参数可供设置。 ```c #include <stdio.h> // 员工信息结构体 typedef struct EmployeeInfo { char empID[10]; /* 工作证号码 */ double baseSalary; /* 底薪 */ double bonusRate; /* 加班费比率 */ } EMPLOYEE_INFO; double calculateTotalPay(const EMPLOYEE_INFO*); int main(){ EMPLOYEE_INFO employee = {"E001", 6000.0, 0.1}; printf("Employee %s total pay this month is $%.2lf.\n",employee.empID,calculateTotalPay(&employee)); return EXIT_SUCCESS; } /* 计算总薪酬函数 */ double calculateTotalPay(const EMPLOYEE_INFO* einfo){ const static double OVERTIME_HOURS = 20.0; return einfo->baseSalary+(einfo->bonusRate*einfo->baseSalary)*OVERTIME_HOURS; } ``` 这段源码说明了当涉及到复杂业务逻辑时,可以通过封装成独立功能模块的方式来提高可读性和维护性[^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_索伦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值