C++复习

第7章 作业1

1

在这里插入图片描述

void expand(char *s, char *t)
{
	for (int i = 0, j = 0;s[i] != '\0';i++)
	{
		switch (s[i]) {
		case'\n':
			t[j++] = '\\';
			t[j++] = 'n';
			break;
		case'\t':
			t[j++] = '\\';
			t[j++] = 't';
			break;
		default:
			t[j++] = s[i];
		}
		t[j] = '\0';
	}
}

2

在这里插入图片描述

#include<iostream>
using namespace std;

int main(){
    int n;
    cin>>n;
    int s=n;
    if(n%2&&n>=1&&n<80){
        for(int i=0;i<=n/2;i++){
                for(int k=0;k<(n-s)/2;k++) cout<<' ';
                for(int j=1;j<=s;j++)cout<<'*';
                s=s-2;
            cout<<endl;
        }
    }
    else cout<<"error";
    return 0;
}

3

在这里插入图片描述

#include<cstdio>
double x=0;
int main(){
    double i=1;
    while(2*i-1<1e8){
        if((int)i%2==1){
            x=x+1/(2*i-1);
        }
        else x=x-1/(2*i-1);
        i++;
    }
    printf("steps=%d PI=%.5f",int(i),4*x);
    return 0;
}

4

在这里插入图片描述

#include<iostream>
using namespace std;
#include<cstdio>
int main() {
    char s[20], t[20],c;
    int i=0;
    do {
         c = getchar();
        t[i] = c;
        i++;  
    } while (c>='a'&& c<='z');
    i--;
    t[i++] = '\\';
    t[i] = '0';
    for (int j = 0; j <= i; j++)
        cout << t[j];
    return 0;
}

5

在这里插入图片描述

#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
using namespace std;
void Matrix_Mul(int a[3][2], int b[2][4])
{
    int mul[3][4];
    for(int i=0;i<3;i++)
        for (int j = 0;j < 4;j++)
        {
            mul[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
        }
    for(int i=0;i<3;i++)
        for (int j = 0;j < 4;j++)
        {
            cout << mul[i][j] << " ";
            if (j == 3)cout << "\n";
        }
}
int main()
{
    int a[3][2] = { {1,2},{3,4},{5,6} };
    int b[2][4] = { {1,0,1,1},{0,1,0,1} };
    Matrix_Mul(a, b);
    return 0;
}

6

在这里插入图片描述

#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
using namespace std;
int SubStrNum(char* str, char* substr)
{
	int L1, L2, de = 0, times = 0;
	L1 = strlen(str);
	L2 = strlen(substr);
	for (int i = 0;i < L1;i++)
	{
		if (str[i] == substr[0])
		{
			de = 1;
			int m = i;
			for (int j = 0;j < L2;m++,j++)
			{
				if (j == L2 - 1)break;
				if (str[m + 1] == substr[j + 1])de = 1;
				else {de = 0;break;}
			}
		}
		if (de == 1) { times += 1;de = 0;i += L2 - 1; }
	}
	cout << "match times=" << times;
	return 0;
}

7

在这里插入图片描述

#include<iostream>
#include<math.h>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
	char s1[100] = { }, s2[100] = { }, s3[100] = { };
	int n, lenmax = 0, lenmin = 0, de = 0, i = 0, j = 0;
	scanf_s("%s", &s1, 100);
	scanf_s("%s", &s2, 100);
	//判断s1和s2哪个更长
	if (strlen(s1) > strlen(s2))de = 1;
	else if (strlen(s1) < strlen(s2))de = -1;
	else { de = 0;n = strlen(s1); }
	//如果一样长,判断哪个大
	if (de == 0)
	{
		for (i = 0;i < n && de == 0;i++)
		{
			if (s1[i] == s2[i])continue;
			else if (s1[i] > s2[i]) { de = 1; }
			else if (s1[i] < s2[i]) { de = -1; }
		}
	}
	//如果s1>s2,用s1减去s2,然后前面加'+'
	if (de == 1)
	{
		lenmax = strlen(s1);lenmin = strlen(s2);
		for (i = lenmax - 1, j = lenmin - 1;j >= 0;i--, j--)
		{
			s3[i] = s1[i] - s2[j];
		}
		for (i = 0;i < (lenmax - lenmin);i++)
		{
			s3[i] = s1[i];
		}
		printf("+");
		//借位
		for (i = lenmax - 1;i > (lenmax - lenmin - 1);i--)
		{
			if (s3[i] < 0) 
			{
				s3[i] += 10;
				if(i>0)s3[i - 1] -= 1;
			}
		}
		for (i = 0;i < (lenmax - lenmin);i++)
		{
			printf("%c", s3[i]);
		}
		for (i = (lenmax - lenmin);i < lenmax;i++)
		{
			//'0'的ascii码是48
			printf("%c", s3[i] + 48);
		}
	}
	//如果s2>s1,用s2减去s1,前面加'-'
	else if (de == -1)
	{
		lenmax = strlen(s2);lenmin = strlen(s1);
		for (i = lenmax - 1, j = lenmin - 1;j >= 0;i--, j--)
		{
			s3[i] = s2[i] - s1[j];
		}
		for (i = 0;i < (lenmax - lenmin);i++)
		{
			s3[i] = s2[i];
		}
		printf("-");
		//借位
		for (i = lenmax - 1;i > (lenmax - lenmin - 1);i--)
		{
			if (s3[i] < 0)
			{
				s3[i] += 10;
				if (i > 0)s3[i - 1] -= 1;
			}
		}
		for (i = 0;i < (lenmax - lenmin);i++)
		{
			printf("%c", s3[i]);
		}
		for (i = (lenmax - lenmin);i < lenmax;i++)
		{
			//'0'的ascii码是48
			printf("%c", s3[i] + 48);
		}
	}
	else if (de == 0)cout << '0';
	return 0;
}

