好运来

weiyo / 2023-06-18 / 原文

第一

A 第一个C++程序

程序填空。输出"Hello C++" 。

#include<iostream>
using namespace std;

int main()
{
printf("Hello C++");

return 0;

}


B 使用名字空间

使用名字空间。计算一个整数的平方值。

#include<iostream>
#include<iomanip>
using namespace std;
//定义名字空间 x,y,定义变量a和函数power(..)

namespace x
{
int a;
}
namespace y
{

int power(int x)
{
return x*x;
}
}

int main()
{

cin >> x::a;

 

cout << y::power(x::a) << endl;

 

return

0;

}

 

 

 

 

C 格式化输出1

输入一个整数,输出其8进制值、10进制值、16进制值。
8进制值加前缀0
16进制值加前缀0x

 

#include<iostream>

 

#include<iomanip>

using

namespace

std;

 

 

int

main()

{

 

int

a;

 

cin>>a;

 

cout<< showbase;

 

cout<<oct<<a<<

" "

;

 

cout<<dec<<a<<

" "

;

 

cout<<hex<<a<<endl;

}

 

 

 

 

D 格式化输出2

输入5个小数,分别用左对齐,内部对齐,右对齐方式输出,宽度为10,保留2位小数,用"|"隔开。

 

#include<iostream>

 

#include<iomanip>

using

namespace

std;

 

 

int

main()

{

 

double

a;

 

cin >> a;

 

cout << fixed << setprecision(2);

 

cout

 

<< setw(10) << left << a <<

"|"

 

<< setw(10) << internal << a <<

"|"

 

<< setw(10) << right << a <<endl;

 

cin >> a;

 

cout << fixed << setprecision(2);

 

cout

 

<< setw(10) << left << a <<

"|"

 

<< setw(10) << internal << a <<

"|"

 

<< setw(10) << right << a <<endl;

 

cin >> a;

 

cout << fixed << setprecision(2);

 

cout

 

<< setw(10) << left << a <<

"|"

 

<< setw(10) << internal << a <<

"|"

 

<< setw(10) << right << a <<endl;

 

cin >> a;

 

cout << fixed << setprecision(2);

 

cout

 

<< setw(10) << left << a <<

"|"

 

<< setw(10) << internal << a <<

"|"

 

<< setw(10) << right << a <<endl;

 

cin >> a;

 

cout << fixed << setprecision(2);

 

cout

 

<< setw(10) << left << a <<

"|"

 

<< setw(10) << internal << a <<

"|"

 

<< setw(10) << right << a <<endl;

 

 

 

return

0;

 

}

 

 

E 默认参数值

程序填空,只定义一个函数,使得输出为:
1116
116
16
6

#include<iostream>

#include<iomanip>

using

namespace

std;

int

add(

int

x=1001,

int

y=102,

int

z=13)

{

 

return

x+y+z;

}

int

main()

{

 

cout << add() << endl;

 

cout << add(1) << endl;

 

cout << add(1, 2) << endl;

 

cout << add(1, 2, 3) << endl;

}

 

 

F 函数重载1

程序填空,使用函数重载,使得输出为
1
7
55.55
99.994

#include<iostream>

#include<iomanip>

using

namespace

std;

bool

add(

bool

a,

bool

b)

{

 

return

a+b;

}

double

add(

double

a,

double

b)

{

 

return

a+b;

}

int

add(

int

a,

int

b)

{

 

return

a+b;

}

double

add(

double

a,

double

b,

double

c)

{

 

return

a+b+c;

}

int

main()

{

 

cout << add(

true

,

true

) << endl;

 

cout << add(3, 4) << endl;

 

cout << add(22.22, 33.33) << endl;

 

cout << add(22.22, 33.33, 44.444) << endl;

 

return

0;

}

 

 

G 函数重载2

补充代码,使得输出为:
7
-1
12

#include<iostream>

#include<iomanip>

using

namespace

std;

void

swap1(

int

x,

int

y)

{

 

int

t;

 

t=x;

 

x=y;

 

y=t;

}

void

swap2(

int

&x,

int

&y)

{

 

int

t;

 

t=x;

 

x=y;

 

y=t;

}

int

main()

{

 

int

a, b;

 

cin >> a >> b;

 

swap1(a, b);

 

cout << a <<

","

<< b << endl;

 

swap2(a, b);

 

cout << a <<

","

<< b << endl;

 

return

0;

}

 

 

H 引用的使用1

按输入输出要求补充程序。

 

#include<iostream>

#include<iomanip>

using

namespace

std;

void

swap1(

int

x,

int

y)

{

 

int

t;

 

t=x;

 

x=y;

 

y=t;

}

void

swap2(

int

&x,

int

&y)

{

 

int

t;

 

t=x;

 

x=y;

 

y=t;

}

int

main()

{

 

int

a, b;

 

cin >> a >> b;

 

swap1(a, b);

 

cout << a <<

","

<< b << endl;

 

swap2(a, b);

 

cout << a <<

","

<< b << endl;

 

return

0;

}

 

 

 

I 引用的使用2(较难)

程序填空,按输入输出要求,实现数值交换。

#include<iostream>

#include<iomanip>

using

namespace

std;

 

 

void

swap(

int

*&p,

int

*&q

)

{

 

int

* t;

 

t = p;

 

p = q;

 

q = t;

}

int

main()

{

 

int

a;

 

int

b;

 

int

* p1;

 

int

* p2;

 

 

 

p1 = &a;

 

p2 = &b;

 

 

 

cin >> a >> b;

 

swap(p1, p2);

 

cout << a <<

","

<< b << endl;

 

cout << *p1 <<

","

<< *p2 << endl;

 

return

0;

}

 

 

J string的使用1

使用string类,对英文人名的字符串处理。
若一个人的姓名有 4 个单词,输入 4,以及这 4 个单词。
输出3行:
第一行:4 个单词,全部字母大写,单词用空格隔开。
第二行:字符总数。
第三行:姓名缩写,取每个单词的首字母大写。

#include<iostream>

#include<string>

using

namespace

std;

int

main()

{

 

int

n;

 

string s[1000];

 

cin >> n;

 

for

(

int

i=0;i<n;i++)

 

{

 

cin >> s[i];

 

}

 

int

len = 0;

 

string sn =

""

;

 

for

(

int

i=0;i<n;i++)

 

{

 

len += s[i].size();

 

for

(

int

j=0;j<s[i].size();j++)

 

{

 

s[i][j] =

toupper

(s[i][j]);

 

 

 

}

 

sn += s[i].substr(0,1);

 

cout << s[i] << (i<n-1?

" "

:

""

);

 

}

 

cout <<endl

 

<< len << endl

 

<< sn <<endl;

 

 

 

 

}

 

 

K string的使用2

使用 string,进行姓氏字符串排序,升序输出。

#include<iostream>

#include<string>

using

namespace

std;

 

 

int

main()

{

 

int

n;

 

string a[100],t;

 

 

 

cin >> n;

 

for

(

int

i=0;i<n;i++)

 

{

 

cin >> a[i];

 

}

 

for

(

int

i=0;i<n-1;i++)

 

{

 

int

pos = i;

 

for

(

int

j=i+1;j<n;j++)

 

{

 

if

(a[j]<a[pos]) pos=j;

 

}

 

 

 

t = a[i];

 

a[i] = a[pos];

 

a[pos] = t;

 

 

 

}

 

for

(

int

i=0;i<n;i++)

 

{

 

cout << a[i] << endl;

 

 

 

}

 

 

 

 

}

 

 

L 动态内存管理

补充代码,实现如下功能:
输入 n,然后是 n 个浮点数。
逆序输出这 n 个数。

#include<iostream>

using

namespace

std;

 

 

int

main()

{

 

int

n;

 

double

* p;

 

 

 

cin >> n;

p =

new

double

[n];

 

for

(

double

* t = p; t<p+n; t++)

 

{

 

cin >> *t;

 

}

 

 

 

for

(

double

* t = p+n-1; t>=p; t--)

 

{

 

cout << *t << endl;

 

}

 

delete

[] p;

 

 

 

return

0;

}

 

 

M 面向过程编程 - 计算面积和周长

补充程序,定义若干个函数。
输入三角形的 3 边长和矩形的 2 边长,计算并输出他们的面积和周长。

#include <iostream>

#include <iomanip>

#include <cmath>

using

namespace

std;

 

 

struct

TRIANGLE

{

 

double

a;

 

double

b;

 

double

c;

};

struct

RECTANGLE

{

 

double

w;

 

double

h;

};

double

getTriangleArea(TRIANGLE *t)

{

 

double

p=(t->a+t->b+t->c)/2;

 

return

sqrt

((p-t->a)*(p-t->b)*(p-t->c)*p);

}

double

getTrianglePerimeter(TRIANGLE *p)

{

 

return

p->a+p->b+p->c;

}

double

getRectangleArea(RECTANGLE *p)

{

 

return

((p->w)*(p->h));

}

double

getRectanglePerimeter(RECTANGLE *p)

{

 

return

(2*(p->w)+(p->h)*2);

}

int

main()

{

 

TRIANGLE t1;

 

RECTANGLE r1;

 

 

 

cin >> t1.a >> t1.b >> t1.c;

 

cin >> r1.w >> r1.h;

 

 

 

cout << fixed << setprecision(2);

 

cout << getTriangleArea(&t1) << endl;

 

cout << getTrianglePerimeter(&t1) << endl;

 

cout << getRectangleArea(&r1) << endl;

 

cout << getRectanglePerimeter(&r1) << endl;

}

 第二

A1.2

面向过程编程 - 计算面积和周长

补充程序,定义若干个函数。
输入三角形的 3 边长和矩形的 2 边长,计算并输出他们的面积和周长。

#include <iostream>

 

#include <iomanip>

#include <cmath>

using

namespace

std;

 

 

struct

TRIANGLE

{

 

double

a;

 

double

b;

 

double

c;

};

struct

RECTANGLE

{

 

double

w;

 

double

h;

};

double

getTrianglePerimeter(TRIANGLE * p)

{

 

return

p->a+p->b+p->c;

}

double

getTriangleArea(TRIANGLE * t)

{

 

double

p=(t->a+t->b+t->c)/2;

 

return

sqrt

((p-t->a)*(p-t->b)*(p-t->c)*p);

}

double

getRectangleArea(RECTANGLE *p)

{

 

return

(p->w)*(p->h);

}

double

