基础数据结构7:上机题

这篇博客包含了三个不同的C语言程序示例。第一个程序计算给定日期的总天数,考虑了闰年的情况。第二个程序处理学生信息输入与输出,包括姓名、学号和三门课程的成绩,并计算平均分和最高分。最后一个程序将标准输入中的小写字母转换为大写并写入文件,同时提供了两个文件内容的合并操作,按字典序排列。
摘要由CSDN通过智能技术生成

在这里插入图片描述

//1 2
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

typedef struct Day
{
	int year;
	int month;
	int days;
}Day;

int Get_days(Day* Pday)//传递结构体指针或者结构体普通变量都可以
{
	assert(Pday != NULL);
	int dayofMonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

	if ((Pday->year % 4 == 0 && Pday->year % 100 != 0) || (Pday->year % 400 == 0))
		dayofMonth[1] = 29;
	int count = 0;
	switch (Pday->month)
	{
	case 12:count += dayofMonth[10];
	case 11:count += dayofMonth[9];
	case 10:count += dayofMonth[8];
	case 9:count += dayofMonth[7];
	case 8:count += dayofMonth[6];
	case 7:count += dayofMonth[5];
	case 6:count += dayofMonth[4];
	case 5:count += dayofMonth[3];
	case 4:count += dayofMonth[2];
	case 3:count += dayofMonth[1];
	case 2:count += dayofMonth[0];
	case 1:count += 0; break;
	default:
		return -1;
		break;
	}
	count += Pday->days;
	return count;
}

int main()
{
	Day dy = { 2021,7,28 };
	int  days = Get_days(&dy);
	printf("%d\n", days);

	Student stu[3];
	int len = sizeof(stu) / sizeof(stu[0]);

	return 0;
}
//3 4 5
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

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

void Input(Student* arr, int len)
{
	assert(arr != NULL);
	for (int i = 0; i < len; i++)
	{
		scanf_s("%s %s %f %f %f\n", &arr[i].num, &arr[i].name, arr[i].score[0], arr[i].score[1], arr[i].score[2]);
	}
}
void Print(Student* arr, int len)
{
	assert(arr != NULL);
	for (int i = 0; i < len; i++)
	{
		printf("%s ", arr[i].num);
		printf("%s ", arr[i].name);
		printf("%s ", arr[i].score[0]);
		printf("%s ", arr[i].score[1]);
		printf("%s ", arr[i].score[2]);
	}
	printf("\n");
}
void StudentScore(struct Student arr[], int len)
{
	assert(arr != NULL);
	double total[3] = { 0 };//保存所有学生的单独每门课的总成绩
	double* score = (double*)malloc(sizeof(double) * len);//保存每个学生三门课的总成绩
	assert(score != NULL);

	for (int i = 0; i < len; i++)
	{
		total[0] += arr[i].score[0];
		total[1] += arr[i].score[1];
		total[2] += arr[i].score[2];
		score[i] = arr[i].score[0] + arr[i].score[1] + arr[i].score[2];
	}
	printf("第一门课avg:%f\n", total[0] / len);
	printf("第二门课avg:%f\n", total[1] / len);
	printf("第三门课avg:%f\n", total[2] / len);

	double highscore = 0;
	for (int i = 0; i < len; i++)
	{
		if (score[i] > highscore)
		{
			highscore = score[i];
		}
	}
	for (int i = 0; i < len; i++)
	{
		if (score[i] = highscore)
		{
			printf("%s ", arr[i].num);
			printf("%s ", arr[i].name);
			printf("%f ", arr[i].score[0]);
			printf("%f ", arr[i].score[1]);
			printf("%f ", arr[i].score[2]);
			printf("总成绩=%f\n", score[i] / 3);
		}
	}
}
int main()
{
	Student stu[3];
	int len = sizeof(stu) / sizeof(stu[0]);
	Input(stu, len);
	Print(stu, len); 

	return 0;
}
//3 4
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<ctype.h>
void ShowToUpper()
{
	char* path = "D:\\test.txt";
	FILE* fw = fopen(path, "wb");
	assert(fw != NULL);

	char ch;
	while ((ch = getchar()) != '!')
	{
		if (islower(ch))
		{
			ch = toupper(ch);
		}
		fwrite(&ch, sizeof(char), 1, fw);
	}
	fclose(fw);
}
void Merge(const char* pathA, const char* pathB, const char* pathC)
{
	//assert
	FILE* fw = fopen(pathC, "w");
	FILE* frA = fopen(pathC, "r");
	FILE* frB = fopen(pathC, "r");

	char ch_a;
	char ch_b;
	int len1 = 0;//通过从fread从文件中读取完整的项目个数
	int len2 = 0;

	len1 = fread(&ch_a, sizeof(char), 1, frA);
	len2 = fread(&ch_b, sizeof(char), 1, frB);

	while (len1 > 0 && len2 > 0)//两个文件都有数据
	{
		if (ch_a <= ch_b)
		{
			fwrite(&ch_a, sizeof(char), 1, fw);
			len1 = fread(&ch_a, sizeof(char), 1, frA);

		}
		else
		{
			fwrite(&ch_b, sizeof(char), 1, fw);
			len2 = fread(&ch_b, sizeof(char), 1, frB);
		}
	}

	while (len1 > 0)//A文件没读取完,B读取完了
	{
		fwrite(&ch_a, sizeof(char), 1, fw);
		len1 = fread(&ch_a, sizeof(char), 1, frA);
	}
	while (len2 > 0)
	{
		fwrite(&ch_b, sizeof(char), 1, fw);
		len2 = fread(&ch_b, sizeof(char), 1, frB);
	}

	fclose(fw);
	fclose(frA);
	fclose(frB);
}
int main()
{
	ShowToUpper();

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值