程序设计实习实验班2017作业(C++ 作业2, 3, 6, 7)
01:编程填空:学生信息处理程序
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 |
#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <sstream> #include <cstdlib> using namespace std; class Student { private: string name; int age, id; int score[5]; double ave; public: Student(){ name = ""; memset(score, 0, sizeof(score)); age = id = ave = 0; } void input(){ string str; int tmp = 0, num = 0; getline(cin, str); str += ','; for(int i = 0; i < str.length(); i++) if(str[i] == ',') { if(num == 1)age = tmp; if(num == 2)id = tmp; if(num > 2)score[num - 2] = tmp; tmp = 0; num++; } else { if(num == 0)name += str[i]; else tmp = tmp * 10 + str[i] - '0'; } } void calculate(){ for(int i = 1; i <= 4; i++) ave += score[i]; ave /= 4; } void output(){ cout << name << ',' << age << ',' << id << ',' << ave << endl; } }; int main() { Student student; // 定义类的对象 student.input(); // 输入数据 student.calculate(); // 计算平均成绩 student.output(); // 输出数据 } |
02:返回什么才好呢
this指针
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 |
#include <iostream> using namespace std; class A { public: int val; A(int x = 123){ val = x; } A& GetObj(){ return *this; } }; int main() { int m,n; A a; cout << a.val << endl; while(cin >> m >> n) { a.GetObj() = m; cout << a.val << endl; a.GetObj() = A(n); cout << a.val<< endl; } return 0; } |
03:第三周程序填空题2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; class A { public: int val; A(){ val = 0; } int& GetObj(){ return val; } }; main() { A a; cout << a.val << endl; a.GetObj() = 5; cout << a.val << endl; } |
05:第四周程序填空题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 |
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" << i << "i" << endl; } Complex& operator=(const string& x){ bool opt = 0; this->r = this->i = 0; for(int i = 0; i < x.length(); i++) if(x[i] == '+')opt = 1; else if(x[i] >= '0' && x[i] <= '9') { if(opt)this->i = this->i * 10 + x[i] - '0'; else this->r = this->r * 10 + x[i] - '0'; } return *this; } }; int main() { Complex a; a = "3+4i"; a.Print(); a = "5+6i"; a.Print(); return 0; } |
06:第四周程序填空题3
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 |
#include <iostream> #include <cstring> using namespace std; class Array2 { private: int *a; int i,j; public: Array2(){a = NULL;} Array2(int _i, int _j){ i = _i, j = _j; a = new int[i * j]; } Array2(Array2 &t){ i = t.i; j = t.j; a = new int[i * j]; memcpy(a, t.a, sizeof(int) * i * j); } int* operator[](int _i){ return a + _i * j; } int operator()(int _i, int _j){ return a[_i * j + _j]; } }; int main() { Array2 a(3,4); int i,j; for( i = 0;i < 3; ++i ) for( j = 0; j < 4; j ++ ) a[i][j] = i * 4 + j; for( i = 0;i < 3; ++i ) { for( j = 0; j < 4; j ++ ) { cout << a(i,j) << ","; } cout << endl; } cout << "next" << endl; Array2 b; b = a; for( i = 0;i < 3; ++i ) { for( j = 0; j < 4; j ++ ) { cout << b[i][j] << ","; } cout << endl; } return 0; } |
07:第三周程序填空题4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; class Base { public: int k; Base(int n):k(n) { } }; class Big { public: int v; Base b; public: Big():b(0){} Big(int x):v(x),b(x){} }; int main() { Big a1(5); Big a2 = a1; cout << a1.v << "," << a1.b.k << endl; cout << a2.v << "," << a2.b.k << endl; return 0; } |
08:别叫,这个大整数已经很简化了!
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 106 107 108 109 110 111 |
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> using namespace std; const int MAX = 110; class CHugeInt { private: int *a, l; public: CHugeInt(){ a = new int[MAX]; l = 1; memset(a, 0, sizeof(int) * MAX); } CHugeInt(const CHugeInt& x){ a = new int[MAX]; memset(a, 0, sizeof(int) * MAX); l = x.l; for(int i = 1; i <= l; i++) a[i] = x.a[i]; } ~CHugeInt(){ if(a)delete [] a; } CHugeInt(int n){ a = new int[MAX]; memset(a, 0, sizeof(int) * MAX); if(!n)l = 1; else l = 0; while(n){ a[++l] = n % 10; n /= 10; } } CHugeInt(char *c){ a = new int[MAX]; memset(a, 0, sizeof(int) * MAX); l = strlen(c); for(int i = l - 1; i >= 0; i--) a[l - i] = c[i] - '0'; } int& operator[](int x){ return a[x]; } CHugeInt& operator =(const CHugeInt& x){ a = new int[MAX]; memset(a,0,sizeof(*a)); l = x.l; for(int i = 1; i <= l; i++) a[i] = x.a[i]; return *this; } friend CHugeInt operator +(CHugeInt a, CHugeInt b){ CHugeInt c; c.l = max(a.l, b.l); for(int i = 1; i <= c.l; i++) c[i] = a[i] + b[i]; for(int i = 1; i <= c.l; i++) if(c[i] >= 10) { if(i == c.l)c.l++; c[i + 1] += c[i] / 10; c[i] %= 10; } return c; } friend ostream& operator << (ostream& os, CHugeInt x){ for(int i = x.l; i >= 1; i--) os << x.a[i]; return os; } friend CHugeInt operator +(CHugeInt a, int b){ return a + CHugeInt(b); } friend CHugeInt operator +(int a, CHugeInt b){ return CHugeInt(a) + b; } CHugeInt operator +=(int x){ *this = *this + CHugeInt(x); return *this; } CHugeInt operator ++(){ *this = *this + CHugeInt(1); return *this; } CHugeInt operator ++(int){ CHugeInt tmp = *this; *this = CHugeInt(1) + *this; return tmp; } }; int main() { char s[210]; int n; while (cin >> s >> n) { CHugeInt a(s); CHugeInt b(n); cout << a + b << endl; cout << n + a << endl; cout << a + n << endl; b += n; cout << ++ b << endl; cout << b++ << endl; cout << b << endl; } return 0; } |
02:自己编写排序函数mysort
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 |
#include <iostream> using namespace std; struct A { int nouse; int n; A(int i):n(i) { }; A() { } }; int mysort(void * a,int n,int w, int (* compare)(const void * e1,const void * e2)) { for(int i = 0; i < n; i++) for(int j = 0; j < n - 1; j++) { char *t = (char*)a; t += j * w; if(compare(t, t + w) > 0) { for(int k = 0; k < w; k++) swap(*(t + k), *(t + w + k)); } } } int MyCompare1( const void * e1,const void * e2) { int * p1 = (int * ) e1; int * p2 = (int * ) e2; return * p1 - * p2; } int MyCompare2( const void * e1,const void * e2) { int * p1 = (int * ) e1; int * p2 = (int * ) e2; return (* p1 %10) - (* p2 % 10); } int MyCompare3( const void * e1,const void * e2) { A * p1 = (A*) e1; A * p2 = (A*) e2; return p1->n - p2->n; } int a[20]; A b[20]; int main () { int n; while(cin >> n) { for(int i = 0;i < n; ++i) { cin >> a[i]; b[i] = a[i]; } mysort(a,n,sizeof(int),MyCompare1); for(int i = 0;i < n; ++i) cout << a[i] << "," ; cout << endl; mysort(a,n,sizeof(int),MyCompare2); for(int i = 0;i < n; ++i) cout << a[i] << "," ; cout << endl; mysort(b,n,sizeof(A),MyCompare3); for(int i = 0;i < n; ++i) cout << b[i].n << "," ; cout << endl; } return 0; } |
03:看上去像多态
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 |
#include <iostream> using namespace std; class B { private: int nBVal; public: void Print() { cout << "nBVal="<< nBVal << endl; } void Fun() {cout << "B::Fun" << endl; } B ( int n ) { nBVal = n;} }; class D : public B{ private: int nDVal; public: D(int n):B(3 * n){ nDVal = n; } void Fun(){ cout << "D::Fun" << endl; } void Print(){ B::Print(); cout << "nDVal=" << nDVal << endl; } }; int main() { B * pb; D * pd; D d(4); d.Fun(); pb = new B(2); pd = new D(8); pb -> Fun(); pd->Fun(); pb->Print (); pd->Print (); pb = & d; pb->Fun(); pb->Print(); return 0; } |
04:Fun和Do
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> using namespace std; class A { private: int nVal; public: void Fun() { cout << "A::Fun" << endl; }; void Do() { cout << "A::Do" << endl; } }; class B:public A { public: virtual void Do() { cout << "B::Do" << endl;} }; class C:public B { public: void Do( ) { cout <<"C::Do"<<endl; } void Fun() { cout << "C::Fun" << endl; } }; void Call( class B& p ) { p.Fun(); p.Do(); } int main() { C c; Call( c); return 0; } |
05:这是什么鬼delete
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; class A { public: A() { } virtual ~A() { cout << "destructor A" << endl; } }; class B:public A { public: ~B() { cout << "destructor B" << endl; } }; int main() { A * pa; pa = new B; delete pa; return 0; } |
06:怎么又是Fun和Do
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> using namespace std; class A { private: int nVal; public: void Fun() { cout << "A::Fun" << endl; }; virtual void Do() { cout << "A::Do" << endl; } }; class B:public A { public: virtual void Do() { cout << "B::Do" << endl;} }; class C:public B { public: void Do( ) { cout <<"C::Do"<<endl; } void Fun() { cout << "C::Fun" << endl; } }; void Call( A* p ) { p->Fun(); p->Do(); } int main() { Call( new A()); Call( new C()); return 0; } |
07:全面的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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
#include <cstdlib> #include <iostream> using namespace std; int strlen(const char * s) { int i = 0; for(; s[i]; ++i); return i; } void strcpy(char * d,const char * s) { int i = 0; for( i = 0; s[i]; ++i) d[i] = s[i]; d[i] = 0; } int strcmp(const char * s1,const char * s2) { for(int i = 0; s1[i] && s2[i] ; ++i) { if( s1[i] < s2[i] ) return -1; else if( s1[i] > s2[i]) return 1; } return 0; } void strcat(char * d,const char * s) { int len = strlen(d); strcpy(d+len,s); } class MyString { private: char *p; public: MyString(const char * s) { p = new char[strlen(s) + 1]; strcpy(p, s); } ~MyString() { if(p) delete [] p; } MyString(const MyString& a) { p = new char[strlen(a.p) + 1]; strcpy(p, a.p); } MyString(){ p = new char[1]; } char& operator[](int x){ return p[x]; } friend bool operator <(MyString a, MyString b){ return (-1 == strcmp(a.p, b.p)); } friend bool operator ==(MyString a, MyString b){ return (!strcmp(a.p, b.p)); } friend bool operator >(MyString a, MyString b){ return (1 == strcmp(a.p, b.p)); } friend ostream & operator <<(ostream & os, MyString & x){ int l = strlen(x.p); for(int i = 0; i < l; i++) os << x[i]; return os; } friend MyString operator +(MyString a, MyString b){ char *tmp = new char[strlen(a.p) + strlen(b.p) + 1]; strcpy(tmp, a.p); strcat(tmp, b.p); return MyString(tmp); } MyString operator +=(const char * c){ *this = *this + MyString(c); return *this; } char* operator ()(int x, int y){ char *tmp = new char[y]; for(int i = 0; i < y; i++) tmp[i] = p[x + i]; return tmp; } MyString & operator=(const MyString &x){ p = new char[strlen(x.p) + 1]; strcpy(p, x.p); return *this; } }; int CompareString( const void * e1, const void * e2) { MyString * s1 = (MyString * ) e1; MyString * s2 = (MyString * ) e2; if( * s1 < *s2 ) return -1; else if( *s1 == *s2) return 0; else if( *s1 > *s2 ) return 1; } int main() { MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); MyString SArray[4] = {"big","me","about","take"}; cout << "1. " << s1 << s2 << s3<< s4<< endl; s4 = s3; s3 = s1 + s3; cout << "2. " << s1 << endl; cout << "3. " << s2 << endl; cout << "4. " << s3 << endl; cout << "5. " << s4 << endl; cout << "6. " << s1[2] << endl; s2 = s1; s1 = "ijkl-"; s1[2] = 'A' ; cout << "7. " << s2 << endl; cout << "8. " << s1 << endl; s1 += "mnop"; cout << "9. " << s1 << endl; s4 = "qrst-" + s2; cout << "10. " << s4 << endl; s1 = s2 + s4 + " uvw " + "xyz"; cout << "11. " << s1 << endl; qsort(SArray,4,sizeof(MyString),CompareString); for( int i = 0;i < 4;i ++ ) cout << SArray[i] << endl; //s1的从下标0开始长度为4的子串 cout << s1(0,4) << endl; //s1的从下标5开始长度为10的子串 cout << s1(5,10) << endl; return 0; } |
08:继承自string的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 |
#include <cstdlib> #include <iostream> #include <string> #include <algorithm> using namespace std; class MyString:public string { public: MyString(){} MyString(const char* x):string(x){} MyString operator +(MyString x){ string str = (*this) + string(x); return MyString(str.c_str()); } MyString operator +(const char* x){ string str = (*this); str += x; return MyString(str.c_str()); } friend MyString operator +(const char* a, const MyString b){ MyString str; str += a + string(b); return str; } string operator()(int a, int b){ string str = ""; for(int i = 0; i < b; i++) str += (*this)[a + i]; return str; } }; int main() { MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); MyString SArray[4] = {"big","me","about","take"}; cout << "1. " << s1 << s2 << s3<< s4<< endl; s4 = s3; s3 = s1 + s3; cout << "2. " << s1 << endl; cout << "3. " << s2 << endl; cout << "4. " << s3 << endl; cout << "5. " << s4 << endl; cout << "6. " << s1[2] << endl; s2 = s1; s1 = "ijkl-"; s1[2] = 'A' ; cout << "7. " << s2 << endl; cout << "8. " << s1 << endl; s1 += "mnop"; cout << "9. " << s1 << endl; s4 = "qrst-" + s2; cout << "10. " << s4 << endl; s1 = s2 + s4 + " uvw " + "xyz"; cout << "11. " << s1 << endl; sort(SArray,SArray+4); for( int i = 0;i < 4;i ++ ) cout << SArray[i] << endl; //s1的从下标0开始长度为4的子串 cout << s1(0,4) << endl; //s1的从下标5开始长度为10的子串 cout << s1(5,10) << endl; return 0; } |
09:惊呆!Point竟然能这样输入输出
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 |
#include <iostream> using namespace std; class Point { private: int x; int y; public: Point() { }; friend istream& operator >>(istream& is, Point& p){ cin >> p.x >> p.y; return is; } friend ostream& operator <<(ostream& os, Point& p){ cout << p.x << ',' << p.y; return os; } }; int main() { Point p; while(cin >> p) { cout << p << endl; } return 0; } |
10:Big & Base 封闭类问题
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 |
#include <iostream> #include <string> using namespace std; class Base { public: int k; Base(int n):k(n) { } }; class Big { public: int v; Base b; Big(int n):v(n),b(Base(n)){} }; int main() { int n; while(cin >>n) { Big a1(n); Big a2 = a1; cout << a1.v << "," << a1.b.k << endl; cout << a2.v << "," << a2.b.k << endl; } } |
11:看上去好坑的运算符重载
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> using namespace std; class MyInt { int nVal; public: MyInt( int n) { nVal = n ;} MyInt& operator-(int x){ nVal-=x; return *this; } friend int Inc(MyInt x){ return x.nVal + 1; } }; int Inc(int n) { return n + 1; } int main () { int n; while(cin >>n) { MyInt objInt(n); objInt-2-1-3; cout << Inc(objInt); cout <<","; objInt-2-1; cout << Inc(objInt) << endl; } return 0; } |
12:为啥 while(cin >> 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 |
#include <iostream> using namespace std; class MyCin { private: bool flag; public: MyCin(){ flag = 1; } MyCin& operator >> (int& p){ p = -1; if(flag)cin >> p; if(p == -1)flag = 0; return (*this); } operator bool(){ return flag; } }; int main() { MyCin m; int n1,n2; while( m >> n1 >> n2) cout << n1 << " " << n2 << endl; return 0; } |
13:这个模板并不难
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 |
#include <iostream> #include <string> #include <cstring> using namespace std; template <class T> class myclass { int size; T *p; public: myclass(T *c, int size_){ p = new T[size_]; size = size_; for(int i = 0; i < size_; i++) p[i] = c[i]; } ~myclass( ) { delete [] p; } void Show() { for( int i = 0;i < size;i ++ ) { cout << p[i] << ","; } cout << endl; } }; int a[100]; int main() { char line[100]; while( cin >> line ) { myclass<char> obj(line,strlen(line));; obj.Show(); int n; cin >> n; for(int i = 0;i < n; ++i) cin >> a[i]; myclass<int> obj2(a,n); obj2.Show(); } return 0; } |
14:排序,又见排序!
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 |
#include <iostream> using namespace std; bool Greater2(int n1,int n2) { return n1 > n2; } bool Greater1(int n1,int n2) { return n1 < n2; } bool Greater3(double d1,double d2) { return d1 < d2; } template <class T1,class T2> void mysort( T1 l, T1 r, T2 cmp){ for(T1 i = l; i != r; i++) for(T1 j = i + 1; j != r; j++) if(cmp(*j, *i))swap(*i, *j); } #define NUM 5 int main() { int an[NUM] = { 8,123,11,10,4 }; mysort(an,an+NUM,Greater1); //从小到大排序 for( int i = 0;i < NUM; i ++ ) cout << an[i] << ","; mysort(an,an+NUM,Greater2); //从大到小排序 cout << endl; for( int i = 0;i < NUM; i ++ ) cout << an[i] << ","; cout << endl; double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1}; mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 for( int i = 0;i < 6; i ++ ) cout << d[i] << ","; return 0; } |
15:哭死!三维数组类模板增强版!
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 |
#include <iostream> #include <iomanip> #include <cstring> using namespace std; template <class T> class CArray3D { class CArray2D{ private: int i, j; T* a; public: void ini(int _i, int _j){ i = _i; j = _j; a = new T[i * j]; } ~CArray2D(){ delete [] a; } T* operator[](int _i){ return a + _i * j; } operator T*(){ return this->a; } void print(int id){ cout << "layer " << id << ":" << endl; for(int x = 0; x < i; x++) { for(int y = 0; y < j; y++) cout << (*this)[x][y] << ','; cout<<endl; } } }; int k; CArray2D* a; public: CArray3D(){} ~CArray3D(){ delete [] a; } CArray3D(int _k, int _i, int _j):k(_k){ a = new CArray2D[k]; for(int i = 0; i < k; i++) a[i].ini(_i, _j); } CArray2D& operator[](int _k) { return a[_k]; } void PrintAll(){ for(int i = 0; i < k; i++) (*this)[i].print(i); } }; int main() { CArray3D<int> a(3,4,5); int No = 0; for( int i = 0; i < 3; ++ i ) for( int j = 0; j < 4; ++j ) for( int k = 0; k < 5; ++k ) a[i][j][k] = No ++; a.PrintAll(); memset(a[1],-1 ,20*sizeof(int)); a.PrintAll(); memset(a[1][1],0 ,5*sizeof(int)); a.PrintAll(); CArray3D<double> b(3,2,2); for( int i = 0; i < 3; ++ i ) for( int j = 0; j < 2; ++j ) for( int k = 0; k < 2; ++k ) b[i][j][k] = 10.0/(i+j+k+1); b.PrintAll(); return 0; } |
16:山寨版istream_iterator
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 |
#include <iostream> #include <string> using namespace std; template <class T> class CMyistream_iterator { private: istream& ist; T now; public: CMyistream_iterator(){} CMyistream_iterator(istream &ist_):ist(ist_){ cin >> now; } T operator*(){ return now; } void operator ++(int){ cin >> now; } }; int main() { int t; cin >> t; while( t -- ) { CMyistream_iterator<int> inputInt(cin); int n1,n2,n3; n1 = * inputInt; //读入 n1 int tmp = * inputInt; cout << tmp << endl; inputInt ++; n2 = * inputInt; //读入 n2 inputInt ++; n3 = * inputInt; //读入 n3 cout << n1 << " " << n2<< " " << n3 << " "; CMyistream_iterator<string> inputStr(cin); string s1,s2; s1 = * inputStr; inputStr ++; s2 = * inputStr; cout << s1 << " " << s2 << endl; } return 0; } |
01:简单的PrintArray
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> using namespace std; template <class T> void PrintArray( T a, T b) { for(T i = a; i != b; i++) cout << *i << ','; cout << endl; } int main() { string array[4] = { "Tom","Jack","Mary","John"}; PrintArray(array,array+4); int a[4] = { 1, 2, 3, 4}; PrintArray(a,a+4); return 0; } |
02:简单的SumArray
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> using namespace std; template <class T> T SumArray( T* a, T* b) { T sum = *a; for(T* i = a + 1; i != b; i++) sum = sum + *i; return sum; } int main() { string array[4] = { "Tom","Jack","Mary","John"}; cout << SumArray(array,array+4) << endl; int a[4] = { 1, 2, 3, 4}; //提示:1+2+3+4 = 10 cout << SumArray(a,a+4) << endl; return 0; } |
03:简单的foreach
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 |
#include <iostream> #include <string> using namespace std; template <class T1, class T2> void MyForeach(T1 l, T1 r, T2 f) { for(T1 i = l; i != r; i++) f(*i); } void Print(string s) { cout << s; } void Inc(int & n) { ++ n; } string array[100]; int a[100]; int main() { int m,n; while(cin >> m >> n) { for(int i = 0;i < m; ++i) cin >> array[i]; for(int j = 0; j < n; ++j) cin >> a[j]; MyForeach(array,array+m,Print); cout << endl; MyForeach(a,a+n,Inc); for(int i = 0;i < n; ++i) cout << a[i] << ","; cout << endl; } return 0; } |
04:简单的Filter
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 |
#include <iostream> #include <string> using namespace std; template <class T1, class T2> T1 Filter(T1 l, T1 r, T1 con, T2 f) {// 返回尾指针 for(T1 i = l; i != r; i++) if(f(*i)) { *con = *i; con++; } return con; } bool LargerThan2(int n) { return n > 2; } bool LongerThan3(string s) { return s.length() > 3; } string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"}; string as2[5]; int a1[5] = { 1,2,3,4,5}; int a2[5]; int main() { string * p = Filter(as1,as1+5,as2,LongerThan3); for(int i = 0;i < p - as2; ++i) cout << as2[i]; cout << endl; int * p2 = Filter(a1,a1+5,a2,LargerThan2); for(int i = 0;i < p2-a2; ++i) cout << a2[i] << ","; return 0; } |
05:goodcopy
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 |
#include <iostream> using namespace std; template <class T> struct GoodCopy { void operator()(T* l, T* r, T* st){ T* p = new T[r - l + 2]; for(T* i = l; i != r; i++) p[i - l] = *i; for(int i = 0; i <= r - l + 1; i++) *(st + i) = p[i]; delete [] p; } }; int a[200]; int b[200]; string c[200]; string d[200]; template <class T> void Print(T s,T e) { for(; s != e; ++s) cout << * s << ","; cout << endl; } int main() { int t; cin >> t; while( t -- ) { int m ; cin >> m; for(int i = 0;i < m; ++i) cin >> a[i]; GoodCopy<int>()(a,a+m,b); Print(b,b+m); GoodCopy<int>()(a,a+m,a+m/2); Print(a+m/2,a+m/2 + m); for(int i = 0;i < m; ++i) cin >> c[i]; GoodCopy<string>()(c,c+m,d); Print(c,c+m); GoodCopy<string>()(c,c+m,c+m/2); Print(c+m/2,c+m/2 + m); } return 0; } |
06:你真的搞清楚为啥 while(cin >> 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 |
#include <iostream> using namespace std; class MyCin { private: bool flag; public: MyCin(){ flag = 1; } MyCin& operator >> (int& p){ if(flag)scanf("%d", &p); if(p == -1)flag = 0; return (*this); } operator bool(){ return flag; } }; int main() { MyCin m; int n1,n2; while( m >> n1 >> n2) cout << n1 << " " << n2 << endl; return 0; } |
07:按距离排序
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 |
#include <iostream> #include <cmath> #include <algorithm> #include <string> using namespace std; template <class T1,class T2> struct Closer { private: T1 bas; T2 f; public: Closer(){} Closer(T1 bas_, T2 f_):bas(bas_),f(f_){} bool operator()(const T1 &a, const T1 &b){ if(f(a, bas) == f(b, bas)) return a < b; else return f(a, bas) < f(b, bas); } }; int Distance1(int n1,int n2) { return abs(n1-n2); } int Distance2(const string & s1, const string & s2) { return abs((int)s1.length()- (int) s2.length()); } int a[10] = { 0,3,1,4,7,9,20,8,10,15}; string b[6] = {"American","Jack","To","Peking","abcdefghijklmnop","123456789"}; int main() { int n;string s; while( cin >> n >> s ) { sort(a,a+10,Closer<int ,int (*)(int ,int)> (n,Distance1)); for(int i = 0;i < 10; ++i) cout << a[i] << "," ; cout << endl; sort(b,b+6,Closer<string,int (*)(const string &,const string & )> (s,Distance2)); for(int i = 0;i < 6; ++i) cout << b[i] << "," ; cout << endl; } return 0; } |
08:自己实现bitset
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 |
#include <iostream> #include <cstring> using namespace std; template <int bitNum> struct MyBitset { char a[bitNum/8+1]; MyBitset() { memset(a,0,sizeof(a));}; void Set(int i,int v) { char & c = a[i/8]; int bp = i % 8; if( v ) c |= (1 << bp); else c &= ~(1 << bp); } class A{ public: const int x; char* a; A(int _x, char *_a):x(_x), a(_a){} A& operator=(int v){ if(v)a[x / 8] |= (1 << (x % 8)); else a[x / 8] &= ~(1 << (x % 8)); return *this; } operator bool(){ return 1 & (a[x / 8] >> (x % 8)); } A& operator=(A& p){ *this = bool(p); return *this; } }; A operator[](int x){ return A(x, a); } void Print() { for(int i = 0;i < bitNum; ++i) cout << (*this) [i]; cout << endl; } }; int main() { int n; int i,j,k,v; while( cin >> n) { MyBitset<20> bs; for(int i = 0;i < n; ++i) { int t; cin >> t; bs.Set(t,1); } bs.Print(); cin >> i >> j >> k >> v; bs[k] = v; bs[i] = bs[j] = bs[k]; bs.Print(); cin >> i >> j >> k >> v; bs[k] = v; (bs[i] = bs[j]) = bs[k]; bs.Print(); } return 0; } |
09:很难蒙混过关的CArray3d三维数组模板类
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 |
#include <iostream> #include <iomanip> #include <cstring> using namespace std; template <class T> class CArray3D { class CArray2D{ private: int i, j; T* a; public: CArray2D(){} void ini(int _i, int _j){ i = _i; j = _j; a = new T[i * j]; } ~CArray2D(){ delete [] a; } T* operator[](int _i){ return a + _i * j; } operator T*(){ return this->a; } }; private: CArray2D* a; public: CArray3D(){} ~CArray3D(){ delete [] a; } CArray3D(int _k, int _i, int _j){ a = new CArray2D[_k]; for(int i = 0; i < _k; i++) a[i].ini(_i, _j); } CArray2D& operator[](int _k) { return a[_k]; } }; CArray3D<int> a(3,4,5); CArray3D<double> b(3,2,2); void PrintA() { for(int i = 0;i < 3; ++i) { cout << "layer " << i << ":" << endl; for(int j = 0; j < 4; ++j) { for(int k = 0; k < 5; ++k) cout << a[i][j][k] << "," ; cout << endl; } } } void PrintB() { for(int i = 0;i < 3; ++i) { cout << "layer " << i << ":" << endl; for(int j = 0; j < 2; ++j) { for(int k = 0; k < 2; ++k) cout << b[i][j][k] << "," ; cout << endl; } } } int main() { int No = 0; for( int i = 0; i < 3; ++ i ) { a[i]; for( int j = 0; j < 4; ++j ) { a[j][i]; for( int k = 0; k < 5; ++k ) a[i][j][k] = No ++; a[j][i][i]; } } PrintA(); memset(a[1],-1 ,20*sizeof(int)); memset(a[1],-1 ,20*sizeof(int)); PrintA(); memset(a[1][1],0 ,5*sizeof(int)); PrintA(); for( int i = 0; i < 3; ++ i ) for( int j = 0; j < 2; ++j ) for( int k = 0; k < 2; ++k ) b[i][j][k] = 10.0/(i+j+k+1); PrintB(); int n = a[0][1][2]; double f = b[0][1][1]; cout << "****" << endl; cout << n << "," << f << endl; return 0; } |
1:高阶函数F(x)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; template <class T1, class T2> class f{ T1 a; public: f(T1 _a):a(_a){} T2 operator()(T2 b){ return b + a; } }; int main() { cout << f<int,int>(7)(9) << endl; //16 cout << f<string,string> (" hello!")("world") <<endl; // world hello! cout << f<char,string> ('!')("world") << endl; return 0; //world! } |
2:高阶函数Combine
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; template <class T1, class T2, class T3> class combine{ T1 f; T2 g; public: combine(T1 _f, T2 _g):f(_f),g(_g){} T3 operator()(T3 x){ return f(f(x) + g(x)); } }; int main() { auto Square = [] (double a) { return a * a; }; auto Inc = [] (double a) { return a + 1; }; cout << combine<decltype(Square),decltype(Inc),int>(Square,Inc)(3) << endl; cout << combine<decltype(Inc),decltype(Square),double>(Inc,Square)(2.5) << endl; return 0; } |
3:实现sharedptr
每个类用一段地址记录 cnt,派生类的 cnt 地址继承基类并将其 +1,改变类中指针的时候 new 一段新的 cnt 地址,原来的保留
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 |
#include <iostream> using namespace std; //#define MySharedPtr shared_ptr template <class T> struct MySharedPtr { mutable int* cnt; T *p; void dec(){ if(cnt) { (*cnt)--; if(*cnt == 0 && p)(*p).~T(); } } void set(){ cnt = new int; *cnt = 1; } MySharedPtr(const MySharedPtr& a){ p = a.p; cnt = a.cnt; (*cnt)++; } MySharedPtr(T* _p){ set(); p = _p; } MySharedPtr(){ cnt = NULL; p = NULL; } ~MySharedPtr(){ dec(); } void operator=(const MySharedPtr& a){ p = a.p; cnt = a.cnt; (*cnt)++; } void reset(T* _p = NULL){ dec(); set(); p = _p; } T* get(){return p;} T* operator->(){return p;} T& operator*(){return *p;} operator bool(){return (p != NULL);} }; struct A { int n; A(int v = 0):n(v){ } ~A() { cout << n << " destructor" << endl; } }; int main() { MySharedPtr<A> sp1(new A(2)); MySharedPtr<A> sp2(sp1); cout << "1)" << sp1->n << "," << sp2->n << endl; MySharedPtr<A> sp3; A * p = sp1.get(); cout << "2)" << p->n << endl; sp3 = sp1; cout << "3)" << (*sp3).n << endl; sp1.reset(); if( !sp1 ) cout << "4)sp1 is null" << endl; //会输出 A * q = new A(3); sp1.reset(q); // cout << "5)" << sp1->n << endl; MySharedPtr<A> sp4(sp1); MySharedPtr<A> sp5; sp5.reset(new A(8)); sp1.reset(); cout << "before end main" <<endl; sp4.reset(); cout << "end main" << endl; return 0; //程序结束,会delete 掉A(2) } |
黄学长使用的是什么评论系统? /06