1.1②?運(yùn)動(dòng)會(huì)分?jǐn)?shù)統(tǒng)計(jì)
【問(wèn)題描述】
參加運(yùn)動(dòng)會(huì)的n個(gè)學(xué)校編號(hào)為1~n。比賽分成m個(gè)男子項(xiàng)目和w個(gè)女子項(xiàng)目,項(xiàng)目編號(hào)分別為1~m和m+1~m+w。由于各項(xiàng)目參加人數(shù)差別較大,有些項(xiàng)目取前五名,得分順序?yàn)?,5,3,2,1;還有些項(xiàng)目只取前三名,得分順序?yàn)?;3,2。寫(xiě)一個(gè)統(tǒng)計(jì)程序產(chǎn)生各種成績(jī)單和得分報(bào)表。
【基本要求】
產(chǎn)生各學(xué)校的成績(jī)單,內(nèi)容包括各校所取得的每項(xiàng)成績(jī)的項(xiàng)目號(hào)、名次(成績(jī))、姓名和得分;產(chǎn)生團(tuán)體總分報(bào)表,內(nèi)容包括校號(hào)、男子團(tuán)體總分、女子團(tuán)體總分和團(tuán)體總分。
【測(cè)試數(shù)據(jù)】
對(duì)于n=4,m=3,w=2,編號(hào)為奇數(shù)的項(xiàng)目取前五名,編號(hào)為偶數(shù)的項(xiàng)目取前三名,設(shè)計(jì)一組實(shí)例數(shù)據(jù)。
【實(shí)現(xiàn)提示】
可以假設(shè)n≤20,m≤30,w≤20,姓名長(zhǎng)度不超過(guò)20個(gè)字符。每個(gè)項(xiàng)目結(jié)束時(shí),將其編號(hào)、類(lèi)型符(區(qū)分取前五名還是前三名)輸入,并按名次順序輸入運(yùn)動(dòng)員姓名、校名(和成績(jī))。
【選作內(nèi)容】
允許用戶(hù)指定某項(xiàng)目采取其他名次取法。
廢話(huà)不多說(shuō),直接上代碼?。g迎私信和評(píng)論!)
#include<math.h>
#include<process.h>
#include <iostream>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#define N 20 //學(xué)校最大數(shù)目
#define M 30 //男子項(xiàng)目最大數(shù)
#define W 20 //女子項(xiàng)目最大數(shù)
//存放項(xiàng)目信息的結(jié)構(gòu)體
typedef struct
{
int inum;//項(xiàng)目編號(hào)
int top;//取名次的數(shù)目
int range[5];//名次
int mark[5];//分?jǐn)?shù)
}itemnode;
//存放學(xué)校信息的結(jié)構(gòu)體
typedef struct
{
int snum;//學(xué)校編號(hào)
int score;//學(xué)??偡? int mscore;//男子總分
int wscore;//女子總分
itemnode t[M + W];//項(xiàng)目數(shù)組
}snode;
snode a[N];//定義一個(gè)學(xué)校數(shù)組
//菜單函數(shù)
void menu(int n, int m, int w)
{//n代表學(xué)校數(shù),m代表男子數(shù),w代表女子數(shù)
int c;
void input(int n, int m, int w);//輸入功能
void output(int n, int m, int w);//輸出功能
void sortput(int n, int m, int w);//排序輸出
void search(int n, int m, int w);//查詢(xún)功能
cout<<"\t\t\t歡迎使用\t\t\t\t\t"<<endl;
cout<<"運(yùn)動(dòng)會(huì)分?jǐn)?shù)統(tǒng)計(jì)系統(tǒng)"<<endl;
cout << endl;
cout<<"1.信息輸入"<<endl;
cout<<"2.統(tǒng)計(jì)輸出" << endl;
cout <<"3.排序輸出" << endl;
cout<<"4.信息查詢(xún)"<<endl;
cout<<"5.退出系統(tǒng)"<<endl;
cout << endl;
cout<<"======================================================="<<endl;
cout << endl;
cout<<"請(qǐng)輸入您想要實(shí)現(xiàn)的功能(0--4):"<<endl;
cin >> c;
switch (c) {
case 1:
input(n, m, w);
break;
case 2:
output(n, m, w);
break;
case 3:
sortput(n, m, w);
break;
case 4:
search(n, m, w);
break;
case 5:
cout<<"感謝使用,祝您天天開(kāi)心!!"<<endl;
exit(0);//正常退出
default:
cout<<"您輸入有誤,請(qǐng)重新輸入!";
menu(n, m, w);
}
}
//將信息寫(xiě)入文件中
void savetofile()
{
FILE* fp;//定義一個(gè)文件指針
int i;
if (NULL == (fp = fopen("file.txt", "w"))) {
cout<<"打開(kāi)文件失敗!"<<endl;
return;
}
for (i = 0; i < N; i++) {
if ('\0' != a[i].snum)
if (fwrite(&a[i], sizeof(snode), 1, fp) != 1) {
cout << "存入信息失敗!" << endl;
return;
}
}
fclose(fp);//關(guān)閉文件
}
//將信息從文件里取出
void readfromfile()
{
int i;
FILE* fp;
if ((fp = fopen("file.txt", "rb")) == NULL) {
cout<<"文件打開(kāi)失敗!"<<endl;
return;
}
for (i = 0; i < N; i++) {
fread(&a[i], sizeof(snode), 1, fp);
}
fclose(fp);
}
//信息輸入功能
void input(int n, int m, int w)
{
int i, j, s, k, q = 1;
for (i = 0; i < n; i++) {
cout<<"請(qǐng)輸入學(xué)校的編號(hào):"<<endl;
cin >> a[i].snum;
for (j = 0; j < m + w; j++) {//總的項(xiàng)目的輸入
cout << "請(qǐng)輸入項(xiàng)目編號(hào):";
cin>> a[i].t[j].inum;
/*
cout << "請(qǐng)輸入該項(xiàng)目取前3還是前5(輸入3或5):";
cin >> a[i].t[j].top;
*/
if (a[i].t[j].inum % 2 == 0) {
cout<<"編號(hào)為偶數(shù)取前三" << endl;
cout << "獲得的名次的個(gè)數(shù)(1--5):";
}
else if (a[i].t[j].inum % 2 == 1) {
cout <<"編號(hào)為奇數(shù)的項(xiàng)目取前五"<<endl;
cout << "獲得的名次的個(gè)數(shù)(1--5):";
}
else {
cout << "輸入有誤!程序退出....";
return;
}
cin >> k;//輸入獲得名次的個(gè)數(shù)
for (s = 0; s < k; s++) {
//if (3 == a[i].t[j].top) {
if(a[i].t[j].inum % 2 == 0){
cout<<"請(qǐng)輸入獲得的名次(1--3):";
}
else {
cout<<"請(qǐng)輸入獲得的名次(1--5):";
}
cin >> a[i].t[j].range[s];//輸入所獲得的名次的信息
}
cout << endl;
}
}
for (i = 0; i < n; i++) {
//初始化分?jǐn)?shù)
a[i].score = 0;//學(xué)??偡? a[i].mscore = 0;//男子總分
a[i].wscore = 0;//女子總分
}
for (i = 0; i < n; i++) {
for (j = 0; j < m + w; j++) {
cout << "項(xiàng)目" << j + 1 << "取得是前3還是前5(輸入3或5) :";
cin >> a[i].t[j].top;
for (s = 0; s < 5; s++) {
if (a[i].t[j].top == 3) {
switch (a[i].t[j].range[s]) {
case 0:
a[i].t[j].mark[s] = 0;
break;
case 1:
a[i].t[j].mark[s] = 5;
break;
case 2:
a[i].t[j].mark[s] = 3;
break;
case 3:
a[i].t[j].mark[s] = 2;
break;
}
}
else if (a[i].t[j].top == 5) {
switch (a[i].t[j].range[s]) {
case 0:
a[i].t[j].mark[s] = 0;
break;
case 1:
a[i].t[j].mark[s] = 7;
break;
case 2:
a[i].t[j].mark[s] = 5;
break;
case 3:
a[i].t[j].mark[s] = 3;
break;
case 4:
a[i].t[j].mark[s] = 2;
break;
case 5:
a[i].t[j].mark[s] = 1;
break;
}
}
/*else {
cout << "信息輸入錯(cuò)誤!程序退出" << endl;
cout << endl;
exit(0);
}*/
a[i].score = a[i].score + a[i].t[j].mark[s];//學(xué)??偡? if (j < m) {
a[i].mscore = a[i].mscore + a[i].t[j].mark[s];
}
else {//女子總分
a[i].wscore = a[i].wscore + a[i].t[j].mark[s];
}
}
}
}
cout<<"輸入完畢!(返回菜單請(qǐng)輸入1):";
cin >> q;
cout << endl;
if (q != 1) {
cout<<"不能再添加信息了!";
}
cout << endl;
savetofile();//保存文件
menu(n, m, w);
}
#if(1)
void output(int n, int m, int w) /*2.統(tǒng)計(jì)輸出*/
{
readfromfile();
int i, j, s, q = 0;
for (i = 0; i < n; i++) /*顯示結(jié)果*/
{
cout << "學(xué)校編號(hào):" << a[i].snum << endl;
cout << "學(xué)??偡?" << a[i].score << endl;
cout << "男子總分" << a[i].mscore <<" " << "女子總分" << a[i].wscore << endl;
for (j = 0; j < m + w; j++)
{
// cout<<"項(xiàng)目編號(hào):%d 所取名次數(shù)量:%d\n", a[i].t[j].inum, a[i].t[j].top;
cout << "項(xiàng)目編號(hào):" << a[i].t[j].inum <<" " << "所取名次取前:" << a[i].t[j].top<<"名"<< endl;
for (s = 0; s < 5; s++)
{
if (a[i].t[j].range[s] != 0)
cout<<"名次:"<< a[i].t[j].range[s] <<" " << "分?jǐn)?shù):"<< a[i].t[j].mark[s]<<" "<<endl;
}
}
cout << endl;
}
cout<<"\n";
cout<<"統(tǒng)計(jì)完畢!返回? 1是 2否"; /*返回菜單*/
cin >> q;
cout << endl;
if (q != 1)
cout<<"統(tǒng)計(jì)已經(jīng)結(jié)束!";
cout << endl;
menu(n, m, w);
}
#endif
//排序輸出
void sortput(int n, int m, int w)//n為學(xué)校數(shù),m為男子數(shù),w為女子數(shù)
{
readfromfile();
int c, i, j, k, q = 0;
int temp[N]={};
cout << "\t**************排序輸出系統(tǒng)**************" << endl;
cout << endl;
cout << "\t\t****1.按學(xué)校編號(hào)輸出****" << endl;
cout<<"\t\t****2.按學(xué)??偡州敵?***"<<endl;
cout<<"\t\t****3.按男子總分輸出****"<<endl;
cout<<"\t\t****4.按女子總分輸出****"<<endl;
cout<<"======================================================="<<endl;
cout << endl;
do {
cout<<"請(qǐng)選擇您想實(shí)現(xiàn)的功能的編號(hào)(1--4):";
cin >> c;
switch (c) {
case 1:
for (i = 0; i < n; i++) {
temp[i] = i;
}
//用的是冒泡排序輸出
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[temp[i]].snum > a[j].snum) {
k = temp[i];
temp[i] = temp[j];
temp[j] = k;
}
}
}
for (i = 0; i < n; i++) {
//cout<<"學(xué)校標(biāo)號(hào):%d 學(xué)校總分:%d 男子總分:%d 女子總分:%d\n", a[temp[i]].snum, a[temp[i]].score, a[temp[i]].mscore, a[temp[i]].wscore;
cout << "學(xué)校標(biāo)號(hào):" << a[temp[i]].snum << " " << "學(xué)??偡?" << a[temp[i]].score << endl;
cout << "男子總分" << a[temp[i]].mscore << " " << "女子總分:" << a[temp[i]].wscore << endl;
}
break;
case 2:
for (i = 0; i < n; i++) {
temp[i] = i;
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[temp[i]].score < a[j].score) {
k = temp[i];
temp[i] = temp[j];
temp[j] = k;
}
}
}
for (i = 0; i < n; i++) {
// cout<<"學(xué)校編號(hào):%d 學(xué)??偡?%d 男子總分:%d 女子總分:%d\n", a[temp[i]].snum, a[temp[i]].score, a[temp[i]].mscore, a[temp[i]].wscore;
cout << "學(xué)校標(biāo)號(hào):" << a[temp[i]].snum << " " << "學(xué)??偡?" << a[temp[i]].score << endl;
cout << "男子總分" << a[temp[i]].mscore << " " << "女子總分:" << a[temp[i]].wscore << endl;
}
break;
case 3:
for (i = 0; i < n; i++) {
temp[i] = i;
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[temp[i]].mscore < a[j].mscore) {
k = temp[i];
temp[i] = temp[j];
temp[j] = k;
}
}
}
for (i = 0; i < n; i++) {
printf("學(xué)校編號(hào):%d 學(xué)??偡?%d 男團(tuán)總分:%d 女團(tuán)總分:%d\n", a[temp[i]].snum, a[temp[i]].score, a[temp[i]].mscore, a[temp[i]].wscore);
}
break;
case 4:
for (i = 0; i < n; i++) {
temp[i] = i;
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[temp[i]].wscore < a[j].wscore) {
k = temp[i];
temp[i] = temp[j];
temp[j] = k;
}
}
}
for (i = 0; i < n; i++) {
printf("學(xué)校編號(hào):%d 學(xué)??偡?%d 男團(tuán)總分:%d 女團(tuán)總分:%d\n", a[temp[i]].snum, a[temp[i]].score, a[temp[i]].mscore, a[temp[i]].wscore);
}
break;
default:
printf("您的輸入有誤!請(qǐng)從新輸入...");
}
printf("請(qǐng)選擇 1.返回主菜單 0.繼續(xù)");
scanf_s("%d", &q);
printf("\n");
}
//=======================
while (0 == q);
printf("\n");
//=======================
if (q != 0) {
menu(n, m, w);
}
}
//查詢(xún)功能
void search(int n, int m, int w)
{
readfromfile();
int c, i, j, k, d, l, q = 0;
cout<<"\t****************查詢(xún)系統(tǒng)****************"<<endl;
cout << endl;
cout << "\t\t****1.按學(xué)校編號(hào)查詢(xún)****"<<endl;
cout << "\t\t****2.按項(xiàng)目編號(hào)查詢(xún)****" << endl;
cout << "======================================================="<<endl;
cout << endl;
do
{
k = -1; d = -1; l = -1;
cout<<"請(qǐng)選擇要實(shí)現(xiàn)功能的編號(hào)(1--2):";
//scanf_s("%d", &c);
cin >> c;
switch (c) {
case 1:
cout<<"要查詢(xún)的學(xué)校編號(hào):"; /*查找學(xué)校編號(hào)下標(biāo)*/
// scanf_s("%d", &c);
cin >> c;
for (i = 0; i < n; i++) {
if (c == a[i].snum) {
k = i;
}
}
if (-1 == k) {
cout<<"錯(cuò)誤:這個(gè)學(xué)校沒(méi)有參加此次運(yùn)動(dòng)會(huì)!"<<endl;
}
else {
cout<<"要查詢(xún)的項(xiàng)目編號(hào):"; /*查找項(xiàng)目編號(hào)下標(biāo)*/
//scanf_s("%d", &c);
cin >> c;
for (j = 0; j < m + w; j++) {
if (c == a[k].t[j].inum) {
d = j;
}
}
if (-1 == d) {
cout<<"此次運(yùn)動(dòng)會(huì)沒(méi)有這個(gè)項(xiàng)目"<<endl;
}
else {
//cout<<"這個(gè)項(xiàng)目取前 %d名,該學(xué)校的成績(jī)?nèi)缦?\n", a[k].t[d].top);
cout << "這個(gè)項(xiàng)目取前" << a[k].t[d].top << "名,該學(xué)校的成績(jī)?nèi)缦?" << endl;
for (i = 0; i < 5; i++) {
if (a[k].t[d].range[i] != 0) {
//cout<<"名次:%d\n", a[k].t[d].range[i];
cout << "名次:" << a[k].t[d].range[i];
}
}
}
}
break;
case 2:
cout<<"要查詢(xún)的項(xiàng)目編號(hào):"; /*查找項(xiàng)目編號(hào)下標(biāo)*/
//scanf_s("%d", &c);
cin >> c;
for (i = 0; i < n; i++) {
for (j = 0; j < m + w; j++) {
if (c == a[i].t[j].inum) {
l = j;
}
if (-1 == l) {
cout<<"此次運(yùn)動(dòng)會(huì)沒(méi)有該項(xiàng)目";
}
else {
//printf("該項(xiàng)目取前 %d名,取得名次的學(xué)校\n", a[0].t[l].top);
cout << "該項(xiàng)目取前a[0].t[l].top名,取得名次的學(xué)校" << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < 5; j++) {
if (a[i].t[l].range[j] != 0) {
//cout<<"學(xué)校編號(hào):%d,名次:%d\n", a[i].snum, a[i].t[l].range[j]);
cout << "學(xué)校編號(hào):" << a[i].snum << " " << ", 名次:" << a[i].t[l].range[j] << endl;
}
}
}
}
}
}
break;
default:
cout << "輸入錯(cuò)誤,請(qǐng)重試!" << endl;
}
cout<<"請(qǐng)選擇:1.返回主菜單 0.繼續(xù)";
//scanf_s("%d", &q);
cin >> q;
//printf("\n");
cout << endl;
} while (0 == q);
//printf("\n");
cout << endl;
if (q != 0) {
menu(n, m, w);
}
}
//主函數(shù)
int main()
{
int n, m, w;//n為學(xué)校個(gè)數(shù),m為男子數(shù),w為女子數(shù)
cout<<"\t\t\t歡迎使用\t\t\t\t"<<endl;
cout << endl;
cout << "\t***********運(yùn)動(dòng)會(huì)分?jǐn)?shù)統(tǒng)計(jì)系統(tǒng)***********" << endl;
cout << endl;
cout<<"請(qǐng)先輸入運(yùn)動(dòng)會(huì)主要信息"<<endl;
cout<<"輸入學(xué)校個(gè)數(shù):";
cin >> n;
cout<<"輸入男子項(xiàng)目個(gè)數(shù):";
cin >> m;
cout<<"輸入女子項(xiàng)目個(gè)數(shù):";
cin >> w;
menu(n, m, w);
}
結(jié)果截圖?
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-775126.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-775126.html
到了這里,關(guān)于數(shù)據(jù)結(jié)構(gòu)課設(shè)----運(yùn)動(dòng)會(huì)分?jǐn)?shù)統(tǒng)計(jì)系統(tǒng)(C++版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!