8

在这里插入图片描述

#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
using namespace std;
int main() {
	char c;
	float a, b, result(0);
	int tag(1); //标志,1;合法,0:数据或操作不合法
	cin >> a >> b >> c;
	switch (c) {
	case '+':result = a + b; break;
	case '-':result = a - b;break;
	case '*':result = a * b;break;
	case '/':
		if (fabs(b) < 1e-6) {
			cout << "divide 0" << endl;//注意0前面有空格
			tag = 0;
			break;
		}
		result = a / b;
		cout << result << endl;
		break;
	default:
		tag = 0;
		cout << "invalid operation" << endl;
		break;
	}
	if (tag) {
		cout << result << endl;
	}
	return 0;
}

第8章 作业1

9

在这里插入图片描述

#include<iostream>
using namespace std;

class Location {
private:
	int X, Y;
public:
	void init(int initX, int initY) {
		X = initX;
		Y = initY;
	}
	int GetX() {
		return X;
	}
	int GetY() {
		return Y;
	}
};
int main() {
	Location A1;
	A1.init(20, 90);
	Location rA1 = A1;
	cout << rA1.GetX() << rA1.GetY();
	return 0;
}

10

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;
class Clock {
public:
      Clock(int h,int m, int s);
	  void SetAlarm(int h,int m,int s);
	  void run();
	  void ShowTime(){
		  cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;
	  }
private:
	int hour;   //时
	int minute;  //分
	int second;  //秒

	int Ahour;   //时(闹钟)
	int Aminute;  //分(闹钟)
	int Asecond;   //秒(闹钟)
};
Clock::Clock(int h, int m,int s) { 
    hour = h;
	minute = m;
	second = s;
    if(hour<0 || hour>23)
    {
        hour = 0;
    }
    if(minute<0 || minute>59)
    {
        minute=0;
    }
    if(second<0 || second>59)
    {
        second=0;
    }
}

void Clock::SetAlarm(int h, int m, int s) {
		Ahour = h;
		Aminute = m;
		Asecond = s;
        if(Ahour>23){
        Ahour=0;
        }
        if(Aminute>59){
        Aminute=0;
        }
        if(Asecond>59){
        Asecond=0;
        }
	}
void Clock::run() {
		if (second < 59) second += 1;
		else if (minute < 59) {
			minute += 1;
			second = 0;
		}
		else if (hour < 23) {
			hour += 1;
		    minute = 0;
			second = 0;
		}
		else {
			hour = 0;
			minute = 0;
			second = 0;
		}
		if (hour == Ahour && minute == Aminute && second == Asecond) {
			cout << "Plink!plink!plink!..." << endl;
		}
	}

11

在这里插入图片描述

#include<iostream>
#include<cstring>
using namespace std;
int countt = 0;
class User {
public:
	User(char* name, char* pass);
	void AddUser(char* name, char* pass);
	int login(char* name, char* pass);
private:
	char *nameu[1000];
	char *passu[1000];
};
User::User(char* name, char* pass) {
	AddUser(name,  pass);
}
void User::AddUser(char* name, char* pass) {
	nameu[countt] = name;
	passu[countt] = pass;	
	countt++;

}
int User::login(char *name, char *pass) {
	for (int i = 0; i < countt; i++) {
		if (strcmp(nameu[i],name)==0 && strcmp(passu[i],pass)==0) {
			return i;
		}
	}
	return -1;
}
int main() {
	char name[10], name1[10], pass[10], pass1[10];
	cin >> name >> pass >> name1 >> pass1;
	User user("Liwei","liwei101");
	user.AddUser(name, pass);

	if (user.login(name1, pass1) >= 0)
	{
		cout << "Success Login!" << endl;
	}
	else {
		cout << "Login failed!" << endl;
	}
	return 0;
}

第8章 作业2

12

在这里插入图片描述

#include<cmath>
#include<iostream>
using namespace std;

class Ctriangle {
public:
		Ctriangle(float a, float b, float c);
	    float GetPerimeter();
	    float GetArea();
	    void display();
	    float la;
	    float lb;
	    float lc;
};
Ctriangle::Ctriangle(float a, float b, float c) {
	la = a;
	lb = b;
	lc = c;
}
float  Ctriangle::GetPerimeter() {
	return la + lb + lc;
}
float Ctriangle::GetArea() {
	float p = (la + lb + lc)/2;
	return   sqrt(p*(p-la)*(p-lb)*(p-lc));
}
void Ctriangle::display() {
	cout << "Ctriangle:a=" << la << ",b=" << lb << ",c=" << lc<<endl;
}
int main() {
	double a, b, c;
	cin >> a >> b >> c;
	Ctriangle T(a, b, c);
	T.display();
	cout << "Perimeter:" << T.GetPerimeter() << endl;
	cout << "Area:" << T.GetArea() << endl;
	return 0;
}

