Bootstrap

C++复习

参考文献:

https://blog.csdn.net/weixin_44368437/article/details/117563488?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522B29965E7-F195-42CD-9DA3-3DC2BE6C70B4%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=B29965E7-F195-42CD-9DA3-3DC2BE6C70B4&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~top_positive~default-1-117563488-null-null.nonecase&utm_term=C%2B%2B&spm=1018.2226.3001.4450

代码路径:
在这里插入图片描述
代码:
main.c

/*
 * 参考博客:https://blog.csdn.net/weixin_44368437/article/details/117563488?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522B29965E7-F195-42CD-9DA3-3DC2BE6C70B4%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=B29965E7-F195-42CD-9DA3-3DC2BE6C70B4&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~top_positive~default-1-117563488-null-null.nonecase&utm_term=C%2B%2B&spm=1018.2226.3001.4450

*/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <fstream>
#include <fstream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <queue>
#include <functional>
#include <cstdlib>
#include <ctime>
#include <set>
#include <string>
#include <map>

//#include "func.h"
using namespace std;



int main(int argc, char**argv)
{
    #if 0
    func_test1();
    #endif
    
    #if 0
    func_test2();
    #endif

    #if 0
    func_test3();
    #endif

    #if 0
    vector<int> nums;
	nums.insert(nums.begin(), 99);
	nums.insert(nums.begin(), 34);
	nums.insert(nums.end(), 1000);
	nums.push_back(669);

	cout << "\n当前nums中元素为: " << endl;
	for (int i = 0; i < nums.size(); i++)
		cout << nums[i] << " " ;
    cout << endl;

	cout << nums.at(2);
	nums.erase(nums.begin());
	nums.pop_back();

	cout << "\n当前nums中元素为: " << endl;
	for (int i = 0; i < nums.size(); i++)
		cout << nums[i] << " " ;
    cout << endl;
    #endif


    #if 0
    list<int> number;
	list<int>::iterator niter;
	number.push_back(123);
	number.push_back(234);
	number.push_back(345);

	cout << "链表内容:" << endl;
	for (niter = number.begin(); niter != number.end(); ++niter)
		cout << *niter << endl;
	number.reverse();
	cout << "逆转后的链表内容:" << endl;
	for (niter = number.begin(); niter != number.end(); ++niter)
		cout << *niter << endl;
	number.reverse();
    #endif


    #if 0
    stack<int> st;
	int num = 100;
	cout << "100的八进制表示为:";
	while (num) {
		st.push(num % 8);
		num /= 8;
	}
	int t;
	while (!st.empty()) {
		t = st.top();
		cout << t;
		st.pop();
	}
	cout << endl;
    #endif

    #if 0
    queue<int> qu;
	for (int i = 0; i < 10; i++) 
		qu.push(i * 3 + i);
	while (!qu.empty()) {
		cout << qu.front() << " ";
		qu.pop();
	}
	cout << endl;
    #endif

    #if 0
    priority_queue<int> pq;
	srand((unsigned)time(0));
	for (int i = 0; i < 6; i++) {
		int t = rand();
		cout << t << endl;
		pq.push(t);
	}
	cout << "优先队列的值:" << endl;
	for (int i = 0; i < 6; i++) {
		cout << pq.top() << endl;
		pq.pop();
	}
    #endif

    #if 0
    set<string> s;
	s.insert("aaa");
	s.insert("bbb");
	s.insert("ccc");
	if (s.count("aaa") != 0) {
		cout << "存在元素aaa" << endl;
	}
	set<string>::iterator iter;
	for (iter = s.begin(); iter != s.end(); ++iter) 
		cout << *iter << endl;
    #endif

    #if 1
    map<string, int> m;
	m["aaa"] = 111;
	m["bbb"] = 222;
	m["ccc"] = 333;
	if (m.count("aaa")) {
		cout << "键aaa对应的值为" << m.at("aaa") << endl;
		cout << "键aaa对应的值为" << m["aaa"] << endl;
	}
    #endif


    return 0;
}

func.h


int add1(int a, int b);

//内联函数
inline double circle(double r);

//函数重载
int add(int x, int y);
double add(double x, double y);
int add(int x, int y, int z);

