南邮程序设计保龄球分数计算系统

  • 格式:doc
  • 大小:94.00 KB
  • 文档页数:18

保龄球分数计算一课题内容和要求【问题描述】本程序完成多名学生的保龄球计分。

用户依序输入每局所打的球数,程序会根据保龄球的计分规则计算所得到的分数,并根据用户要求完成所得分数的排序和查找。

【功能要求】(1)学生信息包括:学号,姓名,出生(年,月,日),得分。

(2)需要实现的功能1)建立学生信息结构数组2)从键盘输入并显示每局的球数3)求得并显示每名学生的分数4)按分数递减选择排序5)显示按分数排序后学生信息6)按学号查学生信息和分数(顺序查找法)7)将排序后的内容存为文件,以后显示时直接从文件中调出而不用再执行排序算法。

二,需求分析功能架构图1,记录学生信息1)建立学生信息结构数组2)学生信息包括:学号,姓名,出生(年,月,日),得分。

2,记录对应学生每局的分数并排序从键盘输入并显示每局的球数求得并显示每名学生的分数按分数递减选择排序显示按分数排序后学生信息3,生成文件保存每局分数并调用按学号查学生信息和分数(顺序查找法)将排序后的内容存为文件,以后显示时直接从文件中调出而不用再执行排序算法。

三、概要设计struct student{ /* 学生信息结构 */char no[9]; /* 学号 */char name[9]; /* 姓名 */struct date birthday; /* 出生日期 */int score; /* 保龄球得分 */};四、源程序代码#include "stdafx.h"#include "stdio.h"#include <string.h>#include <process.h>struct date{int year,month,day;};struct student{ /* 学生信息结构 */char no[9]; /* 学号 */char name[9]; /* 姓名 */struct date birthday; /* 出生日期 */ int score; /* 保龄球得分 */};#define N 20struct student stu[N];int count=0;//学生数FILE *sfp;//格式化显示分数信息void showscore(int *score){printf("这一局的分数信息\n");int i;for(i=1;i<11;i++){printf(" %d\t",i);}for(i=1;i<10;i++){printf("%-4d%-4d",1,2);}printf("1 2 3\n");for(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]);}elseif(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,test;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[co unt].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<count;i++)fprintf(fp,"%-16s%-16s%d\n",stu[i].no,stu[i].name,stu[i].score); }////求得并显示每个学生的分数的信息void scoreofstus(){int score[21];for(int i=0;i<count;i++){printf("请输入学号为%s的学生的瓶数:\n",stu[i].no);#ifdef Finputscorefile(score);#elseinputscore(score);#endifstu[i].score=fingerout(score);}//显示所有学生的信息printf("显示所有学生的信息\n");showstudent(stdout);}void sort(){////简单选择排序struct student t=stu[0];int min=0,j;for(int i=0;i<count;i++){for(j=0;j<count-i;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<count&&strcmp(stu[i].no,no)!=0)i++; if(i==count){printf("not find");return -1;}return i;}////////////////////////////////int main(){sfp=fopen("score.txt","w+");#ifdef Fcount=inputstudentfile();#elsecount=inputstudent();#endifscoreofstus();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;}五、测试数据及其结果分析1,测试学生数据的建立数据如下:学号姓名生日分数101 学生1 19910101 151102 学生2 19910102 200105 学生5 19910105 197测试项目:1、输入学生信息,2、输入球数计算结果进入程序需要先建立学生数据出现如下提示:Please input the information of the student.'end' for End.学号姓名year month day(用enter键隔开各值):按要求键如完整的数据后自动跳入下一学生输入。