13

在这里插入图片描述

#include<cmath>
#include<iostream>
using namespace std;

class Point {
public:
	double la, lb;
	double Distance(const Point& B);
	Point(double a, double b);

};
 Point::Point(double a, double b) {
	la = a;
	lb = b;
}
double Point::Distance(const Point& B) {
	return sqrt((this->la - B.la) * (this->la - B.la) + (this->lb - B.lb) * (this->lb - B.lb));
}
int main() {
	double a, b, c, d;
	cin >> a >> b >> c >> d;
	Point A(a, b), B(c, d);
	cout << A.Distance(B) << endl;
	return 0;
}

14

在这里插入图片描述

#include<cmath>
#include<iostream>
using namespace std;
class Date {
public:
    Date(int y , int , int ) ;
    int days(int year, int month) ;
    void NewDay();
    void display()
    {
        cout << year << "-" << month << "-" << day << endl;
    }
private:
    int year; //年
    int month; //月
    int day;  // 日
};
Date::Date(int y , int m , int d ) {
    year = y;
    month = m;
    day = d;
    if (month < 1 || month>12) {
        cout << "Invalid month!" << endl;
        month = 1;
    }
    if (day == 31) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {}
        else
        {
            cout << "Invalid day!" << endl;
            day = 1;
        }
    }
    else if (day == 30) {
        if (!(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12||month==2)) {}
        else {
            cout << "Invalid day!" << endl;
            day = 1;
        }
    }
    else if (day == 28) {
        if (month == 2 && !((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) {}
        else {
            cout << "Invalid day!" << endl;
            day = 1;
        }
    }
    else if (day == 29) {
        if (month == 2 && ((year % 4 == 0 && year % 100 != 0)) || year % 400 == 0) {}
        else {
            cout << "Invalid day!" << endl;
            day = 1;
        }
    }
    else if(day<1||day>31){
        cout << "Invalid day!" << endl;
        day = 1;
    }
}
void Date::NewDay() {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        if (day < 31) {
            day += 1;
        }
        else {
            day = 1;
            if (month < 12) {
                month += 1;
            }
            else {
                month = 1;
                year += 1;
            }
        }
    }
    else if (month == 2) {
        if (day < 28) {
            day += 1;
        }
        else if (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) && day == 28){
        day += 1;}
        else {
            day = 1;
            month += 1;
        }
    }
    else {
        if (day < 30) {
            day += 1;
        }
        else {
            day = 1;
            month += 1;
        }
    }
}
int Date::days(int year, int month) {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31;
    else if (month == 2) {
        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 29;
        else return 28;
    }
    else { return 30; }
}

15

在这里插入图片描述

#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
float a1=0,a2=0,a3=0;
	int count=0;
class Student {
public:
	Student(char* name1, char* num1, int grade10,int grade11,int grade12);
	void display();
	float average1();
	float average2();
	float average3();
	char num[11];
	char name[13];
	int grade0,grade1,grade2;

};
Student::Student(char* name1, char* num1, int grade10, int grade11, int grade12) {
	strcpy(name,name1);
	strcpy(num, num1);
	grade0 = grade10;
	grade1 = grade11;
	grade2 = grade12;
	a1+=float(grade0);
	a2+=float(grade1);
	a3+=float(grade2);
	count++;
}
void Student::display() {
	cout << name << " " << num << " " << grade0 << " " << grade1 << " " << grade2 << endl;
}
float Student::average1() {
	return a1/count;
}
float Student::average2() {
	return a2/count;
}
float Student::average3() {
	return a3/count;
}
int main() {
	Student* stu1, * stu2, * stu3;
	char name1[10], name2[10], name3[10];
	char num1[12], num2[12], num3[12];
	int grade1[3], grade2[3], grade3[3];
	cin >> name1 >> num1 >> grade1[0] >> grade1[1] >> grade1[2];
	cin >> name2 >> num2 >> grade2[0] >> grade2[1] >> grade2[2];
	cin >> name3 >> num3 >> grade3[0] >> grade3[1] >> grade3[2];
	stu1 = new Student(name1, num1, grade1[0], grade1[1], grade1[2]);
	stu2 = new Student(name2, num2, grade2[0], grade2[1], grade2[2]);
	stu3 = new Student(name3, num3, grade3[0], grade3[1], grade3[2]);

	stu1->display();
	stu2->display();
	stu3->display();

	cout << "The average grade of course1:" << stu3->average1() << endl;
	cout << "The average grade of course2:" << stu3->average2()<< endl;
	cout << "The average grade of course3:" << stu3->average3()<< endl;
	return 0;
}

16

在这里插入图片描述

#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
class String {
private:
	char* mystr;    //字符串
	int len;    //字符串长度
public:
	String() {
		len = 0;
		mystr = NULL;
	}
	String(const char* p) {
		len = strlen(p);
		mystr = new char[len + 1];
		strcpy(mystr, p);
	}
	String(String& s) {
		len = s.len;
		if (len != 0)
		{
			mystr = new char[len + 1];
			strcpy(mystr, s.mystr);
		}
	}
	~String() {
		if (mystr != NULL)
		{
			delete[]mystr;
			mystr = NULL;
			len = 0;
		}
	}

