Chapter 9 [C + + operation]

title: [cpp] Chapter 9 operation
date: 2020-01-13 11:27:05
categories:

  • cpp
  • task
    tags: course
    comments: true
    description:
    cover: https://i.loli.net/2020/01/17/trs2EOodlxgGJSc.jpg
    top_img: https://i.loli.net/2020/01/17/9Ty2tFiNYqQXKes.jpg

Student achievement statistics

[problem description]

First, define a class Beeline that can describe a line segment on a plane, including the coordinates of two endpoints of the line segment with private data members (X1, Y1, X2, Y2). In the class, define a constructor whose parameter default value is 0. Calculate the length of the public member function of the line segment Length(), and display the public member function of the coordinates of two endpoints of the line segment show(). Then we define a Triangle class that can describe triangles on a plane. Its data members are line1, line2 and line3 defined by Beeline. The constructor defined in the class should be able to initialize the object members. Then define the function Area() to calculate the Triangle area and the function Print() to display the coordinates of the end points of the three sides and the area. In the Print() function, you can call the show() function to display the coordinates of the end points of the three sides.
    
[input form]

Enter the coordinates of the three vertices of the triangle (x1,y1), (x2,y2), (x3,y3).

Where - 100 < = x1, X2, X3, Y1, Y2, Y3 < = 100, and is an integer.

In the main function, create the class object tri(x1,y1,x2,y2,x3,y3), corresponding to line1(x1, y1, x2, y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1).

[output form]

Call the Print() function to set the endpoint coordinates and area of the three sides of the triangle. Two decimal places shall be reserved for the area.

See the example for the specific format.

[sample input]

0 0
0 4
3 0

[sample output]

Three edges' points are listed as follows:
(0, 0),(0, 4)
(0, 4),(3, 0)
(3, 0),(0, 0)
The area of this triangle is: 6.00.

[hint]

1. Output in strict accordance with the output sample. It is recommended to copy.

2. Helen's formula is recommended for area calculation.

3. Strictly control and keep 2 decimal places.

4. If the class is not strictly used, the score is 0.

[Abstract]

