Huazhong University of Science and Technology SPOC Programming Questions Chapter 7

1 Define the ball (20 points)
title content: define a ball, the data member is the radius of the ball (double)

Design the member function to calculate the area and volume of the sphere, and write the class for the main function test.

The formula for finding the area of ​​the sphere: S=4 pi r^2

The formula for finding the volume of the sphere: V=4/3 pi r^3

Where pi is the pi, r 2 represents the square of r, and r 3 represents the cube of r,

Please note that the expressions given here are mathematical expressions, not c++ expressions

Please add the definition of the complete class

#include

using namespace std;

const double PI=3.14159;

void Sphere::show()

{

    cout<<"半径为"<<r<<"的球表面积为"<<getS()<<endl;

    cout<<"半径为"<<r<<"的球体积为"<<getV()<<endl;

}

int main ()

{

      double r1,r2;

      cin>>r1>>r2;

      Sphere  q1(r1),q2;

    q1.show();

    q2.show();

    q2.setR(r2);

    q2.show();

    return 0;

}

Input format: sphere radius of two balls

Output format: output according to the show function format

Input example: 5 6

Sample output:

The surface area of ​​a sphere with a radius of 5 is 314.159

A ball with a radius of 5 has a volume of 523.598

The surface area of ​​a sphere with a radius of 0 is 0

A ball with a radius of 0 has a volume of 0

The surface area of ​​a sphere with a radius of 6 is 452.389

The volume of a sphere with a radius of 6 is 904.778

Time limit: 500ms Memory limit: 128000kb

#include <iostream>
using namespace std;
const double PI=3.14159;
 class Sphere
{
    
    double r;
public:
Sphere(double R);
Sphere();
void setR(double R);
double getS();
double getV();
void show();
};
Sphere::Sphere(double R){
    
    r=R;};
Sphere::Sphere(){
    
    r=0;};
void Sphere::setR(double R)
{
    
    
r=R;
}
double Sphere::getS()
{
    
    return 4*PI*r*r;}
double Sphere::getV()
{
    
    return 4*PI*r*r*r/3;}
void Sphere::show()
{
    
    
        cout<<"半径为"<<r<<"的球表面积为"<<getS()<<endl;
        cout<<"半径为"<<r<<"的球体积为"<<getV()<<endl;

}
int main()
{
    
            
          double r1,r2;
          cin>>r1>>r2;
          Sphere  q1(r1),q2;
        q1.show();
        q2.show();
        q2.setR(r2);
        q2.show();
        return 0;

}

2 Define a date category (25 points)
topic content:

Define a date class Date, including three data members (int) of year, month, and day, and define a constructor with 3 parameters,

And a member function that asks the date to be the day of the year and a member function that outputs the date,

The display format of the date is year/month/day. Write the main function for testing.

(The number of days in each month in a year are 31,28,31,30,31,30,31,31,30,31,30,31, 29 days in a leap year,

Leap year conditions year%40&&year % 100!=0)||year%4000))

Please add the definition of the complete class

#include

using namespace std;

void Date::show()

{

    cout<<year<<"/"<<month<<"/"<<day<<endl;

}

int main ()

{

    int y,m,d;

    cin>>y>>m>>d;

   Date d1(y,m,d),d2;   

    d1.show();

    cout<<d1.getDay()<<endl;

    cin>>y>>m>>d;

    d2.SetD(y,m,d);    

    d2.show();

    cout<<d2.getDay()<<endl;

	system("pause");

    return 0;

}

Input format: year month day

Output format: see output sample

Input example: 2020 4 8

             1997 7 1

Sample output:

2020/4/8

99

1997/7/1

182

Time limit: 500ms Memory limit: 128000kb

#include <iostream>

