當前位置:編程學習大全網 - 編程語言 - C++編程 存入N個同學的成績表(學號,姓名 成績),輸入學好或姓名 ,查找成績

C++編程 存入N個同學的成績表(學號,姓名 成績),輸入學好或姓名 ,查找成績

#include <stdio.h>

#include <string.h>

const int MAX_NUM = 5;

const double MAX_NUMS = 5.0;

const int MAX_NAME_LEN = 127;

const int MAX_SUBJECT = 3;

const char *g_strSubjects[] =

{

"語文",

"數學",

"英語",

"非法"

};

typedef struct _Student

{

char szName[MAX_NAME_LEN + 1];

int nArryScore[MAX_SUBJECT];

}Student;

Student students[MAX_NUM];

static int read_line ( FILE *fp, char *line, int max_length )

{

int i;

char ch;

/* initialize index to string character */

i = 0;

/* read to end of line, filling in characters in string up to its

maximum length, and ignoring the rest, if any */

for(;;)

{

/* read next character */

ch = fgetc(fp);

/* check for end of file error */

if ( ch == EOF )

return -1;

/* check for end of line */

if ( ch == '\n' )

{

/* terminate string and return */

line[i] = '\0';

return 0;

}

/* fill character in string if it is not already full*/

if ( i < max_length )

line[i++] = ch;

}

/* the program should never reach here */

return -1;

}

void menu_add_student()

{

Student* pStudent = NULL;

int nCnt = 0;

int nPos = 0;

while (MAX_NUM > nCnt)

{

printf("第%d個同學的記錄:", nCnt + 1);

pStudent = &students[nCnt];

scanf("%s", pStudent->szName);

for (nPos = 0; nPos < MAX_SUBJECT; nPos++)

{

scanf("%d", &pStudent->nArryScore[nPos]);

}

nCnt++;

getchar();

}

}

void query_student(int n)

{

Student *pStudent = &students[n];

printf("%s ", pStudent->szName);

int nPos = 0;

for (nPos = 0; nPos < MAX_SUBJECT; nPos++)

{

printf("%d ", pStudent->nArryScore[nPos]);

}

printf("\n");

}

void menu_query_detail()

{

int n = 0;

scanf("%d", &n);

getchar();

int nPos = 0;

for (nPos = 0; nPos < MAX_SUBJECT; nPos++)

{

printf("%s ", g_strSubjects[nPos]);

}

printf("\n");

query_student(n - 1);

}

void menu_query_all()

{

int nPos = 0;

for (nPos = 0; nPos < MAX_SUBJECT; nPos++)

{

printf("%s ", g_strSubjects[nPos]);

}

printf("\n");

int n = 0;

for (n = 0; n < MAX_NUM; n++)

{

query_student(n);

}

}

void menu_subject_average()

{

Student* pStudent = NULL;

int n3Score[MAX_SUBJECT] = {0};

int nCnt = 0;

int nPos = 0;

while (MAX_NUM > nCnt)

{

pStudent = &students[nCnt];

for (nPos = 0; nPos < MAX_SUBJECT; nPos++)

{

if (n3Score[nPos] < pStudent->nArryScore[nPos])

{

n3Score[nPos] += pStudent->nArryScore[nPos];

}

}

nCnt++;

}

for (nPos = 0; nPos < MAX_SUBJECT; nPos++)

{

printf("%s 平均分(%.2f)\n", g_strSubjects[nPos], n3Score[nPos] / MAX_NUMS);

}

}

void menu_subject_max()

{

Student* pStudent = NULL;

int n3Cnt[MAX_SUBJECT] = {0};

int n3Score[MAX_SUBJECT] = {0};

int nCnt = 0;

int nPos = 0;

while (MAX_NUM > nCnt)

{

pStudent = &students[nCnt];

for (nPos = 0; nPos < MAX_SUBJECT; nPos++)

{

if (n3Score[nPos] < pStudent->nArryScore[nPos])

{

n3Score[nPos] = pStudent->nArryScore[nPos];

n3Cnt[nPos] = nCnt + 1;

}

}

nCnt++;

}

for (nPos = 0; nPos < MAX_SUBJECT; nPos++)

{

printf("%s 最高分(%d), 編號(%d)\n", g_strSubjects[nPos], n3Score[nPos], n3Cnt[nPos]);

}

}

/* codes for menu */

#define ADD_CODE 0

#define PRINT_CODE 1

#define DETAIL_CODE 2

#define SUB_MAX_CODE 3

#define SUB_AVE_CODE 4

#define EXIT_CODE 5

int main (void)

{

for(;;)

{

int choice, result;

char line[301];

// FILE *fp1=fopen("input2","r");

/* print menu to standard error */

fprintf ( stderr, "\nOptions:\n" );

fprintf ( stderr, "%d: 錄入成績\n", ADD_CODE );

fprintf ( stderr, "%d: 輸出全班學生的成績\n", PRINT_CODE );

fprintf ( stderr, "%d: 輸入序號,輸出對應同學三門課的成績\n", DETAIL_CODE );

fprintf ( stderr, "%d: 輸出每門課最高分以及相應同學的序號\n", SUB_MAX_CODE );

fprintf ( stderr, "%d: 輸出每門課的平均分\n", SUB_AVE_CODE );

fprintf ( stderr, "%d: 退出系統\n", EXIT_CODE );

fprintf ( stderr, "\nEnter option: " );

if ( read_line ( stdin, line, 300 ) != 0 ) continue;

// read_line (fp1 , line, 300 ) ;

//printf("\n-----------%s-----------\n",line);

result = sscanf ( line, "%d", &choice );

if ( result != 1 )

{

fprintf ( stderr, "corrupted menu choice\n" );

continue;

}

switch ( choice )

{

case ADD_CODE: /* add book to database */

menu_add_student();

break;

case PRINT_CODE: /* get book details from database */

menu_query_all();

break;

case DETAIL_CODE: /* delete book from database */

menu_query_detail();

break;

case SUB_MAX_CODE: /* print database contents to screen

(standard output) */

menu_subject_max();

break;

case SUB_AVE_CODE: /* print tree to screen (standard output) */

menu_subject_average();

break;

/* exit */

case EXIT_CODE:

break;

default:

fprintf ( stderr, "illegal choice %d\n", choice );

break;

}

/* check for exit menu choice */

if ( choice == EXIT_CODE )

break;

}

return 0;

}

  • 上一篇:深圳市龍翔偉業數控設備有限公司怎麽樣?
  • 下一篇:QA SA SE PM LM 分別是什麽職位
  • copyright 2024編程學習大全網