C + + object oriented programming Tan Haoqiang Chapter 7 after class questions

Posted by DaveEverFade on Sat, 04 Dec 2021 06:20:44 +0100

1. Enter the three sides a, B and C of the triangle. The formula for calculating the area of the triangle is area=

s=(a+b+c)/2 the conditions for forming a triangle are: a + b > C, B + C > A, C + a > B

Write a program, input a, B and C, and check whether a, B and C meet the above conditions. If not, cerr will output relevant error information.

 

#include <iostream>
using namespace std;
class Triangle {
public:
	double a, b, c, s, area;
	void SetTriangle();
	void IsTriangle();
};
void Triangle::SetTriangle()
{
	cin >> a >> b >> c;
}

void Triangle::IsTriangle()
{
	if ((a + b) <= c || (b + c) <= a || (c + a) <= b) {
		cerr << "Illegal input" << endl;
	}
	else
	{
		s = (a + b + c) / 2;
		area = sqrt(s * (s - a) * (s - b) * (s - c));
		cout << "area=" << area << endl;
	}
}

int main()
{
	Triangle t;
	t.SetTriangle();
	t.IsTriangle();
}

2. Input a batch of values from the keyboard. It is required to keep 3 decimal places, and align the up and down decimal points during output.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	float a[5];
	int i = 0;
	for (i = 0; i < 5; i++) {
		cin >> a[i];
	}
	cout << setiosflags(ios::fixed) << setprecision(3);

	for (i = 0; i < 5; i++) {
		cout << setw(10) << a[i] << endl;
	}

}

3. Program to display a triangle composed of letter B on the display screen.

 

#include  <iostream>
#include<iomanip> 
#pragma warning(disable:26451)
using namespace std;
int main()
{
	int size = 0;
	cin >> size;
	for (int i = 1; i < size; i++) {
			cout << setw(10 - i) << setfill(' ') << " " << setw(2 * i - 1) << setfill('B') << "B" << endl;
	}
}

four   (short answer)

Establish two disk files f1.dat and f2.dat, and program to achieve the following work:  

(1) Input 20 integers from the keyboard and store them in two disk files respectively (10 integers in each file);

(2) Read 10 numbers from f1.dat and store them behind the original data in f2.dat file;

(3) Read 20 integers from f2.dat and store them in f2.dat from small to large (without retaining the original data).