void swap(int &a, int &b)
{
	int t = a;
	a = b;
	b = t;
}

int a[] = {1, 3, 5, 7, 9};

int& index(int i)
{
	return a[i];
}

int value;   //定义全局变量value

class Score{
    public:
    Score();//无参构造,默认构造函数
    ~Score();//析构函数
    Score(int m, int f);//构造函数
    Score(const Score &p);  //拷贝构造函数
    void setScore(int m, int f);
    void showScore();
    private:
    int mid_exam;
    int fin_exam;
};
void Score::setScore(int m, int f) 
{
	mid_exam = m;
	fin_exam = f;
}

void Score::showScore()
{
	cout << "期中成绩: " << mid_exam << endl;
	cout << "期末成绩:" << fin_exam << endl;
}

Score::Score(int m, int f)
{
	mid_exam = m;
	fin_exam = f;
    cout << "进入了含参构造函数" << endl;
}
Score::Score()
{
    cout << "进入了默认构造函数" << endl;
}
Score::~Score()
{
    cout << "析构函数被调用" << endl;
}
Score::Score(const Score &p)
{
    mid_exam = p.mid_exam;
    fin_exam = p.fin_exam;
    cout << "进入了拷贝构造函数" << endl;
}

class A{
private:
	int x;
	int& rx;
	const double pi;
public:
	A(int v) : x(v), rx(x), pi(3.14)    //成员初始化列表
	{	}
	void print()
	{
		cout << "x = " << x << " rx = " << rx << " pi = " << pi << endl;
	}
};

class A1{
public:
	char name[10];
	int no;
};

class Student2{
public:
    Student2(char *name1, float score1);
    ~Student2();
    Student2(const Student2& stu);
    void printData();
private:
    char *name;
    float score;
};
Student2::Student2(char *name1, float score1)
{
    name = name1;
    score = score1;
}
Student2::Student2(const Student2& stu)
{
    name = new char[strlen(stu.name) + 1];
    if (name != 0) {
        strcpy(name, stu.name);
        score = stu.score;
    }
}
Student2::~Student2()
{
    delete name;
}

void Student2::printData()
{
    cout << "name :" << name <<"," << "score:" << score << endl;
}


class Point{
public:
	int x;
	int y;
	Point(int x1, int y1) : x(x1), y(y1)  //成员初始化列表
    { }
	int getDistance() 
	{
		return x * x + y * y;
	}
};

void changePoint1(Point point)    //使用对象作为函数参数
{
	point.x += 1;
	point.y -= 1;
}

void changePoint2(Point *point)   //使用对象指针作为函数参数
{
	point->x += 1;
	point->y -= 1;
}

void changePoint3(Point &point)  //使用对象引用作为函数参数
{
	point.x += 1;
	point.y -= 1;
}



int add1(int a, int b)
{
    return a + b;
}

inline double circle(double r)  //内联函数
{
	double PI = 3.14;
	return PI * r * r;
}

int add(int x, int y)
{
	return x + y;
}

double add(double x, double y)
{
	return x + y;
}

int add(int x, int y, int z)
{
	return x + y + z;
}

