This commit is contained in:
zhuyijun 2021-08-16 08:12:49 +08:00
parent d0a39e6349
commit 00407a1645
12 changed files with 383 additions and 1 deletions

View File

@ -29,4 +29,13 @@ add_executable(queueTest2 zzu/queue/queueTest2.cpp)
add_executable(LG2278 zzu/queue/LG2278.cpp)
add_executable(queueTest3 zzu/queue/queueTest3.cpp)
add_executable(settest zzu/set/settest.cpp)
add_executable(stest1 dfs/stest1.cpp)
add_executable(stest1 dfs/stest1.cpp)
add_executable(inline func/inline.cpp)
add_executable(firstref quote/firstref.cpp)
add_executable(sceref quote/sceref.cpp)
add_executable(functemp template/functemp.cpp)
add_executable(twotemps template/twotemps.cpp)
add_executable(twotemp template/twotemp.cpp)
add_executable(temptempover template/temptempover.cpp)
add_executable(choices template/choices.cpp)
add_executable(testtemplate template/testtemplate.cpp)

25
base/func/inline.cpp Normal file
View File

@ -0,0 +1,25 @@
//
// Created by nicemoe on 2021/8/9.
//
#include <iostream>
//inline 使函数内联
inline double square(double x) {
return x * x;
}
using namespace std;
int main() {
double a,b;
double c=13.0;
a = square(5.0);
b = square(4.5 + 7.5);
cout << "a = " << a << ", b = " << b <<"\n";
cout << "c = " << c;
cout <<", c squared = " << square(c++) << "\n";
cout << "Now c= " << c << "\n";
return 0;
}

View File

@ -0,0 +1,19 @@
//
// Created by nicemoe on 2021/8/15.
//
#ifndef BASE_COORDIN_H
#define BASE_COORDIN_H
struct polar{
double distance;
double angle;
};
struct rect{
double x;
double y;
};
polarrect_to_polar(rect xypos);
void show_polar(polar dapos);
#endif //BASE_COORDIN_H

View File

@ -0,0 +1,14 @@
//
// Created by nicemoe on 2021/8/15.
//
#include <iostream>
#include "coordin.h"
using namespace std;
int main() {
return 0;
}

25
base/quote/firstref.cpp Normal file
View File

@ -0,0 +1,25 @@
//
// Created by nicemoe on 2021/8/9.
//
#include <iostream>
using namespace std;
/*
*
*/
int main() {
int rats = 101;
int &rodents = rats;
cout << "rats = " << rats;
cout << ", rodents = " << rodents << endl;
rodents++;
cout << "rats = " << rats;
cout << ", rodents = " << rodents << endl;
cout << "rats address = " << &rats;
cout << ", rodents address = " << &rodents << endl;
return 0;
}

30
base/quote/sceref.cpp Normal file
View File

@ -0,0 +1,30 @@
//
// Created by nicemoe on 2021/8/9.
//
#include <iostream>
using namespace std;
int main() {
int rats = 101;
int &rodents = rats;
cout << "rats = " << rats;
cout << ", rodents = " << rodents << endl;
cout << "rats address = " << &rats;
cout << ", rodents address = " << &rodents << endl;
int bunnies = 50;
//只改变了值,没有改变地址
rodents = bunnies;
cout << "bunnies = " << bunnies;
cout << "rats = " << rats;
cout << ", rodents = " << rodents << endl;
cout << "bunnies address = " << &bunnies;
cout << ", rodents address = " << &rodents << endl;
return 0;
}

29
base/template/choices.cpp Normal file
View File

@ -0,0 +1,29 @@
//
// Created by nicemoe on 2021/8/14.
//
#include <iostream>
using namespace std;
template<typename T>
T lesser(T a, T b) {
return a < b ? a : b;
}
int lesser(int a, int b) {
a = a < 0 ? -a : a;
b = b < 0 ? -b : b;
return a < b ? a : b;
}
int main() {
int m = 20, n = -30;
double x = 15.5, y = 25.9;
cout << lesser(m, n)<< endl;
cout << lesser(x, y)<< endl;
cout << lesser<>(m, n)<< endl;
cout << lesser<int>(x, y)<< endl;
return 0;
}

View File

