2016 程序设计实习之C++部分作业题汇总(34 / 34)
由于一些题无法评测,求大佬帮助指正
http://cxsjsx.openjudge.cn/hw2016total01/
部分可以在http://cdsdzx.openjudge.cn/评测
A01:编程填空:第i位替换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int bitManipulation1(int n, int m, int i) { return n - (n & (1 << i)) + (m & (1 << i)); } int main() { int n, m, i, t; cin >> t; while (t--) { cin >> n >> m >> i; cout << bitManipulation1(n, m, i) << endl; } return 0; } |
A02:编程填空:第i位取反
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int bitManipulation2(int n, int i) { return n ^ (1 << i); } int main() { int t, n, i; cin >> t; while (t--) { cin >> n >> i; cout << bitManipulation2(n, i) << endl; } return 0; } |
A03:编程填空:左边i位取反
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int bitManipulation2(int n, int i) { return (-1 >> (32 - i) << (32 - i)) ^ n; } int main() { int t, n, i; cin >> t; while (t--) { cin >> n >> i; cout << bitManipulation2(n, i) << endl; } return 0; } |
B01:编程填空:学生信息处理程序
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(); // 输出数据 } |
B02:Apple
构造函数和析构函数的运用
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 |
#include <iostream> using namespace std; class Apple { private: static int nTotalNumber; public: Apple(){ nTotalNumber++; } ~Apple(){ nTotalNumber--; } static void PrintTotal() { cout << nTotalNumber << endl; } }; int Apple::nTotalNumber = 0; Apple Fun(const Apple & a) { a.PrintTotal(); return a; } int main() { Apple * p = new Apple[4]; Fun(p[2]); Apple p1,p2; Apple::PrintTotal (); delete [] p; p1.PrintTotal (); return 0; } |
B03:Strange class copy
复制构造函数
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 Sample { public: int v; Sample(){} Sample(const Sample &s){ v = s.v + 2; } Sample(int x){ v = x; } }; void PrintAndDouble(Sample o) { cout << o.v; cout << endl; } int main() { Sample a(5); Sample b = a; PrintAndDouble(b); Sample c = 20; PrintAndDouble(c); Sample d; d = a; cout << d.v; return 0; } |
B04:返回什么才好呢
考虑到a.GetObj()将一个类或int赋值给a,那么其应该返回类自身,用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 26 |
#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; } |
B05:第四周程序填空题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; } |
B06: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 |
#include <iostream> #include <string> #include <cstring> using namespace std; class MyString { char * p; public: MyString(const char * s) { if(s) { p = new char[strlen(s) + 1]; strcpy(p,s); } else p = NULL; } ~MyString() { if(p) delete [] p; } void Copy(const char * s) { p = new char[strlen(s) + 1]; strcpy(p, s); } friend ostream & operator<<(ostream & os, const MyString & x){ int l = strlen(x.p); for(int i = 0; i < l; i++) os << x.p[i]; return os; } MyString & operator=(const MyString &x){ p = new char[strlen(x.p) + 1]; strcpy(p, x.p); return *this; } }; int main() { char w1[200],w2[100]; while( cin >> w1 >> w2) { MyString s1(w1),s2 = s1; MyString s3(NULL); s3.Copy(w1); cout << s1 << "," << s2 << "," << s3 << endl; s2 = w2; s3 = s2; s1 = s3; cout << s1 << "," << s2 << "," << s3 << endl; } } |
B07: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; } } |
C01:看上去好坑的运算符重载
不知道他给的Inc是要干嘛
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; } |
C02:惊呆!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; } |
C03:第四周程序填空题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; } |
C04:别叫,这个大整数已经很简化了!
用new和delete的话比较麻烦,还要重载一下‘=’
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; } |
直接开int a[MAX]会省事一些
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 |
private: int a[MAX], l; public: CHugeInt(){ l = 1; memset(a, 0, sizeof(a)); } CHugeInt(const CHugeInt& x){ memset(a, 0, sizeof(a)); l = x.l; for(int i = 1; i <= l; i++) a[i] = x.a[i]; } CHugeInt(int n){ memset(a, 0, sizeof(a)); if(!n)l = 1; else l = 0; while(n){ a[++l] = n % 10; n /= 10; } } CHugeInt(char *c){ memset(a, 0, sizeof(a)); l = strlen(c); for(int i = l - 1; i >= 0; i--) a[l - i] = c[i] - '0'; } int& operator[](int x){ return a[x]; } 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; } |
D01:Complete 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 |
#include <cstdlib> #include <iostream> using namespace std; 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; } 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; } |
D02:继承自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; } |
E01:看上去像多态
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 |
#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; } |
E02:Fun and 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 |
#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; } |
E03:这是什么鬼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; //A -> B delete pa; return 0; } |
F01:这个模板并不难
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; } |
F02:排序,又见排序!
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> 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; } |
F03:山寨版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; } |
F04:简单的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 *l, T *r) { T sum = *l; if(l != r) for(T* i = l + 1; i != r; i++) 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; } |
F05:简单的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; } |
F06:简单的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; } |
F07:你真的搞清楚为啥 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; } |
G01:白给的list排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <cstdio> #include <iostream> #include <algorithm> #include <list> using namespace std; int main() { double a[] = {1.2,3.4,9.8,7.3,2.6}; list<double> lst(a,a+5); lst.sort(greater<double>()); for(list<double>::iterator i = lst.begin(); i != lst.end(); ++i) cout << * i << "," ; return 0; } |
G02:按距离排序
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; } |
G03: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; } |
G04:函数对象的过滤器
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 |
#include <iostream> #include <vector> using namespace std; struct A { int v; A() { } A(int n):v(n) { }; bool operator<(const A & a) const { return v < a.v; } }; template <class T> class FilterClass{ T l, r; public: FilterClass(){} FilterClass(T l_, T r_):l(l_), r(r_){} bool operator()(T x){ return (l < x && x < r); } }; template <class T> void Print(T s,T e) { for(;s!=e; ++s) cout << *s << ","; cout << endl; } template <class T1, class T2,class T3> T2 Filter( T1 s,T1 e, T2 s2, T3 op) { for(;s != e; ++s) { if( op(*s)) { * s2 = * s; ++s2; } } return s2; } ostream & operator <<(ostream & o,A & a) { o << a.v; return o; } vector<int> ia; vector<A> aa; int main() { int m,n; while(cin >> m >> n) { ia.clear(); aa.clear(); int k,tmp; cin >> k; for(int i = 0;i < k; ++i) { cin >> tmp; ia.push_back(tmp); aa.push_back(tmp); } vector<int> ib(k); vector<A> ab(k); vector<int>::iterator p = Filter(ia.begin(),ia.end(),ib.begin(),FilterClass<int>(m,n)); Print(ib.begin(),p); vector<A>::iterator pp = Filter(aa.begin(),aa.end(),ab.begin(),FilterClass<A>(m,n)); Print(ab.begin(),pp); } return 0; } |
G05:很难蒙混过关的CArray3d三维数组模板类
每一层用一个类B表示二维数组
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; } |
G06:我自己的 ostream_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 |
#include <iostream> #include <list> #include <string> using namespace std; template <class T1,class T2> void Copy(T1 s,T1 e, T2 x) { for(; s != e; ++s,++x) *x = *s; } template<class T> class myostream_iteraotr { private: T a; string str; ostream& os; public: myostream_iteraotr(ostream& os_, string str_):os(os_), str(str_){} void operator ++(){ cout << a << str; } T& operator *(){ return a; } }; int main() { const int SIZE = 5; int a[SIZE] = {5,21,14,2,3}; double b[SIZE] = { 1.4, 5.56,3.2,98.3,3.3}; list<int> lst(a,a+SIZE); myostream_iteraotr<int> output(cout,","); Copy( lst.begin(),lst.end(),output); cout << endl; myostream_iteraotr<double> output2(cout,"--"); Copy(b,b+SIZE,output2); return 0; } |
G07:编程填空:数据库内的学生信息
继承一下multimap和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 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 |
#include <iostream> #include <string> #include <map> #include <iterator> #include <algorithm> using namespace std; // 在此处补充你的代码 template <class T1, class T2, class T3 = greater<T1> > class MyMultimap: public multimap<T1, T2, T3>{ public: void Set(T1 a, T2 b){ for(auto it = this->find(a); it->first == a; it++) it->second = b; } }; template <typename T1,typename T2> ostream& operator <<(ostream& os, pair<T1, T2> p){ os << '(' << p.first << ',' << p.second << ')'; return os; } struct Student { string name; int score; }; template <class T> void Print(T first,T last) { for(;first!= last; ++ first) cout << * first << ","; cout << endl; } int main() { Student s[] = { {"Tom",80},{"Jack",70}, {"Jone",90},{"Tom",70},{"Alice",100} }; MyMultimap<string,int> mp; for(int i = 0; i<5; ++ i) mp.insert(make_pair(s[i].name,s[i].score)); Print(mp.begin(),mp.end()); //按姓名从大到小输出 mp.Set("Tom",78); //把所有名为"Tom"的学生的成绩都设置为78 Print(mp.begin(),mp.end()); MyMultimap<int,string,less<int> > mp2; for(int i = 0; i<5; ++ i) mp2.insert(make_pair(s[i].score,s[i].name)); Print(mp2.begin(),mp2.end()); //按成绩从小到大输出 mp2.Set(70,"Error"); //把所有成绩为70的学生,名字都改为"Error" Print(mp2.begin(),mp2.end()); cout << "******" << endl; mp.clear(); string name; string cmd; int score; while(cin >> cmd ) { if( cmd == "A") { cin >> name >> score; if(mp.find(name) != mp.end() ) { cout << "erroe" << endl; } mp.insert(make_pair(name,score)); } else if(cmd == "Q") { cin >> name; MyMultimap<string,int>::iterator p = mp.find(name); if( p!= mp.end()) { cout << p->second << endl; } else { cout << "Not Found" << endl; } } } return 0; } |
正在续
Subscribe