admin 管理员组文章数量: 1086019
2024年8月27日发(作者:java在类中找不到main方法)
第一章
5:
#include
using namespace std;
int main()
{
cout<<"This"<<"is";
cout<<"a"<<"C++";
cout<<"program."< return 0; } 6: #include using namespace std; int main() { int a,b,c; a=10; b=23; c=a+b; cout<<"a+b="; cout< cout< return 0; } 7: #include using namespace std; int main() { int a,b,c; int f(int x,int y,int z); cin>>a>>b>>c; c=f(a,b,c); cout< return 0; } int f(int x,int y,int z) { int m; if (x else m=y; if (z return(m); } 8: #include using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< return 0; } 9: #include using namespace std; int main() {int add(int x,int y); int a,b,c; cin>>a>>b; c=add(a,b); cout<<"a+b="< return 0; } int add(int x,int y) {int c; c=x+y; return(c); } 10: #include using namespace std; int main() {void sort(int x,int y,int z); int x,y,z; cin>>x>>y>>z; sort(x,y,z); return 0; } void sort(int x, int y, int z) { int temp; if (x>y) {temp=x;x=y;y=temp;} //{ }内3个语句的作用是将x和y的值互换) if (z else if (z else cout< } 11: #include using namespace std; int main() {int max(int a,int b,int c=0); int a,b,c; cin>>a>>b>>c; cout<<"max(a,b,c)="< cout<<"max(a,b)="< return 0; } int max(int a,int b,int c) {if(b>a) a=b; if(c>a) a=c; return a; } 12: #include using namespace std; int main() { void change(int ,int ); int a,b; cin>>a>>b; if(a cout<<"max="< return 0; } void change(int ,int ) { int r1,r2,temp; temp=r1; r1=r2; r2=temp; } 13: #include using namespace std; int main() {void sort(int &,int &,int &); int a,b,c,a1,b1,c1; cout<<"Please enter 3 integers:"; cin>>a>>b>>c; a1=a;b1=b;c1=c; sort(a1,b1,c1); cout< cout< return 0; } void sort(int &i,int &j,int &k) { void change(int &,int &); if (i>j) change(i,j); if (i>k) change(i,k); if (j>k) change(j,k); } void change(int &x,int &y) { int temp; temp=x; x=y; y=temp; } 14: #include #include using namespace std; int main() { string s1="week",s2="end"; cout<<"s1="< cout<<"s2="< s1=s1+s2; cout<<"The new string is:"< return 0; } 15: #include #include using namespace std; int main() { string str; int i,n; char temp; cout<<"please input a string:"; cin>>str; n=(); for(i=0;i {temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;} cout< return 0; } 16: #include #include using namespace std; int main() { int i; string str[5]={"BASIC","C","FORTRAN","C++","PASCAL"}; void sort(string []); sort(str); cout<<"the sorted strings :"< for(i=0;i<5;i++) cout< cout< return 0; } void sort(string s[]) {int i,j; string t; for (j=0;j<5;j++) for(i=0;i<5-j;i++) if (s[i]>s[i+1]) {t=s[i];s[i]=s[i+1];s[i+1]=t;} }17: #include #include using namespace std; int main() { long c[5]={10100,-, ,-, 3456}; int a[5]={1,9,0,23,-45}; float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 }; void sort(int []); void sort(float []); void sort(long []); sort(a); sort(b); sort(c); return 0; } void sort(int a[]) {int i,j,t; for (j=0;j<5;j++) for(i=0;i<5-j;i++) if (a[i]>a[i+1]) {t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"< for(i=0;i<5;i++) cout< cout< } void sort(long a[]) {int i,j; long t; for (j=0;j<5;j++) for(i=0;i<5-j;i++) if (a[i]>a[i+1]) {t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"< for(i=0;i<5;i++) cout< cout< } void sort(float a[]) {int i,j; float t; for (j=0;j<5;j++) for(i=0;i<5-j;i++) if (a[i]>a[i+1]) {t=a[i];a[i]=a[i+1];a[i+1]=t;} cout<<"the sorted numbers :"< for(i=0;i<5;i++) cout< cout< } 18: #include #include using namespace std; template void sort(T a[]) {int i,j,min; T t; for(i=0;i<5;i++) {min=i; for (j=i+1;j<5;j++) if(a[min]>a[j]) min=j; t=a[i]; a[i]=a[min]; a[min]=t; } cout<<"the sorted numbers :"< for(i=0;i<5;i++) cout< cout< } int main() { int a[5]={1,9,0,23,-45}; float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 }; long c[5]={10100,-, ,-, 3456}; sort(a); sort(b); sort(c); return 0; } 第二章 1 #include using namespace std; class Time { public: void set_time(); void show_time(); private: //成员改为公用的 int hour; int minute; int sec; }; void Time::set_time() //在main函数之前定义 { cin>>hour; cin>>minute; cin>>sec; } void Time::show_time() //在main函数之前定义 { cout< } int main() {Time t1; _time(); _time(); return 0; } 2: #include using namespace std; class Time {public: void set_time(void) {cin>>hour; cin>>minute; cin>>sec; } void show_time(void) {cout< private: int hour; int minute; int sec; }; Time t; int main() { _time(); _time(); return 0; } 3: #include using namespace std; class Time {public: void set_time(void); void show_time(void); private: int hour; int minute; int sec; }; void Time::set_time(void) {cin>>hour; cin>>minute; cin>>sec; } void Time::show_time(void) {cout< Time t; int main() { _time(); _time(); return 0; } 4: //() #include using namespace std; #include "xt2-4.h" int main() {Student stud; _value(); y(); return 0; } //(即) #include "xt2-4.h" #include using namespace std; void Student::display( ) { cout<<"num:"< cout<<"name:"< cout<<"sex:"< } void Student::set_value() { cin>>num; cin>>name; cin>>sex; } 5: //() #include #include "xt2-5.h" int main() {Array_max arrmax; _value(); _value(); _value(); return 0; } //() #include using namespace std; #include "xt2-5.h" void Array_max::set_value() //在此文件中进行函数的定义 //不要漏写此行 { int i; for (i=0;i<10;i++) cin>>array[i]; } void Array_max::max_value() {int i; max=array[0]; for (i=1;i<10;i++) if(array[i]>max) max=array[i]; } void Array_max::show_value() {cout<<"max="< } 6:解法一 #include using namespace std; class Box {public: void get_value(); float volume(); void display(); public: float lengh; float width; float height; }; void Box::get_value() { cout<<"please input lengh, width,height:"; cin>>lengh; cin>>width; cin>>height; } float Box::volume() { return(lengh*width*height);} void Box::display() { cout< int main() {Box box1,box2,box3; _value(); cout<<"volmue of bax1 is "; y(); _value(); cout<<"volmue of bax2 is "; y(); _value(); cout<<"volmue of bax3 is "; y(); return 0; } 解法二: #include using namespace std; class Box {public: void get_value(); void volume(); void display(); public: float lengh; float width; float height; float vol; }; void Box::get_value() { cout<<"please input lengh, width,height:"; cin>>lengh; cin>>width; cin>>height; } void Box::volume() { vol=lengh*width*height;} void Box::display() { cout< int main() {Box box1,box2,box3; _value(); (); cout<<"volmue of bax1 is "; y(); _value(); (); cout<<"volmue of bax2 is "; y(); _value(); (); cout<<"volmue of bax3 is "; y(); return 0; } 第三章 2: #include using namespace std; class Date {public: Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int year; }; Date::Date(int m,int d,int y):month(m),day(d),year(y) { } Date::Date(int m,int d):month(m),day(d) {year=2005;} Date::Date(int m):month(m) {day=1; year=2005; } Date::Date() {month=1; day=1; year=2005; } void Date::display() {cout< int main() { Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; y(); y(); y(); y(); return 0; } 3: #include using namespace std; class Date {public: Date(int=1,int=1,int=2005); void display(); private: int month; int day; int year; }; Date::Date(int m,int d,int y):month(m),day(d),year(y) { } void Date::display() {cout< int main() { Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; y(); y(); y(); y(); return 0; } 4: #include using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void display(); private: int num; float score; }; void Student::display() {cout< int main() {Student stud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; Student *p=stud; for(int i=0;i<=2;p=p+2,i++) p->display(); return 0; } 5: #include using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} int num; float score; }; void main() {Student stud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5)}; void max(Student* ); Student *p=&stud[0]; max(p); } void max(Student *arr) {float max_score=arr[0].score; int k=0; for(int i=1;i<5;i++) if(arr[i].score>max_score) {max_score=arr[i].score;k=i;} cout< } 6: #include using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display(){cout< private: int num; float score; }; int main() {Student stud(101,78.5); y(); (101,80.5); y(); return 0; } 7: 解法一 #include using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout< //可改为:void display() const {cout< private: int num; float score; }; int main() {const Student stud(101,78.5); y(); //(101,80.5); y(); return 0; } 解法二: #include using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) const {num=n;score=s;} void display() const {cout< private: mutable int num; mutable float score; }; int main() {const Student stud(101,78.5); y(); (101,80.5); y(); return 0; } 解法三: #include using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout< private: int num; float score; }; int main() {Student stud(101,78.5); Student *p=&stud; p->display(); p->change(101,80.5); p->display(); return 0; } 8: #include using namespace std; class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout< private: int num; float score; }; int main() {Student stud(101,78.5); void fun(Student&); fun(stud); return 0; } void fun(Student &stu) {y(); (101,80.5); y(); } 9: #include using namespace std; class Product {public: Product(int n,int q,float p):num(n),quantity(q),price(p){}; void total(); static float average(); static void display(); private: int num; int quantity; float price; static float discount; static float sum; static int n; }; void Product::total() {float rate=1.0; if(quantity>10) rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount); n=n+quantity; } void Product::display() {cout< cout< } float Product::average() {return(sum/n);} float Product::discount=0.05; float Product::sum=0; int Product::n=0; int main() { Product Prod[3]={ Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5) }; for(int i=0;i<3;i++) Prod[i].total(); Product::display(); return 0; } 10: #include using namespace std; class Date; class Time {public: Time(int,int,int); friend void display(const Date &,const Time &); private: int hour; int minute; int sec; }; Time::Time(int h,int m,int s) {hour=h; minute=m; sec=s; } class Date {public: Date(int,int,int); friend void display(const Date &,const Time &); private: int month; int day; int year; }; Date::Date(int m,int d,int y) {month=m; day=d; year=y; } void display(const Date &d,const Time &t) { cout<<<<"/"<<<<"/"<<< cout<<<<":"<<<<":"<<< } int main() { Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0; } 11: #include using namespace std; class Time; class Date {public: Date(int,int,int); friend Time; private: int month; int day; int year; }; Date::Date(int m,int d,int y):month(m),day(d),year(y){ } class Time {public: Time(int,int,int); void display(const Date &); private: int hour; int minute; int sec; }; Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){ } void Time::display(const Date &d) { cout<<<<"/"<<<<"/"<<< cout< } int main() { Time t1(10,13,56); Date d1(12,25,2004); y(d1); return 0; } 12: #include using namespace std; template class Compare {public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; }; template Compare {x=a;y=b;} template numtype Compare {return (x>y)?x:y;} template numtype Compare {return (x int main() {Compare cout<<()<<" is the Maximum of two integer numbers."< cout<<()<<" is the Minimum of two integer numbers."< Compare cout<<()<<" is the Maximum of two float numbers."< cout<<()<<" is the Minimum of two float numbers."< Compare cout<<()<<" is the Maximum of two characters."< cout<<()<<" is the Minimum of two characters."< return 0; } 第四章 1: #include using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: double real; double imag; }; double Complex::get_real() {return real;} double Complex::get_imag() {return imag;} void Complex::display() {cout<<"("< Complex operator + (Complex &c1,Complex &c2) { return Complex(_real()+_real(),_imag()+_imag()); } int main() {Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c3="; y(); return 0; } 2: #include using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display(); private: double real; double imag; }; Complex Complex::operator+(Complex &c2) {Complex c; =real+; =imag+; return c;} Complex Complex::operator-(Complex &c2) {Complex c; =; =; return c;} Complex Complex::operator*(Complex &c2) {Complex c; =real*-imag*; =imag*+real*; return c;} Complex Complex::operator/(Complex &c2) {Complex c; =(real*+imag*)/(*+*); =(imag*-real*)/(*+*); return c;} void Complex::display() {cout<<"("< int main() {Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1+c2="; y(); c3=c1-c2; cout<<"c1-c2="; y(); c3=c1*c2; cout<<"c1*c2="; y(); c3=c1/c2; cout<<"c1/c2="; y(); return 0; } 3: #include using namespace std; //用VC++时为取消此行 class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2); Complex operator+(int &i); friend Complex operator+(int&,Complex &); void display(); private: double real; double imag; }; Complex Complex::operator+(Complex &c) {return Complex(real+,imag+);} Complex Complex::operator+(int &i) {return Complex(real+i,imag);} void Complex::display() {cout<<"("< Complex operator+(int &i,Complex &c) {return Complex(i+,);} int main() {Complex c1(3,4),c2(5,-10),c3; int i=5; c3=c1+c2; cout<<"c1+c2="; y(); c3=i+c1; cout<<"i+c1="; y(); c3=c1+i; cout<<"c1+i="; y(); return 0; } 4: #include using namespace std; class Matrix {public: Matrix(); friend Matrix operator+(Matrix &,Matrix &); void input(); void display(); private: int mat[2][3]; }; Matrix::Matrix() {for(int i=0;i<2;i++) for(int j=0;j<3;j++) mat[i][j]=0; } Matrix operator+(Matrix &a,Matrix &b) {Matrix c; for(int i=0;i<2;i++) for(int j=0;j<3;j++) {[i][j]=[i][j]+[i][j];} return c; } void Matrix::input() {cout<<"input value of matrix:"< for(int i=0;i<2;i++) for(int j=0;j<3;j++) cin>>mat[i][j]; } //定义Matrix类 //默认构造函数 //重载运算符“+” //输入数据函数 //输出数据函数 //定义构造函数 //定义重载运算符“+”函数 //定义输入数据函数 void Matrix::display() //定义输出数据函数 {for (int i=0;i<2;i++) {for(int j=0;j<3;j++) {cout< cout< } int main() {Matrix a,b,c; (); (); cout< y(); cout< y(); c=a+b; 加 cout< y(); return 0; } 5: #include //using namespace std; class Matrix {public: Matrix(); friend Matrix operator+(Matrix &,Matrix &); friend ostream& operator<<(ostream&,Matrix&); friend istream& operator>>(istream&,Matrix&); private: int mat[2][3]; }; Matrix::Matrix() {for(int i=0;i<2;i++) for(int j=0;j<3;j++) mat[i][j]=0; } Matrix operator+(Matrix &a,Matrix &b) {Matrix c; for(int i=0;i<2;i++) //用重载运算符“+”实现两个矩阵相 for(int j=0;j<3;j++) {[i][j]=[i][j]+[i][j]; } return c; } istream& operator>>(istream &in,Matrix &m) {cout<<"input value of matrix:"< for(int i=0;i<2;i++) for(int j=0;j<3;j++) in>>[i][j]; return in; } ostream& operator<<(ostream &out,Matrix &m) {for (int i=0;i<2;i++) {for(int j=0;j<3;j++) {out<<[i][j]<<" ";} out< return out; } int main() { Matrix a,b,c; cin>>a; cin>>b; cout< cout< c=a+b; cout< return 0; } 6: #include using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r){real=r;imag=0;} Complex(double r,double i){real=r;imag=i;} operator double(){return real;} void display(); private: double real; double imag; }; void Complex::display() {cout<<"("< int main() {Complex c1(3,4),c2; double d1; d1=2.5+c1; cout<<"d1="< c2=Complex(d1); cout<<"c2="; y(); return 0; } 7: #include using namespace std; class Student {public: Student(int,char[],char,float); int get_num(){return num;} char * get_name(){return name;} char get_sex(){return sex;} void display() {cout<<"num:"< private: int num; char name[20]; char sex; float score; }; Student::Student(int n,char nam[],char s,float so) {num=n; strcpy(name,nam); sex=s; score=so; } class Teacher {public: Teacher(){} Teacher(Student&); Teacher(int n,char nam[],char sex,float pay); void display(); private: int num; char name[20]; char sex; float pay; }; Teacher::Teacher(int n,char nam[],char s,float p) {num=n; strcpy(name,nam); sex=s; pay=p; } Teacher::Teacher(Student& stud) {num=_num(); strcpy(name,_name()); sex=_sex(); pay=1500;} void Teacher::display() {cout<<"num:"< int main() {Teacher teacher1(10001,"Li",'f',1234.5),teacher2; Student student1(20010,"Wang",'m',89.5); cout<<"student1:"< y(); teacher2=Teacher(student1); cout<<"teacher2:"< y(); return 0; } 第五章 1: #include using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< private : int num; char name[10]; char sex; }; class Student1: public Student {public: void get_value_1() {get_value(); cin>>age>>addr;} void display_1() { cout<<"age: "< cout<<"address: "< private: int age; char addr[30]; }; int main() {Student1 stud1; _value_1(); y(); y_1(); return 0; } 2: #include using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< //引用派生类的私有成员,正确。 //引用派生类的私有成员,正确。 private : int num; char name[10]; char sex; }; class Student1: private Student {public: void get_value_1() {get_value(); cin>>age>>addr;} void display_1() {display(); cout<<"age: "< cout<<"address: "< private: int age; char addr[30]; }; int main() {Student1 stud1; _value_1(); y_1(); return 0; } 3: #include using namespace std; class Student //声明基类 {public: //基类公用成员 void get_value(); void display( ); protected : //基类保护成员 int num; char name[10]; char sex; }; void Student::get_value() {cin>>num>>name>>sex;} void Student::display( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< } class Student1: protected Student //声明一个保护派生类 {public: void get_value_1(); void display1( ); private: int age; char addr[30]; }; void Student1::get_value_1() {get_value(); cin>>age>>addr; } void Student1::display1( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< cout<<"age: "< cout<<"address: "< } int main( ) {Student1 stud1; _value_1(); y1( ); return 0; } 4: 解法一 #include using namespace std; class Student//声明基类 {public: void get_value(); void display( ); protected : int num; char name[10]; char sex; }; //引用基类的保护成员 //引用基类的保护成员 //引用基类的保护成员 //引用派生类的私有成员 //引用派生类的私有成员 //stud1是派生类student1类的对象 //调用派生类对象stud1的公用成员函数 //调用派生类对象stud1的公用成员函数 //基类公用成员 //基类保护成员 void Student::get_value() {cin>>num>>name>>sex;} void Student::display( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< } class Student1: public Student {public: void get_value_1(); void display1( ); private: int age; char addr[30]; }; void Student1::get_value_1() {get_value(); cin>>age>>addr; } void Student1::display1( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< cout<<"age: "< cout<<"address: "< } int main( ) {Student1 stud1; _value_1(); get_value_1 y1( ); return 0; } 解法二 #include using namespace std; class Student {public: void get_value(); void display( ); //声明一个公用派生类 //引用基类的保护成员,合法 //引用基类的保护成员,合法 //引用基类的保护成员,合法 //引用派生类的私有成员,合法 //引用派生类的私有成员,合法 //stud1是派生类student1类的对象 //调用派生类对象stud1的公用成员函数 //调用派生类对象stud1的公用成员函数display1 //声明基类 //基类公用成员 protected : //基类保护成员 int num; char name[10]; char sex; }; void Student::get_value() {cin>>num>>name>>sex;} void Student::display( ) {cout<<"num: "< cout<<"name:"< cout<<"sex:"< } class Student1: protected Student //声明一个公用派生类 {public: void get_value_1(); void display1( ); private: int age; char addr[30]; }; void Student1::get_value_1() {cin>>age>>addr;} void Student1::display1( ) {cout<<"age:"< cout<<"address:"< } int main( ) {Student1 stud1; //stud1是派生类student1类的对象 _value(); _value_1(); y( ); y1(); //合法。display1是派生类中的公用成员函数 return 0; } 5: class A //A为基类 {public: void f1( ); int i; protected: void f2(); int j; private: int k; }; class B: public A //B为A的公用派生类 {public: void f3( ); protected: int m; private: int n; }; class C: public B {public: void f4(); private: int p; }; int main() {A a1; B b1; C//c1是派生类C的对象 return 0; } 6: #include using namespace std; class A {public: void f1( ); protected: void f2(); private: int i; }; class B: public A {public: //C为B的公用派生类 //a1是基类A的对象 //b1是派生类B的对象 void f3( ); int k; private: int m; }; class C: protected B {public: void f4(); protected: int n; private: int p; }; class D: private C {public: void f5(); protected: int q; private: int r; }; int main() {A a1; B b1; C c1; D d1; return 0; } 7: #include using namespace std; class A { public: A(){a=0;b=0;} A(int i){a=i;b=0;} A(int i,int j){a=i;b=j;} void display(){cout<<"a="< private: int a; int b; }; class B : public A { public: B(){c=0;} B(int i):A(i){c=0;} B(int i,int j):A(i,j){c=0;} B(int i,int j,int k):A(i,j){c=k;} void display1() {display(); cout<<" c="< } private: int c; }; int main() { B b1; B b2(1); B b3(1,3); B b4(1,3,5); y1(); y1(); y1(); y1(); return 0; } 8: #include using namespace std; class A { public: A(){cout<<"constructing A "< ~A(){cout<<"destructing A "< }; class B : public A { public: B(){cout<<"constructing B "< ~B(){cout<<"destructing B "< }; class C : public B { public: C(){cout<<"constructing C "< ~C(){cout<<"destructing C "<