You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
6.4 KiB

/*this code is just a very simple example.
it only works when the id and class both less than INT_MAX
also,you need make sure the id and the class will not be the same
,and the name should consist of only letters
Finally, fxxk the C89*/
#define _CRT_SECURE_NO_WARNINGS 1
/*declare including*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
/*declaration*/
int Mycontinue(int num); void Search(char info[]);
void Inputf(); void Deletef(); void Selectf(); void Orderf(); void Outputf(); void Quitf();
void mainLoop();
/*create the main struct*/
typedef struct
{
int id;
int class;
char name[50];
float score[3];
float total_score;
} Student;
/*initialize the global variable*/
#define MAX_STUDENTS 100
Student usrlist[MAX_STUDENTS];
Student tmplist[MAX_STUDENTS];
int slist[MAX_STUDENTS];
int listlength = -1;
int tmplength = -1;
int slength;
int run = 1;
/*create secondary module*/
int Mycontinue(int num) /*completed*/
{
char choice[10];
printf("continue?\n");
choose:
{
scanf("%9s", choice);
getchar();
switch (strlen(choice))
{
case 2:return 0;
case 3:return 1;
default:printf("Please input the proper choice (yes/no)\n"); goto choose;
}
}
}
void Search(char info[]) /*completed*/
{
int k = 0;
int i;
int j;
int snum;
int same;
slength = -1;
for (; k < 100; k++)slist[k] = 0;
if (info[0] > 47 && info[0] < 58)
{
snum = 0;
for (i = 0; i < strlen(info); i++)
{
snum *= 10;
snum += info[i] - '0';
}
for (i = 0; i <= listlength; i++)if (usrlist[i].id == snum || usrlist[i].class == snum)slist[++slength] = i;
}
else
{
for (i = 0; i <= listlength; i++)if (strlen(info) == strlen(usrlist[i].name))
{
same = 1;
for (j = 0; j < strlen(info); j++)if (info[j] != usrlist[i].name[j])
{
same = 0;
break;
}
if (same)slist[++slength] = i;
}
}
}
/*create primary module*/
void Inputf() /*completed*/
{
printf("You are trying to Input info\n");
int irun = 1;
while (irun)
{
listlength++;
int tmp = listlength;
printf("Id ");
scanf("%d", &usrlist[tmp].id);
getchar();
printf("class ");
scanf("%d", &usrlist[tmp].class);
getchar();
printf("name ");
scanf("%49s", usrlist[tmp].name);
getchar();
printf("score1 ");
scanf("%f", &usrlist[tmp].score[0]);
getchar();
printf("score2 ");
scanf("%f", &usrlist[tmp].score[1]);
getchar();
printf("score3 ");
scanf("%f", &usrlist[tmp].score[2]);
getchar();
usrlist[tmp].total_score = usrlist[tmp].score[0] + usrlist[tmp].score[1] + usrlist[tmp].score[2];
irun = Mycontinue(irun);
}
}
void Deletef() /*completed*/
{
int drun;
char info[50];
start:
printf("You are trying to delete info\n");
if (listlength == 0)printf("There is only one data,please add some new\n");
else if(listlength == -1)printf("There is no data,please add some new\n");
else
{
drun = 1;
scanf("%49s", info);
Search(info);
int i;
int j;
for (i = 0; i <= listlength; i++)for (j = 0; j <= slength; j++)if (i == slist[j])usrlist[i].id = -1;
tmplength = -1;
for (i = 0; i <= listlength; i++) if (usrlist[i].id + 1)tmplist[++tmplength] = usrlist[i];
listlength = -1;
for (i = 0; i <= tmplength; i++)usrlist[++listlength] = tmplist[i];
Outputf();
drun = Mycontinue(drun);
if (drun)goto start;
}
}
void Selectf() /*completed*/
{
int srun;
char info[50];
int i;
int j;
int fit;
printf("You are trying to select info\n");
if (listlength == -1)printf("There is no data,please add some new\n");
else
{
srun = 1;
while (srun)
{
scanf("%49s", info);
Search(info);
for (i = 0; i <= listlength; i++)
{
fit = 0;
for (j = 0; j <= slength; j++)if (i == slist[j])
{
fit = 1;
break;
}
if (fit)printf("%d %d %s %.1f %.1f %.1f %.1f\n", usrlist[i].id, usrlist[i].class, usrlist[i].name, usrlist[i].score[0], usrlist[i].score[1], usrlist[i].score[2], usrlist[i].total_score);
}
srun=Mycontinue(srun);
}
}
}
void Orderf() /*completed*/
{
tmplength = -1;
int idmin;
int classmin;
int i;
int j;
printf("You are trying to Make things orderd\n");
while (tmplength < listlength)
{
idmin = INT_MAX;
classmin = INT_MAX;
for (i = 0; i <= listlength; i++)if (usrlist[i].class < classmin)classmin = usrlist[i].class;
for (i = 0; (i <= listlength); i++)if ((usrlist[i].id < idmin) && (classmin == usrlist[i].class))
{
idmin = usrlist[i].id;
j = i;
}
tmplist[++tmplength] = usrlist[j];
usrlist[j].id = INT_MAX;
usrlist[j].class = INT_MAX;
}
listlength = -1;
for (i = 0; i <= tmplength; i++)usrlist[++listlength] = tmplist[i];
Outputf();
}
void Outputf() /*completed*/
{
int index = -1;
printf("You are trying to Output info\n");
while (index < listlength)
{
index++;
printf("%d %d %s %.1f %.1f %.1f %.1f\n", usrlist[index].id, usrlist[index].class, usrlist[index].name, usrlist[index].score[0], usrlist[index].score[1], usrlist[index].score[2], usrlist[index].total_score);
}
}
void Quitf() /*completed*/
{
printf("You are about to Quit\n");
run = 0;
}
/*create main module*/
void mainLoop()
{
while (run)
{
printf("1.input\n");
printf("2.delete\n");
printf("3.select\n");
printf("4.order\n");
printf("5.output\n");
printf("6.quit\n");
printf("Please input your opinion\n");
char cin = getchar();
getchar();
switch (cin)
{
case '1':Inputf(); break;
case '2':Deletef(); break;
case '3':Selectf(); break;
case '4':Orderf(); break;
case '5':Outputf(); break;
case '6':Quitf(); break;
default:printf("Wrong input\n"); break;
}
}
}
/*create main function*/
int main()
{
mainLoop();
return 0;
}