标签:math close ice shadow malloc name 查找 putchar temp
该小项目为共分为5个单元─────────────────────────────
①main.c
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include "Grade.h"
int main()
{
int p;
char choice, item;
PERSON person;
GRADE grade;
char filename[100] = "MyGrade.txt";
GradeCreate(&grade);
GradeRead(filename, &grade);
system("color 1f");
do
{
system("cls");
printf("A-添加 S-显示 F-查找 M-修改 Q-退出 > ");
scanf(" %c", &choice);
choice = toupper(choice);
switch (choice)
{
case ‘A‘:
printf("添加\n");
PersonInput(&person);
GradeAppend(&grade,&person);
break;
case ‘F‘:
item = SelectCol();
if (item != 0)
{
while (getchar() != ‘\n‘);
PersonInput(&person);
p = GradeFind(&grade,&person);
SelectAll();
if (p >= 0)
{
GradeOutputPerson(&grade,p);
}
else
{
printf("Not found!\n ");
}
}
printf("Press any key to continue...");
getch();
break;
case ‘M‘:
printf("修改\n");
printf("查找:");
item = SelectCol();
if (item != 0)
{
while (getchar() != ‘\n‘);
PersonInput(&person);
p = GradeFind(&grade,&person);
SelectAll();
if (p >= 0)
{
GradeOutputPerson(&grade, p);
printf("修改:");
item = SelectCol();
if (item != 0)
{
GradeInputPerson(&grade, p);
}
}
}
break;
case ‘S‘:
printf("显示\n");
item = SelectCol();
GradeSort(&grade);
GradeOutput(&grade);
printf("Press any key to continue...");
getch();
break;
case ‘Q‘:
break;
default:
printf("错误的选项!\n");
}
}while (choice != ‘Q‘);
GradeWrite(filename, &grade);
GradeDestroy(&grade);
return 0;
}
─────────────────────────────
②Grade.c
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Grade.h"
// 创建成绩单
void GradeCreate(GRADE grade)
{
grade->size = maxSize;
grade->number = 0;
grade->person = (PERSON)malloc(grade->size * sizeof(PERSON));
}
// 销毁成绩单
void GradeDestroy(GRADE *grade)
{
free(grade->person);
grade->person = NULL;
grade->size = grade->number = 0;
}
// 添加学生
void GradeAppend(GRADE grade, const PERSON person)
{
if (grade->number < grade->size)
{
grade->person[grade->number] = *person;
++grade->number;
}
}
// 查找学生
int GradeFind(GRADE grade, const PERSON person)
{
int r = -1, k;
for (k = 0; -1 == r && k < grade->number; ++k)
{
if (!PersonCmp(&grade->person[k],person))
{
r = k;
}
}
return r;
}
// 输入个人信息
void GradeInputPerson(const GRADE *grade, int index)
{
if (index >= 0 && index < grade->number)
{
PersonInput(&grade->person[index]);
}
else
{
puts("内部错误!");
}
}
// 输出个人信息
void GradeOutputPerson(const GRADE *grade, int index)
{
if (index >= 0 && index < grade->number)
{
Card();
PersonOutput(&grade->person[index]);
}
else
{
puts("内部错误!");
}
}
// 显示成绩单
void GradeOutput(const GRADE *grade)
{
int k;
Table();
Title();
for(k = 0; k < grade->number; ++k)
{
PersonOutput(&grade->person[k]);
}
}
// 排序
void GradeSort(GRADE *grade)
{
int i, j, k, n = grade->number - 1;
for (i = 0; i < n; ++i)
{
for (k = i, j = i + 1; j < grade->number; ++j)
{
if (PersonCmp(&grade->person[j],&grade->person[k]) < 0)
{
k = j;
}
}
if (k > i)
{
PersonSwap(&grade->person[i], &grade->person[k]);
}
}
}
// 写成绩单
void GradeWrite(const char filename, const GRADE grade)
{
int k;
FILE *file = fopen(filename, "w");
if (file)
{
fprintf(file, "%d\n", grade->number);
for (k = 0; k < grade->number; ++k)
{
PersonWrite(file, &grade->person[k]);
}
fclose(file);
}
else
{
puts("Error!");
}
}
// 读成绩单
void GradeRead(const char filename,GRADE grade)
{
int k;
FILE file = fopen(filename, "r");
if (file)
{
fscanf(file, "%d%c", &grade->number);
for (k = 0; k < grade->number; ++k)
{
PersonRead(file, &grade->person[k]);
}
fclose(file);
}
else
{
fclose(file);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Grade.h"
// 创建成绩单
void GradeCreate(GRADE grade)
{
grade->size = maxSize;
grade->number = 0;
grade->person = (PERSON)malloc(grade->size * sizeof(PERSON));
}
// 销毁成绩单
void GradeDestroy(GRADE *grade)
{
free(grade->person);
grade->person = NULL;
grade->size = grade->number = 0;
}
// 添加学生
void GradeAppend(GRADE grade, const PERSON person)
{
if (grade->number < grade->size)
{
grade->person[grade->number] = *person;
++grade->number;
}
}
// 查找学生
int GradeFind(GRADE grade, const PERSON person)
{
int r = -1, k;
for (k = 0; -1 == r && k < grade->number; ++k)
{
if (!PersonCmp(&grade->person[k],person))
{
r = k;
}
}
return r;
}
// 输入个人信息
void GradeInputPerson(const GRADE *grade, int index)
{
if (index >= 0 && index < grade->number)
{
PersonInput(&grade->person[index]);
}
else
{
puts("内部错误!");
}
}
// 输出个人信息
void GradeOutputPerson(const GRADE *grade, int index)
{
if (index >= 0 && index < grade->number)
{
Card();
PersonOutput(&grade->person[index]);
}
else
{
puts("内部错误!");
}
}
// 显示成绩单
void GradeOutput(const GRADE *grade)
{
int k;
Table();
Title();
for(k = 0; k < grade->number; ++k)
{
PersonOutput(&grade->person[k]);
}
}
// 排序
void GradeSort(GRADE *grade)
{
int i, j, k, n = grade->number - 1;
for (i = 0; i < n; ++i)
{
for (k = i, j = i + 1; j < grade->number; ++j)
{
if (PersonCmp(&grade->person[j],&grade->person[k]) < 0)
{
k = j;
}
}
if (k > i)
{
PersonSwap(&grade->person[i], &grade->person[k]);
}
}
}
// 写成绩单
void GradeWrite(const char filename, const GRADE grade)
{
int k;
FILE *file = fopen(filename, "w");
if (file)
{
fprintf(file, "%d\n", grade->number);
for (k = 0; k < grade->number; ++k)
{
PersonWrite(file, &grade->person[k]);
}
fclose(file);
}
else
{
puts("Error!");
}
}
// 读成绩单
void GradeRead(const char filename,GRADE grade)
{
int k;
FILE file = fopen(filename, "r");
if (file)
{
fscanf(file, "%d%c", &grade->number);
for (k = 0; k < grade->number; ++k)
{
PersonRead(file, &grade->person[k]);
}
fclose(file);
}
else
{
fclose(file);
}
}
─────────────────────────────
③Grade.h
代码如下:
#ifndef _Gradeh
#define _Gradeh
#include "Person.h"
#define maxSize 100;
typedef struct
{
int size, number;
PERSON *person;
}GRADE;
// 创建成绩单
void GradeCreate(GRADE grade);
// 销毁成绩单
void GradeDestroy(GRADE grade);
// 添加学生
void GradeAppend(GRADE grade, const PERSON person);
// 查找学生
int GradeFind(GRADE grade, const PERSON person);
// 输入个人信息
void GradeInputPerson(const GRADE grade, int index);
// 输出个人信息
void GradeOutputPerson(const GRADE grade, int index);
// 显示成绩单
void GradeOutput(const GRADE grade);
// 排序
void GradeSort(GRADE grade);
// 写成绩单
void GradeWrite(const char filename, const GRADE grade);
// 读成绩单
void GradeRead(const char filename,GRADE grade);
#endif
─────────────────────────────
④Person.c
代码如下:
#include <stdio.h>
#include <string.h>
#include "Person.h"
static int col;
static FORMAT format;
void SelectAll()
{
col = 0;
}
void SelectId()
{
col = 1;
}
void SelectName()
{
col = 2;
}
void SelecChinese()
{
col = 3;
}
void SelecMath()
{
col = 4;
}
void SelecComputer()
{
col = 5;
}
void SelecEnglish()
{
col = 6;
}
void SelecHistory()
{
col = 7;
}
void SelecPhysics()
{
col = 8;
}
void SelecChemistry()
{
col = 9;
}
void SelecBiology()
{
col = 10;
}
void SelecSum()
{
col = 11;
}
int SelectCol()
{
int t;
printf("1-学号 2-姓名 3-语文 4-数学 5-计算机 6-英语 7-历史 8-物理 9-化学 10-生物 11-总分 0-退出 > ");
scanf(" %d", &t);
if (t >= 1 && t <= 11)
{
col = t;
}
}
void Card()
{
format = card;
}
void Table()
{
format = table;
}
void Title()
{
putchar(‘\n‘);
printf("┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐\n");
printf("│学号 姓名 语文 数学");
printf(" 计算机 英语 历史 物理 化学 生物 总分│\n");
printf("├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤\n");
}
void PersonInput(PERSON person)
{
switch (col)
{
case 0:
printf("学号:");
scanf("%d%c", &person->id);
printf("姓名:");
gets(person->name);
printf("语文:");
scanf("%lg%c", &person->chinese);
printf("数学:");
scanf("%lg%c", &person->math);
printf("计算机:");
scanf("%lg%c", &person->computer);
printf("英语:");
scanf("%lg%c", &person->english);
printf("历史:");
scanf("%lg%c", &person->history);
printf("物理:");
scanf("%lg%c", &person->physics);
printf("化学:");
scanf("%lg%c", &person->chemistry);
printf("生物:");
scanf("%lg%c", &person->biology);
person->sum = person->chinese + person->math + person->computer + person->english + person->history + person->physics + person->chemistry + person->biology;
break;
case 1:
printf("学号:");
scanf("%d%c", &person->id);
break;
case 2:
printf("姓名:");
gets(person->name);
break;
case 3:
printf("语文:");
scanf("%lg%c", &person->chinese);
break;
case 4:
printf("数学:");
scanf("%lg%c", &person->math);
break;
case 5:
printf("计算机:");
scanf("%lg%c", &person->computer);
break;
case 6:
printf("英语:");
scanf("%lg%c", &person->english);
break;
case 7:
printf("历史:");
scanf("%lg%c", &person->history);
break;
case 8:
printf("物理:");
scanf("%lg%c", &person->physics);
break;
case 9:
printf("化学:");
scanf("%lg%c", &person->chemistry);
break;
case 10:
printf("生物:");
scanf("%lg%*c", &person->biology);
case 11:
printf("总分:");
person->sum = person->chinese + person->math + person->computer + person->english + person->history + person->physics + person->chemistry + person->biology;
}
}
void PersonOutput(const PERSON *person)
{
switch (format)
{
case card:
switch (col)
{
case 0:
printf("学号:%d\n", person->id);
printf("姓名:%s\n", person->name);
printf("语文:%11g\n", person->chinese);
printf("数学:%11g\n", person->math);
printf("计算机:%9g\n", person->computer);
printf("英语:%11g\n", person->english);
printf("历史:%11g\n", person->history);
printf("物理:%11g\n", person->physics);
printf("化学:%11g\n", person->chemistry);
printf("生物:%11g\n", person->biology);
printf("总分:%11g\n", person->sum);
break;
case 1:
printf("学号:%d\n", person->id);
break;
case 2:
printf("姓名:%s\n", person->name);
break;
case 3:
printf("语文:%11g\n", person->chinese);
break;
case 4:
printf("数学:%11g\n", person->math);
break;
case 5:
printf("计算机:%11g\n", person->computer);
break;
case 6:
printf("英语:%11g\n", person->english);
break;
case 7:
printf("历史:%11g\n", person->history);
break;
case 8:
printf("物理:%11g\n", person->physics);
break;
case 9:
printf("化学:%11g\n", person->chemistry);
break;
case 10:
printf("生物:%11g\n", person->biology);
break;
case 11:
printf("总分:%11g\n", person->sum);
}
break;
case table:
printf("│%4d %-8s ", person->id, person->name);
printf("%11g", person->chinese);
printf("%11g", person->math);
printf("%11g", person->computer);
printf("%11g", person->english);
printf("%11g", person->history);
printf("%11g", person->physics);
printf("%11g", person->chemistry);
printf("%11g", person->biology);
printf("%11g│\n", person->sum);
printf("├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤\n");
break;
}
}
int PersonCmp(const PERSON x,const PERSON y)
{
int r;
switch (col)
{
case 1:
r = x->id - y->id;
break;
case 2:
r = strcmp(x->name,y->name);
break;
case 3:
r = x->chinese - y->chinese;
break;
case 4:
r = x->math - y->math;
break;
case 5:
r = x->computer - y->computer;
break;
case 6:
r = x->english - y->english;
break;
case 7:
r = x->history - y->history;
break;
case 8:
r = x->physics - y->physics;
break;
case 9:
r = x->chemistry - y->chemistry;
break;
case 10:
r = x->biology - y->biology;
break;
case 11:
r = x->sum - y->sum;
}
return r;
}
void PersonSwap(PERSON x, PERSON y)
{
PERSON t = x;
x = y;
y = t;
}
void PersonWrite(FILE file, const PERSON person)
{
fprintf(file, "│%5d\n", person->id);
fprintf(file, "%10s\n", person->name);
fprintf(file, "%11g\n", person->chinese);
fprintf(file, "%11g\n", person->math);
fprintf(file, "%11g\n", person->computer);
fprintf(file, "%11g\n", person->english);
fprintf(file, "%11g\n", person->history);
fprintf(file, "%11g\n", person->physics);
fprintf(file, "%11g\n", person->chemistry);
fprintf(file, "%11g\n", person->biology);
fprintf(file, "%11g│\n", person->sum);
fprintf(file, "└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘\n");
}
void PersonRead(FILE file, PERSON person)
{
int n;
char temp[1000];
fscanf(file, "%d%c", &person->id);
fgets(temp, 1000, file);
n = strlen(temp);
temp[n - 1] = ‘\0‘;
strcpy(person->name, temp);
fscanf(file, "%lg%c", &person->chinese);
fscanf(file, "%lg%c", &person->math);
fscanf(file, "%lg%c", &person->computer);
fscanf(file, "%lg%c", &person->english);
fscanf(file, "%lg%c", &person->history);
fscanf(file, "%lg%c", &person->physics);
fscanf(file, "%lg%c", &person->chemistry);
fscanf(file, "%lg%*c", &person->biology);
person->sum = person->chinese + person->math + person->computer + person->english + person->history + person->physics + person->chemistry + person->biology;
}
─────────────────────────────
⑤Person.h
代码如下:
#ifndef _Personh
#define _Personh
typedef struct
{
int id;
char name[16];
double chinese;
double math;
double computer;
double english;
double history;
double physics;
double chemistry;
double biology;
double sum;
}PERSON;
typedef enum {card, table} FORMAT;
void SelectAll();
void SelectId();
void SelectName();
void SelecChinese();
void SelecMath();
int SelectCol();
void Card();
void Table();
void Title();
void PersonInput(PERSON person);
void PersonOutput(const PERSON person);
int PersonCmp(const PERSON x,const PERSON y);
void PersonSwap(PERSON x, PERSON y);
void PersonWrite(FILE file, const PERSON person);
void PersonRead(FILE file, PERSON person);
#endif
=============================================
运行结果:
标签:math close ice shadow malloc name 查找 putchar temp
原文地址:https://blog.51cto.com/14298195/2381608