在PC机上用C语言编程完成一个可实现成绩录入、管理的软件,包括图形操作界面,1.要求输入10个学生3门课的成绩,分别用子函数求出:(1)每个学生的平均分;(2)每门功课的平均分;(3)找出最高分所对应的学生和功课。 2.具备图形界面,且能方便地操作。
时间: 2024-02-05 13:13:16 浏览: 56
这是一个比较复杂的项目,需要使用一些库和工具来实现。以下是一个简单的实现思路:
1. 使用C语言中的结构体来存储学生信息和成绩信息。
2. 使用C语言中的文件操作函数将学生信息和成绩信息保存到文件中,以便下次打开软件时可以读取。
3. 使用C语言中的图形库(如SDL或OpenGL)来实现图形界面。
4. 在图形界面中使用文本框和按钮来输入和保存学生信息和成绩信息。
5. 在输入完毕后,使用子函数来计算每个学生的平均分和每门功课的平均分,并找出最高分所对应的学生和功课。
6. 在图形界面中显示计算结果。
以下是一个简单示例代码,使用SDL库实现图形界面,使用文件操作函数将学生信息和成绩信息保存到文件中:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#define MAX_STUDENTS 10
#define MAX_GRADES 3
typedef struct {
char name[20];
int grades[MAX_GRADES];
float average;
} Student;
Student students[MAX_STUDENTS];
void calculateAverages() {
int i, j;
float sum;
for (i = 0; i < MAX_STUDENTS; i++) {
sum = 0.0f;
for (j = 0; j < MAX_GRADES; j++) {
sum += students[i].grades[j];
}
students[i].average = sum / MAX_GRADES;
}
}
float calculateSubjectAverage(int subjectIndex) {
int i;
float sum = 0.0f;
for (i = 0; i < MAX_STUDENTS; i++) {
sum += students[i].grades[subjectIndex];
}
return sum / MAX_STUDENTS;
}
void findHighestGrade(int* studentIndex, int* subjectIndex) {
int i, j;
float max = 0.0f;
for (i = 0; i < MAX_STUDENTS; i++) {
for (j = 0; j < MAX_GRADES; j++) {
if (students[i].grades[j] > max) {
max = students[i].grades[j];
*studentIndex = i;
*subjectIndex = j;
}
}
}
}
void saveData() {
FILE* fp;
int i;
fp = fopen("data.bin", "wb");
if (fp == NULL) {
printf("Error: cannot open file\n");
return;
}
fwrite(students, sizeof(Student), MAX_STUDENTS, fp);
fclose(fp);
}
void loadData() {
FILE* fp;
int i;
fp = fopen("data.bin", "rb");
if (fp == NULL) {
printf("Error: cannot open file\n");
return;
}
fread(students, sizeof(Student), MAX_STUDENTS, fp);
fclose(fp);
}
int main(int argc, char* argv[]) {
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event event;
SDL_Rect nameRect, gradeRect, averageRect, buttonRect;
SDL_Surface* nameSurface, *gradeSurface, *averageSurface, *buttonSurface;
SDL_Texture* nameTexture, *gradeTexture, *averageTexture, *buttonTexture;
TTF_Font* font;
SDL_Color textColor = { 255, 255, 255 };
int i, j, subjectIndex, studentIndex;
char text[20];
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
font = TTF_OpenFont("arial.ttf", 16);
window = SDL_CreateWindow("Student Grades", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
loadData();
while (1) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
saveData();
exit(0);
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
nameRect.x = 10;
nameRect.y = 10;
nameSurface = TTF_RenderText_Solid(font, "Name", textColor);
nameTexture = SDL_CreateTextureFromSurface(renderer, nameSurface);
SDL_RenderCopy(renderer, nameTexture, NULL, &nameRect);
for (i = 0; i < MAX_STUDENTS; i++) {
gradeRect.x = 10;
gradeRect.y = 50 + i * 20;
sprintf(text, "Student %d", i+1);
gradeSurface = TTF_RenderText_Solid(font, text, textColor);
gradeTexture = SDL_CreateTextureFromSurface(renderer, gradeSurface);
SDL_RenderCopy(renderer, gradeTexture, NULL, &gradeRect);
for (j = 0; j < MAX_GRADES; j++) {
gradeRect.x = 100 + j * 50;
sprintf(text, "%d", students[i].grades[j]);
gradeSurface = TTF_RenderText_Solid(font, text, textColor);
gradeTexture = SDL_CreateTextureFromSurface(renderer, gradeSurface);
SDL_RenderCopy(renderer, gradeTexture, NULL, &gradeRect);
}
averageRect.x = 350;
averageRect.y = 50 + i * 20;
sprintf(text, "%.2f", students[i].average);
averageSurface = TTF_RenderText_Solid(font, text, textColor);
averageTexture = SDL_CreateTextureFromSurface(renderer, averageSurface);
SDL_RenderCopy(renderer, averageTexture, NULL, &averageRect);
}
for (j = 0; j < MAX_GRADES; j++) {
averageRect.x = 100 + j * 50;
averageRect.y = 50 + MAX_STUDENTS * 20;
sprintf(text, "%.2f", calculateSubjectAverage(j));
averageSurface = TTF_RenderText_Solid(font, text, textColor);
averageTexture = SDL_CreateTextureFromSurface(renderer, averageSurface);
SDL_RenderCopy(renderer, averageTexture, NULL, &averageRect);
}
findHighestGrade(&studentIndex, &subjectIndex);
sprintf(text, "Highest: Student %d, Subject %d", studentIndex+1, subjectIndex+1);
buttonSurface = TTF_RenderText_Solid(font, text, textColor);
buttonTexture = SDL_CreateTextureFromSurface(renderer, buttonSurface);
SDL_QueryTexture(buttonTexture, NULL, NULL, &buttonRect.w, &buttonRect.h);
buttonRect.x = 10;
buttonRect.y = 50 + MAX_STUDENTS * 20 + 20;
SDL_RenderCopy(renderer, buttonTexture, NULL, &buttonRect);
SDL_RenderPresent(renderer);
SDL_FreeSurface(nameSurface);
SDL_FreeSurface(gradeSurface);
SDL_FreeSurface(averageSurface);
SDL_FreeSurface(buttonSurface);
SDL_DestroyTexture(nameTexture);
SDL_DestroyTexture(gradeTexture);
SDL_DestroyTexture(averageTexture);
SDL_DestroyTexture(buttonTexture);
}
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
```
这段代码使用SDL库实现图形界面,使用TTF库实现文本渲染。在程序运行时,读取之前保存的学生信息和成绩信息文件,并在窗口中显示出来。用户可以在文本框中输入新的成绩信息,并按下保存按钮保存到文件中。在计算完平均分和最高分后,程序会在窗口中显示出来。用户可以在窗口的关闭按钮上按下退出程序,并在退出之前将当前学生信息和成绩信息保存到文件中。
阅读全文