#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
class beeline
{
private:
	int x1, y1, x2, y2;
public:
	beeline()
	{
		x1 = 0; x2 = 0; y1 = 0; y2 = 0;
	}
	beeline(int, int, int, int);
	float length();
	void show();
};
beeline::beeline(int a, int b, int c, int d)
{
	x1 = a;
	y1 = b;
	x2 = c;
	y2 = d;
}
float beeline::length()
{
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void beeline::show()
{
	cout << "(" << x1 << ", " << y1 << ")" << "," << "(" << x2 << ", " << y2 << ")" << endl;
}
class Triangle
{
private:
	beeline line1, line2, line3;
public:
	Triangle(int, int, int, int, int, int);
	float Area();
	void print();
};
Triangle::Triangle(int a, int b, int c, int d, int e, int f) :line1(a, b, c, d), line2(c, d, e, f), line3(e, f, a, b)
{
}
float Triangle::Area()
{
	float x, y, z;
	x = line1.length();
	y = line2.length();
	z = line3.length();
	return ((1 / 4.00) * sqrt((x + y + z) * (x + y - z) * (x + z - y) * (y + z - x)));
}
void Triangle::print()
{
	cout << "Three edges' points are listed as follows:";
	cout << endl;
	line1.show();
	line2.show();
	line3.show();
	cout << "The area of this triangle is: ";
	cout << std::fixed << std::setprecision(2) << (*this).Area() << ".";
}
int main()
{
	int a, b, c, d, e, f;
	cin >> a >> b >> c >> d >> e >> f;
	Triangle x(a, b, c, d, e, f);
	x.print();
}

Student achievement

[problem description]

Design student Score. Define the student Score object array s [] in the main function. Use Sum() to calculate the total Score of each student, and Show() to display the Score of each student. Add the static member function getAvg() to return the total average Score of students. This is accomplished by adding appropriate members, modifying member functions, etc.

[input form]

Contains a set of test data. Enter an integer n (1 < = n < = 100) in the first line.

Next n lines. Enter an integer op for each line:

When op==1, enter x, y, z. Represents the input of Chinese, math and English scores of a new student i(i starts from 1) without output.

When op==2, input i and output the total score of the ith student. Data to ensure that the student's score has been entered.

When op==3, input i, and output the Chinese, math and English scores of the ith student in turn. The scores are separated by spaces.

When op==4, output the total average score of the currently entered students, and keep two decimal places for the result.

(1 < = n < = 100, 1 < = ID < = 10, 1 < = OP < = 3, 0 < = x, y, Z < = 100, all inputs are integers)

[output form]

[failed to transfer the pictures in the external link. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-72Oas3eQ-1579607168308)(https://s2.ax1x.com/2020/01/13/l7nmJP.png))

Note that there will be some output between the inputs, but the test only looks at the cout results.

[Abstract]

#include  <iostream>
#include  <cstdio>
#include  <cstdlib>
#include  <iomanip>
using  namespace  std;
class  Score
{
private:
	int  Chinese, Math, English;
	static  int  TotalScore;
	static  int  TotalStudent;
public:
	Score() {}
	void  setScore(int  c, int  m, int  e)
	{
		Chinese = c;
		Math = m;
		English = e;
		TotalScore += c + m + e;
		TotalStudent++;
	}
	int Sum()
	{
		return(Chinese + Math + English);
	}
	void Show()
	{
		cout << Chinese << " " << Math << " " << English;
	}
	double static getAve()
	{
		return (double)TotalScore / TotalStudent;
	}
};
 int Score::TotalScore = 0;
 int Score::TotalStudent = 0;
 int  main()
 {
	 int  n, op, i, c, m, e;
	 cin >> n;
	 int  id = 1;
	 Score  sco[11];
	 while (n--)
	 {
		 cin >> op;
		 if (op == 1)
		 {
			 cin >> c >> m >> e;
			 sco[id].setScore(c, m, e);
			 id++;
		 }
		 else  if (op == 2)
		 {
			 cin >> i;
			 cout << sco[i].Sum();
			 cout << endl;
		 }
		 else  if (op == 3)
		 {
			 cin >> i;
			 sco[i].Show();
			 cout << endl;
		 }
		 else
		 {
			 cout << std::fixed << std::setprecision(2) << Score::getAve();
			 cout << endl;
		 }
	 }
	 return 0;
}

TV class

[problem description]

Complete the design of a TV class and a Remote class. The member function of the Remote class is a friend of the TV class. The TV class has the basic properties of status, channel and volume. The default initial channel is 5, and the default initial volume is 20. The status includes on and off (- 1 indicates shutdown status, others are startup status).

In the main function, different operations are performed according to the input op value. The completion code makes the program meet the following requirements.

[input form]

When op==1,

Enter the TV operation command as follows:

Off? On

Volume up (TV volume + 1)

Vol? Down (TV volume-1)

Cha? Next (TV channel + 1)

Cha_pre (TV channel-1)

CHA TO x (0<=x<=100, switch TV channel to x)

VOL TO x (0<=x<=100, turn TV volume to x)

Cha to and Vol to are implemented by calling friend classes.

When op==2, the current TV status is output.

When op==3, the program ends.

[output form]

When op==2, output the current TV status. See the example for the specific format.

[Abstract]

#include <iostream>
using namespace std;
class TV;
class Remote
{
public:
    Remote() {};
    void volume_to(TV &tv, int x);
    void channel_to(TV &tv, int x);
};

class TV
{
private:
    int state;
    int channel;
    int volume;
public:
    friend void Remote::volume_to(TV &tv, int x);
    friend void Remote::channel_to(TV &tv, int x);
    TV() {};
    TV(int st) :state(st),volume(20),channel(5){}

    void onoff() {
                state = -state;

    }
    void cha_next() {
            channel++;
    }
    void cha_pre() {
            channel--;
    }
    void vol_up() {
            volume++;
    }
    void vol_down() {
            volume--;
    }
    void print() {
        if(state == -1) 
        {
                    cout << "The TV is OFF" << endl;
        } 
        else {
                    cout << "The TV is ON" << endl;
                    cout << "The channel is " << channel << endl;
                    cout << "The volume is " << volume << endl;

        }
    }
};

void Remote::volume_to(TV &tv, int x) {
    tv.volume = x;
}
void Remote::channel_to(TV &tv, int x) {
    tv.channel = x;
}

int main()
{
    int x, op;
    string s;
    TV tv(-1);
    Remote rem;
    while(1) {
        cin >> op;
        if(op == 1) {
            cin >> s;
            if(s == "OFF_ON") tv.onoff();
            else if(s == "VOL_UP") tv.vol_up();
            else if(s == "VOL_DOWN") tv.vol_down();
            else if(s == "CHA_NEXT") tv.cha_next();
            else if(s == "CHA_PRE") tv.cha_pre();
            else if(s == "CHA_TO") {
                cin >> x;
                rem.channel_to(tv, x);
            } else if(s == "VOL_TO") {
                cin >> x;
                rem.volume_to(tv, x);
            }
        } else if(op == 2){
            tv.print();
        } else {
            break;

        }
    }
    return 0;
}
Published 10 original articles, praised 0, visited 57
Private letter follow

Posted by Tyco on Tue, 21 Jan 2020 05:23:11 -0800