欢迎光临散文网 会员登陆 & 注册

666

2023-06-07 14:06 作者:狂気のMadScientists  | 我要投稿

1.

#include <iostream>

using namespace std;

 

// Declarations (prototypes)

 

int area(int) ;

double area(double) ;

int area(int, int);

 

int main()

{  int a;

   cout<<” Side length of square:\n;

   cin>>a;

   cout << “area of square is :<<area(a) << "\n";

   double r;

   cout<<radius of circle:\n;

   cin>>r;

   cout << “area of circle is :<<area(r) << "\n";

   int len,wid;

   cout<<length and width of rectangle:\n;

   cin>>len>>wid;

   cout <<”area of rectangle is:<< area(len,wid) << "\n";

   return 0;

}

 

// Function definitions

int area(int s)  // cube

 {

   return (s*s) ;

}

double area(double r)   // cylinder

{

   return (3.14519*r*r) ;

}

int area(int b, int h) // rectangular box

{

   return (b*h) ;

}

 

2. 

#include <iostream>

using namespace std;

void max(int a , int b)

{ cout<<"输出两个整数的最大整数"<<endl;

if(a>b) cout<<a<<endl;

else cout<<b<<endl; }

void max(float a , float b)

{ cout<<"输出两个实数的最大实数"<<endl;

if(a>b) cout<<a<<endl;

else cout<<b<<endl;}

 void max(int a , int b ,int c)

 { cout<<"输出三个整数的最大整数"<<endl;

 int m=a;

 if(max < b) m=b;

 if(max < c) m=c;

cout<<m<<endl; }

 void max(float a , float b ,float c)

 { cout<<"输出三个实数的最大实数"<<endl;

 float m=a;

if(max < b) m=b;

 if(max < c) m=c;

cout<<m<<endl; }

 int main()

 { int a1 , b1 , c1;

 float a2 , b2 , c2;

  cout<<"输入两个整数:";

 cin>>a1>>b1;

 max(a1,b1);

 cout<<"输入三个整数:";

 cin>>a1>>b1>>c1;

 max(a1,b1,c1);

 cout<<"输入两个实数:";

 cin>>a2>>b2;

 max(a2,b2);

 cout<<"输入三个实数:";

 cin>>a2>>b2>>c2;

 max(a2,b2,c2);

 return 0; }

 

3.

 

#include <iostream>

using namespace std;

 

class ST

{float score[30];

 float max_score, ave_score;

public:

 void input (void);

 void average (void);

 void max(void);

 void show(void);

};

 void ST::input(void)

{cout<<"input students score:\n";

 for(int i=0; i<30;i++)

    cin>>score[i];

}

void ST::average(void)

{ float sum=0;

 for(int i=0; i<30;i++)

    sum=sum+score[i];

    ave_score=sum/30;

}

void ST::max(void)

{  max_score=score[0];

 for(int i=1; i<30;i++)

    if(max_score<score[i])

    max_score=score[i];

 }

void ST::show(void)

{ cout<<"The student scores are:\n";

   for(int i=0; i<30;i++)

    cout<<score[i]<<' ';;

  cout<<endl;

  cout<<"the average score is:"<<ave_score<<endl;

  cout<<"the max score is:"<<max_score<<endl;

}

int main()

{ST stu;

 stu.input ( );

 stu.average( );

 stu.max( );

 stu.show( );

 return 0;

}

4.

 

 

#include <iostream>

using namespace std;

class List

{ float price;

int num;

public:

void input();

void output();

float sum();

};

void List::input()

{

cin>>price;

cin>>num;

}

float List::sum()

{

return price*num;

}

void List::output()

{

cout<<"单价是"<<price<<”数量是”<<num<<”总价格是”<<sum()<<endl;

}

int main()

{

List a;

a.input();

a.sum();

a.output();

return 0;

}

5.

 

 

#include <iostream>

#include <string.h>

using namespace std;

 class student

 {

  long int xh;

 char name[10];

 float cj1,cj2,cj3;

 public:

 student( )

 { cout << "请输入姓名和学号:" << endl;

 cin>> name>>xh;

cout << "请输入3门课成绩:" << endl;

 cin >> cj1>>cj2>>cj3;  }

 student(char n[],long int x, float c1,float c2,float c3)

 { strcpy(name,n); xh = x; cj1= c1; cj2= c2; cj3= c3; }

void display( );

 };

 void student :: display( )

 { cout << "姓名:";

 cout << name << endl;

 cout << "学号:";

 cout << xh << endl;

 cout << "三门课成绩:";

 cout<< cj1<<","<<cj2<<","<<cj3<<endl;}