void func_test1()
{
    #if 0
    int x;
    int y;
    cout << "Please enter two num:" << endl;
    cin >> x >> y;
    cout << "add x and y is " << add1(x,y) << endl;
    #endif

    #if 0
    void* pc;
    int i = 123;
    char c = 'a';
    pc = &i;
	cout << pc << endl;         //输出指针地址006FF730
	cout << *(int*)pc << endl;  //输出值123
    pc = &c;
	cout << *(char*)pc << endl; //输出值a
    #endif

    #if 0 
    for(int i = 1; i <= 3; i++)
    {
        cout << circle(i) << endl;
    }
    #endif

    //函数重载
    # if 0
    int a = 3, b = 5, c = 7;
	double x = 10.334, y = 8.9003;
	cout << add(a, b) << endl;
	cout << add(x, y) << endl;
	cout << add(a, b, c) << endl;
    #endif

    //变量的作用域
    #if 0
    int value;
    value = 100;
    ::value = -1000;
    cout << " local value is " << value <<endl;
    cout << " global value is " << ::value <<endl;
    #endif

    //强制类型转换
    #if 0
    int i = 10;
    double x = (double)i;
    x = double(i);
    #endif

    //引用
    #if 0
    int i = 10;
	int &j = i;
	cout << "i = " << i << " j = " << j << endl;
	cout << "i的地址为 " << &i << endl;
	cout << "j的地址为 " << &j << endl;
    #endif

    #if 0
    int a = 5, b = 10;
	//交换数字a和b
	swap(a, b);
	cout << "a = " << a << " b = " << b << endl;
	cout << index(2) << endl;   //等价于输出元素a[2]的值
	index(2) = 100;             //等价于将a[2]的值赋为100;
	cout << index(2) << endl;
    #endif

    #if 0
    Score op1, op2;
    op1.setScore(88, 99);
    //op1.showScore();

    Score op, *sc;
	sc = &op;
	sc->setScore(99, 100);
	//op.showScore();

    op2 = *sc;
	//op2.showScore();
    
    Score op9(99,100);
    //op9.showScore();

    Score *op10 = new Score(99,99);
    op10->showScore();
    #endif

    #if 0
    Score op1;
    op1.setScore(88, 99);
    op1.showScore();

    Score op2(op1);
    op2.showScore();
    #endif

    #if 0
    A1 a = {"chen", 23};
    cout << a.name << "\t" << a.no << endl;
    #endif

    #if 0
    char str[] = "white";
    Student2 stu1(str, 89);
    stu1.printData();
    Student2 stu2(stu1);
    stu2.printData();
    #endif

    #if 0
    Score score;
    Score *p;
    p = &score;
    p->setScore(88,99);
    p->showScore();
    #endif

    #if 0
    Score score[2];
    score[0].setScore(90, 99);
    score[1].setScore(67, 89);

    Score *p;
    p = score;   //将对象score的地址赋值给p
    p->showScore();
    p++;    //对象指针变量加1
    p->showScore();

    Score *q;
    q =&score[1];   //将第二个数组元素的地址赋值给对象指针变量q
    q->showScore();
    #endif

    /* 
     * string的相关操作
    */
    #if 0
    string str1 = "ABC";
	string str2("dfdf");
	string str3 = str1 + str2;
	cout<< "str1 = " << str1 << "  str2 = " << str2 << "  str3 = " << str3 << endl;
	str2 += str2;
	str3 += "aff";
	cout << "str2 = " << str2 << "  str3 = " << str3 << endl;
	cout << "str1[1] = " << str1[1] << "  str1 == str2 ? " << (str1 == str2) << endl;
	string str = "ABC";
	cout << "str == str1 ? " << (str == str1) << endl;
    #endif

    #if 0
    Point point[3] = {Point(1, 1), Point(2, 2), Point(3, 3)};
	Point *p = point;
	changePoint1(*p);
	cout << "the distance is " << p[0].getDistance() << endl;
	p++;
	changePoint2(p);
	cout << "the distance is " << p->getDistance() << endl;
	changePoint3(point[2]);
	cout << "the distance is " << point[2].getDistance() << endl;
    #endif
}


class Score{
private:
	int mid_exam;
	int fin_exam;
	static int count;     //静态数据成员,用于统计学生人数
	static float sum;     //静态数据成员,用于统计期末累加成绩
	static float ave;     //静态数据成员,用于统计期末平均成绩
public:
	Score(int m, int f);
	~Score();
	static void show_count_sum_ave();   //静态成员函数
};

Score::Score(int m, int f)
{
	mid_exam = m;
	fin_exam = f;
	++count;
	sum += fin_exam;
	ave = sum / count;
}

Score::~Score()
{

}

/*** 静态成员初始化 ***/
int Score::count = 0;
float Score::sum = 0.0;
float Score::ave = 0.0;

void Score::show_count_sum_ave()
{
	cout << "学生人数: " << count << endl;
	cout << "期末累加成绩: " << sum << endl;
	cout << "期末平均成绩: " << ave << endl;
}


class Score{
private:
	int mid_exam;
	int fin_exam;
public:
	Score(int m, int f);
	void showScore();
	friend int getScore(Score &ob);
};

