/* Copyright (c) 2004 aprin
*
*
*/
#include <stdio.h>
#include <string.h>
#define MAXN 35
#define MAX_NAME 256
typedef struct student{
int no;
char name[MAX_NAME];
int english;
int math;
int computer;
int mark_ave;
int mark_tot;
} STUDENT;
/*-----------input sub program---------*/
void input(STUDENT *data, int *len)
{
int no;
char ch[MAX_NAME];
int mark;
putchar('\n');
printf("%s\n", "Enter the new records.And if you want exit, enter -1.");
printf("%s\n", "format: number");
printf("%s\n", " name english math computer average total");
putchar('\n');
printf("such as:\n");
printf("1\n");
printf("Gates 100 100 100 100 400\n");
scanf("%d", &no);
while(no != -1) {
data[*len].no = no;
scanf("%s %d %d %d %d %d", data[*len].name, &data[*len].english, &data[*len].math,
&data[*len].computer, &data[*len].mark_ave, &data[*len].mark_tot);
(*len)++;
scanf("%d", &no);
}
}
/*---------------output sub program---------------*/
void output(STUDENT *data, int len)
{
int i;
clrscr();
printf("%8s", "number");
printf("%8s", "name");
printf("%8s", "english");
printf("%8s", "math");
printf("%10s", "computer");
printf("%12s", "average");
printf("%10s", "total");
putchar('\n');
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
for (i = 0; i< len; i++)
{
printf("%8d", data[i].no);
printf("%8s", data[i].name);
printf("%8d", data[i].english);
printf("%8d", data[i].math);
printf("%10d", data[i].computer);
printf("%12d", data[i].mark_ave);
printf("%10d", data[i].mark_tot);
putchar('\n');
}
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("Press enter to continue.");
getchar();
getchar();
}
/*--------------sort sub program------------*/
void sort(STUDENT *data, int len)
{
int i,j,k;
STUDENT temp;
for (i=0; i<len-1; i++) {
for (k=i, j=i+1; j<len; j++)
if (data[k].mark_ave > data[j].mark_ave) k=j;
if (k != i) {
temp = data[i];
data[i] = data[k];
data[k] = temp;
}
}
}
/*--------------------find sub program-----------------------*/
void find(STUDENT *data, int len)
{
int find_no, result ;
int i;
lab: result=0;
printf("%s\n", "Enter the number which you want to find. If you want exit, enter -1.");
scanf("%d", &find_no);
if (find_no == -1) return; /*exit the fine sub program*/
while( data[result].no != find_no && result < len) result ++;
if (result >= len ) {
printf("%s\n", "There is no the record which you want.");
goto lab;
}
else {
clrscr();
printf("%s\n", "The flowing record is you want.");
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("%8s", "number");
printf("%8s", "name");
printf("%8s", "english");
printf("%8s", "math");
printf("%10s", "computer");
printf("%12s", "average");
printf("%10s", "total");
putchar('\n');
printf("%8d", data[result].no);
printf("%8s", data[result].name);
printf("%8d", data[result].english);
printf("%8d", data[result].math);
printf("%10d", data[result].computer);
printf("%12d", data[result].mark_ave);
printf("%10d", data[result].mark_tot);
putchar('\n');
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
goto lab;
}
}
/*-----------------insert sub program-------------*/
void insert(STUDENT *data, int *len)
{
int no, pos, english, math, computer, mark_ave, mark_tot, i;
char name[MAX_NAME];
lab: printf("%s\n", "Enter the new records. If you want exit, enter -1.");
printf("%s\n", "format: number");
printf("%s\n", " name english math computer average total");
scanf("%d", &no);
if (no == -1) return;
data[*len].no = no;
scanf("%s %d %d %d %d %d", name, &english, &math, &computer, &mark_ave, &mark_tot);
pos = 0;
while ((data[pos].mark_ave < mark_ave) && (pos < *len) )
pos ++;
for (i = *len-1; i >= pos; i--)
data[i+1] = data[i];
data[pos].no = no;
strcpy(data[pos].name, name);
data[pos].english = english;
data[pos].math = math;
data[pos].computer = computer;
data[pos].mark_ave = mark_ave;
data[pos].mark_tot = mark_tot;
(*len)++;
goto lab;
}
/*--------------delete sub program--------------*/
void delete(STUDENT *data, int *len)
{
int no, i, pos;
lab: pos=0;
printf("%s\n", "Enter the number which you want to delete. If you want exit, enter -1.");
scanf("%d", &no);
if (no == -1) return;
while( (data[pos].no != no) && (pos < *len) ) pos = pos +1;
if (pos >= *len) {
printf("%s\n", "There is no the record which you want.");
goto lab;
}
else {
for (i = pos+1;i < *len; i++)
data[i-1] = data[i];
*len = *len -1;
if (*len == 0) {
printf("%s\n", "The number of records is zero, press enter to return.");
getchar();
getchar();
return;
}
goto lab;
}
}
/*---------------stat sub program------------*/
void stat(STUDENT *data, int len)
{
int no_59=0, no_69=0, no_79=0, no_89=0, no_100=0;
int i;
for (i = 0;i < len; i++) {
if (data[i].mark_ave <=59) no_59++;
else if (data[i].mark_ave <=69) no_69++;
else if (data[i].mark_ave <=79) no_79++;
else if (data[i].mark_ave <= 89) no_89++;
else no_100++;
}
clrscr();
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("%10s", "score");
printf("%10s", "0--59");
printf("%10s", "60--69");
printf("%10s", "70--79");
printf("%10s", "80--89");
printf("%10s", "90--100");
putchar('\n');
putchar('\n');
printf("%10s", "student");
printf("%10d", no_59);
printf("%10d", no_69);
printf("%10d", no_79);
printf("%10d", no_89);
printf("%10d", no_100);
putchar('\n');
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("Press enter to return.");
getchar();
getchar();
}
/*---------------manu------------*/
void paint()
{
int i;
clrscr();
printf("%43s\n", "Manu");
for (i = 0; i < 80; i++)
putchar('=');
putchar('\n');
printf(" 1 input 2 output\n");
printf(" 3 sort(average) 4 find(number)\n");
printf(" 5 insert 6 delete(number)\n");
printf(" 7 stat(average) 0 quit\n");
for (i = 0; i<= 79; i++)
putchar('=');
putchar('\n');
printf("%s\n", "Please enter the command:");
}
/*------------main program---------------*/
main()
{
STUDENT data[MAXN];
int len = 0;
char ctrl_ch;
paint();
scanf("%c", &ctrl_ch);
while (ctrl_ch != '0') {
switch(ctrl_ch) {
case '1':
input(data, &len);
break;
case '2':
output(data, len);
break;
case '3':
sort(data, len);
break;
case '4':
find(data, len);
break;
case '5':
insert(data, &len);
break;
case '6':
delete(data, &len);
break;
case '7':
stat(data, len);
break;
default:
if (ctrl_ch != '\n') printf("%s\n", "Error command.");
break;
}
if (ctrl_ch != '\n') paint();
scanf("%c", &ctrl_ch);
}
}