using namespace std;
class Date
{
    
    
    int year, month, day;
public:
    Date(int a, int b, int c);
    Date();
    void SetD(int a, int b, int c);
    void show();
    int getDay();
};
Date::Date()
{
    
    
    year = 1970; month = 1; day = 1;
};
Date::Date(int a, int b, int c)
{
    
    
    year = a; month = b; day = c;
};
void Date::SetD(int a, int b, int c)
{
    
    
    year = a; month = b; day = c;
}
int Date::getDay()
{
    
    
    int s = 0;
    int a[12] = {
    
     31,28,31,30,31,30,31,31,30,31,30,31 };
    if(year % 4 == 0 && year % 100 != 0|| year % 400 == 0)
        a[1] += 1;
    for (int i = 0; i < month - 1; i++)
        s += a[i];
    s += day;
    return s;
}
void Date::show()

{
    
    

    cout << year << "/" << month << "/" << day << endl;



}

int main()

{
    
    

    int y, m, d;

    cin >> y >> m >> d;

    Date d1(y, m, d), d2;

    d1.show();

    cout << d1.getDay() << endl;

    cin >> y >> m >> d;

    d2.SetD(y, m, d);

    d2.show();

    cout << d2.getDay() << endl;

    system("pause");

    return 0;

}

3 Define the content of a plural category (25 points)
:

Define complex number class Complex, and use the member function add to realize complex number addition and output the result. Note that when the imaginary part is a negative number, it should be written as 3-4i, not 3±4i. If the real or imaginary part is 0, it can not be output, for example: 4i cannot be output as 0+4i, but directly output 4i,4 Can not output as 4+0i, but directly output 4.

Please define the Complex class.

#include

using namespace std;

int main ()

{

int r1,r2,i1,i2;

cin>>r1>>i1>>r2>>i2;

Complex a(r1,i1),b(r2,i2),c;

c=a.add(b);//c=a+b

    c.show();

return 0;

}

Input format: real part and imaginary part

Output format: see output sample

Input example: 3 4 5 6

Sample output: 8+10i

Time limit: 500ms Memory limit: 128000kb

#include<iostream>

using namespace std;
class Complex
{
    
    
    int r, i;
public:
    Complex();
    Complex(int R, int I);
    Complex add(Complex b);
    void show();
};
Complex::Complex()
{
    
    
    r = 0; i = 0;
}
Complex::Complex(int R, int I)
{
    
    
    r = R; i = I;
}
Complex Complex::add(Complex b)
{
    
    
    Complex c;
    c.r = b.r + r;
    c.i = b.i + i;
    return c;
}
void Complex::show()
{
    
    
    if (r == 0 && i == 0)
        cout << 0;
    else if (r == 0 && i != 0)
        cout << i << 'i';
    else if (r != 0 && i == 0)
        cout << r;
     else if (i > 0)cout << r << '+' << i << 'i';
    else cout << r << i << 'i';
}

int main()

{
    
    

    int r1, r2, i1, i2;

    cin >> r1 >> i1 >> r2 >> i2;

    Complex a(r1, i1), b(r2, i2), c;

    c = a.add(b);//c=a+b

    c.show();

    return 0;

}

4 Element class and collection class (30 points)
Topic content: The following is the definition of the element class Element and the collection class Set. Please see the code and the comments after the code to complete the definition of the class. Among them, there can be no duplicate elements in the collection.

#include

#include

using namespace std;

const int MaxElement=100;

class Element{ //"Element" class

public:

   int n;

   Element(int i=0):n(i){}

};

class MySet{ //"Set" class

   Element *element;                            //指向存储元素的数组的指针

   int size;                       //数组大小

   int counter;                               //数组中元素的个数

   int current;                    //用于表示扫描位置,及当前被扫描元素在数组中的下标

public:

   MySet():element(new Element[100]), size(100), counter(0), current(0){}

   ~MySet(){ delete[]element; }

   void add(Element ele);                     //向集合中添加一个元素,保持由小到大的顺序。

   void remove(Element ele);        //删除集合中指定的元素

   void scanFirst(){ current=0; }    //将扫描位置定位于首元素

          void scanNext(){ ++current; }    //将扫描位置定位于下一个元素