getRectanglePerimeter(RECTANGLE * p)

{

 

return

2*((p->w)+(p->h));

}

int

main()

{

 

TRIANGLE t1;

 

RECTANGLE r1;

 

 

 

cin >> t1.a >> t1.b >> t1.c;

 

cin >> r1.w >> r1.h;

 

 

 

cout << fixed << setprecision(2);

 

cout << getTriangleArea(&t1) << endl;

 

cout << getTrianglePerimeter(&t1) << endl;

 

cout << getRectangleArea(&r1) << endl;

 

cout << getRectanglePerimeter(&r1) << endl;

}

 

 

 

 

B2 面向对象编程 - 计算面积和周长

补充程序,定义一个类。
输入三角形的3边长,计算并输出他的面积和周长。

 

 

#include <iostream>

 

#include <iomanip>

#include <cmath>

using

namespace

std;

class

Triangle

{

public

:

 

double

a;

 

double

b;

 

double

c;

 

double

getArea()

 

{

 

double

l=(a+b+c)/2;

 

return

sqrt

((l-a)*(l-b)*(l-c)*l);

 

}

 

double

getPerimeter()

 

{

 

return

a+b+c;

 

}

};

int

main()

{

 

Triangle t1;

 

cin >> t1.a >> t1.b >> t1.c;

 

 

 

cout << fixed << setprecision(2);

 

cout << t1.getArea() << endl;

 

cout << t1.getPerimeter() << endl;

}

 

 

 

 

C3 自定义默认构造函数

补充程序,自定义默认构造函数,要求:
三角形的三边长默认为:3、4、5。

 

 

 


#include <iostream>

 

#include <iomanip>

#include <cmath>

using

namespace

std;

 

 

class

Triangle

{

public

:

 

double

a;

 

double

b;

 

double

c;

public

:

 

double

getPerimeter()

 

{

 

return

a + b + c;

 

}

 

double

getArea()

 

{

 

double

l = (a + b + c) / 2;

 

return

sqrt

((l - a) * (l - b) * (l - c) * l);

 

}

Triangle()

{

 

 

 

a=3;b=4;c=5;

 

 

}

};

 

 

int

main()

{

 

Triangle t1;

 

 

 

cout << fixed << setprecision(2);

 

cout << t1.getArea() << endl;

 

cout << t1.getPerimeter() << endl;

}

 

 

 

 

D4 带参数的构造函数

补充构造函数。
要求:
三角形的三边长可以在定义对象的同时指定。

 

 

#include <iostream>

 

#include <iomanip>

#include <cmath>

using

namespace

std;

 

 

class

Triangle

{

public

:

 

double

a;

 

double

b;

 

double

c;

public

:

 

double

getPerimeter()

 

{

 

return

a + b + c;

 

}

 

double

getArea()

 

{

 

double

l = (a + b + c) / 2;

 

return

sqrt

((l - a) * (l - b) * (l - c) * l);

 

}

Triangle(

double

x,

double

y,

double

z)

 

:a(x),b(y),c(z)

 

{

 

 

 

 

 

}

};

 

 

int

main()

{

 

double

x, y, z;

 

cin >> x >> y >> z;

 

Triangle t1(x, y, z);

 

 

 

cout << fixed << setprecision(2);

 

cout << t1.getArea() << endl;

 

cout << t1.getPerimeter() << endl;

}

 

 

 

 

E5 重载构造函数

补充构造函数。
要求:
一个参数时,构造等边三角形。
二个参数时,构造等腰三角形。第一个参数为底长,第二个参数为腰长。
三个参数时,构造普通三角形。

 

 

 


#include <iostream>

 

#include <iomanip>

#include <cmath>

using

namespace

std;

 

 

class

Triangle

{

public

:

 

double

a;

 

double

b;

 

double

c;

public

:

 

double

getPerimeter()

 

{

 

return

a + b + c;

 

}

 

double

getArea()

 

{

 

double

l = (a + b + c) / 2;

 

return

sqrt

((l - a) * (l - b) * (l - c) * l);

 

}

Triangle(

double

x)

 

:a(x),b(x),c(x)

 

{

 

 

 

}

 

 

Triangle(

double

x,

double

y)

 

:a(x),b(y),c(y)

 

{

 

 

 

}

Triangle(

double

x,

double

y,

double

z)

 

:a(x),b(y),c(z)

 

{

 

 

 

}

};

 

 

int

main()

{

 

double

x, y, z;

 

cin >> x >> y >> z;

 

Triangle t1(x);

 

Triangle t2(x, y);

 

Triangle t3(x, y, z);

 

 

 

cout << fixed << setprecision(2);

 

cout << t1.getArea() <<

" "

<< t1.getPerimeter() << endl;

 

cout << t2.getArea() <<

" "

<< t2.getPerimeter() << endl;

 

cout << t3.getArea() <<

" "

<< t3.getPerimeter() << endl;

}

 

 

 

 

F6 复合类的构造函数 - 复合类(对象)中子对象的初始化

补充程序,实现复合类(对象)中子对象的初始化。

 

#include <iostream>

 

#include <iomanip>

#include<string>

using

namespace

std;

 

 

class

Date

{

public

:

 

int

y, m, d;

 

Date(

int

y,

int

m,

int

d) :y(y), m(m), d(d) {}

 

void

Show()

 

{

 

cout << setfill(

'0'

);

 

cout << setw(4) << y <<

"-"

 

<< setw(2) << m <<

"-"

 

<< setw(2) << d << endl;

 

}

};

class

Student

{

public

:

 

string name;

 

Date birthday;

 

void

Show()

 

{

 

cout << name <<

"'s birtday is "

;

 

birthday.Show();

 

}

Student(string a,

int

b,

int

c,

int

d)

 

:name(a),birthday(b,c,d)

 

{

 

 

 

 

 

}

};

 

 

int

main()

{

 

string n;

 

int

y, m, d;

 

cin >> n >> y >> m >> d;

 

 

 

Student s1(n, y, m, d);

 

s1.Show();

 

return

0;

}

 

 

 

 

G7 析构函数

补充程序,创建对象,并销毁对象,输出对象的创建和销毁过程。
每个对象依据创建过程编号,从编号1开始。

 

 

#include <iostream>

 

#include <iomanip>

using

namespace

std;

 

 

int

ID_CNT = 0;

class

A

{

public

:

 

int

id;

 

 

A()

{

 

id= ++ID_CNT;

 

cout <<

"Create Object A "

<<id<< endl;

}

 

 

~A()

{

 

cout <<

"Destory Object A "

<< id << endl;

}

 

 

 

 

};

 

 

int

main()

{

 

int

n;

 

cin>>n;

 

 

 

A a1;

 

 

 

A* p =

new

A[n];

 

delete

[] p;

 

 

 

A* q =

new

A();

 

delete

q;

}

 

 

 

 

H8 拷贝构造函数

请定义一个有趣的拷贝构造函数。
当我们用一个三角形对象 t1 去初始化另一个三角形对象 t2 时,他的边长会自动放大一倍。

 

#include <iostream>

 

#include <iomanip>

using

namespace

std;

 

 

class

Triangle

{

public

:

 

double

a, b, c;

public

:

 

Triangle() :a(3), b(4), c(5) { }

 

Triangle(

double

x,

double

y,

double

z) :a(x), b(y), c(z) { }

 

void

Show()

 

{

 

cout <<

"Tri("

<< a <<

","

<< b <<

","

<< c <<

")"

<< endl;

 

}

Triangle(

const

Triangle& p)

 

{

 

a=p.a*2;

 

b=p.b*2;

 

c=p.c*2;

 

}

};

 

 

int

main()

{

 

double

x, y, z;

 

cin >> x >> y >> z;

 

 

 

Triangle t1(x, y, z);

 

Triangle t2(t1);

 

Triangle t3 = Triangle(t2);

 

 

 

t1.Show();

 

t2.Show();

 

t3.Show();

}

 

 

 

 

I9 拷贝构造函数2

补充程序,使得符合输出要求。

 

 


#define _CRT_SECURE_NO_WARNINGS

 

#include <iostream>

#include <iomanip>

#include <cstring>

using

namespace

std;

 

 

class

Student

{

public

:

 

char

* name;

 

int

age;

 

Student(

const

char

* n =

"Liu You Ji"

,

int

a = 18) :age(a) {

 

name =

new

char

[50];

 

strcpy

(name, n);

 

}

 

~Student() {

 

//delete [] name;

 

}

 

void

Show() {

 

cout << name <<

", "

<< age <<

" years old.\n"

;

 

}

Student(

const

Student& p)

{

 

int

len =

strlen

(p.name) * 1;

 

name =

new

char

[len];

 

strcpy

(name,p.name);

 

age = p.age + 1;

}

};

 

 

Student f(Student s) {

 

return

s;

}

 

 

int

main() {

 

Student s1;

 

 

 

Student s2(s1);

 

strcpy

(s2.name,

"Tom"

);

 

 

 

Student s3 = s2;

 

strcpy

(s3.name,

"Jerry"

);

 

 

 

Student s4 = f(s3);

 

strcpy

(s4.name,

"Mike"

);

 

 

 

s1.Show();

 

s2.Show();

 

s3.Show();

 

s4.Show();

}

 

 

 

 

J10 常成员变量、常成员函数和常对象

补充程序,使得能够按格式要求输出学生的信息。

 

#include <iostream>

 

#include <iomanip>

#include <string>

using

namespace

std;

 

 

class

Student

{

public

:

 

const

string name;

 

int

age;

 

Student(string n,

int

a) :name(n), age(a) { }

void

Show()

 

{

 

cout <<

"StudInfo: "

<< name <<

" "

<< age

 

<<

", by void Show()."

<< endl;

 

}

 

void

Show()

const

 

{

 

cout <<

"StudInfo: "

<< name <<

" "

<< age

 

<<

", by void Show()"

<<

" const."

<< endl;

 

}

};

int

main()

{

 

string n;

 

int

a;

 

cin>>n>>a;

 

 

 

Student s1(n, a);

 

const

Student& s2 = s1;

 

 

 

s1.Show();

 

 

 

cout <<

"--"

<< endl;

 

s2.Show();

 

 

 

cout <<

"--"

<< endl;

 

static_cast

<

const

Student>(s1).Show();

}

 

 

 

 

K11

静态成员变量与静态成员函数

统计曾经创建的对象的个数。

#include <iostream>

 

#include <iomanip>

