admin管理员组文章数量:1580420
67
#include<iostream>
#include<cstdio>
#include"ShapeFactory.h"
#define PI 3.14
using namespace std;
class Triangle:public ShapeFactory {
private:
float a, b, c;
public:
Triangle(float aa, float bb, float cc) {
a = aa;
b = bb;
c = cc;
}
float Circumstance() {
return a + b + c;
}
};
class Quadrilateral :public ShapeFactory {
private:
float a, b, c, d;
public:
Quadrilateral(float aa,float bb,float cc,float dd){
a = aa;
b = bb;
c = cc;
d = dd;
}
float Circumstance() {
return a + b + c + d;
}
};
class Circle :public ShapeFactory {
private:
float radius;
public:
Circle(float r) {
radius = r;
}
float Circumstance() {
return PI*radius*2;
}
};
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;
}
69
#include <iostream>
#include"CNumberFactory.h"
using namespace std;
class CNumber:public CNumberFactory
{
private:
int num;
public:
void SetValue(int number) { num = number; }
int GetValue() { return num; };
void Add(int number) { num += number; };
void Sub(int number) { num -= number; };
};
CNumberFactory* CNumberFactory::Create()
{
return new CNumber();
}
151
#include<iostream>
#include"Building.h"
#include<cstring>
using namespace std;
class TeachBuilding: public Building
{
public:
TeachBuilding(char* name, int floor, int room, int area, char* function) :Building(name, floor, room, area)
{
int i = strlen(function);
strcpy_s(this->function, ++i, function);
}
void display() {
cout << "Name:" << name << endl;
cout << "Floor:" << floor << endl;
cout << "Room:" << room << endl;
cout << "Area:" << area << endl;
cout << "Function:" << function << endl;
};
private:
char function[20];
};
class DormBuilding:public Building
{
public:
DormBuilding(char* name, int floor, int room, int area, int peoples) :Building(name, floor, room, area)
{
this->peoples = peoples;
};
void display() {
cout << "Name:" << name << endl;
cout << "Floor:" << floor << endl;
cout << "Room:" << room << endl;
cout << "Area:" << area << endl;
cout << "Peoples:" << peoples << endl;
}
private:
int 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);
}
152
#include <iostream>
using namespace std;
#define Pi 3.14
class Table
{
private:
float high;
public:
float GetHigh() { return high; }
Table(float h) { high = h; }
};
class Circle
{
private:
float radius;
public:
float GetArea() { return Pi * radius * radius; }
Circle(float r) { radius = r; }
};
class RoundTable:public Table,public Circle
{
private:
char color[20];
public:
char* GetColor() { return color; }
RoundTable(float r, float h, char* c) :Table(h), Circle(r) { strcpy(color, c); }
};
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;
}
154
#include"Clock.h"
class NewClock :public Clock {
int hour, minute, second;
public:
NewClock(int h, int m, int s) : Clock(h, m, s) {
hour = getHour();
minute = getMinute();
second = getSecond();
}
void showTime() {
if (hour < 12)
{
cout << "Now:" << hour << ":" << minute << ":" << second << "AM" << endl;
}
else
{
cout << "Now:" << hour - 12 << ":" << minute << ":" << second << "PM" << 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;
}
}
};
Clock* Clock::createNewClock(int h, int m, int s) {
return new NewClock(h, m, s);
}
167
#include<iostream>
#include"Shape.h"
using namespace std;
class Rectangle:public Shape
{
private:
double high, width;
public:
Rectangle(double l, double w) { high = l; width = w; };
double GetPerimeter() { return 2 * (high + width); }
double GetArea() { return high * width; }
};
class Circle:public Shape
{
private:
double radius;
public:
Circle(double r) { radius = r; };
double GetPerimeter() { return 2 * 3.14*radius; }
double GetArea() { return 3.14*radius*radius; }
};
Shape* Shape::createRectangle(double l, double w) { return new Rectangle(l, w); }
Shape* Shape::createCircle(double r) { return new Circle(r); }
155
#include"ClockAndDate.h"
int Date::days(int year, int month)
{
int monthsInLeapYear[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
int monthsInNormalYear[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return monthsInLeapYear[month - 1];
else
return monthsInNormalYear[month - 1];
}
void Date::NewDay()
{
int m_d[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
day++;
switch (month)
{
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (day > 29)
{
day = 1; month = 3;
}
}
else
{
if (day > 28)
{
day = 1; month = 3;
}
}
break;
default:
if (day > m_d[month - 1])
{
day = 1; month++;
}
break;
}
if (month > 12)
{
month = 1; year++;
}
}
class ClockWithDate :public Clock, public Date
{
public:
ClockWithDate(int h, int m, int s, int year, int month, int day):Clock(h,m,s),Date(year,month,day){}
virtual void showTime()
{
cout << "Now:" << hour << ":" << minute << ":" << second << endl << year << "-" << month << "-" << day << endl;
}
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;
NewDay();
}
}
};
Clock* Clock::createClockWithDate(int h, int m, int s, int year, int month, int day) {
return new ClockWithDate(h, m, s, year, month, day);
}
156
#include<cstdlib>
#include <iostream>
#include<string.h>
using namespace std;
class String{
protected:
char *mystr;
int len;
public:
String(const char *p){
len = strlen(p);
mystr = new char[len+1];
strcpy(mystr,p);
}
~String(){
if (mystr !=NULL)
{
delete []mystr;
mystr = NULL;
len = 0;
}
}
void showStr(){cout <<mystr;}
char * GetStr(){return mystr;}
virtual bool IsSubString(const char *str){
int i,j;
for (i =0;i<len;i++)
{
int k = i;
for (j =0;str[j] !='\0';j++,k++)
{
if (str[j]!= mystr[k]) break;
}
if(str[j] =='\0') return true;
}
return false;
}
};
class EditString:public String{
public:
EditString(const char *p):String(p){}
int IsSubString(int start, const char *str);
void EditChar(char s, char d);
void EditSub(char * subs,char *subd);
void DeleteChar(char ch);
void DeleteSub(char * sub);
};
int EditString::IsSubString(int start, const char *str) {
int len = strlen(mystr);
int i, j;
for (i = start; i<len; i++)
{
int k = i;
for (j = 0; str[j] != '\0'; j++, k++)
{
if (str[j] != mystr[k])
break;
}
if (str[j] == '\0')
return i;
}
return -1;
}
void EditString::EditChar(char s, char d) {
for (int i = 0; mystr[i] != NULL; i++)
{
if (mystr[i] == s)
mystr[i] = d;
}
}
void EditString::EditSub(char * subs,char *subd)
{
int start=0;
char *temp = new char[len+1];
while(start<len)
{
start=IsSubString(start,subs);
if(start!=-1){
strncpy(temp,mystr,start);
temp[start] = '\0';
strcat(strcat(temp,subd),&mystr[start+strlen(subs)]);
strcpy(mystr,temp);
len = strlen(mystr);
}
else{
break;
}
}
delete []temp;
}
void EditString::DeleteChar(char ch) {
int k = 0, len = strlen(mystr);
char s[1000];
for (int i = 0; i < len; ++i) {
if (mystr[i] == ch)continue;
s[k++] = mystr[i];
}
for (int i = 0; i <= k; ++i)
mystr[i] = s[i];
}
void EditString::DeleteSub(char *sub) {
int ss = 0;
char s[1000];
int start = 0, lens = strlen(sub), len = strlen(mystr);
while (1) {
if (start >= len)
break;
int k = IsSubString(start, sub);
if (k == -1)
break;
for (int i = start; i < k; ++i)
s[ss++] = mystr[i];
start = k + lens;
}
for (int i = start; i < len; ++i)
s[ss++] = mystr[i];
for (int i = 0; i <= strlen(s); ++i)
mystr[i] = s[i];
}
void fun(char *s,char str[][1000]){
int index = 0;
char *p = s,*q = s;
while(*p)
{
if(*p==','){
*p = '\0';
strcpy(str[index++],q);
q = p+1;
}
p++;
}
strcpy(str[index++],q);
}int main()
{
char s[1000];
char str[999][1000]={0};
gets(s);
fun(s,str);
EditString es1(str[0]);
cout<<es1.IsSubString(atoi(str[1]),str[2])<<",";
EditString es2(str[0]);
es2.EditChar(str[3][0],str[4][0]);
es2.showStr();
cout<<",";
EditString es3(str[0]);
es3.EditSub(str[5],str[6]);
es3.showStr();
cout<<",";
EditString es4(str[0]);
es4.DeleteChar(str[7][0]);
es4.showStr();
cout<<",";
EditString es5(str[0]);
es5.DeleteSub(str[8]);
es5.showStr();
cout<<endl;
return 0;
}
164
#include"Location.h"
#include<iostream>
using namespace std;
Location& Location:: operator +(Location &offset){
x+=offset.getX();
y+=offset.getY();
return *this;
}
Location& Location:: operator -(Location &offset){
x-=offset.getX();
y-=offset.getY();
return *this;
}
int main(){
Location L1(1,1),L2(2,2);
L1 =L1+L2 ;//L1为(3,3)
cout<<L1.getX()<<endl;
cout<<L1.getY()<<endl;
system("pause");
return 0;
}
166
#include"Vehicle.h"
class Car :public Vehicle
{
public:
Car() { cout << "Car constructor..." << endl; }
virtual void display()const
{
cout << "This is a car!"<<endl;
}
~Car() { cout << "Car destructor..." << endl; }
};
class Truck :public Vehicle
{
public:
Truck() { cout << "Truck constructor..." << endl; }
virtual void display()const
{
cout << "This is a truck!"<<endl;
}
~Truck() { cout << "Truck destructor..." << endl; }
};
class Boat :public Vehicle
{
public:
Boat() { cout << "Boat constructor..." << endl; }
virtual void display()const
{
cout << "This is a boat!"<<endl;
}
~Boat() { cout << "Boat destructor..." << endl; }
};
Vehicle* Vehicle::createCar() { return new Car(); }
Vehicle* Vehicle::createTruck() { return new Truck(); }
Vehicle* Vehicle::createBoat() { return new Boat(); }
本文标签: 第九章
版权声明:本文标题:C++码图第九章2 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1726616248a1078237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论