電子商務(wù)網(wǎng)站建設(shè)前的分析網(wǎng)站排名優(yōu)化手機(jī)
題目來源:程序設(shè)計(jì)與算法(三)測驗(yàn)和作業(yè)題匯總
文章目錄
- 001:簡單的swap
- 002:難一點(diǎn)的swap
- 003:好怪異的返回值
- 004:神秘的數(shù)組初始化
- 005:編程填空:學(xué)生信息處理程序
- 006:奇怪的類復(fù)制
- 007:返回什么才好呢
- 008:超簡單的復(fù)數(shù)類
- 009:哪來的輸出
- 010:返回什么才好呢
- 011:Big & Base 封閉類問題
- 012:這個(gè)指針哪來的
- 013:魔獸世界之一:備戰(zhàn)
- 014:MyString
- 015:看上去好坑的運(yùn)算符重載
- 016:驚呆!Point竟然能這樣輸入輸出
- 017:二維數(shù)組類
- 018:別叫,這個(gè)大整數(shù)已經(jīng)很簡化了!
- 019:全面的MyString
- 020:繼承自string的MyString
- 021:魔獸世界之二:裝備
- 022:看上去像多態(tài)
- 023:Fun和Do
- 024:這是什么鬼delete
- 025:怎么又是Fun和Do
- 026:編程填空:統(tǒng)計(jì)動(dòng)物數(shù)量
001:簡單的swap
#include <iostream>
using namespace std;class A
{public:int x;int getX() { return x; }
};void swap(A &a, A &b)
{int tmp = a.x;a.x = b.x;b.x = tmp;
}int main()
{A a,b;a.x = 3;b.x = 5;swap(a,b);cout << a.getX() << "," << b.getX();return 0;
}
輸出:
5,3
002:難一點(diǎn)的swap
#include <iostream>
using namespace std;void swap(int *&a, int *&b)
{int * tmp = a;a = b;b = tmp;
}int main()
{int a = 3,b = 5;int * pa = & a;int * pb = & b;swap(pa,pb);cout << *pa << "," << * pb;return 0;
}
輸出:
5,3
003:好怪異的返回值
#include <iostream>
using namespace std;// 當(dāng)函數(shù)引用時(shí),函數(shù)可用作左值
// 函數(shù)的返回類型決定函數(shù)調(diào)用是否是左值,當(dāng)調(diào)用一個(gè)返回引用的函數(shù)得到左值,其他返回類型得到右值
int &getElement (int * a, int i)
{return a[i]; // 返回對(duì)a[i]的引用
}int main()
{int a[] = {1,2,3};getElement(a,1) = 10;cout << a[1] ;return 0;
}
輸出:
10
004:神秘的數(shù)組初始化
#include <iostream>
using namespace std;int main()
{int * a[] = {NULL, NULL, new int, new int[6]};*a[2] = 123;a[3][5] = 456;if(! a[0] ) {cout << * a[2] << "," << a[3][5];}return 0;
}
輸出:
123,456
005:編程填空:學(xué)生信息處理程序
描述:實(shí)現(xiàn)一個(gè)學(xué)生信息處理程序,計(jì)算一個(gè)學(xué)生的四年平均成績。
要求實(shí)現(xiàn)一個(gè)代表學(xué)生的類,并且類中所有成員變量都是【私有的】。
補(bǔ)充下列程序中的 Student 類以實(shí)現(xiàn)上述功能。
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;class Student {
// 在此處補(bǔ)充你的代碼
};int main() {Student student; // 定義類的對(duì)象student.input(); // 輸入數(shù)據(jù)student.calculate(); // 計(jì)算平均成績student.output(); // 輸出數(shù)據(jù)
}
輸入:輸入數(shù)據(jù)為一行,包括:姓名,年齡,學(xué)號(hào),第一學(xué)年平均成績,第二學(xué)年平均成績,第三學(xué)年平均成績,第四學(xué)年平均成績。其中姓名為由字母和空格組成的字符串(輸入保證姓名不超過20個(gè)字符,并且空格不會(huì)出現(xiàn)在字符串兩端),年齡、學(xué)號(hào)和學(xué)年平均成績均為非負(fù)整數(shù)。信息之間用逗號(hào)隔開。
Tom Hanks,18,7817,80,80,90,70
輸出:輸出一行數(shù)據(jù),包括:姓名,年齡,學(xué)號(hào),四年平均成績。信息之間用逗號(hào)隔開。
Tom Hanks,18,7817,80
題解:
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;class Student {private:char c;char name[20];int age;int num;int grade[5];double ave;public:void input(){cin.getline(name,20,',');cin>>age>>c>>num>>c>>grade[1]>>c>>grade[2]>>c>>grade[3]>>c>>grade[4];}void calculate(){ave=(double)(grade[1]+grade[2]+grade[3]+grade[4])/4;}void output(){cout<<name<<","<<age<<","<<num<<","<<ave;}};int main() {Student student; // 定義類的對(duì)象student.input(); // 輸入數(shù)據(jù)student.calculate(); // 計(jì)算平均成績student.output(); // 輸出數(shù)據(jù)
}
006:奇怪的類復(fù)制
只有在初始化對(duì)象的時(shí)候,才會(huì)調(diào)用構(gòu)造函數(shù)或復(fù)制構(gòu)造函數(shù)!
#include <iostream>
using namespace std;class Sample {
public:int v;
// 在此處補(bǔ)充你的代碼Sample (int x = 0): v(x) {} //類型隱式轉(zhuǎn)換構(gòu)造函數(shù)Sample (const Sample &o){v = o.v + 2; }
};void PrintAndDouble(Sample o) // 形參作為實(shí)參時(shí),會(huì)調(diào)用復(fù)制構(gòu)造函數(shù)
{cout << o.v;cout << endl;
}int main()
{Sample a(5);Sample b = a; // 調(diào)用復(fù)制構(gòu)造函數(shù),是初始化語句 PrintAndDouble(b); // 調(diào)用復(fù)制構(gòu)造函數(shù)Sample c = 20; // 20轉(zhuǎn)換為臨時(shí)對(duì)象,調(diào)用類型轉(zhuǎn)換構(gòu)造函數(shù),是初始化語句 PrintAndDouble(c); // 調(diào)用復(fù)制構(gòu)造函數(shù)Sample d;d = a; // 不會(huì)調(diào)用復(fù)制構(gòu)造函數(shù),因?yàn)閐已經(jīng)初始化定義過了,是賦值語句 cout << d.v;return 0;
}
輸出:
9
22
5
007:返回什么才好呢
#include <iostream>
using namespace std;class A {
public:int val;A(int v = 123): val(v) {} // 類型轉(zhuǎn)換構(gòu)造函數(shù) A &GetObj(){return *this;}
};int main()
{int m,n;A a;cout << a.val << endl;while(cin >> m >> n) {a.GetObj() = m; // m轉(zhuǎn)換為臨時(shí)對(duì)象 cout << a.val << endl;a.GetObj() = A(n); cout << a.val<< endl;}return 0;
}
輸入:多組數(shù)據(jù),每組一行,是整數(shù) m 和 n
2 3
4 5
輸出:先輸出一行:123,然后,對(duì)每組數(shù)據(jù),輸出兩行,第一行是m,第二行是n
123
2
3
4
5
008:超簡單的復(fù)數(shù)類
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex {
private:double r,i;
public:void Print() {cout << r << "+" << i << "i" << endl;}Complex(char *s=" "){r=s[0]-'0';i=s[2]-'0';}
};
int main() {Complex a;a = "3+4i"; a.Print();a = "5+6i"; a.Print();return 0;
}
輸出:
3+4i
5+6i
009:哪來的輸出
#include <iostream>
using namespace std;class A {public:int i;A(int x) { i = x; }
// 在此處補(bǔ)充你的代碼~A(){cout << i << endl;}
};int main()
{A a(1);A * pa = new A(2);delete pa;return 0;
}
輸出:
2
1
010:返回什么才好呢
(與007相同,略)
011:Big & Base 封閉類問題
#include <iostream>
#include <string>
using namespace std;class Base {
public:int k;Base(int n):k(n) { }
};class Big
{
public:int v;Base b;
// 在此處補(bǔ)充你的代碼Big(int n):v(n), b(v) { }
};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;}
}
輸入:多組數(shù)據(jù),每組一行,是一個(gè)整數(shù)
3
4
輸出:對(duì)每組數(shù)據(jù),輸出兩行,每行把輸入的整數(shù)打印兩遍
3,3
3,3
4,4
4,4
012:這個(gè)指針哪來的
#include <iostream>
using namespace std;struct A
{int v;A(int vv):v(vv) { }
// 在此處補(bǔ)充你的代碼const A *getPointer() const{ // 常量成員函數(shù)不能修改成員變量,返回值既可以是常量,也可以是變量 return this;}
};int main()
{const A a(10);const A * p = a.getPointer(); // 常量對(duì)象只能調(diào)用常量成員函數(shù) cout << p->v << endl;return 0;
}
輸出:
10
013:魔獸世界之一:備戰(zhàn)
見:【POJ C++題目】魔獸世界之一:備戰(zhàn)
014:MyString
#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);}elsep = NULL;}~MyString() { if(p) delete [] p; }// 在此處補(bǔ)充你的代碼// 深拷貝 void Copy (const char * s){if(s) {p = new char[strlen(s) + 1];strcpy(p, s);}elsep = NULL;}// 深拷貝MyString (const MyString &o){if(o.p) {p = new char[strlen(o.p) + 1];strcpy(p, o.p);}elsep = NULL;}// 深拷貝 MyString &operator = (const MyString &o){if (p == o.p)return *this;if (p)delete [] p;if(o.p) {p = new char[strlen(o.p) + 1];strcpy(p, o.p);}elsep = NULL;return *this;}// 重載輸出流 friend ostream & operator << (ostream &out, const MyString &o){out << o.p;return out;}
};int main()
{char w1[200], w2[100];while(cin >> w1 >> w2) {MyString s1(w1), s2 = s1; // 需要實(shí)現(xiàn)深拷貝 MyString s3(NULL);s3.Copy(w1);cout << s1 << "," << s2 << "," << s3 << endl;s2 = w2;s3 = s2;s1 = s3;cout << s1 << "," << s2 << "," << s3 << endl;}
}
輸入:多組數(shù)據(jù),每組一行,是兩個(gè)不帶空格的字符串
abc def
123 456
輸出:對(duì)每組數(shù)據(jù),先輸出一行,打印輸入中的第一個(gè)字符串三次,然后再輸出一行,打印輸入中的第二個(gè)字符串三次
abc,abc,abc
def,def,def
123,123,123
456,456,456
015:看上去好坑的運(yùn)算符重載
#include <iostream>
using namespace std;class MyInt
{ int nVal; public: MyInt(int n) { nVal = n ;}
// 在此處補(bǔ)充你的代碼 MyInt &operator - (int n){ // 運(yùn)算符重載為成員函數(shù)時(shí),參數(shù)個(gè)數(shù)要比運(yùn)算符目數(shù)少1 this->nVal -= n;return *this;}operator int (){ // 重載類型強(qiáng)制轉(zhuǎn)換運(yùn)算符 return this->nVal;}
}; 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;
}
輸入:多組數(shù)據(jù),每組一行,整數(shù)n
20
30
輸出:對(duì)每組數(shù)據(jù),輸出一行,包括兩個(gè)整數(shù), n-5和n-8
15,12
25,22
016:驚呆!Point竟然能這樣輸入輸出
#include <iostream>
using namespace std;class Point { private: int x; int y; public: Point() { };
// 在此處補(bǔ)充你的代碼friend istream &operator >> (istream &in, Point &p){in >> p.x >> p.y;return in;}friend ostream &operator << (ostream &out, const Point &p){out << p.x << "," << p.y;return out;}
}; int main()
{ Point p;while(cin >> p) {cout << p << endl;}return 0;
}
輸入:多組數(shù)據(jù),每組兩個(gè)整數(shù)
2 3
4 5
輸出:對(duì)每組數(shù)據(jù),輸出一行,就是輸入的兩個(gè)整數(shù)
2,3
4,5
017:二維數(shù)組類
#include <iostream>
#include <cstring>
using namespace std;class Array2 {
// 在此處補(bǔ)充你的代碼private:int row;int col;int data[10][10];public:Array2 (int r = 0, int c = 0): row(r), col(c) {}// 應(yīng)付 a[i][j] int *operator [] (const int i){return this->data[i];}// 應(yīng)付 a(i, j) int operator () (int i, int j){return this->data[i][j];}// 應(yīng)付 b = a Array2 &operator = (Array2 &x){this->row = x.row;this->col = x.col;for(int i = 0; i < x.row; i++)for(int j = 0; j < x.col; j++)this->data[i][j] = x.data[i][j];return *this;}
};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;
}
輸出:
0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,
018:別叫,這個(gè)大整數(shù)已經(jīng)很簡化了!
(提示:使用高精度算法)
019:全面的MyString
(略)
020:繼承自string的MyString
(略)
021:魔獸世界之二:裝備
見:【POJ C++題目】魔獸世界之二:裝備
見:【POJ C++題目】魔獸世界之二:裝備(簡化)
022:看上去像多態(tài)
#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;}
};// 在此處補(bǔ)充你的代碼
class D: public B{ private: int nDVal; public: void Print() { B::Print(); cout << "nDVal="<< nDVal << endl; } void Fun() {cout << "D::Fun" << endl; } D (int n): B(n * 3){ nDVal = n;}
};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;
}
輸出:
D::Fun
B::Fun
D::Fun
nBVal=2
nBVal=24
nDVal=8
B::Fun
nBVal=12
023:Fun和Do
#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(B &p) { p.Fun(); // B類沒有Fun,先調(diào)用基類(A類)的同名成員函數(shù) p.Do(); // 多態(tài),調(diào)用哪個(gè)Do,取決于p引用了哪個(gè)類的對(duì)象
} int main() { C c; Call(c); return 0;
}
輸出:
A::Fun
C::Do
024:這是什么鬼delete
#include <iostream>
using namespace std;class A
{ public:A() { }
// 在此處補(bǔ)充你的代碼virtual ~A() { cout << "destructor A" << endl; }
}; class B:public A { public: ~B() { cout << "destructor B" << endl; }
}; int main()
{ A * pa; pa = new B; // 先調(diào)用構(gòu)造函數(shù)A,再調(diào)用構(gòu)造函數(shù)B delete pa; // 先調(diào)用虛析構(gòu)函數(shù)B,再調(diào)用虛析構(gòu)函數(shù)A(若沒有加virtual,則只會(huì)調(diào)用析構(gòu)函數(shù)A) return 0;
}
輸出:
destructor B
destructor A
025:怎么又是Fun和Do
#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指向A時(shí),A::Fun被調(diào)用;// p指向C時(shí),由于是基類(A類)指針,Fun不是虛函數(shù),所以A::Fun被調(diào)用 p->Do(); // p指向A時(shí),A::Do被調(diào)用; // p指向C時(shí),由于是基類(A類)指針,且Do是虛函數(shù),多態(tài),所以C::Do被調(diào)用
}int main() {Call(new A());Call(new C());return 0;
}
輸出:
A::Fun
A::Do
A::Fun
C::Do
026:編程填空:統(tǒng)計(jì)動(dòng)物數(shù)量
描述:代碼填空,使得程序能夠自動(dòng)統(tǒng)計(jì)當(dāng)前各種動(dòng)物的數(shù)量
#include <iostream>
using namespace std;
// 在此處補(bǔ)充你的代碼
void print() {cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}int main() {print();Dog d1, d2;Cat c1;print();Dog* d3 = new Dog();Animal* c2 = new Cat;Cat* c3 = new Cat;print();delete c3;delete c2;delete d3;print();
}
輸出:
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
題解:
#include <iostream>
using namespace std;
class Animal{public:static int number;Animal(){number++;}virtual ~Animal(){number--;}
};class Dog:public Animal
{public:static int number;Dog(){number++;}~Dog(){number--;}
};class Cat:public Animal
{public:static int number;Cat(){number++;}~Cat(){number--;}
};int Dog::number = 0;
int Cat::number = 0;
int Animal::number = 0;
void print() {cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}int main() {print();Dog d1, d2;Cat c1;print();Dog* d3 = new Dog();Animal* c2 = new Cat;Cat* c3 = new Cat;print();delete c3;delete c2;delete d3;print();
}