using

namespace

std;

class

A

{

public

:

 

static

int

cnt;

 

static

int

GetCreatedCnt()

 

{

 

return

cnt;

 

}

 

A()

 

{

 

cnt++;

 

}

};

int

A::cnt = 0;

int

main()

{

 

int

n;

 

cin >> n;

 

 

 

cout << A::cnt << endl;

 

 

 

A a1;

 

cout << A::cnt << endl;

 

cout << A::GetCreatedCnt() << endl;

 

 

 

A list[20];

 

cout << A::cnt << endl;

 

cout << A::GetCreatedCnt() << endl;

 

 

 

A *p =

new

A[n];

 

cout << A::cnt << endl;

 

cout << A::GetCreatedCnt() << endl;

 

 

 

delete

[] p;

 

return

0;

}

 

 

 

 

L12 友元函数

平面几何。
计算两点间的距离。

 

 

 


#include <iostream>

 

#include <iomanip>

#include <cmath>

using

namespace

std;

 

 

class

Point

{

private

:

 

int

x, y;

public

:

 

Point(

int

a,

int

b) :x(a), y(b)

 

{

 

}

 

void

Show()

 

{

 

cout <<

"P("

<< x <<

","

<< y <<

")"

;

 

}

friend

double

Distance(Point& p1, Point& p2);

};

 

 

double

Distance(Point& p1, Point& p2)

{

 

return

sqrt

((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));

}

int

main()

{

 

int

a, b, c, d;

 

cin >> a >> b >> c >> d;

 

Point p1(a, b), p2(c, d);

 

 

 

p1.Show();

 

cout <<

" - "

;

 

p2.Show();

 

cout <<

" = "

;

 

cout << fixed << setprecision(4) << Distance(p1, p2) << endl;

 

 

 

return

0;

}

 

 

 

 

M13 友元类

平面几何。
平面上三角形的平移。
Point 表示平面上的点。
Triangle 表示平面上的三角形。由三个点 Point 确定。
类的部分代码如下:

 

#include <iostream>

 

#include <iomanip>

#include <cmath>

using

namespace

std;

class

Point

{

private

:

 

int

x, y;

public

:

 

Point(

int

a,

int

b) :x(a), y(b)

 

{

 

}

 

void

Show()

 

{

 

cout <<

"P("

<< x <<

","

<< y <<

")"

;

 

}

 

friend

class

Triangle;

};

 

 

class

Triangle {

public

:

 

Point A, B, C;

 

Triangle(

int

a,

int

b,

int

c,

int

d,

int

e,

int

f) :A(a, b), B(c, d), C(e, f)

 

{

 

}

 

void

Show() {

 

cout <<

"Triangle["

;

 

A.Show(); cout <<

","

;

 

B.Show(); cout <<

","

;

 

C.Show(); cout <<

"]"

<< endl;

 

}

 

void

Offset(

int

dx,

int

dy) {

 

A.x += dx;

 

A.y += dy;

 

B.x += dx;

 

B.y += dy;

 

C.x += dx;

 

C.y += dy;

 

}

};

int

main()

{

 

int

a, b, c, d, e, f;

 

int

dx, dy;

 

cin >> a >> b >> c >> d >> e >> f;

 

cin >> dx >> dy;

 

 

 

Triangle t1(a, b, c, d, e, f);

 

t1.Show();

 

t1.Offset(dx, dy);

 

t1.Show();

 

 

 

return

0;

}

 

第三

A1.3 复数的加减,全局函数实现
补充代码,实现复数的加减运算。

#include <iostream>

#include <string>
#include <iomanip>
using
namespace
std;

class
Complex
{
public
:

double
r;
// real

double
i;
// image
public
:

Complex() :r(0), i(0) {}

Complex(
double
a,
double
b) :r(a), i(b) {}

void
Show()
const

{

cout <<
"("
<< noshowpos << r << showpos << i << noshowpos <<
"i)"
<< endl;

}
};
Complex operator +(Complex c1, Complex c2)
{

Complex t;

t.r = c1.r + c2.r;

t.i = c1.i + c2.i;

return
t;
}
Complex operator -(Complex c1, Complex c2)
{

Complex t;

t.r = c1.r - c2.r;

t.i = c1.i - c2.i;

return
t;
}
int
main()
{

double
a, b, c, d;

cin >> a >> b >> c >> d;


Complex c1(a, b), c2(c, d);

Complex c3, c4;


c3 = c1 + c2;

c4 = c1 - c2;


c3.Show();

c4.Show();

return
0;
}

 


B1.3 复数的加减,成员函数实现
补充代码,实现复数的加减运算。

#include <iostream>

#include <string>
#include <iomanip>
using
namespace
std;

class
Complex
{
public
:

double
r;
// real

double
i;
// image
public
:

Complex() :r(0), i(0) {}

Complex(
double
a,
double
b) :r(a), i(b) {}

void
Show()
const

{

cout <<
"("
<< noshowpos << r << showpos << i << noshowpos <<
"i)"
<< endl;

}
Complex operator+(Complex c2)

{

Complex t;

t.r =
this
->r + c2.r;

t.i =
this
->i + c2.i;

return
t;

}

Complex operator-(Complex c2)

{

Complex t;

t.r =
this
->r - c2.r;

t.i =
this
->i - c2.i;

return
t;

}
};

int
main()
{

double
a, b, c, d;

cin >> a >> b >> c >> d;


Complex c1(a, b), c2(c, d);

Complex c3, c4;


c3 = c1 + c2;

c4 = c1 - c2;


c3.Show();

c4.Show();

return
0;
}

 


C 复数的自增,前缀和后缀实现
补充程序,实现复数的自增,前缀和后缀实现。

#include <iostream>

#include <string>
#include <iomanip>
using
namespace
std;

class
Complex
{
public
:

double
r;
// real

double
i;
// image
public
:

Complex() :r(0), i(0) {}

Complex(
double
a,
double
b) :r(a), i(b) {}

void
Show()
const

{

cout <<
"("
<< noshowpos << r << showpos << i << noshowpos <<
"i)"
<< endl;

}
Complex operator ++(
int
)

{

Complex t;

t.r =
this
->r++;

t.i =
this
->i++;

return
t;

}

Complex operator ++()

{

Complex t;

t.r = ++
this
->r;

t.i = ++
this
->i;

return
t;

}
};

int
main()
{

double
a, b, c, d;

cin >> a >> b >> c >> d;


Complex c1(a, b), c2(c, d);

Complex c3, c4;


c3 = ++c1;

c4 = c2++;


c1.Show();

c2.Show();

c3.Show();

c4.Show();


return
0;
}

 


D 提取与插入运算的重载(输出输出)
补充代码,实现重载提取与插入运算(输出输出)。

#include <iostream>

#include <string>
#include <iomanip>
using
namespace
std;

class
Complex
{
public
:

double
r;
// real

double
i;
// image
public
:

Complex() :r(0), i(0) {}

Complex(
double
a,
double
b) :r(a), i(b) {}

void
Show()
const

{

cout <<
"("
<< noshowpos << r << showpos << i << noshowpos <<
"i)"
<< endl;

}
};
istream& operator >> (istream& is, Complex& c1)
{

is >> c1.r >> c1.i;

return
is;
}
ostream& operator << (ostream& os,
const
Complex& c1)
{

if
(c1.i < 0)

os <<
"("
<< c1.r << c1.i <<
"i)"
;

else

os <<
"("
<< c1.r <<
"+"
<< c1.i <<
"i)"
;

return
os;
}
int
main()
{

Complex c1, c2;


cin >> c1 >> c2;

cout << c1 << endl << c2 << endl;


return
0;
}

 


E 关系运算的重载
补充代码,重载关系运算。
注意:
本题中,double a,b 相等的标准为 |a-b| < 1e-6 ;
== : 复数的实部和虚部同时相等;
!= : 复数的实部和虚部至少有一个不相等。

#include <iostream>

#include <string>
#include <iomanip>
#include <cmath>
using
namespace
std;

class
Complex
{
public
:

double
r;
// real

double
i;
// image
public
:

Complex() :r(0), i(0) {}

Complex(
double
a,
double
b) :r(a), i(b) {}

void
Show()
const

{

cout <<
"("
<< noshowpos << r << showpos << i << noshowpos <<
"i)"
<< endl;

}
bool
operator ==(Complex &c2)

{

return
(
fabs
(
this
->r - c2.r) < 1e-6 &&
fabs
(
this
->i - c2.i) < 1e-6);

}

bool
operator !=(Complex &c2)

{

return
(!(
fabs
(
this
->r - c2.r) < 1e-6) || !(
fabs
(
this
->i - c2.i) < 1e-6));

}
};

int
main()
{

double
a, b, c, d;

cin >> a >> b >> c >> d;


Complex c1(a, b), c2(c, d);

cout << boolalpha;

cout <<
"(c1 == c2) = "
<< (c1 == c2) << endl;

cout <<
"(c1 != c2) = "
<< (c1 != c2) << endl;

return
0;
}

 


F 奇怪的拷贝和赋值运算
奇怪的拷贝和赋值运算。(本题的设计是错误的设计,仅用于演示错误的原因,请勿用于实际项目)
实现拷贝构造函数和赋值运算。
* 拷贝构造:新复数放大2倍。
* 赋值运算:操作数1放大3倍。 (返回类型是 Complex 还是 Complex & ,正确的实现是返回 Complex &)
使得符合样例输出的格式和数值要求。

#include <iostream>

#include <string>
#include <iomanip>
using
namespace
std;

class
Complex
{
public
:

double
r;
// real

double
i;
// image
public
:

Complex() :r(0), i(0) {}

Complex(
double
a,
double
b) :r(a), i(b) {}

void
Show()
const

{

cout <<
"("
<< noshowpos << r << showpos << i << noshowpos <<
"i)"
<< endl;

}
Complex (
const
Complex &c)

{

r = c.r * 2;

i = c.i * 2;

}

Complex operator =(
const
Complex &c)

{

r = c.r * 3;

i = c.i * 3;

return
*
this
;

}
};

int
main()
{

double
a, b;

cin >> a >> b;


Complex c1(a, b);

Complex c3 = c1;

Complex c4, c5;

c5 = c4 = c1;

const
Complex& c6 = c4 = c1;


c1.Show();

c3.Show();

c4.Show();

c5.Show();

c6.Show();


return
0;
}

 


