2016程序设计实习实验班免修考试(C++)
1:编程填空:计算数列平方和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> using namespace std; int sqr(int n); int sum(int *a, int n, int f(int)) { int ans = 0; for(int i = 0; i < n; i++) ans += f(a[i]); return ans; } int sqr(int n) { return n * n; } int main() { int t, n, a[0x100]; cin >> t; for (int c = 0; c < t; ++c) { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; cout << sum(a, n, sqr) << endl; } return 0; } |
2:编程填空: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 |
#include <cstring> #include <cstdlib> #include <string> #include <iostream> using namespace std; struct MyString{ string str; MyString(){} MyString(string _str):str(_str){} MyString(char *c){ int l = strlen(c); str = ""; for(int i = 0; i < l; i++) str += c[i]; } char& operator[](int x){ return str[x]; } string operator()(int x, int y){ string ans = ""; for(int i = 0; i < y; i++) ans += str[i + x]; return ans; } string operator+=(MyString a){ return str += a.str; } friend string operator+(MyString a, MyString b){ return a.str + b.str; } friend bool operator<(MyString a, MyString b){ return a.str < b.str; } friend bool operator>(MyString a, MyString b){ return a.str > b.str; } friend bool operator==(MyString a, MyString b){ return a.str == b.str; } friend ostream& operator <<(ostream& os, MyString a){ os << a.str; return os; } }; 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; } |
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 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 <string> #include <list> using namespace std; class A{ private: string name; public: A(string n) :name(n){} friend bool operator < (const class A& a1, const class A &a2); friend bool operator == (const class A &a1, const class A &a2){ if (a1.name.size() == a2.name.size()) return true; else return false; } friend ostream & operator << (ostream &o, const A &a){ o << a.name; return o; } string get_name() const{ return name; } int get_size() const{ return name.size(); } }; bool operator<(const A &a, const A &b){ return a.get_size() < b.get_size(); } template <class T> class MyLarge{ public: bool operator()(const T &a, const T &b){ return a.get_name()[0] < b.get_name()[0]; } }; class Print{ public: void operator()(const string &x){ cout << x << ' '; } }; template <class T> void Show(T l, T r, Print f) { while(l != r){ f((*l).get_name()); l++; } } int main(int argc, char* argv[]) { list<A> lst; int ncase, n, i = 1; string s; cin >> ncase; while (ncase--){ cout << "Case: "<<i++ << endl; cin >> n; for (int i = 0; i < n; i++){ cin >> s; lst.push_back(A(s)); } lst.sort(); Show(lst.begin(), lst.end(), Print()); cout << endl; lst.sort(MyLarge<A>()); Show(lst.begin(), lst.end(), Print()); cout << endl; lst.clear(); } return 0; } |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> #include <set> #include <iterator> #include <algorithm> using namespace std; class A{ public: int k; A(){} A(int _k):k(_k){} virtual void print(){ cout << 'A' << ' ' << k << endl; } }; class B:public A{ public: B(int _k):A(_k){} void print(){ cout << 'B' << ' ' << k << endl; } }; class Comp{ public: bool operator()(const A* a, const A* b){ return a->k < b->k; } }; void Print(A* a) { a->print(); } int main() { int t; cin >> t; set<A*,Comp> ct; while( t -- ) { int n; cin >> n; ct.clear(); for( int i = 0;i < n; ++i) { char c; int k; cin >> c >> k; if( c == 'A') ct.insert(new A(k)); else ct.insert(new B(k)); } for_each(ct.begin(),ct.end(),Print); cout << "****" << endl; } } |
5:编程填空:数据库内的学生信息
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; } |
6:编程填空:输出指定结果二
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 <map> using namespace std; class A{ public: static int count; int x; A(){ count++; } virtual ~A(){ puts("A::destructor"); count--; } A(int _x):x(_x){ count++; } }; class B: public A{ public: B(int _x):A(_x){ } B(const B &a){ } ~B(){ puts("B::destructor"); } }; int A::count = 0; void func(B b) { } int main() { A a1(5),a2; cout << A::count << endl; B b1(4); cout << A::count << endl; func(b1); cout << A::count << endl; A * pa = new B(4); cout << A::count << endl; delete pa; cout << A::count << endl; return 0; } |
7:MyOstream_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 |
#include <iostream> #include <vector> #include <list> using namespace std; template <class T> class MyOstream_iterator{ public: int p; int& operator*(){ return p; } void operator++(int x){ cout << p << ','; } }; template <class T1, class T2> void MyCopy(T1 l, T1 r, T2 f) { while(l != r) { *f = *l; f++; l++; } } int main() { int n; int a[100]; while( cin >> n) { MyOstream_iterator<int> o; vector<int> v(n); for(int i = 0;i < n; ++i) cin >> a[i]; MyCopy(a,a+n,v.begin()); MyCopy(a,a+n,v.begin()); MyCopy(v.begin(),v.end(),o); cout << endl; list<int> ls(n); MyCopy(v.begin(),v.end(),ls.begin()); MyCopy(v.begin(),v.end(),ls.begin()); MyCopy(ls.begin(),ls.end(),o); cout << endl; } return 0; } |
8:奇怪的输出
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> #include <string> #include <cstdlib> #include <cstring> #include <cstdio> using namespace std; class A { public: int v; A(int n):v(n) { } operator string(){ string ans = "A:v="; ans += char(v + '0'); return ans; } friend ostream& operator <<(ostream& os, A a){ os << a.v; return os; } }; int main() { A a(5),b(6); cout << (string)a << endl; cout << b << endl; return 0; } |
Subscribe