	char* GetStr() { return mystr; }
	void ShowStr() { cout << mystr; }

	bool IsSubstring(const char* str);
	bool IsSubstring(const String& str);
	int str2num();
	void toUppercase();
};
bool String::IsSubstring(const char* str) {
	int tt = 1;
	int len1 = strlen(str);
	if (len1 <= len) {
		for (int i = 0; i < len; i++) {
			if (mystr[i] == str[0]) {
				for (int j = 1; j < len1; j++) {
					if (mystr[i + j] != str[j]) {
						tt = 0;
						break;
					}
				}
				if (tt == 1) return true;
			}
		}
		return false;
	}
	else return false;
}
bool String::IsSubstring(const  String & str) {
	int tt = 1;
	int len1 = str.len;
	if (len1<=len) {
		for (int i = 0; i < len; i++) {
			if (mystr[i] == str.mystr[0]) {
				for (int j = 1; j < len1; j++) {
					if (mystr[i + j] != str.mystr[j]) {
						tt = 0;
						break;
					}
				}
				if (tt == 1) return true;
			}
		}
		return false;
	}
	else return false;
}
int String::str2num() {
	int m=0;
	for (int i = 0; i < len; i++) {
		if (mystr[i] >= '0' && mystr[i] <= '9') {
			m = m + mystr[i] - '0';
			m = m * 10;
		}
	}
	return m / 10;
}
void String::toUppercase() {
	for (int i = 0; i < len; i++) {
		if (mystr[i] >= 'a' && mystr[i] <= 'z') {
			mystr[i] = mystr[i] - 32;
		}
	}
}

第9章 作业1

17

在这里插入图片描述

#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
class Point {
public:
	Point(float xx, float yy);
private:
	float x;
	float y;
};
Point::Point(float xx, float yy){
		x = xx;
		y = yy;
	}
class Rectangle  {
public:
	Rectangle(float xx, float yy, float w, float h);
	float Area();
private:
	float width;
	float high;
};
Rectangle::Rectangle(float xx, float yy, float w, float h){
	width = w;
	high = h;
}
float Rectangle::Area(){
	return width * high;
}
class Circle  {
public:
	Circle(float xx, float yy, float r);
	float Area() ;
private:
	float radius;
};
Circle::Circle(float xx, float yy, float r) {
		radius = r;
	}
float Circle::Area() {
		return radius * radius * 3.14;
	}

18

在这里插入图片描述

#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;

class Date {
public:
	Date(int y = 1996, int m = 1, int d = 1) {
		day = d;
		month = m;
		year = y;
		if (m > 12 || m < 1)
		{
			month = 1;
		}
		if (d > days(y, m))
		{
			cout << "Invalid day!" << endl;
			day = 1;
		}
	};
	int days(int y, int m) {
		if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) return 31;
		else if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) return 29;
		else if (m == 2) return 28;
	    else return 30;
	}

	void display() {
		cout << year << "-" << month << "-" << day << endl;
	}
private:
	int year;
	int month;
	int day;
};
 ostream & operator<<(ostream  & out,Date &dt) {
	dt.display();
	return out;
}
int main() {
	int y, m, d;
	cin >> y >> m >> d;
	Date dt(y, m, d);
	cout << dt;
	return 0;
}

19

在这里插入图片描述

#include <iostream>
using namespace std;
class Base {
public:
	virtual void fun() { cout << 1 << endl; }
};
class Derived :public Base {
public:
	void fun() { cout << 2 << endl; }
};
int main() {
	Base* p = new Derived;
	p->fun();
	delete p;
	return 0;
}

20

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;
class Set{
private:
	int n;
	int * ps; //集合元素
public:
	Set(){n = 0;ps =NULL;}
	Set(Set &s){
		n = s.n;
		if (n !=0)
		{
			ps= new  int[n+1];
			for (int i =1;i<=n;i++) //集合的下标从1开始,集合中不能有重复元素
				ps[i] = s.ps[i];
		}
	}
	~Set(){
		if (ps)
		{
			delete []ps;
			ps = NULL;
			n =0;
		}
	}
	void ShowElement()const{ //输出集合的元素
		int temp = 0;
		for(int i=1;i<n;i++) 
		{
			for(int j=i+1;j<n;j++) 
			{
				if(ps[i] > ps[j]) 
				{
					temp = ps[i];
					ps[i] = ps[j];
					ps[j] = temp;
				}
			}
		}
		cout<<"{";
		for(int i =1;i<n;i++)
			cout <<ps[i]<<",";
		if (IsEmpty())
			cout<<"}"<<endl;
		else cout<<ps[n]<<"}"<<endl;
	}
	bool IsEmpty()const{return n?false:true;} //判断集合是否为空
	int size(){return n;}
	bool IsElement(int e)const {
		for (int i =1;i<=n;i++)
			if (ps[i] ==e)
			return true;
		return  false;
	}
	bool operator <=(const Set &s)const;//this <= s判断当前集合是否包于集合s
	bool operator ==(const Set &s)const; //判断集合是否相等
	Set & operator +=(int e);    // 向集合中增元素e
	Set & operator -=(int e);    //删除集合中的元素e