G 深的拷贝和赋值运算
数组也可以赋值啦!
补充代码。自定义类Arr,处理int数组。
支持拷贝、赋值。


#include <iostream>

#include <iomanip>
using
namespace
std;

class
Arr {
public
:

int
cnt;

int
* pData;
public
:

Arr(
int
cnt = 5) :cnt(cnt)

{

pData =
new
int
[cnt];

}

~Arr()

{

if
(pData) {

delete
[] pData;

pData = 0; cnt = 0;

}

}

void
Show()
const

{

for
(
int
i = 0; i < cnt; i++)

cout << pData[i] << ((i == cnt - 1) ?
"\n"
:
" "
);

}

Arr(
const
Arr&a)

{

cnt=a.cnt;

pData=
new
int
[cnt];

for
(
int
i=0;i<cnt;i++)

pData[i]=a.pData[i];

}

Arr operator=(
const
Arr& a)

{

cnt=a.cnt;

pData=
new
int
[cnt];

for
(
int
i=0;i<cnt;i++)

pData[i]=a.pData[i];

return
*
this
;

}
};
int
main()
{

Arr a1(5);


for
(
int
i = 0; i < a1.cnt; i++)

cin >> a1.pData[i];


Arr a2(a1);

Arr a3;

a3 = a2;


a2.pData[2] = 22;

a3.pData[3] = 333;


a1.Show();

a2.Show();

a3.Show();


return
0;
}

 


H 下标运算
自定义类Arr,管理int数组。
允许通过对象下标访问元素。
注意:下标越界时,访问最近的有效下标对应的元素。

#include <iostream>

#include <iomanip>
using
namespace
std;

class
Arr {
public
:

int
* pData;

int
cnt;
public
:

Arr(
int
cnt = 5) :cnt(cnt)

{

pData =
new
int
[cnt];

}

~Arr()

{

if
(pData) {

delete
[] pData;

pData = 0; cnt = 0;

}

}

void
Show()
const

{

for
(
int
i = 0; i < cnt; i++)

cout << pData[i] << ((i == cnt - 1) ?
"\n"
:
" "
);

}
int
& operator[](
int
n)
{

if
(n<0)n=0;

if
(n>cnt-1)n=cnt-1;

return
pData[n];

}

 

 

 

 

};
int
main()
{

Arr a1(5);

for
(
int
i = 0; i < 5; i++)

cin >> a1[i];


a1.Show();

a1[3] = a1[2] * 10;

a1[-1] = -1;

a1[6] = 6;

a1.Show();


return
0;
}

 


I 类型转换
有复数类 Complex。
补充程序,支持 Complex 转换为 double,支持 double 转换为 Complex。
Complex 转换为 double:值为复数的模。
double 转换为 Complex:实部为 double 的值,虚部为0。

#include <iostream>

#include <string>
#include <iomanip>
#include <cmath>
using
namespace
std;

class
Complex
{
public
:

double
r;
// real

double
i;
// image
public
:

Complex() :r(0), i(0) {}

Complex(
double
a,
double
b) :r(a), i(b) {}

void
Show()
const

{

cout <<
"("
<< noshowpos << r << showpos << i << noshowpos <<
"i)"
<< endl;

}
operator
double
()
const
{

return
sqrt
(r*r+i*i);

}

Complex(
const
double
& v)

{



r=v;

i=0;

}
};

int
main()
{

double
a, b;

cin >> a >> b;


Complex c1(a, b);

double
tmp;

Complex c2;


tmp = c1;

c2 = tmp;


c1.Show();

cout << tmp << endl;

c2.Show();


return
0;
}

 


J 仿函数
二次多项式(Quadratic polynomial)计算。
如:f(x) = ax2 + bx + c
已知:a、b、c;以及 x1、x2、x3、x4、…、xn
求:f(x1) = ?、f(x2) = ?
定义 Quad 类。实现二次多项式的值的计算。

#include <iostream>

#include <string>
#include <iomanip>
using
namespace
std;

class
Quad

{

public
:


double
a;


double
b;


double
c;


Quad(
double
a,
double
b,
double
c):a(a), b(b), c(c)


{




}


double
operator()(
double
x)


{


return
(a * x * x + b * x + c);


}

};
int
main()
{

double
a, b, c, x1, x2;

cin >> a >> b >> c >> x1 >> x2;


Quad q1(a, b, c);


cout << q1(x1) << endl;

cout << q1(x2) << endl;

return
0;
}

 


K 仿函数2
可变排序。
输入排序方式 asc,元素个数 n (n<=100),以及 n 个元素,输出对 n 个元素按指定方式排序的结果。
asc 为 1 表示升序,为 0 表示降序。

#include<iostream>

#include<iomanip>
using
namespace
std;
class
SortRule

{

public
:


bool
asc;


SortRule(
bool
x):asc(x)


{




}


bool
operator()(
const
int
&a,
const
int
&b )
const


{


if
(!asc)


return
a > b;


else


return
a < b;


}

};
void
showArray(
int
x[],
int
n)
// 输出数组的元素
{

for
(
int
i = 0; i < n; i++) {

cout << x[i] << ((i != n - 1) ?
" "
:
"\n"
);

}
}

void
xSort(
int
x[],
int
n, SortRule rule)
// 排序
{

int
p;

int
t;

for
(
int
i = 0; i < n - 1; i++)

{

p = i;

for
(
int
j = i + 1; j < n; j++)

{

if
(rule(x[j], x[p]))

p = j;

}

t = x[p];

x[p] = x[i];

x[i] = t;

}
}

int
main()
{

bool
asc;

int
n;

int
a[100];


while
(cin >> asc) {

cin >> n;

for
(
int
i = 0; i < n; i++) cin >> a[i];

SortRule rule1(asc);

xSort(a, n, rule1);

showArray(a, n);

}


return
0;
}

 


L 复数的加减乘除、正、负、提取与插入运算
定义 Complex 类型(复数)。
对于 c1、c2(Complex c1, c2;),支持以下运算:
cin>>c1>>c2;
cout<<c1<<c2<<endl;
c1+c2;
c1-c2;
c1*c2;
c1/c2;
+c1;
-c2;

#include <iostream>

#include <string>
#include <iomanip>
using
namespace
std;

class
Complex
{
public
:

double
r;

double
i;
};
istream& operator >>(istream &is, Complex& a)
{

is >> a.r >> a.i;

return
is;
}
ostream& operator <<(ostream &os,
const
Complex& a)
{

//os << a.r << a.i;

os <<
"("
<< noshowpos << a.r << showpos << a.i << noshowpos <<
"i)"
;

return
os;
}
Complex operator+(
const
Complex& a1,
const
Complex& a2)
{

Complex a;

a.r = a1.r + a2.r;

a.i = a1.i + a2.i;

return
a;
}
Complex operator-(
const
Complex& a1,
const
Complex& a2)
{

Complex a;

a.r = a1.r - a2.r;

a.i = a1.i - a2.i;

return
a;
}
Complex operator*(
const
Complex& a1,
const
Complex& a2)
{

Complex a;

a.r = a1.r * a2.r - a1.i * a2.i;

a.i = a1.r * a2.i + a1.i * a2.r;

return
a;
}
Complex operator/(
const
Complex& a1,
const
Complex& a2)
{

Complex a;

a.r = (a1.r * a2.r + a1.i * a2.i) / (a2.r * a2.r + a2.i * a2.i);

a.i = (a1.i * a2.r - a1.r * a2.i) / (a2.r * a2.r + a2.i * a2.i);

return
a;
}
Complex operator+(
const
Complex& a1)
{

Complex a;

a.r = +a1.r;

a.i = +a1.i;

return
a;
}
Complex operator-(
const
Complex& a1)
{

Complex a;

a.r = -a1.r;

a.i = -a1.i;

return
a;
}
int
main()
{

Complex c1, c2;

Complex c3, c4;


cin >> c1 >> c2;


cout <<
"c1 = "
<< c1 << endl;

cout <<
"c2 = "
<< c2 << endl;

cout << c1 <<
" + "
<< c2 <<
" = "
<< c1 + c2 << endl;

cout << c1 <<
" - "
<< c2 <<
" = "
<< c1 - c2 << endl;

cout << c1 <<
" * "
<< c2 <<
" = "
<< c1 * c2 << endl;

cout << c1 <<
" / "
<< c2 <<
" = "
<< c1 / c2 << endl;


cout <<
"+"
<< c1 <<
" = "
<< +c1 << endl;

cout <<
"-"
<< c1 <<
" = "
<< -c1 << endl;


return
0;
}

 


M 比较完美的赋值运算
数组也可以赋值啦!
输入一个变长的数组,通过拷贝和赋值产生三个独立的数组。并且按指定格式输出。

比较完美的赋值运算,应当做到:
1、深赋值
2、无内存泄漏 (注:因编译器无法检测内存泄漏,故:修改了部分代码,尝试通过内存限制间接检查内存泄漏)
3、支持自身赋值 (如 a1 = a1)
4、高效率


#include <iostream>

#include <iomanip>
using
namespace
std;

class
Arr {
public
:

int
cnt;

int
* pData;
public
:

Arr(
int
cnt = 5) :cnt(cnt)

{

pData =
new
int
[cnt];

}

~Arr()

{

if
(pData) {

delete
[] pData;

pData = 0; cnt = 0;

}

}

void
Show()
const

{

for
(
int
i = 0; i < cnt; i++)

cout << pData[i] << ((i == cnt - 1) ?
"\n"
:
" "
);

}

Arr(
const
Arr& a)

{

cnt = a.cnt;

pData =
new
int
[cnt];

for
(
int
i = 0; i < cnt; i++)

pData[i]=a.pData[i];

}

Arr& operator=(
const
Arr & a)
{

if
(
this
==&a)

return
*
this
;

if
(cnt !=a.cnt)

{

delete
[]pData;

cnt=a.cnt;

pData =
new
int
[cnt];



}

for
(
int
i = 0;i<cnt;i++)

pData[i]=a.pData[i];

return
*
this
;








}
};

int
main()
{

int
n;

cin >> n;


Arr a1(n);

for
(
int
i = 0; i < a1.cnt; i++)

cin >> a1.pData[i];


Arr a2(a1);

Arr a3;

a3 = a2;

a3 = a3;


if
(n > 2) a2.pData[2] = 0;

if
(n > 3) a3.pData[3] = 0;


a1.Show();

a2.Show();

a3.Show();


// 以下代码用于间接检测内存泄漏和自赋值,不产生输出,可以忽略

Arr bigarr(1000000);

const
int
N = 20;

Arr arrs[N];

for
(
int
i = 0; i < N; i++)

arrs[i] = bigarr;

for
(
int
i = 0; i < N; i++)

arrs[i] = arrs[0];


return
0;
}


