花了不少时间!,为了程序好读,分了很多模块,多加点分啊,谢啦先
//// 为了便于调试,把从键盘输的数据放到了文件里,到属性--》C/C++ -》
//////编译器命令行选项里加/DF就可以使用文件了
////文件格式,百度死活不让我帖出来,说里边有广告,我汗。
////注意student.txt里字符串后跟回车,整数后面跟空格就行了。end结尾
////
////调用关系
////scoreofstus|<--inputscore<--showscore
//// |<--fingerout
//// |<--showstudent
#include "stdafx.h"
#include "stdio.h"
#include
#include
struct date{
int year,month,day;
};
struct student{ /* 学生信息结构 */
char no[9]; /* 学号 */
char name[9]; /* 姓名 */
struct date birthday; /* 出生日期 */
int score; /* 保龄球得分 */
};
#define N 20
struct student stu[N];
int count=0;//学生数
FILE *sfp;
//格式化显示分数信息
void showscore(int *score)
{
printf("这一局的分数信息\n");
for(int i=1;i<11;i++){
printf(" %d\t",i);
}
for(int i=1;i<10;i++)
{
printf("%-4d%-4d",1,2);
}
printf("1 2 3\n");
for(int i=0;i<10;i++)
{
if(i==9)
{
printf("%-3d",score[i*2]);
if(score[i*2]==10)
{
printf("%-2c",'-');
printf("%-3d",score[i*2+2]);
}
else
if(score[i*2]+score[i*2+1]==10)
{
printf("%-2c",'-');
printf("%-3d",score[i*2+2]);
}
else
{
printf("%-2d",score[i*2+1]);
printf("%-3c",'-');
}
break;
}
printf("%-4d",score[i*2]);
if(score[i*2]==10)printf("%-4c",'-');
else printf("%-4d",score[i*2+1]);
}
}
//输入分数
void inputscore(int * score)
{
int s1,s2,s3;
for(int i=0;i<10;i++)
{
printf("Please input the score of the round%2d:\n",i+1);
scanf("%d",&s1);
if(s1!=10)
{
printf("Please input the score of the second goal in round%2d:\n",i+1);
scanf("%d",&s2);
}
else s2=0;
score[i*2]=s1;score[i*2+1]=s2;
if(i==9&&(s1+s2)==10)
{
printf("Please input the score of the third goal in round%2d:\n",i+1);
scanf("%d",&s3);
}
else s3=0;
score[20]=s3;
}
}
void inputscorefile(int * score)
{
int s1,s2,s3;
for(int i=0;i<10;i++)
{
//printf("Please input the score of the round%2d:\n",i+1);
fscanf(sfp,"%d",&s1);
if(s1!=10)
{
//printf("Please input the score of the second goal in round%2d:\n",i+1);
fscanf(sfp,"%d",&s2);
}
else s2=0;
score[i*2]=s1;score[i*2+1]=s2;
if(i==9&&(s1+s2)==10)
{
//printf("Please input the score of the third goal in round%2d:\n",i+1);
fscanf(sfp,"%d",&s3);
}
else s3=0;
score[20]=s3;
}
showscore(score);
}
int fingerout(int *score)
{
int sum=0,s1,s2,s3;
for(int i=0;i<10;i++)
{
if(score[i*2]==10||score[19]+score[18]==10)sum+=score[i*2]+score[i*2+2];
sum+=score[i*2+1];
}
return sum;
}
//int gettotalscore(int roundscore[21])
//{
// //inputscore(roundscore);
// return fingerout(roundscore);
//}
/////输入学生信息
int inputstudent()
{
int count=0;//学生总人数
while(1)
{
printf("Please input the information of the student.'end' for End.学号 姓名 year month day(用enter键隔开各值):");
scanf("%s",stu[count].no);
if(strcmp(stu[count].no,"end")==0)break;
scanf("%s%d%d%d",stu[count].name,&stu[count].birthday.year,&stu[count].birthday.month,&stu[count].birthday.day);
count++;
}
return count;
}
int inputstudentfile()
{
FILE * filein;
filein=fopen("student.txt","r");
int count=0;//学生总人数
while(1)
{
//printf("Please input the information of the student.'end' for End.学号 姓名 year month day(用enter键隔开各值):");
fscanf(filein,"%s",stu[count].no);
if(strcmp(stu[count].no,"end")==0)break;
fscanf(filein,"%s%d%d%d",stu[count].name,&stu[count].birthday.year,&stu[count].birthday.month,&stu[count].birthday.day);
count++;
}
//count--;
fclose(filein);
return count;
}
//显示学生分数
void showstudent(FILE *fp)
{
fprintf(fp,"%-16s%-16s%-16s\n","学生学号","学生姓名","学生分数");
for(int i=0;i
}
////求得并显示每个学生的分数的信息
void scoreofstus()
{
int score[21];
for(int i=0;i
printf("请输入学号为%s的学生的瓶数:\n",stu[i].no);
#ifdef F
inputscorefile(score);
#else
inputscore(score);
#endif
stu[i].score=fingerout(score);
}
//显示所有学生的信息
printf("显示所有学生的信息\n");
showstudent(stdout);
}
void sort()
{
////简单选择排序,汗
struct student t=stu[0];
int min=0,j;
for(int i=0;i
for(j=0;j
if(stu[min].score>stu[j].score)min=j;
}
j--;
t=stu[j];
stu[j]=stu[min];
stu[min]=t;
}
}
int search(char * no)
{
int i=0;
while(i
{
printf("not find");
return -1;
}
return i;
}
////////////////////////////////
int main()
{
sfp=fopen("score.txt","r");
#ifdef F
count=inputstudentfile();
#else
count=inputstudent();
#endif
scoreofstus();
sort();
showstudent(stdout);
FILE * fp;
fp=fopen("data.dat","w");
showstudent(fp);
fclose(fp);
fclose(sfp);
while(1)
{
char no[9];
int select=0;
printf("请选择:\n");
printf("0.显示学生信息:\n");
printf("1.查找:\n");
printf("2.退出\n");
scanf("%d",&select);
switch(select)
{
case 0:showstudent(stdout);
break;
case 1:
{
printf("请输入学号:");
scanf("%s",no);
int ii=search(no);
printf("%-16s%-16s%-16s\n","学生学号","学生姓名","学生分数");
printf("%-16s%-16s%d\n",stu[ii].no,stu[ii].name,stu[ii].score);
break;
}
case 2:exit(0);
default:
{
printf("输入有误,请重新输入:\n");
break;
}
}
}
return 0;
}