	Set operator |(const Set &s)const;	 //集合并
	Set operator &(const Set &s)const;//集合交
	Set operator -(const Set &s)const; //集合差
};
bool Set::operator <=(const Set &s)const{
	   bool baohan=false;
	   if(n>s.n) return false;
	   else {
          for(int i=1;i<=n;i++){
          	if(s.IsElement(this->ps[i])){
          		return true;
			  }
			  else return false;
		  }
	   } 
	};//this <= s判断当前集合是否包于集合s
bool Set::operator ==(const Set &s)const{
	    if(n!=s.n) return false;
	    else {
          for(int i=1;i<=n;i++){
          	if(s.IsElement(this->ps[i])&&this->IsElement(s.ps[i])){
          		return true;
			  }
			  else return false;            
		  }
		}
	}; //判断集合是否相等
Set &Set::operator +=(int e){
           if(!(this->IsElement(e))){
           	  int *newps=new int [n+2];
           	  for(int i=1;i<=n;i++){
           	  	 newps[i]=this->ps[i];
				 }
			  n++;
			  newps[n]=e;
			  delete []this->ps;
			  this->ps=newps;
			  return *this;
		   }
		   else return *this;
};    // 向集合中增元素e
Set &Set::operator -=(int e){
          if(this->IsElement(e)){
          	int *newps=new int [n];
          	for(int i=1,j=1;i<=n-1;){
          		if(this->ps[j]!=e) newps[i++]=this->ps[j++];
          		else j++;
			  }
			  n--;
			  delete []this->ps;
			  this->ps=newps;
			  return *this;
		  }
		  else return *this;
	};    //删除集合中的元素e

Set Set::operator |(const Set &s)const{
        Set a;
        for(int i=1;i<=this->n;i++){
        	a +=this->ps[i];
		}
		for(int i=1;i<=s.n;i++){
			if(!this->IsElement(s.ps[i])){
				a +=s.ps[i];
			}
		}
		return a;
	}; 
	 //集合并
Set Set::operator &(const Set &s)const{
    Set a;
    for(int i=1;i<=s.n;i++){
    	if(this->IsElement(s.ps[i])){
    		a +=s.ps[i];
		}
	}
	return a;
};//集合交
Set Set::operator -(const Set &s)const{
    Set a;
	for(int i=1;i<=this->n;i++){
		a+=this->ps[i];
	} 
	for(int i=1;i<=s.n;i++){
		if(this->IsElement(s.ps[i])){
			a-=s.ps[i];
		}
	}
	return a;
}; //集合差

第9章 作业2

21

在这里插入图片描述

#include<iostream>
using namespace std;

class ShapeFactory
{
public:
	 ShapeFactory(){};
	 virtual ~ShapeFactory(){};

	 virtual float Circumstance(){return 0;};
	 ShapeFactory *Create(float a,float b,float c);
	 ShapeFactory *Create(float a,float b,float c,float d);
	 ShapeFactory *Create(float r);
};

class Triangle :public ShapeFactory{	
public:
	Triangle(float a0,float b0,float c0){
		a=a0;b=b0;c=c0;
	}
	float Circumstance(){
		return a+b+c;
	}
	private:
		float a,b,c;
};
class Quadrilateral:public ShapeFactory
{
public:
	Quadrilateral(float a0,float b0,float c0,float d0){
		a=a0;b=b0;c=c0;d=d0;
	}
	float Circumstance(){
		return a+b+c+d;
	}     	
private:
	float a,b,c,d;
};
class Circle:public ShapeFactory
{
public:
	Circle (float r0){
		r=r0;
	}
	float Circumstance(){
		return 2*3.14*r;
	}     	
private :
	float r;
};
ShapeFactory * ShapeFactory::Create(float a,float b,float c)
{
	
	ShapeFactory *p=new Triangle(a,b,c);
	return p;
}
ShapeFactory * ShapeFactory::Create(float a,float b,float c,float d)
{
	
	ShapeFactory *p=new Quadrilateral(a,b,c,d);
	return p;
}
ShapeFactory * ShapeFactory::Create(float r)
{
	
	ShapeFactory *p=new Circle(r);
	return p;
}

22

在这里插入图片描述

#include<iostream>
using namespace std;

class CNumberFactory
{
public:
	virtual void Add(int number) {};
	virtual void Sub(int number) {};
	virtual int GetValue() {return -1;};

	virtual void SetValue(int number) {};
	CNumberFactory *Create();
};
class CNumber :public CNumberFactory{
public:
	void SetValue(int number0){
		number =number0;
	}   
	int GetValue(){
		return number;
	}
	void Add(int num){
		number=number+num;
	}
	void Sub(int num){
		number=number-num;
	}
private:
	int number;
};
CNumberFactory *CNumberFactory::Create()
{
	return new CNumber();
}

23

在这里插入图片描述

#include<iostream>
#include<cstring>
using namespace std;