N 类型转换2
温度的知识:
华氏温标 - 1724 年,德国人华伦海特(Fahrenheit)制定了华氏温标,最早,他把一定浓度的盐水凝固时的温度定为 0 ℉,将他妻子的体温设为 100 ℉,后来为了严谨,又把纯水的冰点温度定为 32 ℉,把标准大气压下水的沸点温度定为 212 ℉,中间分为 180 等份,每一等份代表 1 度,这就是华氏温标。
摄氏温标 - 1742 年,瑞典天文学家安德斯·摄尔修斯 (Anders Celsius,1701-1744) 将一大气压下冰水混合物的温度规定为 0 ℃,水的沸点定为 100 ℃,两者间均分成 100 个刻度。1954 年的第十届国际度量衡大会特别将此温标命名为“摄氏温标”,以表彰摄氏的贡献。
开氏温标 - 热力学温标是由威廉·汤姆森(第一代开尔文男爵 Lord Kelvin)于 1848 年利用热力学第二定律的推论卡诺定理引入的。它是一个纯理论上的温标,因为它与测温物质的属性无关。符号 T,单位 K(开尔文,简称开)。国际单位制(SI)的 7 个基本量之一。根据热力学原理得出,测量热力学温度,采用国际实用温标。热力学温度旧称绝对温度(absolute temperature)。单位是“开尔文”,英文是“Kelvin”简称“开”,国际代号“K”,但不加“°”来表示温度。以绝对零度(0K)为最低温度,规定水的三相点的温度为 273.16K,开定义为水三相点热力学温度的 1 / 273.16。

请根据规律,定义 2 个类,实现华氏温标和摄氏温标的自动换算。
做完后,同学们可以尝试实现 3 种温标的相互换算。

#include<iostream>

#include<iomanip>
using
namespace
std;

// 华氏温标
class
Ftemp {
public
:

double
v;

Ftemp(
double
v = 32) :v(v) { }
};

// 摄氏温标
class
Ctemp {
public
:

double
v;

Ctemp(
double
v = 0) :v(v) { }
operator Ftemp()

{

Ftemp f;



f.v = v * 1.8 + 32;

return
f;

}

Ctemp(
const
Ftemp& f)

{

v = (f.v - 32) / 1.8;

}
};

istream& operator>> (istream& is, Ftemp& x) {

return
is >> x.v;
}
istream& operator>> (istream& is, Ctemp& x) {

return
is >> x.v;
}
ostream& operator<< (ostream& os,
const
Ftemp& x) {

return
os << x.v <<
"℉"
;
}
ostream& operator<< (ostream& os,
const
Ctemp& x) {

return
os << x.v <<
"℃"
;
}

int
main()
{

Ftemp f1, f2;

Ctemp c1, c2;


while
(cin >> f1 >> c1)

{

cout <<
"----------"
<< endl;

cout << fixed << setprecision(1);

cout <<
"f1 = "
<< f1 << endl

<<
"c1 = "
<< c1 << endl;


c2 = f1;

f2 = c1;

c2 = (Ctemp)f1;

f2 = (Ftemp)c1;


cout <<
"f1 = "
<< f1 <<
" = "
<< c2 << endl

<<
"c1 = "
<< c1 <<
" = "
<< f2 << endl;

}
}

 

第四

A1.4 简单的继承
补充程序,使得输出符合规定的要求。
#include <iostream>
#include <iomanip>
using namespace std;

class A
{
public:
void f1()
{
cout << "Call function 1." << endl;
}
void f2()
{
cout << "Call function 2." << endl;
}
};

class B
:public A
{
};
int main()
{
B b1;
b1.f1();
b1.f2();
return 0;
}


B 继承与扩展
补充程序,使得输出符合规定的要求。
#include <iostream>
#include <iomanip>
using namespace std;

class A
{
public:
void f1()
{
cout << "Call function 1." << endl;
}
};

class B : public A
{
public:
void fx()
{
cout<<"Call function x."<<endl;

}
};
int main()
{
B b1;
b1.f1();
b1.fx();
return 0;
}


C 基类和派生类的兼容性
补充程序,只增加一个函数,使得输出符合规定的要求。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Animal
{
public:
string name;
int age;
Animal(string s = "Sam", int a = 2) :name(s), age(a) {}
};

class Cat :public Animal
{
public:
Cat(string s = "Tom", int a = 2) :Animal(s, a) {}
};
void Show(Animal a)
{
cout<<a.name<<" is "<<a.age<<" years old. "<<endl;
}
int main()
{
string s1, s2;
int n1, n2;
cin >> s1 >> n1 >> s2 >> n2;

Animal a1(s1, n1);
Cat c2(s2, n2);
Show(a1);
Show(c2);

return 0;
}


D 基类、子对象、派生类的构造与析构
补充程序,使得输出符合规定的要求。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class A
{
public:
int x;
A(int a) :x(a) {
cout << "Create A" << endl;
}
~A() {
cout << "Destory A" << endl;
}
};

class B
{
public:
int y;
B(int b) :y(b) {
cout << "Create B" << endl;
}
~B() {
cout << "Destory B" << endl;
}
};

class C
:public A
{
public:
int y,z;
B b1;
C(int x,int y,int z):A(x),b1(y),z(z)
{
cout<<"Create C"<<endl;
}
~C()
{
cout<<"Destory C"<<endl;
}
}
;
;

int main()
{
int a, b, c;
cin >> a >> b >> c;
C c1(a, b, c);
cout << c1.x << endl
<< c1.b1.y << endl
<< c1.z << endl;
return 0;
}


E 隐藏基类函数
补充程序,使得输出符合规定的要求。
#include <iostream>
using namespace std;

class Vehicle
{
public:
void Run() {
cout << "Call Vehicle::Run()" << endl;
}
};
class Car:public Vehicle
{
public:
void Run()
{
cout<<"Call Car::Run()"<<endl;
}
};
int main()
{
Car car;
car.Run();
car.Vehicle::Run();

Vehicle* pv = &car;
pv->Run();

return 0;
}


F 多继承之V形多继承
补充程序,使得输出符合规定的要求。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class A
{
public:
int x;
};

class B
{
public:
int y;
};
class C:public A,public B
{
public:
int z;
};
int main()
{
C c1;
cin >> c1.x >> c1.y >> c1.z;

cout << c1.A::x << endl
<< c1.B::y << endl
<< c1.z << endl;

cout << "sizeof(C) = " << sizeof(C) << endl;
return 0;
}


G 多继承之菱形多继承
补充程序,使得输出符合规定的要求。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class A
{
public:
int x;
};

class B : public A
{
public:
int y;
};

class C : public A
{
public:
int z;
};
class D : public B,public C
{
public:
int w;
};
int main()
{
D d1;
cin >> d1.B::x >> d1.C::x >> d1.y >> d1.z >> d1.w;

cout << d1.B::x << endl
<< d1.C::x << endl
<< d1.y << endl
<< d1.z << endl
<< d1.w << endl;

cout << "sizeof(D) = " << sizeof(D) << endl;
return 0;
}

J 继承之猫和老鼠
补充代码,使得程序按要求输出。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Animal
{
public:
Animal(string n, int a) :name(n), age(a) {}
void Say()
{
cout << "Hello, I'm an animal. Named "
<< name << ", " << age << " years old.\n";
}
protected:
string name;
int age;
};
class Cat:virtual public Animal
{
public:
Cat(string n, int a) : Animal(n,a){}
void Say()
{
cout << "Miao miao miao, I'm a cat. Named "
<< Animal::name << ", " <<Animal::age << " years old.\n";
}
protected:
string name;
int age;
};
class Mouse :virtual public Animal
{
public:
Mouse(string n, int a) :Animal(n,a){
}
void Say()
{
cout << "Zi zi zi, I'm a mouse. Named "
<<Animal::name<< ", " << Animal::age << " years old.\n";
}
protected:
string name;
int age;
};
int main()
{
Animal a1("Jack", 3);
Cat c1("Tom", 4);
Mouse m1("Jerry", 5);

a1.Say();
c1.Say();
m1.Say();

cout << "----" << endl;

Animal* anis[3] = {
&a1,
&c1,
&m1 };

for (int i = 0; i < 3; i++)
{
anis[i]->Say();
}
return 0;
}


I 多继承之虚继承2
虚基类的初始化。
补充程序,使得输出符合规定的要求。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class A
{
public:
int x;
A(int a):x(a) {}
};

class B : virtual public A
{
public:
int y;
B(int a, int b):A(a),y(b) {}
};

class C : virtual public A
{
public:
int z;
C(int a, int c):A(a),z(c) {}
};
class D : virtual public B,virtual public C
{
public:
int w;
D(int a,int b,int c,int d,int e,int f):A(f),B(a,b),C(a,d),w(e) {}
};
int main()
{
int a,b,c,d,e,f;
cin >> a >> b >> c >> d >> e >> f;
D d1(a,b,c,d,e,f);

cout << d1.A::x << endl
<< d1.B::x << endl
<< d1.C::x << endl
<< d1.x << endl
<< d1.y << endl
<< d1.z << endl
<< d1.w << endl;

return 0;
}

H 多继承之虚继承
补充程序,使得输出符合规定的要求。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class A
{
public:
int x;
};

class B : virtual public A
{
public:
int y;
};

class C : virtual public A
{
public:
int z;
};
class D : virtual public B,virtual public C
{
public:
int w;
};
int main()
{
D d1;
cin >> d1.A::x >> d1.B::x >> d1.C::x >> d1.y >> d1.z >> d1.w;

cout << d1.A::x << endl
<< d1.B::x << endl
<< d1.C::x << endl
<< d1.y << endl
<< d1.z << endl
<< d1.w << endl;

return 0;
}

第五

A 1.5 多态之猫和老鼠
补充代码,使得程序按要求输出。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Animal
{
protected:
string name;
int age;
public:
Animal(string a,int b):name(a),age(b)
{
}
virtual void Say()
{
cout<<"Hello, I'm an animal. Named "
<<name<<", "<<age<<" years old."<<endl;
}
};

