一些C++小记

一 排序模板

1. 直接插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<class T>
void InsertSort(T arr[],int n) {
for (int i = 1; i < n; i++) {
T temp; // 临时存放T类型变量
int j = 0;
temp = arr[i];
while (arr[j] < temp && j < i) {
j++;
}
for (int k = i; k >= j; k--) {
arr[k] = arr[k-1];
}
arr[j] = temp;
output(arr,n );
}
}

2. 直接选择排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class T>
void selectionSort(T arr[], int n) {
for (int i = 0; i < n-1; i++) {
int minIndex = i;
for (int j = i; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
T temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
output(arr, n);
}
}

3. 气泡排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void swap(T *a,T *b) {
T temp = *a;
*a = *b;
*b = temp;
}
template<class T>
void bubbleSort(T arr[], int n) {
bool flag = false;
int i = 0;
for (i; i < n; i++) {
for (int j = 0; j < n -1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
flag = true;
}
}
output(arr, n);
if (!flag) {
break;
}
}
}

二 查找模板

1. 顺序查找

1
2
3
4
5
6
7
8
9
10
11
template<class T>
int seqSearch(const T list[], int n, const T &key)
{
for (int i = 0; i < n; i++) {
if (list[i] == key)
{
return i;
}
}
return -1;
}