class Building{
public:
	Building(){
	}
	virtual void display(){
		cout<<"Name:"<<name<<endl;
		cout<<"Floor:"<<floor<<endl;
		cout<<"Room:"<<room<<endl;
		cout<<"Area:"<<area<<endl;
	}
	Building * createTeachBuilding(char *name,int floor,int room,int area,char *function);
	Building * creatDormBuilding(char *name,int floor,int room,int area,int peoples);
protected:
	char name[20];
	int floor;
	int room;
	float area;

};
class TeachBuilding :public Building{
public:
	TeachBuilding(char *name,int floor,int room,int area,char *function);
    void display(){
		cout<<"Name:"<<name<<endl;
		cout<<"Floor:"<<floor<<endl;
		cout<<"Room:"<<room<<endl;
		cout<<"Area:"<<area<<endl;
		cout<<"Function:"<<function<<endl;
	}	
protected:
 	char name[20];
	int floor;
	int room;
	float area;
	char function[20];  		
};
TeachBuilding::TeachBuilding(char *name,int floor,int room,int area,char *function){
		strcpy(this->name,name);
		this->floor = floor;
		this->room = room;
		this->area = area;
		strcpy(this->function,function);
	}
class DormBuilding :public Building{
public:
	DormBuilding(char *name,int floor,int room,int area,int peoples);
    void display(){
		cout<<"Name:"<<name<<endl;
		cout<<"Floor:"<<floor<<endl;
		cout<<"Room:"<<room<<endl;
		cout<<"Area:"<<area<<endl;
		cout<<"Peoples:"<<peoples<<endl;
	}	
protected:
 	char name[20];
	int floor;
	int room;
	float area;
	int peoples;  		
};
DormBuilding::DormBuilding(char *name,int floor,int room,int area,int peoples){
		strcpy(this->name,name);
		this->floor = floor;
		this->room = room;
		this->area = area;
		this->peoples=peoples;
	}
Building* Building::createTeachBuilding(char *name,int floor,int room,int area,char *function){
	return  new TeachBuilding(name,floor,room,area,function);
}

Building * Building::creatDormBuilding(char *name,int floor,int room,int area,int peoples){
	return new DormBuilding(name,floor,room,area,peoples);
}

24

在这里插入图片描述

#include<iostream>
#include<cstring>
using namespace std;

class Table{
public:

	float GetHigh(){
		return high;
	}
	float high;
};
class Circle{
public:

	float GetArea(){
		return 3.14*radius*radius;
	}
	float radius;
};
class RoundTable:public Table,public Circle{
public:
	RoundTable (float radius0,float high0,char *color0 );
	char* GetColor(){
		return color;
	}
	char color[20];
};
RoundTable::RoundTable (float radius0,float high0,char *color0 ){
		radius=radius0;
		high=high0;
		strcpy(color,color0);
	}
int main(){
    float radius,high;
	char color[20];
	cin>>radius>>high>>color;
	
	RoundTable RT(radius,high,color);
	cout<<"Area:"<<RT.GetArea()<<endl;
	cout<<"High:"<<RT.GetHigh()<<endl;
	cout<<"Color:"<<RT.GetColor()<<endl;
	return 0;
}

25

在这里插入图片描述

#include<iostream>
#include<cstring>
using namespace std;
class Clock{
public:
	 Clock(int h,int m,int s){
	 hour =(h>23? 0:h);
	 minute = (m>59?0:m);
	 second = (s>59?0:s);
}
	virtual void run(){
		second = second+1;
		if (second>59)
		{
			second =0;
			minute+=1;
		}
		if (minute>59)
		{
			minute =0;
			hour+=1;
		}
		if (hour>23)
		{
			hour =0;
		}
	}
	virtual void showTime(){
		cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;
	}
	int getHour(){return hour;}
	int getMinute(){return minute;}
	int getSecond(){return second;}
	
Clock * createNewClock(int h,int m,int s);
private:
	int hour;
	int minute;
	int second;
};
class NewClock:public Clock{
public:
    NewClock(int h, int m, int s) : Clock(h, m, s){
	 hour=( (h>23||h<0)?0:h);
	 minute = ((m>59||m<0)?0:m);
	 second = ((s>59||s<0)?0:s);
	}
    void run(){
		second = second+1;
		if (second>59)
		{
			second =0;
			minute+=1;
		}
		if (minute>59)
		{
			minute =0;
			hour+=1;
		}
		if (hour>23)
		{
			hour =0;
		}
	}	
	void showTime(){
		if(hour<12)	cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<"AM"<<endl;
		else 	cout<<"Now:"<<hour-12<<":"<<minute<<":"<<second<<"PM"<<endl;
	}	
private:
	int hour,minute,second;
};

Clock* Clock::createNewClock(int h,int m,int s){
	return new NewClock(h,m,s);
}

26

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
class Clock{
public:
	Clock(int h,int m,int s){
	 hour =(h>23? 0:h);
	 minute = (m>59?0:m);
	 second = (s>59?0:s);
	}
	virtual void run(){
		second = second+1;
		if (second>59)
		{
			second =0;
			minute+=1;
		}
		if (minute>59)
		{
			minute =0;
			hour+=1;
		}
		if (hour>23)
		{
			hour =0;
		}
	}
	virtual void showTime(){
		cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;
	}
	int getHour(){return hour;}
	int getMinute(){return minute;}
	int getSecond(){return second;}
	
Clock * createClockWithDate(int h,int m,int s,int year,int month,int day);
protected:
	int hour;
	int minute;
	int second;
};