int main()

 { student a;

 a.display();

 student b("wlm",1002, 66,77,88);

 b.display();

 return 0;

}

6.

 

#include<iostream>

using namespace std;

class Arr

{ int n;

 int *a;

public:

  Arr(int m) {n=m;a=new int(n);}

  ~Arr(){delete []a;}

  void input(void );

  void max();

  void average();

  void output(void );

};

void  Arr:: input(void )

{ cout<<"input array elements :\n";

for(int i=0; i<n;i++)

      cin>>a[i];

}

void  Arr:: max()

 {int k=0; int m=a[0];

   for(int i=1;i<n;i++)

     if(m<a[i]){m=a[i]; k=i;}

   cout<<"the max element and subscript is:"<<m<<' '<<k<<endl;

 }

void  Arr:: average()

{ int sum=0;

for(int i=0; i<n;i++)

      sum+=a[i];

   cout<<"the average of array is:"<<1.0*sum/n<<endl;

}

void  Arr:: output(void )

{ cout<<" array elements are:\n";

for(int i=0; i<n;i++)

      cout<<a[i]<<" ";

}

int main()

{ int m;

 cout<<"input array length:\n";

 cin>>m;

 Arr array(m);

 array.input();

 array.max();

 array.average();

 array.output();

 return 0;

}

7.

 

#include <iostream>

using namespace std;

class complex

{

     float x;     // real part

     float y;     // imaginary part

  public:

    complex(){ }                // constructor 1

    complex(float real, float imag)  // constructor 2

    { x = real; y = imag; }

complex operator-(complex);

friend complex operator*(int, complex);

    void display(void);

};

complex complex :: operator-(complex c)

{

   complex temp;     // temporary

   temp.x = x - c.x;   // these are

   temp.y = y - c.y;   // float additions

   return(temp);

}

complex operator*(int m, complex c)

{

   complex temp;     // temporary

   temp.x = m*c.x;   // these are

   temp.y = m* c.y;   // float additions

   return(temp);

}

 

void complex :: display(void)

{

   cout << x << " + j" << y << "\n";

}

int main()

{

   complex C1(1,2.5), C2(4.5,5), C3,C4;     // invokes constructor i

   //C1 = complex(2.5, 3.5);  // invokes constructor 2

   //C2 = complex(1.6, 2.7);

   C3 = C2 – C1;

   C4=2*C1;

   cout << "C1 = "; C1.display();

   cout << "C2 = "; C2.display();

   cout << "C3 = "; C3.display();

   return 0;

}

 

 

8.

#include<iostream>
using namespace std;
class Smart
{
protected:
int a;
int b;
public:
void setab(int x,int y)
{
a=x;
b=y;
}
void print()
{
cout<<"a="<<a<<"\n"<<"b="<<b<<"\n";
}
};
class SmartBut:public Smart
{
int sum;
public:
void display()
{
sum=a*b;
cout<<"sum="<<sum<<endl;
}
};
int main()
{
SmartBut s;
s.setab(2,3);
s.print();
s.display();
return 0;
}

 


 

9.

 

#include<iostream>

using namespace std;

class st_mark

{ protected:

 char n[20];

 int num;

 int cj[4];

public:

void input()

{   cout<<"输入姓名和学号:\n";

        cin>>n>>num;

cout<<"输入成绩:\n";

for(int i=0;i<4;i++)

           cin >> cj[i];

}

void output()

{   cout<<"姓名: "<<n<<endl;

        cout<<"学号: "<<num<<endl;

cout<<"成绩是:\n";

for(int i=0;i<4;i++)

           cout<<cj[i]<<" ";

   cout<<endl;

}

};

class st_award

{ protected:

    int honor;

public:

 void award( )

 { cout<<"输入是否获奖(获奖输入1,未获奖输入0):\n";

   int f;

cin>>f;

if(f==1) honor=5;

else honor=0;

 }

};

class student:public st_award,public st_mark

{

double ave,total;

public:

void average( )

{

 int sum=0;

for(int i=0;i<4;i++)

sum=sum+cj[i];

ave=sum/4.0;

}

float total_score()

{int sum=0;

for(int i=0;i<4;i++)

sum=sum+cj[i];

    total=sum+honor;

}

void display()

{

cout<<"平均分"<<ave<<endl;

 cout<<"期末总分"<<total<<endl;

}

};

int main()

{

student h;

h.input();

h.honor();

h.output();

h.display();

return 0;

 

}

 


666的评论 (共 条)

分享到微博请遵守国家法律