#include <iostream>
#include <fstream>
using namespace std;
void Set1()
{
    int a[10];
    ofstream outfile1("f1.dat");
    ofstream outfile2("f2.dat");
    cout << "Please enter 10 integer numbers";
    for (int i = 0; i < 10; i++){
        cin >> a[i];
        outfile1 << a[i] << " ";
    }
    cout << "Please enter 10 integer numbers";
    for (int i = 0; i < 10; i++){
        cin >> a[i];
        outfile2 << a[i] << " ";
    }
    outfile1.close();
    outfile2.close(); 
}
void Set2()
{
    ifstream infile("f1.dat");
    ofstream outfile("f2.dat", ios::app);
    int a;
    for (int i = 0; i < 10; i++)
    {
        infile >> a;
        outfile << a << " ";
    }
    infile.close();
    outfile.close();
}
void Set3()
{
    ifstream infile("f2.dat");
    int a[20];
    int num = sizeof(a) / sizeof(a[0]);
    int temp = 0;
    for (int i = 0; i < num; i++) {
        infile >> a[i];
    }
    for (int i = 0; i < num-1; i++) {
        for (int j = 0; j < num -1 - i; j++) {
            if (a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
    infile.close(); 
    ofstream outfile("f2.dat", ios::out);
    for (int i = 0; i < 20; i++){
        outfile << a[i] << " ";
        cout << a[i] << " ";
    }
    outfile.close();
}
int main()
{
    Set1();
    Set2();
    Set3();
    return 0;
}

five   (short answer)

Programming realizes the following functions:  

(1) Output the data of 5 employees (including number, name, age and salary) in the order of employee number from small to large to disk file for saving.

(2) Input the data of two employees from the keyboard (the employee number is greater than the existing employee number) and add it to the end of the file.

(3) Output the data of all employees in the file.

(4) Enter a number from the keyboard to find out whether there is this employee number from the file. If so, it will display the number of employees this employee is and all the data of this employee. If not, output "no person". You can query repeatedly. If the employee number entered is 0, the query will end.

#include<iostream>
#include<fstream>
using namespace std;
struct worker
{
	int num;
	char name[20];
	int age;
	double pay;
};
int main()
{
	worker wk[7] =
	{
	2020033193, "xi",20,2300,
	2020033196, "yang", 21, 1555.5,
	2020033191, "wang", 18, 2000.1,
	2020033194, "Xu", 17, 3000.1,
	2020033192, "zhang", 18, 666.6,
	}, wk1;
	ofstream outfile1("worker.dat");
	ofstream outfile("worker.dat", ios::out);
	fstream iofile("worker.dat", ios::in | ios::out | ios::binary);
	int i, m, num,temp1,temp2;
	double temp3;
	char arr[20];
	cout << "Five employees" << endl;
	for (int i = 0; i < 5 - 1; i++) {
		for (int j = 0; j < 5 - i - 1; j++) {
			if (wk[j].num > wk[j + 1].num){
				temp1 = wk[j + 1].num;
				wk[j + 1].num = wk[j].num;
				wk[j].num = temp1;
				strcpy_s(arr, wk[j + 1].name);
				strcpy_s(wk[j + 1].name, wk[j].name);
				strcpy_s(wk[j].name, arr);
				temp2 = wk[j + 1].age;
				wk[j + 1].age = wk[j].age;
				wk[j].age = temp2;
				temp3 = wk[j + 1].pay;
				wk[j + 1].pay = wk[j].pay;
				wk[j].pay = temp3;
			}
		}
	}
	for (i = 0; i < 5; i++){
		cout << wk[i].num << " " << wk[i].name << " " << wk[i].age << " " << wk[i].pay << endl;
		iofile.write((char*)&wk[i], sizeof(wk[i]));
	}
	cout << "Please enter 2 employee data you want to insert:" << endl;
	iofile.seekp(0, ios::end);
	for (i = 0; i < 2; i++){
		cin >> wk1.num >> wk1.name >> wk1.age >> wk1.pay;
		iofile.write((char*)&wk1, sizeof(wk1));
	}
	cout << "Seven employees" << endl;
	iofile.seekg(0, ios::beg);
	for (i = 0; i < 7; i++){
		iofile.read((char*)&wk[i], sizeof(wk[i]));
		cout << wk[i].num << " " << wk[i].name << " " << wk[i].age << " " << wk[i].pay << endl;
	}
	bool find;
	cout << "Enter the employee number to search and enter 0 to stop.";
	cin >> num;
	while (num)
	{
		find = false;
		iofile.seekg(0, ios::beg);
		for (i = 0; i < 7; i++)
		{
			iofile.read((char*)&wk[i], sizeof(wk[i]));
			if (num == wk[i].num)
			{
				m = iofile.tellg();
				cout << num << " no" << m / sizeof(wk1) << endl;
				cout << wk[i].num << " " << wk[i].name << " " << wk[i].age << " " << wk[i].pay << endl;
				find = true;
				break;
			}
		}
		if (!find)
			cout << "Can't find" << endl;
		cout << "Enter the employee number to search and enter 0 to stop.";
		cin >> num;
	}
	iofile.close();
	return 0;
}

six   (short answer)   On the basis of example 7.17, modify the program to read in and display the data stored in the c array.

#include<iostream>
#include<sstream>
#include<cstring>
using namespace std;
struct student
{
	int num;
	char name[20];
	double score;
};
int main()
{
	student stud[3] =
	{
		1001,"Li",78,
		1002,"Wang",89.5,
		1004,"Fang",90
	}, stud1[3];
	char c[50] = { 0 };
	int i;
	ostringstream strout;
	for (i = 0; i < 3; i++){
		strout << stud[i].num << " " << stud[i].name << " " << stud[i].score << " ";
	}
	strout << ends;
	strcpy_s(c, strout.str().c_str());
	cout << "array c:" << endl << c << endl << endl;
	istringstream strin(c);
	for (i = 0; i < 3; i++){
		strin >> stud1[i].num >> stud1[i].name >> stud1[i].score;
	}
	cout << "data from array c to array stud1:" << endl;
	for (i = 0; i < 3; i++){
		cout << stud1[i].num << " " << stud1[i].name << " " << stud1[i].score << endl;
	}
	cout << endl;
	return 0;
}

Topics: C++