|
发表于 2023-9-25 20:20:45
|
显示全部楼层
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
static int b=0;
struct Score
{
float shu;
float li;
float hua;
float average;
void vi(void);
};
void Score::vi(void)
{
average=(shu+li+hua)/3;
}
struct student
{
char number[8];
char name[8];
char sex[8];
float flag;
struct Score sco;
};
int x=0;
struct student sut[50];
//FILE *fp;
/*void data_load()
{
int j;
long k;
fp=fopen("friend.txt","r+");
if(fp!=NULL)
{
for(i=1;i<50;i++)
{
if((j=fgetc(fp))==EOF)
return;
k=i-1;
fseek(fp,k*sizeof(struct student),SEEK_SET);
fread(&sut,sizeof(struct student),1,fp);
}
}
else
{
fp=fopen("friend.txt","w");
i=1;
}
}
*/
/*
void data_save()
{
int j;
printf("%d",x);
fp=fopen("friend.txt","w");
for(j=1;j<=x;j++)
{
fwrite(&sut[j],sizeof(struct student),1,fp);
}
fclose(fp);
}
*/
void putin(int i);
void putout(int i);
void xiugai(void);
void dellist(void);
void delname(void);
void delnumber(void);
void move(int i);
int strcmp1(char *buf1,char *buf2);
void paixu(void);
int main(void)
{
char flag;
printf("\t\t*****************************************\n");
printf("\t\t*\t1:the char i is input \t*\n");
printf("\t\t*\t2:the char o is output\t*\n");
printf("\t\t*\t3:the char g is xiugai\t*\n");
printf("\t\t*\t4:the char q is tui chu\t*\n");
printf("\t\t*\t5:the char s is savelis\t*\n");
printf("\t\t*\t6:the char d is dellist\t*\n");
printf("\t\t*****************************************\n");
while(1)
{
printf("\nput your donzhu:");
//flag=getch();
scanf("%c",&flag);
switch(flag)
{
case 'i' : {int i;
printf("put number student :\t");
scanf("%d",&i);
putin(i);
} break;
case 'o' : {int i;
printf("put number student :\t");
scanf("%d",&i);
putout(i);
}break;
case 'd' : dellist(); break;
case 'g' : xiugai(); break;
// case 's' : data_save(); break;
case 'q' : goto tuichu; break;
}
}//end while
tuichu: // goto结束语
return 0;
}
//输入数据
void putin(int i)
{
b=b+1;
x++;
if(sut.flag==1) {printf("\t\there is a people!\n");return;}
printf("put the number:\t");
scanf("%s",sut.number);
printf("\nput the name:\t");
scanf("%s",sut.name);
printf("\nput the sex:\t");
scanf("%s",sut.sex);
printf("\n");
printf("\nput the score:\n");
printf("put the shu:\t");
scanf("%f",&sut.sco.shu);
printf("\nput the li:\t");
scanf("%f",&sut.sco.li);
printf("\nput the hua:\t");
scanf("%f",&sut.sco.hua);
sut.sco.vi();
sut.flag=1;
if(b>1)
paixu();//输入完后自动排序
}
//输出数据
void putout(int i)
{
printf("\t\tthe number:\t%s",sut.number);
printf("\n\t\tthe name:\t%s",sut.name);
printf("\n\t\tthe sex:\t%s",sut.sex);
printf("\n");
printf("\n\t\tput the score:\n");
printf("\t\tthe shu:\t%f",sut.sco.shu);
printf("\n\t\tthe li: \t%f",sut.sco.li);
printf("\n\t\tthe hua:\t%f\n",sut.sco.hua);
printf("\n\t\the average is:\t%f\n",sut.sco.average);
}
//修改数据
void xiugai(void)
{
int i;
printf("put number student :\t");
scanf("%d",&i);
if(sut.flag!=1) {putin(i);return;}
printf("put the number:\t%s",sut.number);
printf("\nput the xiugai:\t");
scanf("%s",sut.number);
printf("\nput the name:\t%s",sut.name);
printf("\nput the name:\t");
scanf("%s",sut.name);
printf("\nput the sex:\t%s",sut.sex);
printf("\nput the sex:\t");
scanf("%s",sut.sex);
printf("put the shu:\t%d",sut.sco.shu);
printf("\nput the shu:\t");
scanf("%f",&sut.sco.shu);
printf("\nput the li:\t%d",sut.sco.li);
printf("\nput the li:\t");
scanf("%f",&sut.sco.li);
printf("\nput the hua:\t%d\n",sut.sco.hua);
printf("\nput the hua:\t");
scanf("%f",&sut.sco.hua);
}
void dellist(void)
{
char fla=0;
printf("\t\tpress n use name");
printf("\n\t\tpress l use number");
printf("\nyou char is :\t");
getchar();
while(1)
{
scanf("%c",&fla);
printf("%c\n",fla);
if(fla=='n'||fla=='l') break;
printf("\nput again:\t");
}
//不能执行。我加了getchar()后能执行,上面的getchar()接收回车键
switch(fla)
{
case 'n' : delname();break;
case 'l' : delnumber();break;
default : printf("you are wrong\n");
}
}
void delname(void)
{
int i=0;
char name[8];
char flag;
printf("put the name:\t");
scanf("%s",name);
for(;i<b;i++)
{
if(!strcmp(name,sut.name))
{
putout(i);
printf("press Y OR N");
getchar();
scanf("%c",&flag);
if(flag=='y') {move(i);return;}
}
}
if(i>=b) printf("sorry search is no\n");
}
void delnumber(void)
{
int i=0;
char number[8];
char flag;
printf("put the number:\t");
scanf("%s",number);
for(;i<b;i++)
{
if(!strcmp(number,sut.number))
{
putout(i);
printf("press Y OR N");
getchar();
scanf("%c",&flag);
if(flag=='y') {move(i);return;}
}
}
if(i>=b) printf("sorry search is no\n");
}
void move(int i)
{
for(;i<b-1;i++)
{
strcpy(sut.name,sut[i+1].name);
strcpy(sut.number,sut[i+1].number);
strcpy(sut.sex,sut[i+1].sex);
sut.sco.shu=sut[i+1].sco.shu;
sut.sco.li=sut[i+1].sco.li;
sut.sco.hua=sut[i+1].sco.hua;
sut.sco.average=sut[i+1].sco.average;
sut.flag=sut[i+1].flag;
}
}
void paixu(void)
{
char number[8];
int i=b-2;
strcpy(sut.name,sut[b-1].name);
strcpy(sut.number,sut[b-1].number);
strcpy(sut.sex,sut[b-1].sex);
sut.sco.shu=sut[b-1].sco.shu;
sut.sco.li=sut[b-1].sco.li;
sut.sco.hua=sut[b-1].sco.hua;
sut.sco.average=sut[b-1].sco.average;
strcpy(number,sut[b-1].number);
while(((strcmp1(number,sut.number))==1)&&i>=0)
{
strcpy(sut[i+1].name,sut.name);
strcpy(sut[i+1].number,sut.number);
strcpy(sut[i+1].sex,sut.sex);
sut[i+1].sco.shu=sut.sco.shu;
sut[i+1].sco.li=sut.sco.li;
sut[i+1].sco.hua=sut.sco.hua;
sut[i+1].sco.average=sut.sco.average;
sut[i+1].flag=sut.flag;
i=i-1;
}
strcpy(sut[i+1].name,sut.name);
strcpy(sut[i+1].number,sut.number);
strcpy(sut[i+1].sex,sut.sex);
sut[i+1].sco.shu=sut.sco.shu;
sut[i+1].sco.li=sut.sco.li;
sut[i+1].sco.hua=sut.sco.hua;
sut[i+1].sco.average=sut.sco.average;
sut[i+1].flag=1;
}
int strcmp1(char *buf1,char *buf2)
{
while(*buf1!='\0'||*buf2!='\0')
{
if(*buf1>*buf2) return 1;
if(*buf1<*buf2) return -1;
buf1++;
buf2++;
}
return 0;
} |
|