Score::Score(int m, int f)
{
	mid_exam = m;
	fin_exam = f;
}

int getScore(Score &ob)
{
	return (int)(0.3 * ob.mid_exam + 0.7 * ob.fin_exam);
}




class Score;    //对Score类的提前引用说明
class Student{
private:
	string name;
	int number;
public:
	Student(string na, int nu) {
		name = na;
		number = nu;
	}
	friend void show(Score &sc, Student &st);
};

class Score{
private:
	int mid_exam;
	int fin_exam;
public:
	Score(int m, int f) {
		mid_exam = m;
		fin_exam = f;
	}
	friend void show(Score &sc, Student &st);
};

void show(Score &sc, Student &st) {
	cout << "姓名:" << st.name << "  学号:" << st.number << endl;
	cout << "期中成绩:" << sc.mid_exam << "  期末成绩:" << sc.fin_exam << endl;
}



class Score;    //对Score类的提前引用说明
class Student{
private:
	string name;
	int number;
public:
	Student(string na, int nu) {
		name = na;
		number = nu;
	}
	void show(Score &sc);
};

class Score{
private:
	int mid_exam;
	int fin_exam;
public:
	Score(int m, int f) {
		mid_exam = m;
		fin_exam = f;
	}
	friend void Student::show(Score &sc);
};

void Student::show(Score &sc) {
	cout << "姓名:" << name << "  学号:" << number << endl;
	cout << "期中成绩:" << sc.mid_exam << "  期末成绩:" << sc.fin_exam << endl;
}




class Person{
private:
	string name;
	string id_number;
	int age;
public:
	Person(string name1, string id_number1, int age1) {
		name = name1;
		id_number = id_number1;
		age = age1;
        cout << "进入到了Person的构造函数" << endl;
	}
	~Person() {
        cout << "进入到了Person的析构函数" << endl;
	}
	void show() {
		cout << "姓名: " << name << "  身份证号: " << id_number << " 年龄: " << age << endl;
	}
};

class Student:public Person{
private:
	int credit;
public:
	Student(string name1, string id_number1, int age1, int credit1):Person(name1, id_number1, credit1) {
		credit = credit1;
        cout << "进入到了Student的构造函数" << endl;
	}
	~Student() {
        cout << "进入到了Student的析构函数" << endl;
	}
	void show() {
		Person::show();
		cout << "学分: " << credit << endl;
	}
};


class A{
public:
	A() {
		cout << "A类对象构造中..." << endl;
	}
	~A() {
		cout << "析构A类对象..." << endl;
	}
};

class B : public A{
public:
	B() {
		cout << "B类对象构造中..." << endl;
	}
	~B(){
		cout << "析构B类对象..." << endl;
	}
};



class Base{
protected:
	int a;
public:
	Base(){
		a = 5;
		cout << "Base a = " << a << endl;
	}
};

class Base1: public Base{
public:
	Base1() {
		a = a + 10;
		cout << "Base1 a = " << a << endl;
	}
};

class Base2: public Base{
public:
	Base2() {
		a = a + 20;
		cout << "Base2 a = " << a << endl;
	}
};

class Derived: public Base1, public Base2{
public:
	Derived() {
		cout << "Base1::a = " << Base1::a << endl;
		cout << "Base2::a = " << Base2::a << endl;
	}
};


class Base{
protected:
	int a;
public:
	Base(){
		a = 5;
		cout << "Base a = " << a << endl;
	}
};

class Base1: virtual public Base{
public:
	Base1() {
		a = a + 10;
		cout << "Base1 a = " << a << endl;
	}
};

class Base2: virtual public Base{
public:
	Base2() {
		a = a + 20;
		cout << "Base2 a = " << a << endl;
	}
};

class Derived: public Base1, public Base2{
public:
	Derived() {
		cout << "Base1::a = " << Base1::a << endl;
		cout << "Base2::a = " << Base2::a << endl;
	}
};


class Family{
private:
	string flower;
public:
	Family(string name = "鲜花"): flower(name) { }
	string getName() {
		return flower;
	}
	virtual void like() {
		cout << "家人喜欢不同的花: " << endl;
	}
};