class Date{
public:
	Date(int y,int m,int d){
		day =d;
		year =y;
		month =m;
		if (month>12||month<1)
		{
			month=1;
		}
		if(day>days(year,month)){
			day = 1;
		}
	};
	int days(int year,int month){
		switch(month){
            case 2:{
            	if((year%4==0 && year%100!=0)||year %400==0){
            		return 29;
				}
				else return 28;
				break;
			}
			default :{
				return a[month-1];
				break;
			}
		}
	}
	void NewDay(){
		day++;
		switch(month){
			case 2:{
				if((year%4==0 && year%100!=0)||year%400==0) {
					if(day>29) {
						day=1;
						month++;
					}
				}
				else {
					if(day>28){
						day=1;month++;
					}
				}			
				break;
			}
			default:{
				if(day>31){
					day=1;month++;
				}
				else if(day>30){
					if(a[month-1]!=31){
					day=1;month++;}
				}
				break;
			}
		}
		if(month>12) {
			month=1;
			year++;
		}		
	}
	void display(){
		cout<<year<<"-"<<month<<"-"<<day<<endl;
	}
protected:
	int year;
	int month;
	int day;
};
class ClockWithDate:public Clock,public Date{
public:
	ClockWithDate(int h,int m,int s,int y,int mo,int d):Clock(h,m,s),Date(y,mo,d){
	 hour =(h>23? 0:h);
	 minute = (m>59?0:m);
	 second = (s>59?0:s);
	day =d;
	year =y;
	month =mo;
	if (month>12||month<1)
	{
		month=1;
	}
	if(day>days(year,month)){
		day = 1;
	}		
	}
void showTime(){
		cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;
		cout<<year<<"-"<<month<<"-"<<day<<endl;
	}	
void run(){
		second = second+1;
		if (second>59)
		{
			second =0;
			minute+=1;
		}
		if (minute>59)
		{
			minute =0;
			hour+=1;
		}
		if (hour>23)
		{
			hour =0;
			day++;
		}	
		switch(month){
			case 2:{
				if((year%4==0 && year%100!=0)||year%400==0) {
					if(day>29) {
						day=1;
						month++;
					}
				}
				else {
					if(day>28){
						day=1;month++;
					}
				}			
				break;
			}
			default:{
				if(day>31){
					day=1;month++;
				}
				else if(day>30){
					if(a[month-1]!=31){
					day=1;month++;}
				}
				break;
			}
		}
		if(month>12) {
			month=1;
			year++;
		}		
}
protected:
	int year;
	int month;
	int day;
	int hour;
	int minute;
	int second;		
};
Clock* Clock::createClockWithDate(int h,int m,int s,int year,int month,int day){
	return new ClockWithDate(h,m,s,year,month,day);
}

27

第10章 作业1

31

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

int get_sum(int *a,int n){ 	int sum=0; 	for(int i=0;i<n;i++){ 		sum+=a[i]; 	} 	return sum; } double get_sum(double *a,int n){ 	double sum; 	for(int i=0;i<n;i++){ 		sum+=a[i]; 	} 	return sum;	 }

int main()
{
	int arr_int[6] = { 1, 2, 3, 4, 5, 6 };
	double arr_double[6] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
	cout << get_sum(arr_int, 6) << endl;
	cout << get_sum(arr_double, 6) << endl;
	return 0;
}

32

在这里插入图片描述

#include <iostream>
using namespace std;

template<typename ntype> class node { public:     void set_value(ntype a) {         this->data = a;     }      void append(node* c) {         this->next = c;     }     ntype get_value() {         return this->data;     }     node* get_prev() {         return this->prev;     }      node* get_next() {         return this->next;     }  private:     ntype data;     node* next;     node* prev; };

void test1()
{
	node<int> *ptr;
	node<int> node1,node2,node3;
	node1.set_value(1);
	node2.set_value(2);
	node3.set_value(3);
	node1.append(&node2);
	node2.append(&node3);

	for(ptr=&node1 ; ; ptr=ptr->get_next())
	{
		cout << ptr->get_value() << " ";
		if(ptr->get_next()==NULL) break;
	}
}

void test2()
{
	node<float> *ptr;
	node<float> node1,node2,node3;
	node1.set_value(1.1);
	node2.set_value(2.2);
	node3.set_value(3.3);
	node1.append(&node2);
	node2.append(&node3);

	for(ptr=&node1 ; ; ptr=ptr->get_next())
	{
		cout << ptr->get_value() << " ";
		if(ptr->get_next()==NULL) break;
	}
}

void test3()
{
	node<char> *ptr;
	node<char> node1,node2,node3;
	node1.set_value('a');
	node2.set_value('b');
	node3.set_value('c');
	node1.append(&node2);
	node2.append(&node3);

	for(ptr=&node1 ; ; ptr=ptr->get_next())
	{
		cout << ptr->get_value() << " ";
		if(ptr->get_next()==NULL) break;
	}
}

int main( )
{
	int type;
	cin >> type;
	
	switch(type)
	{
		case 1:
		test1();
		break;
		
		case 2:
		test2();
		break;

		case 3:
		test3();
		break;
	}
    return 0;
}

33

在这里插入图片描述

#include <iostream>
using namespace std;

class A{ public: 	A(int x); 	int a,err; }; A::A(int x){ 	err=x;  } class B{ public: 	B(int x); 	int b,err; }; B::B(int x){ 	err=x;  }