2. 折半查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<class T>
int binSearch(const T list[],int min ,int max, const T &key) {
if (min > max) {
return -1;
}
int mid = (min + max) / 2;
if (list[mid] == key) {
return mid;
}
else if (list[mid] > key) {
return binSearch(list, min, mid - 1,key);
}
else {
return binSearch(list, mid + 1, max, key);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<class T>
int binSearch_2(const T list[], int n, const T &key) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (list[mid] == key) {
return mid;
}
else if (list[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}

第六章

  1. 6-21读取字符串统计英文字母个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<fstream>
using namespace std;
int main()
{
ifstream in("C:/Users/lenovo/Desktop/abc.txt",ios_base::binary | ios_base::in);
ofstream os("C:/Users/lenovo/Desktop/abc.txt", ios_base::binary | ios_base::out);
string str;
getline(cin, str);
os << str;
os.close();
string get_s;
getline(in,get_s);
in.close();
cout << get_s;
char *c = &get_s[0];
int count = 0;
while (*c) {
if ((*c >= 'A') && (*c <= 'z')) {
count++;
}
c++;
}
cout << "count: "<< count;
return 0;
}
  1. 6-22 字符串递归逆置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #include<iostream>
    #include<string>
    #include<iomanip>
    using namespace std;
    void reverse(string &s,int n) {
    static int a = 0; //重要
    if (n / 2 <= a ) {
    return;
    }
    else {
    char temp = s[a];
    s[a] = s[n - a -1];
    s[n -a -1] = temp;
    a++;
    reverse(s,n);
    }
    }
    int main() {
    string a = "12345";
    reverse(a, a.length());
    cout << a;
    }
  2. 6-26 矩阵转置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    #include<iostream>
    #include<ctime>
    #include<stdlib.h>
    #include<iomanip>
    using namespace std;
    int row;
    int arrange;

    void output(int *p,int row, int arrange) {
    for (int i = 0; i < row; i++) {
    for (int j = 0; j < arrange; j++) {
    cout << setw(4)<< p[i*arrange + j];
    }
    cout << endl;
    }
    }

    void transfom(int *p) {
    for (int i = 0; i < row; i++) {
    for (int j = i; j < arrange; j++) {
    int temp = p[i*row + j];
    p[i*row + j] = p[j*arrange + i];
    p[j*arrange + i] = temp;
    }
    cout << endl;
    }
    }
    int main() {
    cout << "输入行和列" << endl;
    cin >> row >> arrange;
    int *matrxi = new int[row * arrange];
    for (int i = 0; i < row; i++) {
    for (int j = 0; j < arrange; j++) {
    matrxi[i*arrange + j] = rand()%20;
    }
    }
    output(matrxi,row,arrange);
    transfom(matrxi);
    output(matrxi, arrange, row);
    }
  3. 基于char*设计一个字符串类Mystring,并且具有构造函数,析构函数,赋值构造函数,重载运算符等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<string>
#include<iostream>
using namespace std;

class MyString {
private:
char *data;
unsigned short len;
public:
MyString();
MyString(unsigned short);
MyString(const char *s);
MyString(const MyString &s);
~MyString();

const unsigned short getLen() const { return len; } //返回长度
const char *getString() const { return data; }
MyString& operator +(const MyString &rhs);
MyString& operator=(const MyString &rhs);
char operator[] (unsigned short offset) const;
char& operator [](unsigned short offset);
};
MyString::MyString() {
len = 0;
data = new char[1];
data[0] = '\0';
}
MyString::MyString(unsigned short len) {
data = new char[len + 1];
for (int i = 0; i <= len; i++) {
data[i] = '\0';
}
this->len = len;
}
MyString::MyString(const char *s) {
len = (int)strlen(s);
data = new char[len];
for (int i = 0; i < len; i++) {
data[i] = s[i];
}
data[len] = '\0';
}
MyString::MyString(const MyString &s) {
len = s.getLen();
data = new char[len];
for (int i = 0; i < len; i++) {
data[i] = s[i];
}
data[len] = '\0';
}
MyString::~MyString() {
delete[] data;
len = 0;
}
char MyString::operator[](unsigned short offset) const{
if (offset > len) {
return data[len - 1];
}
else {
return data[offset];
}
}
char& MyString::operator [](unsigned short offset) {
if (offset > len) {
return data[len - 1];
}
else {
return data[offset];
}
}
MyString& MyString::operator+(const MyString &rhs) {
unsigned short totalLen = len + rhs.getLen();
MyString temp(totalLen);
int i = 0;
for (i; i < len; i++) {
temp[i] = data[i];
}
for (int j = 0; j < rhs.getLen(); j++, i++) {
temp[i] = rhs[j];
}
temp[totalLen] = '\0';
return temp;
}
MyString& MyString::operator=(const MyString &rhs) {
if (this == &rhs) {
return *this;
}
delete[] data;
len = rhs.getLen();
data = new char[len + 1];
for (unsigned short i = 0; i < len; i++) {
data[i] = rhs[i];
}
data[len] = '\0';
return *this;
}

int main() {
MyString s1("SEU!");
MyString s2;
s2 = "ilike";
MyString s3 = s2 + s1;
cout << s3.getString();
return 0;
}

第八章

  1. 时钟类重载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算,加时1秒,但要使计时过程能够自动进位。
*/
#include<iostream>
using namespace std;
class Time {
public:
Time(int h = 0, int m = 0, int s = 0) {
hour = h;
minute = m;
second = s;
}
Time& operator++();
Time operator++(int);
void showTime() {
cout << "当前时间为:" << hour << ":" << minute << ":" << second << endl;
}

private:
int hour, minute, second;

};
Time Time::operator++(int) {
Time tmp = *this;
++(*this);
return tmp;
}
Time& Time::operator++() {
++second;
if (second == 60) {
second = 0;
++minute;
if (minute == 60) {
minute = 0;
hour++;
if (hour == 24) {
hour = 0;
}
}
}
return *this;
}

int main(int argc, char const *argv[])
{
Time t(23, 59, 59);
t.showTime();
(t++).showTime();
(++t).showTime();
return 0;
}
  1. 重载complex类 <<

    • <<左操作数为ostream类型的引用,ostreamcout的基类执行

      cout<<c1——->operator<<(cout,c1)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    #include <iostream>
    using namespace std;

    class Complex
    {
    public:
    Complex() { real = 0; imag = 0; }
    Complex(double r, double i) :real(r), imag(i) {}
    void display();
    Complex operator + (Complex &c);
    Complex operator + (int &i);
    friend Complex operator + (int &, Complex &); // i + c
    friend ostream & operator << (ostream &out, const Complex &c);

    private:
    double real; // 复数实部
    double imag; // 复数虚部
    };

    Complex Complex::operator + (Complex &c)
    {
    cout << "Complex operator + (Complex &c)" << endl;
    return Complex(real + c.real, imag + c.imag);
    }

    Complex Complex::operator + (int &i)
    {
    cout << "Complex operator + (int &i)" << endl;
    return Complex(real + i, imag);
    }

    Complex operator + (int &i, Complex &c)
    {
    cout << "Complex operator + (int &i, Complex &c)" << endl;
    return Complex(i + c.real, c.imag);
    }

    void Complex::display()
    {
    cout << "(" << real << ", " << imag << ")" << endl;
    }
    ostream & operator <<(ostream &out, const Complex &c) {
    out << c.real << "+" << c.imag << endl;
    return out;
    }
    int main()
    {
    Complex c1(5, -2), c2(-4, 3), c3;
    int i = 6;
    c3 = c1 + i;
    cout << "c1 + i = ";
    c3.display();
    c3 = i + c1;
    cout << "i + c1 = ";
    c3.display();
    c3 = c1 + c2;
    cout << " c1 + c2 = ";
    cout << c3;
    system("pause");
    return 0;
    }

  2. 例题8-4 重写<<+ 前++后++

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    #include<iostream>
    #include<fstream>
    using namespace std;
    class Counter {
    private:
    int count;
    public:
    Counter() :count(0) { }
    Counter(int num):count(num) { }
    friend ostream& operator <<(ostream &out, const Counter &c);
    Counter operator +(int num);
    Counter operator ++();//前置++
    Counter operator ++(int); //后置++
    };
    Counter Counter::operator+(int num){
    count += num;
    return *this;
    }
    ostream& operator <<(ostream &out, const Counter &c) {
    out << "Count: " << c.count << endl;
    return out;
    }
    Counter Counter::operator++() {
    count++;
    return *this;
    }
    Counter Counter::operator++(int){
    Counter old = *this;
    ++(*this);
    return old;
    }

    int main() {
    Counter c1(10);
    c1 = c1 + 5;
    cout << c1 << endl;
    cout << c1++ << endl;
    cout << ++c1 << endl;
    }

2010

1. 输入n个十进制数转换成二进制写到文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<numeric>
#include<fstream>
#include<string>
using namespace std;

string toBinary(int d) {
string s;
if (d == 0) {
return "0";
}
while (d) {
s += (d % 2) ? "1" : "0";
d /= 2;
}
return s;
}


int main() {
ofstream os("C:/Users/lenovo/Desktop/te.txt", ios::binary | ios::out);
if (!os) {
cout << "File cannot be opened!" << endl;
exit(0);
}
int n;
do {
cout << "Enter the number!" << endl;
cin >> n;
string s = toBinary(n);
os << s << endl;
} while (n != 0);
}

2. 写两个模板函数:插入排序算法的迭代和递归实现

  1. 迭代
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;
template<class T>
void output(T a[]) {
for (int i = 0; i < 5; i++) {
cout << a[i] << "\t";
}
}
template<class T>
void insertSort(T arr[], int n) {
int i = 0,j = 0;
int minVal_index = 0;
for (i; i < n; i++) {
minVal_index = i;
for (j = i; j < n; j++) {
if (arr[minVal_index] > arr[j])
minVal_index = j;
}
j = minVal_index;
T temp = arr[minVal_index];
while(j > i) {
arr[j] = arr[j-1];
j--;
}
arr[i] = temp;
output(arr);
cout << endl;
}
}

int main() {
int a[5] = { 3,2,4,5,1 };
insertSort(a, 5);
}

3. 字符串解析

  • 文件内有(010)(150123456789)|123|(430070)
  • 按照以下格式输出”区号|电话号码|城市编号|邮编”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<iomanip>
using namespace std;

int main() {
//
//(010)(150123456789)|123|(430070)
ifstream in("C:/Users/lenovo/Desktop/te.txt", ios::binary | ios::in);
string origin_data;
getline(in, origin_data); //读取文件
cout << setw(15) << "区号" << setw(15) << "电话号码" << setw(15) << "城市编号" << setw(15) << "邮编" << endl;
string id = "";
for (auto a : origin_data) {
if (a != '(' && a != ')' && a != '|') {
id+=a;
}
else {
if (id.length()) {
cout << setw(15) << id;
id.clear();
}
}
}
}