class Mother: public Family{
public:
	Mother(string name = "月季"): Family(name) { }
	void like() {
		cout << "妈妈喜欢" << getName() << endl;
	}
};

class Daughter: public Family{
public:
	Daughter(string name = "百合"): Family(name) { }
	void like() {
		cout << "女儿喜欢" << getName() << endl;
	}
};


class Base{
public:
	virtual ~Base() {
		cout << "调用基类Base的析构函数..." << endl;
	}
};

class Derived: public Base{
public:
	~Derived() {
		cout << "调用派生类Derived的析构函数..." << endl;
	}
};

/*** 定义一个公共基类 ***/
class Figure{
protected:
	double x, y;
public:
	Figure(double a, double b): x(a), y(b) {  }
	virtual void getArea()      //虚函数
	{  
		cout << "No area computation defind for this class.\n";
	}
};

class Triangle: public Figure{
public:
	Triangle(double a, double b): Figure(a, b){  }
	//虚函数重定义,用于求三角形的面积
	void getArea(){
		cout << "Triangle with height " << x << " and base " << y;
		cout << " has an area of " << x * y * 0.5 << endl;
	}
};

class Square: public Figure{
public:
	Square(double a, double b): Figure(a, b){  }
	//虚函数重定义,用于求矩形的面积
	void getArea(){
		cout << "Square with dimension " << x << " and " << y;
		cout << " has an area of " << x * y << endl;
	}
};

class Circle: public Figure{
public:
	Circle(double a): Figure(a, a){  }
	//虚函数重定义,用于求圆的面积
	void getArea(){
		cout << "Circle with radius " << x ;
		cout << " has an area of " << x * x * 3.14 << endl;
	}
};



class Complex{
private:
	double real, imag;
public:
	Complex(double r = 0.0, double i = 0.0): real(r), imag(i) { }
	/*friend Complex& operator+(Complex& a, Complex& b) {
		Complex * ptemp = new Complex;
		ptemp->real = a.real + b.real;
		ptemp->imag = a.imag + b.imag;
		return *ptemp;
	}*/
    friend Complex operator+(Complex& a, Complex& b) {
		Complex temp;
		temp.real = a.real + b.real;
		temp.imag = a.imag + b.imag;
		return temp;
	}
    
	void display() {
		cout << real;
		if (imag > 0) cout << "+";
		if (imag != 0) cout << imag << "i" << endl;
	}
};



//实现一个求数组中最大值的函数模板
template<typename T>
T Max(T*array, int len)
{
    T MaxVal = array[0];
    for(int i = 0; i < len; i++)
    {
        if(array[i] > MaxVal)
        {
            MaxVal = array[i];
        }
    }
    return MaxVal;
}


template <class Type>
Type Max(Type x, Type y) {
	return x > y ? x : y;
}

template <class Type>
Type Max(Type x, Type y, Type z) {
	Type t = x > y ? x : y;
	t = t > z ? t : z;
	return t;
}


const int size = 10;
template <class T>
class Stack{
private:
	T stack[size];
	int top;
public:
	void init() {
		top = 0;
	}
	void push(T t);
	T pop();
};

template <class T>
void Stack<T>::push(T t) {
	if (top == size) {
		cout << "Stack is full!" << endl;
		return;
	}
	stack[top++] = t;
}

template <class T>
T Stack<T>::pop() {
	if (top == 0) {
		cout << "Stack is empty!" <<endl;
		return 0;
	}
	return stack[--top];
}

