c++第五次作业

1、

 1 #ifndef MACHINEPETS_H
 2 #define MACHINEPETS_H
 3 
 4 #include<string>
 5 using namespace std;
 6 
 7 class MachinePets{
 8     private:
 9         string nickname;
10     public:
11         MachinePets(const string s);
12         virtual string talk()=0;
13         string getNickname()const;
14 };
15 
16 #endif
MachinePets.h
1 #include"MachinePets.h"
2 #include<string>
3 using namespace std;
4 
5 MachinePets::MachinePets(const string s):nickname(s){
6 }
7 string MachinePets::getNickname()const{
8     return nickname;
9 }
MachinePets.cpp
 1 #ifndef PETCATS_H
 2 #define PETCATS_H
 3 
 4 #include<string>
 5 using namespace std;
 6 
 7 #include"MachinePets.h"
 8 
 9 class PetCats:public MachinePets{
10     public:
11         PetCats(const string s);
12         string talk();
13 };
14 
15 #endif
PetCats.h
1 #include"PetCats.h"
2 #include<string>
3 using namespace std;
4 
5 PetCats::PetCats(const string s):MachinePets(s){
6 }
7 string PetCats::talk(){
8     return "nia~~";
9 }
PetCats.cpp
 1 #ifndef PETDOGS_H
 2 #define PETDOGS_H
 3 
 4 #include<string>
 5 using namespace std;
 6 
 7 #include"MachinePets.h"
 8 
 9 class PetDogs:public MachinePets{
10     public:
11         PetDogs(const string s);
12         string talk();
13 };
14 
15 #endif
PetDogs.h
#include"PetDogs.h"
#include<string>
using namespace std;

PetDogs::PetDogs(const string s):MachinePets(s){
}
string PetDogs::talk(){
    return "woof~~";
}
PetDogs.cpp
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 #include"MachinePets.h"
 6 #include"PetCats.h"
 7 #include"PetDogs.h"
 8 
 9 void play(MachinePets *p){
10     cout<<p->getNickname()<<" says "<<p->talk()<<endl;
11 }
12 int main(){
13     PetCats cat("miku");
14     PetDogs dog("Da Huang");
15     play(&cat);
16     play(&dog);
17     return 0;
18 }
main.cpp

 1296

 

2、oj1296 分形宇宙

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 #define MAX 1000
 5 char maps[MAX][MAX];
 6 void print(int n,int x,int y) {
 7     if (n==1) {
 8         maps[x][y]='X';
 9     }
10     else {
11         int k=pow(3,n-2);
12         print(n-1,x,y);
13         print(n-1,x+2*k,y);
14         print(n-1,x,y+2*k);
15         print(n-1,x+k,y+k);
16         print(n-1,x+2*k,y+2*k);
17     }
18 }
19 int main() {
20     int n;
21     while(cin>>n){
22         int size=pow(3,n-1);
23         for (int i=0;i<size;i++) {
24             for (int j=0;j<size;j++) {
25                 maps[i][j]=' ';
26             }
27         }
28         print(n,0,0);
29         for (int i=0;i<size;i++) {
30             for (int j=0;j<size;j++) {
31                 cout<<maps[i][j];
32             }
33             cout<<endl;
34         }
35         cout<<"-"<<endl;
36     }
37     return 0;
38 }
oj1296

 

posted @ 2019-05-25 12:41  淳简拉基茨德  阅读(265)  评论(3)    收藏  举报