|
| » 首页 » 电脑_数码 » 编程 » 一段搞不定的c++代码 |
一段搞不定的c++代码 |
|
#include<iostream> #include<cstring> using namespace std; class student {public: student(int n,char name[],int sc) {num=n; strcpy(name,name); score=sc; } char *getname() {return name;} int getnum() {return num;} int getscore() {return score; } static void display() {cout<<s<<' '<<na<<' '<<temp<<endl; } static void max(student&); private: int num; char name[20]; int score; static int temp; static char na[20]; static int s; }; int student::temp=-1; char student::na[20]=" "; int student::s=0; void student::max(student&stud) {if(temp==-1)temp=stud.getscore(); if(temp<stud.getscore()) {temp=stud.getscore(); strcpy(na,stud.getname()); s=stud.getnum(); } } void main() {student stud[3]={student(1,"k",90),student(2,"m",95),student(3,"u",92)}; for(int i=0;i<3;i++)student::max(stud[i]); student::display(); } |
![]() |
|
|
结果为 7.5 7前面两个空格,最后换行 class student {public: student(int n,char name[],int sc) {num=n; strcpy(this->name,name);//这里修改********** score=sc; } char *getname() {return name;} int getnum() {return num;} int getscore() {return score; } static void display() {cout<<s<<' '<<na<<' '<<temp<<endl; } static void max(student&);// private: int num; char name[20]; int score; static int temp; static char na[20]; static int s; }; int student::temp=-1; char student::na[20]=" "; int student::s=0; void student::max(student&stud) {if(temp==-1)temp=stud.getscore(); if(temp<stud.getscore()) {temp=stud.getscore(); strcpy(na,stud.getname()); s=stud.getnum(); } } void main() {student stud[3]={student(1,"k",90),student(2,"m",95),student(3,"u",92)}; for(int i=0;i<3;i++)student::max(stud[i]); student::display(); } |
| 《一段搞不定的c++代码》答案收集时间:2008-06-14 14:39:47 |