class Cat:public Animal
{
protected:
string name;
int age;
public:
Cat(string a,int b):Animal(a,b),name(a),age(b)
{
}
void Say()
{
cout<<"Miao miao miao, I'm a cat. Named "
<<name<<", "<<age<<" years old."<<endl;
}
};

class Mouse:public Animal
{
protected:
string name;
int age;
public:
Mouse(string a,int b):Animal(a,b),name(a),age(b)
{
}
void Say()
{
cout<<"Zi zi zi, I'm a mouse. Named "
<<name<<", "<<age<<" years old."<<endl;
}
};
int main()
{
Animal a1("Jack", 3);
Cat c1("Tom", 4);
Mouse m1("Jerry", 5);

a1.Say();
c1.Say();
m1.Say();

cout << "----" << endl;

Animal* anis[3] = {
&a1,
&c1,
&m1 };

for (int i = 0; i < 3; i++)
{
anis[i]->Say();
}
return 0;
}


B 多态之虚析构
补充代码,完成类 A、B、C、D 的定义,使得符合输出要求。
要求:每一个类的构造、析构都要有内容输出。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class A
{
public:
A()
{
cout<<"Create A"<<endl;
}
virtual ~A()
{
cout<<"Destory A"<<endl;
}
};
class B:public A
{
public:
B()
{
cout<<"Create B"<<endl;
}
};
class C
{
public:
C()
{
cout<<"Create C"<<endl;
}
virtual ~C()
{
cout<<"Destory C"<<endl;
}
};
class D:public C
{
public:
D()
{
cout<<"Create D"<<endl;
}
virtual ~D()
{
cout<<"Destory D"<<endl;
}
};
const string SPLIT1 = "================";
const string SPLIT2 = "-";
int main()
{
{
cout << SPLIT1 << endl;
A* p1 = new A();
cout << SPLIT2 << endl;
delete p1;
}
{
cout << SPLIT1 << endl;
A* p1 = new B();
cout << SPLIT2 << endl;
delete p1;
}
{
cout << SPLIT1 << endl;
C* p1 = new C();
cout << SPLIT2 << endl;
delete p1;
}
{
cout << SPLIT1 << endl;
C* p1 = new D();
cout << SPLIT2 << endl;
delete p1;
}

return 0;
}

C 抽象类之Shape、Triangle、Rectangle
补充代码,完成类 Triangle、Rectangle 的定义,使得符合输出要求。
注:
Triangle: 3 个顶点的坐标确定一个三角形,
Rectangle : 2 个对角点的坐标确定一个矩形。
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;

typedef double D;

class Point
{
public:
D x, y;
Point(D x = 0, D y = 0) :x(x), y(y) {}
D Distance(const Point& p) const
{
return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
};

class Shape
{
public:
virtual D Area() = 0;
virtual D Perimeter() = 0;
virtual string Name() = 0;
};
class Triangle :public Shape {
public:
Point A, B, C;
Triangle(D a, D b, D c, D d, D e, D f) :A(a, b), B(c, d), C(e, f) {}
D Area() {
D l1 = A.Distance(B);
D l2 = B.Distance(C);
D l3 = C.Distance(A);
D l = (l1 + l2 + l3) / 2;
return sqrt((l - l1) * (l - l2) * (l - l3) * l);
}
D Perimeter() {
D l1 = A.Distance(B);
D l2 = B.Distance(C);
D l3 = C.Distance(A);
return l1 + l2 + l3;
}
string Name() {
return "Triangle";
}
};
class Rectangle :public Shape {
public:
Point A, B;
Rectangle(D a, D b, D c, D d) :A(a, b), B(c, d) {
}
D Area() {
D l1 = fabs(A.x - B.x);
D l2 = fabs(A.y - B.y);
return l1 * l2;
}
D Perimeter() {
D l1 = fabs(A.x - B.x);
D l2 = fabs(A.y - B.y);
return (l1 + l2) * 2;
}
string Name() {
return "Rectangle";
}
};
int main()
{
D a, b, c, d, e, f;
D m, n, x, y;
cin >> a >> b >> c >> d >> e >> f;
cin >> m >> n >> x >> y;

Shape* shps[2] = {
new Triangle(a, b, c, d, e, f),
new Rectangle(m, n, x, y)
};

for (int i = 0; i < 2; i++)
{
cout << shps[i]->Name() << " Area: " << shps[i]->Area() << endl;
cout << shps[i]->Name() << " Perimeter: " << shps[i]->Perimeter() << endl;
}
return 0;
}


D 多态之虚析构2
补充代码,完成类 A、B、C、D 的定义,使得符合输出要求。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class A
{
public:
A() { cout << "Create A" << endl; }
~A() { cout << "Destory A" << endl; }
};

class C
{
public:
C() { cout << "Create C" << endl; }
virtual ~C() { cout << "Destory C" << endl; }
};
class B : public A
{
public:
B() { cout << "Create B" << endl; }
~B() { cout << "Destory B" << endl; }
};

class D : public C
{
public:
D() { cout << "Create D" << endl; }
~D() { cout << "Destory D" << endl; }
};

const string SPLIT1 = "================";
const string SPLIT2 = "-";
int main()
{
{
cout << SPLIT1 << endl;
B* p1 = new B();
cout << SPLIT2 << endl;
delete p1;
}
{
cout << SPLIT1 << endl;
A* p1 = new B();
cout << SPLIT2 << endl;
delete p1;
}
{
cout << SPLIT1 << endl;
D* p1 = new D();
cout << SPLIT2 << endl;
delete p1;
}
{
cout << SPLIT1 << endl;
C* p1 = new D();
cout << SPLIT2 << endl;
delete p1;
}

return 0;
}

 第六

A 1.6 函数模板之加法
补充程序,使得输出符合规定的要求。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
template<class T>
T add(T a , T b)
{
return a+b;
}
int main()
{
int a, b;
double c, d;
string e, f;

cin >> a >> b >> c >> d >> e >> f;

cout << "int a + b = " << add(a, b) << endl;
cout << "int a + b = " << add<int>(a, b) << endl;
cout << "double c + d = " << add(c, d) << endl;
cout << "string e + f = " << add(e, f) << endl;

return 0;
}


B 函数模板之排序1
补充程序,使得输出符合规定的要求。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
template<class T>
void Sort(T a[],int n)
{
T t;
for(int i=0;i<n-1;i++)
{
int p=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[p]) p=j;
}
t=a[i];a[i]=a[p];a[p]=t;
}
}
template<typename T>
void Input(T a[], int n)
{
for (int i = 0; i < n; i++)
cin >> a[i];
}

template<typename T>
void Show(T a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << ((i < n - 1) ? " " : "\n");
}

int main()
{
const int N = 6;
{
long a[N];

Input(a, N);
Show(a, N);
Sort(a, N);
Show(a, N);
}
cout << "-" << endl;
{
double a[N];

Input(a, N);
Show<double>(a, N);
Sort(a, N);
Show(a, N);
}

return 0;
}


C 函数模板之排序2
补充程序,使得输出符合规定的要求。
定义学生类Student(包含成员变量:姓名name、成绩score),以及相关的函数。
排序依旧只使用一个函数模板。

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Student
{
public:
string name;
double score;

bool operator<(const Student& s) const {
return score<s.score;
}
};

istream& operator>>(istream&is,Student& s)
{
return is>>s.name>>s.score;
}
ostream& operator<<(ostream&os,const Student& s)
{
return os<<"("<<s.name<<","<<s.score<<")";
}

template<class T>
void Sort(T a[],int n)
{
T t;
for(int i=0;i<n-1;i++)
{
int p=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[p]) p=j;
}
t=a[i];a[i]=a[p];a[p]=t;
}
}
template<typename T>
void Input(T a[], int n)
{
for (int i = 0; i < n; i++)
cin >> a[i];
}

template<typename T>
void Show(T a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << ((i < n - 1) ? " " : "\n");
}

int main()
{
const int N = 6;
{
long a[N];

Input(a, N);
Show(a, N);
Sort(a, N);
Show(a, N);
}
cout << "-" << endl;
{
Student a[N];
Input(a, N);
Show(a, N);
Sort(a, N);
Show(a, N);
}

return 0;
}


D 函数模板之特殊化以及重载(Sepcialization&Overload)
补充程序,使得输出符合规定的要求。
提示:模板 add(T,T) 特殊化 add(int,int) ,并重载 add(int,int)
分别返回:a+b+2 or a+b+1 or a+b


#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
template<class T>
T add(T a,T b)
{
return a+b;
}

template<> int add<int>(int a,int b)
{
return a+b+a;
}

int add(int a,int b)
{
return a+b+b;
}
int main()
{
double x, y;
cin >> x >> y;
cout << "add(x, y) = " << add(x, y) << endl;

cout << "add('A', '\\2') = " << add('A', '\2') << endl;
cout << "add(1.0, 2.0) = " << add(1.0, 2.0) << endl;
cout << "add(1L, 2L) = " << add(1L, 2L) << endl;

cout << "add<int>(1, 2) = " << add<int>(1, 2) << endl;
cout << "add(1, 2) = " << add(1, 2) << endl;
return 0;
}


E 类模板之简单数组
实现一个Array类模板,支持输入、输出、下标访问。
输入:Input()
输出:Show()
下标访问:operator[]


#include <iostream>
#include <iomanip>
using namespace std;
template<class T>
class Array
{
int cut;
T *p;
public:
Array(int c):cut(c){
p = new T[cut];
}
~Array(){
delete [] p;
}
void Input(){
for(int i=0;i<cut;i++)
cin>>p[i];
}
void Show(){
for(int i=0;i<cut;i++)
cout<<p[i]<<(i!=cut-1?" ":"\n");
}

T& operator[](int idx){
return p[idx];
}
const T& operator[](int idx) const {
return p[idx];
}
};
int main()
{
{
Array<int> a1(5);

a1.Input();
a1.Show();

a1[1] = 7;
a1.Show();
}
cout << "-" << endl;
{
Array<char> a1(5);

a1.Input();
a1.Show();

a1[1] = 'X';
a1.Show();
}

return 0;
}


G 类模板之继承
根据main函数的代码,参考如下的类图,完成相关类的定义。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class A {};
template<typename T>
class B{};
class C :public A {};
template<typename T>
class D :public A {};
class E :public B<int> {};
template<typename T>
class F :public B<T>{};
int main()
{
A a1;

B<int> b1;
B<long> b2;

C c1;
A* c2 = &c1;

D<int> d1;
D<long> d2;
A* d3 = &d1;

E e1;
B<int>* e2 = &e1;

F<char> f1;
B<int>* f2 = new F<int>();
B<long>* f3 = new F<long>();

int x;
cin >> x;
cout << x << endl;
return 0;
}


