#include <iostream>
#include<stdio.h>
using namespace std;
class Student
{
public:
float score;
int num;
char name[30];
public:
Student()
{
strcpy_s(this->name, "nameless");
this->num = 0;
this->score = 0;
}
int compare(float max)
{
if (this->score>max)
{
return 1;
}
else if (this->score == max)
{
return 2;
}
return 3;
}
};
int main()
{
int n = 0;
do {
printf("请输入学生人数(1-30之间):\n");
scanf_s("%d", &n);
} while (n<0|| n>30);
Student*pStudents = new Student[n]; //定义学生类数组记录并比较成绩
printf("请按学号,姓名,成绩的顺序输入学生信息\n");
for (int i = 0; i < n; i++)
{
printf("请输入地%d个学生的信息\n", i + 1);
scanf_s("%d,%d,%d", &pStudents[i].num, &pStudents[i].name, &pStudents[i].score);
Student*pStudents_M = new Student[1];
float Max_score = 0;
int Max_count = 1;
for (int i = 0; i < n; i++)
{
switch (pStudents[i].compare(Max_score))
{
case 1:
Max_score = pStudents[i].score;
Max_count = 1;
if (pStudents_M != NULL)
{
delete[]pStudents_M;
}
pStudents_M = new Student[1];
pStudents_M[0] = pStudents[1];
break;
case 2:
Student*pTemp = pStudents_M;
pStudents_M = new Student[++Max_count];
for (int j = 0; j < Max_count; j++)
{
pStudents_M[j] = pTemp[j];
}
pStudents_M[Max_count - 1] = pStudents[i];
delete[]pTemp;
break;
}
}
printf("当前最高成绩为%d,成绩最高者:\n", Max_score);
for (int i = 0; i < Max_count; i++)
{
printf("%d,%s,%d\n", pStudents_M[i].num, pStudents_M[i].name, pStudents_M[i].score);
}
delete[]pStudents;
delete[]pStudents_M;
return 0;
1、输入信息的for循环范围错了;
2、scanf_s要求“require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.”就是输入字符串时要指定缓冲区的大小。
1、输入信息的for循环范围错了;
2、scanf_s要求“require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.”就是输入字符串时要指定缓冲区的大小。