代码#include<string> #include<iostream> #include<malloc.h> using namespace std; class Student { public: string Name; int Age; string Type; Student(){} Student(string a, int b):Type("Student") { Name = a; Age = b; } void print(string a) { if(a=="Name") cout<<Name<<endl; else if(a=="Age") cout<<Age<<endl; else if(a=="Type") cout<<Type<<endl; else cout<<"none"; } }; class Undergraduate : public Student { public: string Specialty; Undergraduate(){} Undergraduate(string a, int b, string c) : Student (a,b) { Type = "Undergraduate"; Specialty = c; } void print(string a) { if(a=="Name") cout<<Name<<endl; else if(a=="Age") cout<<Age<<endl; else if(a=="Type") cout<<Type<<endl; else if(a=="Specialty") cout<<Specialty<<endl; else cout<<"none"<<endl; } }; class Graduate : public Student { public: string Direction; Graduate(){} Graduate(string a, int b, string c) : Student(a, b) { Type = "Graduate"; Direction = c; } void print(string a) { if(a=="Name") cout<<Name<<endl; else if(a=="Age") cout<<Age<<endl; else if(a=="Type") cout<<Type<<endl; else if(a=="Direction") cout<<Direction<<endl; else cout<<"none"<<endl; } }; int main() { int i = 0; //输入多少条 cin>>i; int j1 = 0; int j2 = 0; string Name; int Age; string Type; string other; void **pp = (void **)malloc(4*i); //变量类型不同,用void指针 string *ptypes = (string *)malloc(100); //配合使用 Graduate a; Undergraduate b; Student c; for(j1=0;j1<i;j1++) { cin>>Name>>Age>>Type; if(Type=="Graduate") { pp[j1] = malloc(sizeof(Graduate)); cin>>other; a = Graduate(Name,Age,other); *((Graduate *)pp[j1]) = a; ptypes[j1] = "Graduate"; } else if(Type=="Undergraduate") { pp[j1] = malloc(sizeof(Undergraduate)); cin>>other; b = Undergraduate(Name,Age,other); *((Undergraduate *)pp[j1]) = b; ptypes[j1] = "Undergraduate"; } else { pp[j1] = malloc(sizeof(Student)); c = Student(Name,Age); *((Student *)pp[j1]) = c; ptypes[j1] = "Student"; } } int k = 0; //输出多少条 cin>>k; string put_out; for(j1=0;j1<k;j1++) { cin>>Name; cin>>put_out; for(j2=0;j2<=i;j2++) { if(ptypes[j2]=="Graduate") { Graduate *P = (Graduate *)pp[i]; if(Name==P->Name) P->print(put_out); } else if(ptypes[j2]=="Undergraduate") { Undergraduate *P = (Undergraduate *)pp[i]; if(Name==P->Name) P->print(put_out); } else { Student *P = (Student *)pp[i]; if(Name==P->Name) P->print(put_out); } if(j2==i) { cout<<"none"<<endl; break; } } } return 0; }为什么无法运行,如果要修改,该怎么修改?
时间: 2024-03-04 21:53:14 浏览: 21
这段代码无法运行的原因是在第二个for循环中,判断条件应该是 j2<i,而不是 j2<=i。同时,在判断指针类型时,应该使用 j2 而不是 i。修改后的代码如下:
```
#include <string>
#include <iostream>
#include <malloc.h>
using namespace std;
class Student {
public:
string Name;
int Age;
string Type;
Student(){}
Student(string a, int b):Type("Student") {
Name = a;
Age = b;
}
void print(string a) {
if(a=="Name")
cout<<Name<<endl;
else if(a=="Age")
cout<<Age<<endl;
else if(a=="Type")
cout<<Type<<endl;
else
cout<<"none";
}
};
class Undergraduate : public Student {
public:
string Specialty;
Undergraduate(){}
Undergraduate(string a, int b, string c) : Student (a,b) {
Type = "Undergraduate";
Specialty = c;
}
void print(string a) {
if(a=="Name")
cout<<Name<<endl;
else if(a=="Age")
cout<<Age<<endl;
else if(a=="Type")
cout<<Type<<endl;
else if(a=="Specialty")
cout<<Specialty<<endl;
else
cout<<"none"<<endl;
}
};
class Graduate : public Student {
public:
string Direction;
Graduate(){}
Graduate(string a, int b, string c) : Student(a, b) {
Type = "Graduate";
Direction = c;
}
void print(string a) {
if(a=="Name")
cout<<Name<<endl;
else if(a=="Age")
cout<<Age<<endl;
else if(a=="Type")
cout<<Type<<endl;
else if(a=="Direction")
cout<<Direction<<endl;
else
cout<<"none"<<endl;
}
};
int main() {
int i = 0; //输入多少条
cin>>i;
int j1 = 0;
int j2 = 0;
string Name;
int Age;
string Type;
string other;
void **pp = (void **)malloc(4*i); //变量类型不同,用void指针
string *ptypes = (string *)malloc(100); //配合使用
Graduate a;
Undergraduate b;
Student c;
for(j1=0;j1<i;j1++) {
cin>>Name>>Age>>Type;
if(Type=="Graduate") {
pp[j1] = malloc(sizeof(Graduate));
cin>>other;
a = Graduate(Name,Age,other);
*((Graduate *)pp[j1]) = a;
ptypes[j1] = "Graduate";
} else if(Type=="Undergraduate") {
pp[j1] = malloc(sizeof(Undergraduate));
cin>>other;
b = Undergraduate(Name,Age,other);
*((Undergraduate *)pp[j1]) = b;
ptypes[j1] = "Undergraduate";
} else {
pp[j1] = malloc(sizeof(Student));
c = Student(Name,Age);
*((Student *)pp[j1]) = c;
ptypes[j1] = "Student";
}
}
int k = 0; //输出多少条
cin>>k;
string put_out;
for(j1=0;j1<k;j1++) {
cin>>Name;
cin>>put_out;
for(j2=0;j2<i;j2++) {
if(ptypes[j2]=="Graduate") {
Graduate *P = (Graduate *)pp[j2];
if(Name==P->Name) P->print(put_out);
} else if(ptypes[j2]=="Undergraduate") {
Undergraduate *P = (Undergraduate *)pp[j2];
if(Name==P->Name) P->print(put_out);
} else {
Student *P = (Student *)pp[j2];
if(Name==P->Name) P->print(put_out);
}
if(j2==i-1) {
cout<<"none"<<endl;
break;
}
}
}
return 0;
}
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)