@ -0,0 +1,29 @@
//
// Created by nicemoe on 2021/8/11.
//
#include <iostream>
using namespace std;
template<typename T>
void Swap(T &a, T &b);
int main() {
int a = 10, b = 20;
Swap(a, b);
cout << a << " " << b << endl;
double m = 21.1, n = 31.1;
Swap(m,n);
cout << m << " " << n << endl;
return 0;
}
template<typename T>
void Swap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}

View File

@ -0,0 +1,58 @@
//
// Created by nicemoe on 2021/8/14.
//
#include <iostream>
using namespace std;
template<typename T>
void ShowArray(T arr[], int n);
template<typename T>
void ShowArray(T *arr[], int n);
struct debts {
char name[50];
double amount;
};
int main() {
int things[6]{
13,31,103,301,310,130
};
struct debts mr_E[3]{
{"Ima Wolfe", 2400.0},
{"Ura Foxe", 1300.0},
{"Iby Stout", 1800.0}
};
double *pd[3];
for (int i = 0; i < 3; ++i) {
pd[i] = &mr_E[i].amount;
}
cout << "Listing Mr. E,s counts of things:\n";
ShowArray(things,6);
cout <<"Listing Mr. E's debts:\n";
ShowArray(pd,3);
return 0;
}
template<typename T>
void ShowArray(T arr[], int n) {
cout << " template A\n";
for (int i = 0; i < n; ++i) {
cout << arr[i] << ' ';
}
cout << endl;
}
template<typename T>
void ShowArray(T *arr[], int n) {
cout << " template B\n";
for (int i = 0; i < n; ++i) {
cout << *arr[i] << ' ';
}
cout << endl;
}

View File

@ -0,0 +1,20 @@
//
// Created by nicemoe on 2021/8/15.
//
#include <iostream>
using namespace std;
template<class T1,class T2>
//c++ 11 后置返回类型
auto gt(T1 x,T2 y) -> decltype(x+y){
return x+y;
}
int main() {
int a =20;
double c = 30.2;
cout << gt(a,c);
return 0;
}

64
base/template/twotemp.cpp Normal file
View File

@ -0,0 +1,64 @@
//
// Created by nicemoe on 2021/8/11.
//
#include <iostream>
using namespace std;
template<typename T>
void Swap(T &a, T &b);
struct job {
char name[40];
double salary;
int floor;
};
template<>
void Swap<job>(job &j1, job &j2);
void Show(job &j);
int main() {
cout.precision(2);
cout.setf(ios::fixed, ios::floatfield);
int i = 10, j = 20;
cout << "i ,j =" << i << " , " << j << endl;
Swap(i, j);
cout << "Now i ,j =" << i << " , " << j << endl;
job sue = {"Susan Yaffee", 73000.60, 7};
job sidney = {"Sidney Yaffee", 78060.72, 9};
Show(sue);
Show(sidney);
Swap(sue, sidney);
cout << "交换后:" << endl;
Show(sue);
Show(sidney);
return 0;
}
template<typename T>
void Swap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
template<>
void Swap<job>(job &j1, job &j2) {
double t1;
int t2;
t1 = j1.salary;
j1.salary = j2.salary;
j2.salary = t1;
t2 = j1.floor;
j1.floor = j2.floor;
j2.floor = t2;
}
void Show(job &j) {
cout << j.name << ": $" << j.salary << " on floor " << j.floor << endl;
}

View File

@ -0,0 +1,60 @@
//
// Created by nicemoe on 2021/8/11.
//
#include <iostream>
using namespace std;
template<typename T>
void Swap(T &a, T &b);
template<typename T>
void Swap(T *a, T *b, int n);
void Show(int a[]);
const int Lim = 8;
int main() {
int i = 10, j = 20;
cout << "i ,j =" << i << " , " << j << endl;
Swap(i, j);
cout << "Now i ,j =" << i << " , " << j << endl;
int d1[Lim] = {0, 7, 0, 4, 1, 7, 7, 6};
int d2[Lim] = {0, 7, 2, 0, 1, 9, 6, 9};
Show(d1);
Show(d2);
cout << "-----------" << endl;
Swap(d1, d2, Lim);
Show(d1);
Show(d2);
return 0;
}
template<typename T>
void Swap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
template<typename T>
void Swap(T *a, T *b, int n) {
T temp;
for (int i = 0; i < n; ++i) {
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void Show(int a[]) {
cout << a[0] << a[1] << "/";
cout << a[2] << a[3] << "/";
for (int i = 4; i < Lim; ++i) {
cout << a[i];
}
cout << endl;
}