當前位置:編程學習大全網 - 編程語言 - 傾我所有求壹道C語言的編程~~~

傾我所有求壹道C語言的編程~~~

嗯,現在的計算機作業似乎十有八九都是學生管理系統這類東西,而且還得用C做,真是...嗯。

這裏有壹個用C寫的,百度的排版不太好,將就看吧。

稍微說壹下思路。先是定義壹個學生結構,接著是壹個類似管理者的結構,管理者中存儲學生數組,並記錄學生數。之後則是壹系列操作,包括排序、計算平均值、讀寫文件等等,這些操作基本上都以管理者作為操作對象。最後當然就是界面菜單的處理。

運行程序時必須有stud.txt文件,另外,保存文件的操作沒有設為自動進行,而是通過菜單選擇進行。默認保存的文件名有stud.txt和stud2.txt,如果要存為其他文件需要自己輸入文件名。

每個函數都有說明,希望妳能看明白。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define NAME_MAX_LENGTH 25

#define STUDENT_MAX_NUMBER 100

/* 定義學生信息結構 */

struct Student

{

int id;

char name[NAME_MAX_LENGTH];

int age;

float score;

};

/* 管理者結構 */

struct Manager

{

struct Student stus[STUDENT_MAX_NUMBER];

int size;

int stu_count;

};

/********************** 相關操作 ********************/

/*

交換兩個學生信息

*/

void SwapStudents(struct Student *pStu1, struct Student *pStu2)

{

struct Student temp;

temp.id = pStu1->id;

temp.age = pStu1->age;

temp.score = pStu1->score;

strcpy(temp.name, pStu1->name);

pStu1->id = pStu2->id;

pStu1->age = pStu2->age;

pStu1->score = pStu2->score;

strcpy(pStu1->name, pStu2->name);

pStu2->id = temp.id;

pStu2->age = temp.age;

pStu2->score = temp.score;

strcpy(pStu2->name, temp.name);

}

/*

初始化管理者

*/

void InitializeManager(struct Manager *pm)

{

int i;

pm->size = NAME_MAX_LENGTH;

pm->stu_count = 0;

for (i=0; i<pm->size; ++i)

{

pm->stus[i].id = 0;

pm->stus[i].name[0] = 0;

pm->stus[i].age = 0;

pm->stus[i].score = 0.0f;

}

}

/*

查找壹個學生

id: 學生id

return: 指定學生的索引,查找失敗返回-1

*/

int SearchForOne(struct Manager *pm, int id)

{

int i;

for (i=0; i<pm->stu_count; ++i)

{

if (pm->stus[i].id != id) continue;

return i;

}

return -1;

}

/*

從文件中加載學生信息

szFileName: 文件名

return: 學生數。若讀取失敗,則返回0。

P.S. 如果文件中的學生數超過數組大小,則多余部分不會讀入

*/

int LoadStudentsInfo(struct Manager *pm, char *szFileName)

{

char ch, str[10];

int i;

FILE *fp = fopen(szFileName, "r");

if (!fp) return 0;

for (i=0; i<pm->size; ++i)

{

if (fscanf(fp, "%d%c%s%d%s", &pm->stus[i].id, &ch, pm->stus[i].name,

&pm->stus[i].age, str) == EOF) break;

pm->stus[i].score = (float)atof(str);

}

fclose(fp);

return pm->stu_count = i;

}

/*

將學生信息保存到文件中

szFileName: 文件名

*/

int SaveStudentsInfo(struct Manager *pm, char *szFileName)

{

char ch = 'a';

int i;

FILE *fp = fopen(szFileName, "w");

if (!fp) return 0;

for (i=0; i<pm->stu_count; ++i)

{

fprintf(fp,"%03d%c %s %d %g\n", pm->stus[i].id, ch, pm->stus[i].name,

pm->stus[i].age, pm->stus[i].score);

}

fclose(fp);

return 1;

}

/*

根據ID排序

mode: 0 - 升序,非0 - 降序

*/

void SortByID(struct Manager *pm, int mode)

{

int i, j;

int flag = 1;

if (mode)

{

for (i=0; (i+1<pm->stu_count) && flag; ++i)

{

flag = 0;

for (j=0; j+i+1<pm->stu_count; ++j)

{

if (pm->stus[j].id < pm->stus[j+1].id)

{

SwapStudents(pm->stus+j, pm->stus+j+1);

flag = 1;

}

}

}

return;

}

for (i=0; (i+1<pm->stu_count) && flag; ++i)

{

flag = 0;

for (j=0; j+i+1<pm->stu_count; ++j)

{

if (pm->stus[j].id > pm->stus[j+1].id)

{

SwapStudents(pm->stus+j, pm->stus+j+1);

flag = 1;

}

}

}

}

/*

根據成績排序

mode: 0 - 升序,非0 - 降序

*/

void SortByScore(struct Manager *pm, int mode)

{

int i, j;

int flag = 1;

if (mode)

{

for (i=0; (i+1<pm->stu_count) && flag; ++i)

{

flag = 0;

for (j=0; j+i+1<pm->stu_count; ++j)

{

if (pm->stus[j].score < pm->stus[j+1].score)

{

SwapStudents(pm->stus+j, pm->stus+j+1);

flag = 1;

}

}

}

return;

}

for (i=0; (i+1<pm->stu_count) && flag; ++i)

{

flag = 0;

for (j=0; j+i+1<pm->stu_count; ++j)

{

if (pm->stus[j].score > pm->stus[j+1].score)

{

SwapStudents(pm->stus+j, pm->stus+j+1);

flag = 1;

}

}

}

}

/*

平均成績

*/

float AverageScore(struct Manager *pm)

