一道c++编程题,求指导错在哪了
#include"stdio.h"#include"stdlib.h"#include"time.h"#include"afxwin.h"#defineN100#defi...
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "afxwin.h"
#define N 100
#define M 6
void Input(int score[N][M]);
void Output(int score[N][M]);
void Modify(int score[N][M]);
void Sum(int score[N][M],int ranking[N]);
void Sort(int sum[N],int ranking[N]);
void main()
{
int score[N][M],sum[N]={0},ranking[N];;
int select;
srand((unsigned)time(0));
while(1)
{
system("cls");
printf("\t\t1----成绩输入\n");
printf("\t\t2----成绩输出\n");
printf("\t\t3----成绩修改\n");
printf("\t\t4----成绩查询\n");
printf("\t\t5----求总分\n");
printf("\t\t6----按总分排名\n");
printf("\t\t0----退出\n");
printf("\t\t\t请选择(0-6):");
scanf("%d",&select);getchar();
switch(select)
{
case 0:return;
case 1:Input(score);break;
case 2:Output(score);break;
case 3:Modify(score);break;
case 5:Sum(score,sum);break;
case 6:Sort(sum,ranking);break;
}
}
}
void Input(int score[N][M])
{
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
score[i][j]=rand()%70+31;
Output(score);
}
void sum(float score[101][6])
{
int i,j;
for(i=0;i<100;i++)
for(j=0;j<5;j++)score[i][5]+=score[i][j];
}
void average(float score[][6])
{
int i,j;
for(i=0;j<5;i++)
for(j=0;j<100;j++)score[100][i]+=score[i][j];
for(i=0;i<5;i++)score[100][i]=score[100][i]/100;
}
void Output(int score[N][M])
{
printf(" 座号 语文 数学 英语 物理 化学 总成绩 平均分\n");
for(int i=0;i<N;i++)
{
printf("%5d",i+1);
for(int j=0;j<M;j++)
printf("%5d",score[i][j]);
printf("\n");
}
getchar();
int j;
printf("语文 数学 英语 物理 化学 的平均成绩为\n");
for(j=0;j<5;j++)printf("%.2f",score[100][j]);
}
void Modify(int score[N][M])
{
int NO,k;
printf("请输入要修改的座号");
scanf("%d",&NO);getchar();
if(NO<=0||NO>100)
{
printf("输入错误");
getchar();
return;
}
for(int i=0;i<M;i++)
printf("%5d",score[NO-1][i]);
printf("请输入要修改的学科(1-6):");
scanf("%d",&k);
printf("请输入新成绩:");
scanf("%d",&score[NO-1][k-1]);
getchar();
}
void Sort(int sum[N],int ranking[N])
{
int s[N];
int NO[N];
for(int i=0;i<N;i++)
{
s[i]=sum[i];
NO[i]=i;
}
for(i=0;i<N-1;i++)
{
int k=i;
for(int j=i+1;j<N;j++)
{
if(s[j]>s[k]) k=j;
}
if(k!=i)
{
int t=s[i];s[i]=s[k];s[k]=t;
t=NO[i];NO[i]=NO[k];NO[k]=t;
}
}
for(i=0;i<N;i++)
ranking[NO[i]]=i+1;
for(i=0;i<N;i++)
printf("%d,%d\n",sum[i],ranking[i]);
getchar();
} 展开
#include "stdlib.h"
#include "time.h"
#include "afxwin.h"
#define N 100
#define M 6
void Input(int score[N][M]);
void Output(int score[N][M]);
void Modify(int score[N][M]);
void Sum(int score[N][M],int ranking[N]);
void Sort(int sum[N],int ranking[N]);
void main()
{
int score[N][M],sum[N]={0},ranking[N];;
int select;
srand((unsigned)time(0));
while(1)
{
system("cls");
printf("\t\t1----成绩输入\n");
printf("\t\t2----成绩输出\n");
printf("\t\t3----成绩修改\n");
printf("\t\t4----成绩查询\n");
printf("\t\t5----求总分\n");
printf("\t\t6----按总分排名\n");
printf("\t\t0----退出\n");
printf("\t\t\t请选择(0-6):");
scanf("%d",&select);getchar();
switch(select)
{
case 0:return;
case 1:Input(score);break;
case 2:Output(score);break;
case 3:Modify(score);break;
case 5:Sum(score,sum);break;
case 6:Sort(sum,ranking);break;
}
}
}
void Input(int score[N][M])
{
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
score[i][j]=rand()%70+31;
Output(score);
}
void sum(float score[101][6])
{
int i,j;
for(i=0;i<100;i++)
for(j=0;j<5;j++)score[i][5]+=score[i][j];
}
void average(float score[][6])
{
int i,j;
for(i=0;j<5;i++)
for(j=0;j<100;j++)score[100][i]+=score[i][j];
for(i=0;i<5;i++)score[100][i]=score[100][i]/100;
}
void Output(int score[N][M])
{
printf(" 座号 语文 数学 英语 物理 化学 总成绩 平均分\n");
for(int i=0;i<N;i++)
{
printf("%5d",i+1);
for(int j=0;j<M;j++)
printf("%5d",score[i][j]);
printf("\n");
}
getchar();
int j;
printf("语文 数学 英语 物理 化学 的平均成绩为\n");
for(j=0;j<5;j++)printf("%.2f",score[100][j]);
}
void Modify(int score[N][M])
{
int NO,k;
printf("请输入要修改的座号");
scanf("%d",&NO);getchar();
if(NO<=0||NO>100)
{
printf("输入错误");
getchar();
return;
}
for(int i=0;i<M;i++)
printf("%5d",score[NO-1][i]);
printf("请输入要修改的学科(1-6):");
scanf("%d",&k);
printf("请输入新成绩:");
scanf("%d",&score[NO-1][k-1]);
getchar();
}
void Sort(int sum[N],int ranking[N])
{
int s[N];
int NO[N];
for(int i=0;i<N;i++)
{
s[i]=sum[i];
NO[i]=i;
}
for(i=0;i<N-1;i++)
{
int k=i;
for(int j=i+1;j<N;j++)
{
if(s[j]>s[k]) k=j;
}
if(k!=i)
{
int t=s[i];s[i]=s[k];s[k]=t;
t=NO[i];NO[i]=NO[k];NO[k]=t;
}
}
for(i=0;i<N;i++)
ranking[NO[i]]=i+1;
for(i=0;i<N;i++)
printf("%d,%d\n",sum[i],ranking[i]);
getchar();
} 展开
2个回答
展开全部
#include <iostream>
#include <iomanip>
using namespace std;
struct Student {
static int _number;
int _math;
int _chinese;
int _english;
int _physics;
int _chemistry;
int _total;
double _average;
Student *p_next;
Student () { _number ++; }
}; /* --- end --- */
int Student::_number = 0; // count student number
Student *add_Student ( Student *p_list ) {
Student *p_student = new Student;
p_student -> _math = 0;
p_student -> _chinese = 0;
p_student -> _english = 0;
p_student -> _physics = 0;
p_student -> _chemistry = 0;
p_student -> _total = 0;
p_student -> _average = 0.0;
p_student -> p_next = p_list;
return p_student;
} /* --- end --- */
void input_Scores ( Student *p_student ) {
cout << "Please input the scores for student[" << p_student->_number << "]\n";
// input scores of each subject
cout << "Math:\t";
cin >> p_student -> _math;
cout << "Chinese:\t";
cin >> p_student -> _chinese;
cout << "English:\t";
cin >> p_student -> _english;
cout << "Physics:\t";
cin >> p_student -> _physics;
cout << "Chemistry:\t";
cin >> p_student -> _chemistry;
// calculate
p_student->_total = p_student->_math + p_student->_chinese + p_student->_english
+ p_student->_physics + p_student->_chemistry;
p_student->_average = (double)p_student->_total / 5.0;
} /* --- end --- */
void output_Scores ( Student *p_student ) {
Student *p_list = p_student;
cout << setw(4+4) << "MATH";
cout << setw(7+4) << "CHINESE";
cout << setw(7+4) << "ENGLISH";
cout << setw(7+4) << "PHYSICS";
cout << setw(9+4) << "CHEMISTRY";
cout << setw(5+4) << "TOTAL";
cout << setw(7+4) << "AVERAGE\n";
while ( p_list != nullptr ) {
cout << setw(4+4) << p_list->_math;
cout << setw(7+4) << p_list->_chinese;
cout << setw(7+4) << p_list->_english;
cout << setw(7+4) << p_list->_physics;
cout << setw(9+4) << p_list->_chemistry;
cout << setw(5+4) << p_list->_total;
cout << setw(7+4) << p_list->_average;
cout << endl;
p_list = p_list->p_next;
}
} /* --- end --- */
void sort_Scores ( Student *p_student, const int size ) {
int i = 0;
Student *p_list = p_student;
double classroom[size][7];
// transfer scores to external matrix
while ( p_list != nullptr ) {
classroom[i][0] = p_list->_math;
classroom[i][1] = p_list->_chinese;
classroom[i][2] = p_list->_english;
classroom[i][3] = p_list->_physics;
classroom[i][4] = p_list->_chemistry;
classroom[i][5] = p_list->_total;
classroom[i][6] = p_list->_average;
p_list = p_list->p_next;
++i;
}
for ( int i=0; i<size; i++ ) {
int index_of_biggest = i;
// find the smallest index of sub-matrix[][5]
for ( int j=i+1; j<size;j++ ) {
if ( classroom[j][5] > classroom[index_of_biggest][5] ) {
index_of_biggest = j;
}
}
if ( i != index_of_biggest ) {
// create a temporary array
int temp[7];
// swap matrix[i][] and matrix[index_of_smallest][]
for ( int k=0; k<7; k++ ) {
temp[k] = classroom[i][k];
classroom[i][k] = classroom[index_of_biggest][k];
classroom[index_of_biggest][k] = temp[k];
}
}
} /* -- end -- */
// display the sorted scores
cout << '\n' << "Sorted Scores\n";
cout << setw(4+4) << "MATH";
cout << setw(7+4) << "CHINESE";
cout << setw(7+4) << "ENGLISH";
cout << setw(7+4) << "PHYSICS";
cout << setw(9+4) << "CHEMISTRY";
cout << setw(5+4) << "TOTAL";
cout << setw(7+4) << "AVERAGE\n";
for ( int i=0; i<size; i++ ) {
cout << setw(4+4) << classroom[i][0];
cout << setw(7+4) << classroom[i][1];
cout << setw(7+4) << classroom[i][2];
cout << setw(7+4) << classroom[i][3];
cout << setw(9+4) << classroom[i][4];
cout << setw(5+4) << classroom[i][5];
cout << setw(7+4) << classroom[i][6];
cout << endl;
}
} /* --- end --- */
int main(int argc, char *argv[]) {
char is_continue = 'Y';
cout << "Press 'Y' to start or 'N' to end\n";
cin >> is_continue;
// initialize Student data structure
Student *p_student = nullptr;
while ( is_continue == 'Y') {
p_student = add_Student( p_student );
input_Scores( p_student );
cout << "Press 'Y' to continue or 'N' to end\n";
cin >> is_continue;
}
output_Scores( p_student );
sort_Scores( p_student, Student::_number );
return 0;
} /* --- end --- */
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询