I 模板之模板类型参数
补充程序,使得输入输出匹配。

注:
1、 sizeof(X) 的值依赖于具体的编译器。
2、 typeid(X).name() 的值依赖于具体的编译器。
#include<iostream>
#include<typeinfo>
using namespace std;

template<typename T> class A {
public:
T x;
A() :x(0) {}
};
template <typename T, long N, template<typename T1> class A >
class B {
public:
A<T>a[N];
};
int main()
{
B<int, 5, A> b1;
cin >> b1.a[3].x;

cout << "sizeof(b1) = " << sizeof(b1) << endl;
cout << "typeid(b1.a).name() = " << typeid(b1.a).name() << endl;
for (int i = 0; i < 5; i++)
cout << "b1.a[" << i << "].x = " << b1.a[i].x << endl;

return 0;
}


H 类模板之半特化、全特化
特化类模板A,使得程序符合输出要求。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

template<typename T1, typename T2>
class A {
public:
void f() { cout << "call A<*,*>::f();" << endl; };
};
template<typename T>
class A<char, T>
{
public:
void f() { cout << "call A<char,*>::f();"<< endl;};

};
template<>
class A<char, double> {
public:
void f() { cout << "call A<char,double>::f();" <<endl; };
};
int main()
{
A<int, double> a1;
A<long, double> a2;
A<char, char> a3;
A<char, double> a4;

cout << "--" << endl;
a1.f();
a2.f();
a3.f();
a4.f();
cout << "--" << endl;

return 0;
}


问题 F: 类模板之高级数组


#include<algorithm>
template<typename T>class
Array{
public:
T* arr;
int size;
Array(int n) :size(n)
{ arr = new T[size]; }
~Array()
{ delete arr; }
void Input()
{
for (int i = 0; i < size; i++)cin >> arr[i];
}
void Show()
{
for (int i = 0; i < size; i++)cout << arr[i]
<< " ";
cout << endl;
}
T& operator[](int idx)
{
return arr[idx];

}
friend istream& operator>>(istream&
input,Array& obj)
{
for (int i = 0; i < obj.size; i++)
input >> obj.arr[i];
return input;
} friend ostream& operator<<(ostream& output, Array& obj) {
for (int i = 0; i < obj.size; i++)
output << obj.arr[i] << " ";
output << endl;
return output;
}
void Sort()
{
sort(arr, arr + size);
}

};

 第七

A 1.7 STL向量基础
STL向量基础,补充代码,使得程序按要求输出。
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
using namespace std;

int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;

vector<int> v1(a, b);
v1.insert(v1.end(), c, d);

for (unsigned int i = 0; i < v1.size(); i++)
cout << (i == 0 ? "" : ", ") << v1[i];
cout << endl;

return 0;
}


B STL算法基础1
整数升序排序。
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#include<functional>
using namespace std;

#define N 10
int main()
{
int a[N] = { 5,6,7,8,9,1,2,3,0,4 };
stable_sort(
a,a+N
);
for (int i = 0; i < N; i++)
cout << (i == 0 ? "" : ", ") << a[i];
cout << endl;
return 0;
}


C STL算法基础2
整数升序排序。
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

#define N 10
int main()
{
vector<int> a = { 5,6,7,8,9,1,2,3,0,4 };
stable_sort(
a.begin(),a.end()
);
for (int i = 0; i < N; i++)
cout << (i == 0 ? "" : ", ") << a[i];
cout << endl;
return 0;
}


D STL向量高级
程序功能:
1、输入n、以及n个整数,接着输入a、b、c、d、e、f,
2、删除第a个整数,(注意:此题中最前面的一个称为第0个,a、b、d的值保证能够删除或插入)
3、删除第b个整数开始的c个整数,
4、在d位置插入e个f,
5、依次输出余下的整数;
6、升序输出;
7、降序输出。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin >> n;

vector<int> v1(100);
vector<int> v2(6);

for (int i = 0; i < n; i++)
cin >> v1[i];

for (int j = 0; j < 6; j++)
cin >> v2[j];

v1.erase(v1.begin() + v2[0]);
v1.erase(v1.begin() + v2[1], v1.begin() + v2[1] + v2[2]);
v1.insert(v1.begin() + v2[3], v2[4], v2[5]);

n = n - 1 - v2[2] + v2[4];

for (int i = 0; i < n; i++)
cout << v1[i] << (i != n - 1 ? ", " : "\n");

stable_sort(v1.begin(), v1.begin() + n);
for (int i = 0; i < n; i++)
cout << v1[i] << (i != n - 1 ? ", " : "\n");

stable_sort(v1.begin(), v1.begin() + n, greater<int>());
for (int i = 0; i < n; i++)
cout << v1[i] << (i != n - 1 ? ", " : "\n");
return 0;
}


E STL算法基础3
整数降序排序。
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#include<functional>
using namespace std;

#define N 10
int main()
{
int a[N] = { 5,6,7,8,9,1,2,3,0,4 };
stable_sort(
a,a+N,greater<int>()
);
for (int i = 0; i < N; i++)
cout << (i == 0 ? "" : ", ") << a[i];
cout << endl;
return 0;
}


F STL排序去重
输入n,以及n个整数,
去掉重复的整数,并升序输出。
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>
#include <set>
using namespace std;
int main(){
int n;
int a[n];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
set<int> s(a,a+n);
set<int>::iterator its;
for(its = s.begin();its!=s.end();its++)
cout<<(its == s.begin()? "" :", ")<<*its;
cout<<endl;
}


G STL查成绩
输入n,n个学生的姓名(string)和成绩(double),可能有重复姓名,若重复,以最后一次成绩为准。
接下来可以进行连续多次查询。
输入姓名:
1、若姓名=="end",则结束程序;
2、若有该生,则输出该生的成绩,保留2位小数;
3、若无该生,则输出"NONE";
#include<iostream>
#include<string>
#include <iomanip>
#include<map>

using namespace std;
int main()
{

int n;
double score;
string name;
cin >> n;
map<string, double>map;
while (n--)
{
cin >> name;
cin >> score;
map[name] = score;
}
while (1)
{
cin >> name;
if (name == "end")
break;
if (map[name] != 0)
cout << setw(8) << name << ": ", printf("%.2lf\n", map[name]);
else
cout << setw(8) << name << ": " << "NONE" << endl;
}
}


H 容器的遍历
遍历输出 vector 中的所有元素。
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;

void Show(const int& x) { cout << x << ", "; }

int main()
{
vector<int> v{ 5,6,7,8,9,0,1,2,3,4 };

for_each(
v.begin(),v.end(),Show
);
}

 

I 积累连续运算
累加和累积。
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#include<functional>
#include <numeric>
using namespace std;
template<class T>
class A
{
public:
T operator()(const T& a, const T& b)
{
return a + b;
}
};
template<class T>
class B
{
public:
T operator()(const T& a, const T& b)
{
return a * b;
}
};
int main()
{
int n;
long a[100];

cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];

cout << "和 = " << accumulate(a, a + n, 0, A<long>()) << endl;
cout << "积 = " << accumulate(a, a + n, 1, B<long>()) << endl;
return 0;
}


J 仿函数
计算 2 个整数的和差积商余以及 6 种关系运算(< <= == != > >=)的结果。
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#include<functional>

using namespace std;

typedef long T;
int main()
{
T a, b;
function<T(T, T)> funcs[] = {
plus<T>(),
minus<T>(),
multiplies<T>(),
divides<T>(),
modulus<T>(),
less<T>(),
less_equal<T>(),
equal_to<T>(),
not_equal_to<T>(),
greater<T>(),
greater_equal<T>(),
};
cin >> a >> b;

for (auto& f : funcs) {
cout << f(a, b) << endl;
}
cout << "--" << endl;
for (auto i = 0; i < sizeof(funcs) / sizeof(funcs[0]); i++) {
cout << funcs[i](a, b) << endl;
}
}


K STL之巅峰一战
三行代码,实现:
任意个整数的输入、排序、输出。
注:本地调试时,输入数据后,按回车,再按 CTRL + Z 结束输入。
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std;

int main()
{
vector<int> v;

copy(
istream_iterator<int>(cin), istream_iterator<int>(), back_inserter< vector<int> >(v)
);
sort(v.begin(), v.end());
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
}


L C++:泛型编程stack(括号匹配)
假设表达式中包含一种括号:圆括号,其嵌套顺序随意,即 (()()) 或 (()) 等为正确的格式,)( 或 ((()) 或 ()) 均为不正确的格式。
检验括号是否匹配可以用堆栈来实现:
当遇到 ( 时进栈,
遇到 ) 时出栈,并进行匹配检验,如果出现不匹配的情况立即结束,否则继续取下一个字符。如果没有遇到不匹配的情况,最后判断栈是否为空,栈为空,括号匹配,否则不匹配。
#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main()
{
string str;
while (cin >> str) {
bool ok = true;
stack<char> s;
for (int i=0; i < str.length(); i++) {
if (str[i] == '(') {s.push(1); }
else {
if (s.empty()) { ok = false; break; }
else s.pop();
}
}
if (!s.empty()) ok = false;
cout << (ok ? "Yes" : "No") << endl;
}
return 0;
}

 第八


A 1.8 格式化输出3
格式化输出:学生姓名和成绩。
姓名 10 字符,左对齐;成绩 6 字符,右对齐。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#define N 5

class Student {
public:
string name;
double score[3];
};
istream& operator>>(istream& is,Student& s)
{
is>>s.name>>s.score[0]>>s.score[1]>>s.score[2];
return is;
}
ostream& operator<<(ostream& os,const Student& s)
{
os<<setw(10)<<left<<s.name
<<right<<fixed<<setprecision(1)
<<setw(6)<<s.score[0]
<<setw(6)<<s.score[1]
<<setw(6)<<s.score[2];
return os;
}
int main()
{
Student s[N];
for (int i = 0; i < N; i++) {
cin >> s[i];
}
for (int i = 0; i < N; i++) {
cout << s[i] << endl;
}
return 0;
}


B 格式化输入和输出1
输入若干组数据。
每组 1 行,为 3 整数 a、b、c,分别使用 8 进制、10 进制、16 进制输入。
输出:
每组 3 行,分别输出 a、b、c的 8 进制、10 进制、16 进制形式,各占 15、12、12 字符宽度。
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a,b,c;
while(cin>>oct>>a>>dec>>b>>hex>>c)
{
cout<<showbase<<uppercase//现实进制前缀,机制转大写
<<setw(15)<<oct<<a
<<setw(12)<<dec<<a
<<setw(12)<<hex<<a<<endl;
cout
<<setw(15)<<oct<<b
<<setw(12)<<dec<<b
<<setw(12)<<hex<<b<<endl;
cout
<<setw(15)<<oct<<c
<<setw(12)<<dec<<c
<<setw(12)<<hex<<c<<endl;
}
return 0;
}


