Student(char *ch,int *a,int b):name(ch),scores(a),numOfScores(b)
时间: 2024-03-19 21:41:31 浏览: 91
这是一个构造函数,它的参数是一个字符指针ch、一个整型指针a和一个整型变量b。它的作用是将ch指向的字符串赋值给name成员变量,将a指向的整型数组赋值给scores成员变量,将b赋值给numOfScores成员变量。这个构造函数可能属于一个名为"Student"的类,用于创建一个学生对象,并将学生的姓名、分数数组和分数数量存储在对象的成员变量中。
相关问题
请定义一个Student类,有4个属性: 1.char *name:姓名。 2.int numOfScores:课程数量 3.int *scores:所有课程的成绩。 4.int id:学生的编号。 只有3个方法: 1. 构造函数 2.析构函数 3.void Student::showStudent()方法:用于输出学生的信息。 请根据样例输出,写出该类的实现。 Input 输入分为多行。 第一行包含3个正整数M,N和P:其中M表明之后输入的测试用例数量;N表示每个人姓名的最大长度;P表示学生学习的课程的数量。 之后有M行,包含一个学生姓名(没有任何空白符)、P门课程的成绩。 Output 见样例。 注意:所有的输出两两之间用一个空格隔开,且每行输出的首尾都没有空格。 Sample Input 3 10 5 Tom 60 61 72 56 89 Jack 99 100 98 89 77 Mary 88 88 88 88 88 Sample Output A student whose name is "Tom" and id is 1 is created! This student is "Tom" whose id is 1. This student's scores are: 60 61 72 56 89 A student whose name is "Tom" and id is 1 is erased! A student whose name is "Jack" and id is 2 is created! This student is "Jack" whose id is 2. This student's scores are: 99 100 98 89 77 A student whose name is "Jack" and id is 2 is erased! A student whose name is "Mary" and id is 3 is created! This student is "Mary" whose id is 3. This student's scores are: 88 88 88 88 88 A student whose name is "Mary" and id is 3 is erased!
以下是该类的实现:
```C++
#include <iostream>
#include <cstring>
using namespace std;
class Student {
private:
char *name;
int numOfScores;
int *scores;
int id;
static int count; // 静态成员变量,表示当前已创建的学生数量
public:
Student(char *n, int nScores, int *s) {// 构造函数
name = new char[strlen(n) + 1];
strcpy(name, n);
numOfScores = nScores;
scores = new int[numOfScores];
for (int i = 0; i < numOfScores; i++) {
scores[i] = s[i];
}
count++; // 当有新的学生创建时,count加1,并且将此时的count值赋给该学生的id
id = count;
printf("A student whose name is \"%s\" and id is %d is created!\n", name, id);
}
~Student() { // 析构函数
printf("A student whose name is \"%s\" and id is %d is erased!\n", name, id);
delete[] name;
delete[] scores;
count--; // 当有学生被删除时,count减1
}
void showStudent() { // 输出学生信息
printf("This student is \"%s\" whose id is %d. This student's scores are: ", name, id);
for (int i = 0; i < numOfScores; i++) {
printf("%d", scores[i]);
if (i != numOfScores - 1) {
printf(" ");
}
}
printf("\n");
}
};
int Student::count = 0;
int main() {
int m, n, p;
cin >> m >> n >> p;
char name[n + 1];
int scores[p];
for (int i = 0; i < m; i++) {
cin >> name;
for (int j = 0; j < p; j++) {
cin >> scores[j];
}
Student *s = new Student(name, p, scores);
s->showStudent();
delete s; // 释放创建的学生对象,触发析构函数
}
return 0;
}
```
如何修改该代码#include <iostream> #include <fstream> using namespace std; struct student { char name[30]; int* scores; int avg; }; student* input(char* file, int& stunum, int& hwnum) { ifstream in(file, ios::in); int itemnum; in >> stunum >> hwnum >> itemnum; student* stulist = new student[stunum]; for (int i = 0; i < stunum; i++) { stulist[i].scores = new int[hwnum]; for (int j = 0; j < hwnum; j++) { stulist[i].scores[j] = 0; } } int k; for (int i = 0; i < hwnum; i++) { char name[100]; int time, score; in >> name >> time >> score; int j; for ( j = 0; j < k; j++) { if (strcmp(name, stulist[i].name) == 0) break; } if (j < k) { stulist[k].scores[time - 1] = score; } else { strcpy(stulist[k].name, name); stulist[k].scores[time - 1] = score; k++; } } in.close(); return stulist; } int main() { int stunum, hwnum; student* stulist = input("score.txt", stunum, hwnum); for (int i = 0; i < stunum; i++) { cout << stulist[i].name << "\t"; for (int j = 0; j < hwnum; j++) cout << stulist[i].scores[j] << "\t"; cout << endl; } return 0; }
这段代码可以通过以下方式进行修改:
1. 在程序开头添加头文件#include <cstring>,以使用strcmp函数。
2. 在函数input的开头将变量k初始化为0。
3. 在函数input中,将stulist[k]改为stulist[j]。
4. 将程序中的所有空格、制表符和换行符替换为单个空格,以保持代码的整洁性。
修改后的代码如下:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct student {
char name[30];
int* scores;
int avg;
};
student* input(char* file, int& stunum, int& hwnum) {
ifstream in(file, ios::in);
int itemnum;
in >> stunum >> hwnum >> itemnum;
student* stulist = new student[stunum];
for (int i = 0; i < stunum; i++) {
stulist[i].scores = new int[hwnum];
for (int j = 0; j < hwnum; j++) {
stulist[i].scores[j] = 0;
}
}
int k = 0;
for (int i = 0; i < hwnum; i++) {
char name[100];
int time, score;
in >> name >> time >> score;
int j;
for (j = 0; j < k; j++) {
if (strcmp(name, stulist[j].name) == 0)
break;
}
if (j < k) {
stulist[j].scores[time - 1] = score;
}
else {
strcpy(stulist[k].name, name);
stulist[k].scores[time - 1] = score;
k++;
}
}
in.close();
return stulist;
}
int main() {
int stunum, hwnum;
student* stulist = input("score.txt", stunum, hwnum);
for (int i = 0; i < stunum; i++) {
cout << stulist[i].name << "\t";
for (int j = 0; j < hwnum; j++)
cout << stulist[i].scores[j] << "\t";
cout << endl;
}
return 0;
}
阅读全文