void func_test2()
{
    #if 0
    Score sco[3] = {Score(90, 89), Score(78, 99), Score(89, 88)};
	sco[2].show_count_sum_ave();
	Score::show_count_sum_ave();
    #endif

    #if 0
    Score score(100, 10);
	cout << "成绩为: " << getScore(score) << endl;
    #endif

    #if 0
    Score sc(89, 99);
	Student st("白", 12467);
	show(sc, st);
    #endif

    #if 0
    Score sc(89, 99);
	Student st("白", 12467);
	st.show(sc);
    #endif

    #if 0
    Student stu("白", "110103**********23", 12, 123);
	stu.show();
    #endif

    #if 0
    B b;
    #endif

    #if 0
    Derived obj;
    #endif

    #if 0
    Family *p;
	Family f;
	Mother mom;
	Daughter dau;
	p = &f;
	p->like();
	p = &mom;
	p->like();
	p = &dau;
	p->like();
    #endif

    #if 0
    Base *p;
	p = new Derived;
	delete p;
    #endif

    #if 0
    Figure *p;
	Triangle t(10.0, 6.0);
	Square s(10.0, 6.0);
	Circle c(10.0);
	p = &t;
	p->getArea();
	p = &s;
	p->getArea();
	p = &c;
	p->getArea();
    #endif

    #if 0
    //运算符重载
    Complex a(2.3, 4.6), b(3.6, 2.8), c;
	a.display();
	b.display();
	c = a + b;
	c.display();
	c = operator+(a, b);
	c.display();
    Complex d(1,1);
    c= a + b+ d;
    c.display();
    #endif

    #if 0
    //函数模板与类模板
    int array_int[] = {783, 78, 234, 34, 90, 1};
	double array_double[] = {99.02, 21.9, 23.90, 12.89, 1.09, 34.9};
	int imax = Max(array_int, 6);
	double dmax = Max(array_double, 6);
	cout << "整型数组的最大值是:" << imax << endl;
	cout << "双精度型数组的最大值是:" << dmax << endl;

    #endif

    #if 0
    cout << "33,66中最大值为 " << Max(33, 66) << endl;
	cout << "33,66,44中最大值为 " << Max(363, 66, 44) << endl;
    #endif

    #if 1
    Stack<string> st;
	st.init();
	st.push("aaa");
	st.push("bbb");
	cout << st.pop() << endl;
	cout << st.pop() << endl;
    #endif
}


ostream &output(ostream &stream)
{
	stream.setf(ios::left);
	stream << setw(10) << hex << setfill('-');
	return stream;
}

int cput() {
	ofstream outf("test.txt", ios::binary);
	if (!outf) {
		cout << "Cannot open output file.\n";
		exit(1);
	}
	char ch = 'a';
	for (int i = 0; i < 26; i++) {
		outf.put(ch);
		ch++;
	}
	outf.close();
	return 0;
}

int cget() {
	fstream inf("test.txt", ios::binary);
	if (!inf) {
		cout << "Cannot open input file.\n";
		exit(1);
	}
	char ch;
	while (inf.get(ch)) {
		cout << ch;
	}
	inf.close();
	return 0;
}


struct list{
	char course[15];
	int score;
};

void func_test3()
{
    #if 0
    cout << 123 << endl;
	cout << output << 123 << endl;
    #endif

    #if 0
    ofstream fout("test.txt", ios::out);
	if (!fout) {
		cout << "Cannot open output file." << endl;
		exit(1);
	}
	fout << "I am a student.";
	fout.close();
    #endif

    #if 0
    ifstream fin("test.txt", ios::in);
	if (!fin) {
		cout << "Cannot open output file." << endl;
		exit(1);
	}
	char str[80];
	fin.getline(str , 80);
	cout << str <<endl;
	fin.close();

    #endif

    #if 0
    cput();
	cget();   //此处文件打不开,不知为什么。。。
    #endif

    #if 0
    list ob[2] = {"Computer", 90, "History", 99};
	ofstream out("test.txt", ios::binary);
	if (!out) {
		cout << "Cannot open output file.\n";
		abort();   //退出程序,作用与exit相同。
	}
	for (int i = 0; i < 2; i++) {
		out.write((char*) &ob[i], sizeof(ob[i]));
	}
	out.close();
    #endif

    #if 0
    list ob[2];
	ifstream in("test.txt", ios::binary);
	if (!in) {
		cout << "Cannot open input file.\n";
		abort();
	}
	for (int i = 0; i < 2; i++) {
		in.read((char *) &ob[i], sizeof(ob[i]));
		cout << ob[i].course << " " << ob[i].score << endl; 
	}
	in.close();
    #endif
}



Makefile


main:main.c
	g++ -o $@ $^
	./$@
clean:
	rm main
;