int main()
{
	int index, dividend, divisor, result;
	int array[10]={1,2,3,4,5,6,7,8,9,10};

	try
	{
		cin >> index >> divisor;

		if( index < 0 || index>9)
			throw A(index);	

		if(divisor==0)
			throw B(divisor);

		dividend = array[index];
		result = dividend / divisor;
		cout << result << endl;
	}
	catch(A a)
	{
		cout << a.err << " out of bound" << endl;
	}
	catch(B b)
	{
		cout << "divide by "<< b.err << endl;
	}
	
	return 0;
}

34

在这里插入图片描述

#include <iostream>
using namespace std;

namespace ns1
{
	   	int x=1,y=2;     namespace ns2{     char x='a',y='b';     int add_one(int z){    	  return z+1;    }    }
}

int main()
{
	using namespace ns1::ns2;
	cout << ns1::x << "," << ns1::y << endl;
	cout << x << "," << y << endl;
	cout << add_one(10) << endl;
    return 0;
}

第10章 作业2

35

在这里插入图片描述

#include <iostream>
using namespace std;

int get_max(int *a,int n){     int max=0; 	for(int i=0;i<n;i++){ 		if(a[i]>max)max=a[i]; 	} 	return max; } double get_max(double *a,int n){     double max=0; 	for(int i=0;i<n;i++){ 		if(a[i]>max)max=a[i]; 	} 	return max; }

int main()
{
	int arr_int[6] = { 1, 2, 3, 4, 5, 6 };
	double arr_double[6] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
	cout << get_max(arr_int, 6) << endl;
	cout << get_max(arr_double, 6) << endl;
	return 0;
}

36

在这里插入图片描述

#include <iostream>
using namespace std;

int get_min(int *a,int n){ 	int min=10; 	for(int i=0;i<n;i++){ 		if(a[i]<min)min=a[i]; 	} 	return min; } double get_min(double *a,int n){ 	double min=10; 	for(int i=0;i<n;i++){ 		if(a[i]<min)min=a[i]; 	} 	return min; }

int main()
{
	int arr_int[6] = { 1, 2, 3, 4, 5, 6 };
	double arr_double[6] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
	cout << get_min(arr_int, 6) << endl;
	cout << get_min(arr_double, 6) << endl;
	return 0;
}

37

在这里插入图片描述

#include <iostream>
using namespace std;

template<typename ntype> class node { public:     void set_value(ntype a) {         this->data = a;     }      void insert(node* c) {         this->prev = c;         c->next = this;     }     ntype get_value() {         return this->data;     }     node* get_prev() {         return this->prev;     }      node* get_next() {         return this->next;     }  private:     ntype data;     node* next;     node* prev; };

void test1()
{
	node<int> *ptr;
	node<int> node1,node2,node3;
	node1.set_value(1);
	node2.set_value(2);
	node3.set_value(3);
	node3.insert(&node2);
	node2.insert(&node1);

	for(ptr=&node1 ; ; ptr=ptr->get_next())
	{
		cout << ptr->get_value() << " ";
		if(ptr->get_next()==NULL) break;
	}
}

void test2()
{
	node<float> *ptr;
	node<float> node1,node2,node3;
	node1.set_value(1.1);
	node2.set_value(2.2);
	node3.set_value(3.3);
	node3.insert(&node2);
	node2.insert(&node1);

	for(ptr=&node1 ; ; ptr=ptr->get_next())
	{
		cout << ptr->get_value() << " ";
		if(ptr->get_next()==NULL) break;
	}
}

void test3()
{
	node<char> *ptr;
	node<char> node1,node2,node3;
	node1.set_value('a');
	node2.set_value('b');
	node3.set_value('c');
	node3.insert(&node2);
	node2.insert(&node1);

	for(ptr=&node1 ; ; ptr=ptr->get_next())
	{
		cout << ptr->get_value() << " ";
		if(ptr->get_next()==NULL) break;
	}
}

int main( )
{
	int type;
	cin >> type;
	
	switch(type)
	{
		case 1:
		test1();
		break;
		
		case 2:
		test2();
		break;

		case 3:
		test3();
		break;
	}
    return 0;
}

38

在这里插入图片描述

#include <iostream>
using namespace std;

int main()
{
	int index, dividend, divisor, result;
	int array[10]={1,2,3,4,5,6,7,8,9,10};

	try
	{
		cin >> index >> divisor;

		if( index < 0 || index>9)
			throw index;	

		if(divisor==0)
			throw divisor;

		dividend = array[index];
		result = dividend / divisor;
		cout << result << endl;
	}
	  catch(int){   	if (divisor) 	  cout<<index<<" out of bound"<<endl; 	else   	cout<<"divide by "<< 0<<endl;    }

	return 0;
}

39

在这里插入图片描述

#include <iostream>
using namespace std;

namespace nsA
{
	  int x=1,y=2;   namespace nsB{   	char x='a',y='b';   	int add_something(int z){   		return 2+z; 	  }   }   int add_something(int z){   	return 1+z;   }
}

int main()
{
	using namespace nsA;
	cout << x << "," << y << endl;
	cout << nsA::nsB::x << "," << nsA::nsB::y << endl;
	cout << add_something(10) << endl;
	cout << nsA::nsB::add_something(10) << endl;
    return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值