   Element get()const{ return element[current]; }             //返回当前被扫描的元素

   bool isEnded()const{ return current >= counter; }       //如果已经没有更多的元素可扫描了,返回true。

   void show();                                                                 //显示集合中所有元素

};

void MySet::show(){

   scanFirst();                 //扫描位置定位于首元素

   while(!isEnded()){

          cout<<get().n<<"  ";

         scanNext();                 //扫描定位于下一个元素

}

   cout<<endl;

}

int main () {

   int a[7],i;

   for(i=0;i<7;i++)

          cin>>a[i];

MySet set;

   i=0;

   set.add(Element(a[i++]));

   set.add(Element(a[i++]));

   set.add(Element(a[i++]));

   set.add(Element(a[i++]));

   set.show();

   set.remove(Element(a[i++]));

   set.remove(Element(a[i++]));

   set.add(Element(a[i++]));

   set.show();

   system("pause");

   return 0;

}

Input format: elements of array a

Output format: see output sample

Input example: 3 8 5 0 5 4 8

Sample output:

0 3 5 8

0 3 8

Time limit: 500ms Memory limit: 128000kb

#include<iostream>
#include<cmath>
using namespace std;
const int MaxElement = 100;
class Element {
    
      //“元素”类
public:
    int n;
    Element(int i = 0) :n(i) {
    
    }
};
class MySet {
    
                                        //“集合”类
    Element* element;                            //指向存储元素的数组的指针
    int size;                       //数组大小
    int counter;                               //数组中元素的个数
    int current;                    //用于表示扫描位置,及当前被扫描元素在数组中的下标
public:
    MySet() :element(new Element[100]), size(100), counter(0), current(0) {
    
    }
    ~MySet() {
    
     delete[]element; }
    void add(Element ele);                     //向集合中添加一个元素,保持由小到大的顺序。
    void remove(Element ele);        //删除集合中指定的元素
    void scanFirst() {
    
     current = 0; }    //将扫描位置定位于首元素
    void scanNext() {
    
     ++current; }    //将扫描位置定位于下一个元素
    Element get()const {
    
     return element[current]; }             //返回当前被扫描的元素
    bool isEnded()const {
    
     return current >= counter; }       //如果已经没有更多的元素可扫描了,返回true。
    void show();                                                                 //显示集合中所有元素
};
void MySet::add(Element ele)
{
    
    
    int flag1 = 0;
    for (current; current< counter; current++)
    {
    
    
        if (get().n == ele.n) {
    
     flag1 = 1; break; }
}
    if (flag1 == 0)
    {
    
    
        int flag2 = 0;
        if (counter == 0) element[0] = ele;
        else
            for (current = counter - 1; current >= 0; current--)
            {
    
    

                if (get().n < ele.n)
                {
    
    
                    element[current + 1] = ele; flag2 = 1; break;
                }
                element[current + 1] = element[current];
            }

        if (flag2 == 0) element[0] = ele;
        counter++;
    }
}
void MySet::remove(Element ele)
{
    
    
    int a = 0;
    for (current = 0; current < counter; current++)
    {
    
    
        if (get().n == ele.n)
            a++;
        element[current] = element[current + a];
    }
    
    counter -= a;
}





void MySet::show() {
    
    
    scanFirst();                 //扫描位置定位于首元素
    while (!isEnded()) {
    
    
        cout << get().n << "  ";
        scanNext();                 //扫描定位于下一个元素
    }
    cout << endl;
}
int main() {
    
    
    int a[7], i;
    for (i = 0; i < 7; i++)
        cin >> a[i];
    MySet set;
    i = 0;
    set.add(Element(a[i++]));
    set.add(Element(a[i++]));
    set.add(Element(a[i++]));
    set.add(Element(a[i++]));
    set.show();
    set.remove(Element(a[i++]));
    set.remove(Element(a[i++]));
    set.add(Element(a[i++]));
    set.show();
    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51236357/article/details/112196181