C 非格式化输入和输出2
输入 2 行字符串(长度<100),可能含空格和制表符。
第 1 行截止于换行符。
第 2 行截止于 'x',不含 'x'。
输出他们的长度和本身。
#include<iostream>
#include<iomanip>
#include<string>
#include<cstring>
using namespace std;
int main()
{
char a[100],b[100];
int x,y;
cin.getline(a,100,'\n');
cin.getline(b,100,'x');
x=strlen(a);
y=strlen(b);
cout<<"line 1: length = "<<x<<", val = "<<"\""<<a<<"\""<<endl;
cout<<"line 2: length = "<<y<<", val = "<<"\""<<b<<"\""<<endl;
return 0;
}

D 字符串流(stringstream)的使用
输入 1 行,含有:姓名、身高、体重。
按样例格式输出(保留1位小数)。
#include<iostream>
#include<sstream>
#include<string>
#include<iomanip>
using namespace std;

int main()
{
string name;
double height, weight;
char str[100];
cin.get(str, 100);

stringstream ss;
ss<<str;
ss>>name>>height>>weight;
cout<<" name: "<<name<<endl;
cout<<"height: "<<fixed<<setprecision(1)<<height<<" cm"<<endl;
cout<<"weight: "<<fixed<<setprecision(1)<<weight<<" kg"<<endl;
return 0;
}


E stringstream随机读
输入一行字符串(不含空白字符),长度为 len (len<100),放入 stringstream。
输入若干个整数 p。
对每个整数 p,从 stringstream 中读取对应的字符,并输出。
若 p<0,则从最后向前数位置。
若 |p|>=len,终止程序。
#include<iostream>
#include<sstream>
#include<string>
#include<cstring>
#include<iomanip>
using namespace std;

int main()
{
char str[100];
char ch;
int len, p;

cin >> str;
stringstream ss;
ss << str;
len = strlen(str);
memset(str, 0, 100);

while (cin >> p) {
if (abs(p) >= len) break;
if(p<0)
{
string s=ss.str();
cout<<"idx "<<p<<": "<<"\'"<<s[len-abs(p)]<<"\'"<<endl;
}
else
{
string s=ss.str();
cout<<"idx "<<p<<": "<<"\'"<<s[abs(p)]<<"\'"<<endl;
}
}
return 0;
}

第九十

1.9A 异常处理的基本原理
编程实现 100 分制 ( x 分制) 到 5 分制的转换。

函数 f2(x,y):实现 100 分制(或其他分制 x )的y分值到 5 分制的转换,返回 5 分制的等级。
(1)对于 100 分制:
y > 95: A 等
y >= 80: B 等
y >= 60: C 等
y >= 45: D 等
y >= 0: E 等
(2)对于其他分制:
先变为 100 分制,再处理。如
f2(100, 80): 表示 100 分制得 80 分,应返回 B。
f2(120, 80): 表示 120 分制得 80 分,应返回 C。
(3)参数无效,则抛出异常:
if (x < 5) 抛出 x;
if (y < 0) 抛出 y;
if (y > x) 抛出 "溢出"; // char* const

函数 f1(x,y):调用 f2(),并进行异常处理,只处理 int 异常,输出错误信息,并返回 'X'。

函数 main():调用 f1(),并进行异常处理,处理所有异常。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

char f2(int, double);
char f1(int, double);
// 在这里输入你的代码
char f2(int x,double y)
{
if(x<5)throw x;
if(y<0)throw y;
if(y>x)throw "溢出";
y=y/x*100;
if(y<95)return 'A';
else if(y>=80)return 'B';
else if(y>=60)return 'C';
else if(y>=45)return 'D';
else return 'E';
}
char f1(int a,double b)
{
try{
return f2(a,b);
}
catch(int& ex)
{
cout<<"f1()处理分母级错误, "<<ex<<endl;
return 'X' ;
}
}
int main()
{
int m;
double n;

while (cin >> m >> n)
{
try {
auto v = f1(m, n);
cout << "f(" << m << ", " << n << ") = " << v << endl;
}
catch (double& c) {
cout << "main() 处理分子过小错误, " << c << endl;
}
catch (const char* const& c) {
cout << "main() 处理分子过大错误, " << c << endl;
}
catch (...) {
cout << "未知异常." << endl;
}
}
return 0;
}


char f2(int x, double y)
{

if (x < 5) throw x;
if (y < 0) throw y;
if (y > x) throw "溢出";

y = y / x * 100;

if (y > 95) return 'A';
else if (y >= 80) return 'B';
else if (y >= 60) return 'C';
else if (y >= 45) return 'D';
else return 'E';
}
char f1(int a, double b)
{
try {
return f2(a, b);
}
catch (int& ex) {
cout << "f1() 处理分母错误, "<< ex << endl;
return 'X';
}
}

B 使用自定义异常类型
编程实现 100 分制( x 分制)到 5 分制的转换。

函数 f2(x,y):实现 100 分制(或其他分制 x )的 y 分值到 5 分制的转换,返回 5 分制的等级。
(1)对于100分制:
y > 95: A 等
y >= 80: B 等
y >= 60: C 等
y >= 45: D 等
y >= 0: E 等
(2)对于其他分制:
先变为 100 分制,再处理。如
f2(100,80): 表示 100 分制得 80 分,应返回 B。
f2(120,80): 表示 120 分制得 80 分,应返回 C。
(3)参数无效,则抛出异常:
if (x < 5) 抛出 ErrA 对象;
if (y < 0) 抛出 ErrB 对象;
if (y > x) 抛出 ErrC 对象;

函数 main():调用 f2(),并进行异常处理,处理所有异常。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

class ErrA {
public:
virtual const char* what() {
return "分母过小";
}
};
class ErrB :public ErrA {
public:
const char* what() {
return "分子过小";
}
};
class ErrC :public ErrB {
public:
const char* what() {
return "分子过大";
}
};
char f2(int, double);
char f2(int x,double y)
{
ErrA a;
ErrB b;
ErrC c;
if(x<5) throw a;
if(y<0) throw b;
if(y>x) throw c;
y=y/x*100;
if(y>95) return 'A';
else if(y>=80) return 'B';
else if(y>=60) return 'C';
else if(y>=45) return 'D';
else return 'E';
}
int main()
{
int n;
double m;
while(cin>>n>>m)
{
try{
auto v=f2(n,m);
cout<< "f(" << n << ", " << m << ") = " <<v<<endl;
}
catch(ErrA& ex)
{
cout<<ex.what()<<endl;
}
catch(ErrB& ex)
{
cout<<ex.what()<<endl;
}
catch(ErrC& ex)
{
cout<<ex.what()<<endl;
}
}
return 0;
}


C 使用标准异常类型
编程实现 100 分制( x 分制)到 5 分制的转换。

函数 f2(x,y):实现 100 分制(或其他分制 x )的 y 分值到 5 分制的转换,返回 5 分制的等级。
(1)对于 100 分制:
y > 95: A 等
y >= 80: B 等
y >= 60: C 等
y >= 45: D 等
y >= 0: E 等
(2)对于其他分制:
先变为 100 分制,再处理。如
f2(100,80): 表示 100 分制得 80 分,应返回 B。
f2(120,80): 表示 120 分制得 80 分,应返回 C。
(3)参数无效,则抛出异常:
if (x < 5) 抛出 runtime_error 对象;
if (y < 0) 抛出 range_error 对象;
if (y > x) 抛出 out_of_range 对象;

函数 main():调用 f2(),并进行异常处理,处理所有异常。
#include<iostream>
#include<string>
#include<iomanip>
#include<exception>
#include<stdexcept>
using namespace std;

char f2(int, double);
// 在这里输入你的代码
char f2(int x,double y)
{
runtime_error a("分母过小");
range_error b("分子过小");
out_of_range c("分子过大");
if(x<5)throw a;
if(y<0)throw b;
if(y>x)throw c;
y=y/x*100;
if(y>95)return 'A';//返回字符时,需用单引号,否则报错,
else if(y>=80)return 'B';
else if(y>=60)return 'C';
else if(y>=45)return 'D';
else return 'E';
}
int main()
{
int n;
double m;
while(cin>>n>>m)
try{
auto v=f2(n,m);
cout<<"f("<<n<<", "<<m<<") = "<<v<<endl;
}
catch(out_of_range& ex){
cout<<ex.what()<<endl;
}
catch(range_error& ex){
cout<<ex.what()<<endl;
}
catch(runtime_error& ex){
cout<<ex.what()<<endl;
}
return 0;
}


D 使用类型推断
补充程序,实现向量元素的输出。
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>

using namespace std;

int main()
{
vector<int> v = { 27,24,33,39,21,50,94,63,19,83 };

for (auto it=v.begin();it!=v.end();it++
// 在这里输入你的代码
) {
cout << " " << *it;
}
cout << endl;
return 0;
}


E 基于范围的 for 循环
补充程序,实现向量元素的输出。
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>

using namespace std;

int main()
{
vector<int> v = { 27,24,33,39,21,50,94,63,19,83 };

for (
auto x:v
) {
cout << " " << x;
}
cout << endl;
return 0;
}

 

 

 

F lambda 函数
补充程序,实现特定排序。要求:
1、输入 n 个整数 ti(10<=ti<=99)。
2、按个位数降序排序,若个位数一致,则按值降序排序。
#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
vector<int> v;
int n, t;

cin >> n;
while (n--) {
cin >> t;
v.push_back(t);
}

sort(v.begin(), v.end(),
[](int a,int b){return ((a%10!=b%10)?(a%10>b%10):(a>b));}
);

for (auto x : v) {
cout << x << endl;
}
return 0;
}