{

float sum = 0.0f;

int i;

for (i=0; i<pm->stu_count; ++i) sum += pm->stus[i].score;

return sum / pm->stu_count;

}

/*

打印壹個學生的信息

index: 學生索引

return: 非法索引值將返回0

*/

int PrintOne(struct Manager *pm, int index)

{

char ch = 'a';

if (index<0 || index>=pm->size) return 0;

printf("* * * * * * * * * * * * * *\n");

printf("ID:\t%03d%c\nName:\t%s\nAge:\t%d\nScore:\t%.1f\n", pm->stus[index].id, ch,

pm->stus[index].name, pm->stus[index].age, pm->stus[index].score);

printf("* * * * * * * * * * * * * *\n");

return 1;

}

/*

打印所有學生信息

*/

void PrintAll(struct Manager *pm)

{

char ch = 'a';

int i;

printf("* * * * * * * * * * * * * * * *\n");

printf("ID Name\tAge\tScore\n");

for (i=0; i<pm->stu_count; ++i)

{

printf("%03d%c: %s\t%d\t%.1f\n", pm->stus[i].id, ch,

pm->stus[i].name, pm->stus[i].age, pm->stus[i].score);

}

printf("* * * * * * * * * * * * * * * *\n");

}

/*

清除緩沖區中的剩余字符

*/

void clear()

{

for (; getchar()!=10; );

}

/*

界面處理

rank: 菜單級次。1: 顯示所有學生信息;

2: 顯示指定學生信息;

3: 修改指定學生信息;

4: 平均成績;

5: 按ID升序排列;

6: 按成績降序排列;

7: 重新從文件中讀入學生數據;(文件可選)

8: 將當前學生信息保存到文件中;(文件可選)

9. 退出程序

return: 如果選擇退出,返回0;否則返回非0值

*/

int InteraceProc(int rank, struct Manager *pm)

{

char sel = 0;

char szFile[30];

int id, index, res;

int age;

float score;

switch (rank)

{

case 0:

printf("\n");

printf("- - - - - - - - - - - - - - - - -\n");

printf("1. Information of all students\n");

printf("2. Information of one student\n");

printf("3. Modify data of one student\n");

printf("4. Average score\n");

printf("5. Sort by ID\n");

printf("6. Sort by score\n");

printf("7. Reload data from...\n");

printf("8. Save current data as...\n");

printf("9. Exit\n");

printf("- - - - - - - - - - - - - - - - -\n");

printf("Please choose one: ");

break;

case 1:

printf("\n");

PrintAll(pm);

clear();

break;

case 2:

id = -1;

printf("\nSpecify the student by ID: ");

scanf("%d", &id);

clear();

index = SearchForOne(pm, id);

if (index < 0) printf("Can't find the student.\n");

else PrintOne(pm, index);

clear();

break;

case 3:

printf("\nSpecify the student by ID: ");

scanf("%d", &id);

clear();

index = SearchForOne(pm, id);

if (index < 0)

{

printf("Can't find the student.\n");

break;

}

PrintOne(pm, index);

printf("Input the new age and new score: ");

res = scanf("%d %f", &age, &score);

clear();

if (!res) break;

printf("Are you sure? (y/n) ");

res = scanf("%c", &sel);

clear();

if (sel!='y' && sel!='Y') break;

pm->stus[index].age = age;

pm->stus[index].score = score;

PrintOne(pm, index);

clear();

break;

case 4:

score = AverageScore(pm);

printf("\nThe average score: %.2f", score);

clear();

break;

case 5:

printf("Are you sure? (y/n) ");

res = scanf("%c", &sel);

clear();

if (sel!='y' && sel!='Y') break;

printf("\n");

SortByID(pm, 0);

PrintAll(pm);

clear();

break;

case 6:

printf("Are you sure? (y/n) ");

res = scanf("%c", &sel);

clear();

if (sel!='y' && sel!='Y') break;

printf("\n");

SortByScore(pm, 1);

PrintAll(pm);

clear();

break;

case 7:

case 8:

printf("\n1. stud.txt");

printf("\n2. stud2.txt");

printf("\n3. other file");

printf("\n4. return");

printf("\nchoose one: ");

res = scanf("%d", &index);

clear();

if (!res || index<1 || index>3) break;

switch (index)

{

case 1: strcpy(szFile, "stud.txt"); break;

case 2: strcpy(szFile, "stud2.txt"); break;

case 3:

printf("Input the file name: ");

scanf("%s", szFile);

clear();

break;

}

printf("Are you sure? (y/n) ");

res = scanf("%c", &sel);

clear();

if (sel!='y' && sel!='Y') break;

if (rank == 7) res = LoadStudentsInfo(pm, szFile);

else res = SaveStudentsInfo(pm, szFile);

if (!res) printf("Can't open the file!\n");

break;

case 9:

printf("\nThe program end.");

getchar();

return 0;

}

return 1;

}

/******************* 主函數 *******************/

int main()

{

int sel, res;

struct Manager manager;

/* 程序的正常運行需要確保存在stud.txt文件 */

InitializeManager(&manager);

if (!LoadStudentsInfo(&manager, "stud.txt"))

{

printf("Can't load students data.\n");

printf("The program abort.");

getchar();

return -1;

}

for (; ; )

{

InteraceProc(0, &manager);

res = scanf("%d", &sel);

clear(); /* 如果沒有這壹句,在主菜單中輸入非數字字符時,程序將陷入死循環 */

if (!res || !sel) continue;

if (!InteraceProc(sel, &manager)) break;

}

return 0;

}

  • 上一篇:單眼皮怎麽變成雙眼皮?
  • 下一篇:信息化建設工作總結優秀範文
  • copyright 2024編程學習大全網