任务描述
设计开发一个程序:能输入一个班学生某门课程的成绩信息,包括学号、姓名、平时成绩、考试成绩,然后通过计算得到总评成绩(=20%×平时成绩+80%×考试成绩),能够将这些信息存入文本文件;能够从文件中读取全部学生信息和查询总评成绩最低和最高的学生信息,并完整无误地显示出来。
测试用例
学生人数N=3
学生0的学号,姓名,平时成绩,考试成绩(空格分隔):
1900310101 张无忌 85 94
学生1的学号,姓名,平时成绩,考试成绩(空格分隔):
1900310102 周润发 78.5 60.4
学生2的学号,姓名,平时成绩,考试成绩(空格分隔):
1900320104 王杰 68 89.5
全部学生、总评最低和最高学生信息如下:
全部学生:
学号=1900310101,姓名=张无忌,平时成绩=85.00,考试成绩=94.00,总评成绩=92.20
学号=1900310102,姓名=周润发,平时成绩=78.50,考试成绩=60.40,总评成绩=64.02
学号=1900320104,姓名=王杰,平时成绩=68.00,考试成绩=89.50,总评成绩=85.20
最低分学生:
学号=1900310102,姓名=周润发,平时成绩=78.50,考试成绩=60.40,总评成绩=64.02
最高分学生:
学号=1900310101,姓名=张无忌,平时成绩=85.00,考试成绩=94.00,总评成绩=92.20
代码实现
//main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include<string.h>
#include "student.h"
int main()
{
Student* stus;
char path[1024];
int N;
getcwd(path, 1024);
strcat(path, "\\data.txt");
//输入学生人数
printf("学生人数N=");
scanf("%d", &N);
//创建动态结构体数组
stus = (Student*)malloc(sizeof(Student) * N);
//输入各学生学号,姓名,某门课平时、考试成绩(这些字段的值之间用英
//文逗号隔开),总评成绩根据平时、考试分别占0.2,0.8比例自动计算
//输入完一个学生后,回车即可输入下一个学生信息
inputInformation(stus, N);
//把学生成绩记录存入文件,存入文件后关闭文件
saveInformation(stus, N, path);
//释放内存
free(stus);
//从文件中读取学生成绩记录,输出最低、最高总评成绩对应的学生完整信息
printf("全部学生、总评最低和最高学生信息如下:\n");
outputInformation(path);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
//student.cpp
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include "student.h"
//请在下面给出程序中所用到的自定义函数的完整定义
void inputInformation(Student* stus, int N)
{
assert(NULL != stus);
for (int i = 0; i < N; i++)
{
printf("学生%d的学号,姓名,平时成绩,考试成绩(空格分隔):\n", i);
scanf("%s", (stus + i)->stu_number);
scanf("%s", (stus + i)->stu_name);
scanf("%f", &(stus + i)->usual_mark);
scanf("%f", &(stus + i)->exam_mark);
(stus + i)->overall_mark = (float)0.2 * (stus + i)->usual_mark + (float)0.8 * (stus + i)->exam_mark;
}
}
void saveInformation(Student* stus, int N, char* filename)
{
assert(NULL != stus);
assert(NULL != filename);
FILE* pfile = NULL;
pfile = fopen(filename, "wb");
if (NULL == pfile)
{
return;
}
for (int i = 0; i < N; i++)
{
fprintf(pfile, "%s %s %f %f %f\n", (stus + i)->stu_number, (stus + i)->stu_name,
(stus + i)->usual_mark, (stus + i)->exam_mark, (stus + i)->overall_mark);
}
fclose(pfile);
pfile = NULL;
}
void outputInformation(char* filename)
{
assert(NULL != filename);
Student* p = NULL;
Student* stus = (Student*)malloc(sizeof(Student) * 100);
if (NULL == stus)
{
return;
}
FILE* pfile = fopen(filename, "rb");
if (NULL == pfile)
{
return;
}
printf("全部学生:\n");
fscanf(pfile, "%s %s %f %f %f", stus->stu_number, stus->stu_name, &stus->usual_mark, &stus->exam_mark, &stus->overall_mark);
int i = 0;
int flag = 1;
while (!feof(pfile))
{
i++;
fscanf(pfile, "%s %s %f %f %f", (stus + i)->stu_number, (stus + i)->stu_name, &(stus + i)->usual_mark, &(stus + i)->exam_mark, &(stus + i)->overall_mark);
}
for (int j = 0; j < i; j++)
{
printf("学号=%s,姓名=%s,平时成绩=%.2f,考试成绩=%.2f,总评成绩=%.2f\n", (stus + j)->stu_number, (stus + j)->stu_name, (stus + j)->usual_mark, (stus + j)->exam_mark, (stus + j)->overall_mark);
}
fclose(pfile);
pfile = NULL;
float min = (stus + 0)->overall_mark;
float max = (stus + 0)->overall_mark;
Student* MAX = (stus + 0);
Student* MIN = (stus + 0);
for (int j = 1; j < i; j++)
{
if (min > (stus + j)->overall_mark)
{
min = (stus + j)->overall_mark;
MIN = (stus + j);
}
}
printf("最低分学生:\n");
printf("学号=%s,姓名=%s,平时成绩=%.2f,考试成绩=%.2f,总评成绩=%.2f\n", MIN->stu_number, MIN->stu_name, MIN->usual_mark, MIN->exam_mark, MIN->overall_mark);
for (int j = 1; j < i; j++)
{
if (max < (stus + j)->overall_mark)
{
max = (stus + j)->overall_mark;
MAX = (stus + j);
}
}
printf("最高分学生:\n");
printf("学号=%s,姓名=%s,平时成绩=%.2f,考试成绩=%.2f,总评成绩=%.2f", MAX->stu_number, MAX->stu_name, MAX->usual_mark, MAX->exam_mark, MAX->overall_mark);
}
//student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
typedef struct student
{
char stu_number[20];
char stu_name[20];
float usual_mark;
float exam_mark;
float overall_mark;
}Student;
void inputInformation(Student* stus, int N);
void saveInformation(Student* stus, int N, char* filename);
void outputInformation(char* filename);
#endif