C++(实验三)

Part 1 画布小球试验

程序源码

 1 #include <iostream>
 2 #include "canvas.h"
 3 #include "ball.h"
 4 
 5 int main() {
 6     Canvas canvas; //创建默认画布,黑底绿色
 7     
 8     Ball ball1(10,10);
 9     system("pause");
10     
11     ball1.left(5);
12     system("pause");
13     
14     ball1.up(20);
15     system("pause");
16 
17     canvas.changeCanvasFg("E"); // 更新画布前景色
18     system("pause");
19     
20     canvas.changeCanvasBg("D"); // 更新画布背景色
21     system("pause");
22 
23     return 0;
24 }
main.cpp
 1 #ifndef CANVAS_H
 2 #define CANVAS_H
 3 
 4 #include <string>
 5 using std::string;
 6 
 7 class Canvas {
 8     public:
 9         Canvas(string bg0="0", string fg0="A");
10         void changeCanvasBg(string bg0);
11         void changeCanvasFg(string fg0);
12         void changeCanvasColor(string bg0, string fg0); 
13     private:
14         string bg;   // background color
15         string fg;   // foreground color 
16 };
17 
18 #endif
canvas.h
 1 #include "canvas.h"
 2 #include <cstdlib>
 3 Canvas::Canvas(string bg0, string fg0):bg(bg0), fg(fg0) {
 4     string color = "color ";
 5     color += bg0;
 6     color += fg0;
 7     system(color.c_str());
 8 }
 9 void Canvas::changeCanvasBg(string bg0) {
10     bg = bg0; // 更新画布背景色
11     string color = "color ";
12     color += bg;
13     color += fg;
14     system(color.c_str());
15 }
16 void Canvas::changeCanvasFg(string fg0) {
17     fg = fg0; // 更新画布前景色
18     string color = "color ";
19     color += bg;
20     color += fg;
21     system(color.c_str());
22 }
23 void Canvas::changeCanvasColor(string bg0, string fg0){
24     bg = bg0; // 更新画布背景色
25     fg = fg0; // 更新画布前景色
26     string color = "color ";
27     color += bg;
28     color += fg;
29     system(color.c_str());
30 }
canvas.cpp
 1 #ifndef BALL_H
 2 #define BALL_H
 3 
 4 class Ball {
 5 public:
 6     Ball(int x0=0, int y0=0);  // 在坐标(x,y)处构造一个小球(小球用字符O表示)
 7     void left(int step=1);     // 左移step
 8     void right(int step=1);    // 右移step
 9     void up(int step=1);       // 上移step
10     void down(int step=1);     // 下移step
11 private:
12     int x;    // x坐标
13     int y;    // y坐标
14    
15 };
16 #endif
ball.h
 1 #include "ball.h"
 2 #include <iostream>
 3 #include <cstdlib> // 因为使用了system("cls"); 所以需要包含这个头文件
 4 
 5 using std::cout;
 6 using std::endl;
 7 
 8 const int SIZE_X=50; // 小球x轴移动范围0~SIZE_X
 9 const int SIZE_Y=50; // 小球y轴移动范围0~SIZE_Y
10 
11 
12 Ball::Ball(int x0, int y0):x(x0), y(y0) { // 打印y0-1行空行
13     for(int line=1; line <= y0-1; line++)
14         cout << endl;
15     // 打印x0-1个空格
16     for(int col=1; col <= x0-1; col++)
17         cout << " "; // 打印小球
18     cout << "O" << endl;
19 }
20 
21 void Ball::left(int step) {
22     x = x-step;
23     if(x <= 0)
24         x=0;
25     // 清屏 system("cls");
26     // 打印y-1行空行
27     for(int line=1; line <= y-1; line++)
28         cout << endl;
29     // 打印x-1个空格
30     for(int col=1; col <= x-1; col++)
31         cout << " "; // 打印小球
32     cout << "O" << endl;
33 }
34 
35 void Ball::right(int step) {
36     x = x+step;
37     if(x >= SIZE_X)
38         x=SIZE_X; // 清屏
39     system("cls");
40     // 打印y-1行空行
41     for(int line=1; line <= y-1; line++)
42         cout << endl;
43     // 打印x-1个空格
44     for(int col=1; col <= x-1; col++)
45         cout << " "; // 打印小球
46     cout << "O" << endl;
47 }
48 
49 void Ball::up(int step) {
50     y = y-step;
51     if(y <= 0)
52         y=0; // 清屏
53     system("cls");
54     // 打印y-1行空行
55     for(int line=1; line <= y-1; line++)
56         cout << endl;
57     // 打印x-1个空格
58     for(int col=1; col <= x-1; col++)
59         cout << " "; // 打印小球
60     cout << "O" << endl;
61 }
62 
63 void Ball::down(int step) {
64     y = y+step;
65     if(y >= SIZE_Y)
66         y = SIZE_Y; // 清屏
67     system("cls");
68     // 打印y-1行空行
69     for(int line=1; line <= y-1; line++)
70         cout << endl;
71     // 打印x-1个空格
72     for(int col=1; col <= x-1; col++)
73         cout << " ";
74     // 打印小球
75     cout << "O" << endl;
76 }
ball.cpp

运行截图

PS:system()在Xcode中运行并不是动画,而且不会改变前景色和背景色,也没有清屏,如图,还在研究中

Part 2 GRAGH

程序源码

 1 #include <iostream>
 2 #include "graph.h"
 3 using namespace std;
 4 
 5 int main() {
 6     
 7     Graph graph1('*',5);
 8     graph1.draw();
 9     
10     system("pause");
11     system("cls");
12     
13     Graph graph2('$',7);
14     graph2.draw();
15     
16     return 0; 
17 } 
main.cpp
 1 #ifndef GRAPH_H
 2 #define GRAPH_H
 3 
 4 class Graph {
 5     public:
 6         Graph(char ch, int n);
 7         void draw(); 
 8     private:
 9         char symbol;
10         int size;
11 };
12 
13 
14 #endif
graph.h
 1 // 类graph的实现
 2 #include "graph.h" 
 3 #include <iostream>
 4 using namespace std;
 5 
 6 // 带参数的构造函数的实现
 7 Graph::Graph(char ch, int n): symbol(ch), size(n) {
 8 }
 9 
10 // 成员函数draw()的实现
11 // 功能:绘制size行,显示字符为symbol的指定图形样式
12 void Graph::draw() {
13     int i,j,k;
14     for(i=1;i<=size;i++)            //利用行数控制
15     {   for(j=size-i;j>=0;j--)      //控制空格的输入
16             cout<<" ";
17         for(k=1;k<=2*i-1;k++)       //控制graph的输入
18             cout<<symbol;
19         cout<<endl;
20     }
21 }
graph.cpp

 

运行截图

PS:依旧和上个程序一样system()的问题

 

Part 3 Fraction 类

程序源码

 1 #include <iostream>
 2 #include "Fraction.hpp"
 3 using namespace std;
 4 
 5 int main(){
 6     Fraction a;
 7     printf("a:");
 8     a.printFraction();
 9     a.compareFraction(5, 8);
10     a.addFraction(1,2);
11     a.printFraction();
12     cout<<endl<<"b:";
13     Fraction b(3,4);
14     b.printFraction();
15     b.compareFraction(9, 5);
16     b.divideFraction(3, 5);
17     b.printFraction();
18     cout<<endl<<"c:";
19     Fraction c(5);
20     c.printFraction();
21     c.compareFraction(8, 3);
22     c.minusFraction(3,4);
23     c.printFraction();
24     cout<<endl<<"d:";
25     Fraction d(1,4);
26     d.printFraction();
27     d.compareFraction(2, 4);
28     d.timeFraction(6, 8);
29     d.printFraction();
30     cout<<endl<<"e:";
31     Fraction e(1,0);
32     e.printFraction();
33     e.compareFraction(2, 0);
34     e.addFraction(6, 0);
35     e.printFraction();
36     return 0;
37 }
main.cpp
 1 #ifndef Fraction_hpp
 2 #define Fraction_hpp
 3 
 4 #include <stdio.h>
 5 class Fraction{
 6 public:
 7     Fraction(int x0=0,int y0=1);
 8     void addFraction(int x1,int y1);       //两分数相加
 9     void minusFraction(int x1,int y1);     //两分数相减
10     void timeFraction(int x1,int y1);      //两分数相乘
11     void divideFraction(int x1,int y1);    //两分数相除
12     void printFraction();                  //输出分数
13     void compareFraction(int x1,int y1);   //两分数相比较
14 private:
15     int top;
16     int bottom;
17     
18 };
19 #endif /* Fraction_hpp */
fraction.h
  1 #include "Fraction.hpp"
  2 #include <iostream>
  3 using namespace std;
  4 
  5 Fraction::Fraction(int x0,int y0):top(x0),bottom(y0){
  6 }
  7 
  8 void Fraction::addFraction(int x1,int y1){
  9     int m,n,r,t;
 10     if(y1==0)                          //控制分母为零的情况
 11         cout<<"error!"<<endl;
 12     else
 13     {
 14       if(bottom==y1)                   //将分数化为最简形式
 15       {  top=top+x1;
 16          if(top%bottom==0)
 17          {   top=top/bottom;
 18              bottom=1;}
 19          else
 20          {   m=top;
 21              n=bottom;
 22              t=0;
 23              r=0;
 24              if(m<n)
 25              {t=m;m=n;n=t;}
 26              r=m%n;
 27              while(r!=0)
 28              {m=n;n=r;r=m%n;}
 29              top=top/n;
 30              bottom=bottom/n;
 31            }
 32         }
 33       else
 34       {   top=top*y1;
 35           x1=x1*bottom;
 36           top=top+x1;
 37           bottom=bottom*y1;
 38           m=top;
 39           n=bottom;
 40           t=0;
 41           r=0;
 42           if(m<n)
 43           {t=m;m=n;n=t;}
 44            r=m%n;
 45            while(r!=0)
 46            {m=n;n=r;r=m%n;}
 47            top=top/n;
 48            bottom=bottom/n;
 49        }
 50     }
 51 }
 52 
 53 void Fraction::minusFraction(int x1,int y1){
 54     int m,n,r,t;
 55     if(y1==0)                      //控制分母为零的情况
 56         cout<<"error!"<<endl;
 57     else{
 58     if(bottom==y1)                 //将分数化为最简形式
 59     {  top=top-x1;
 60         if(top%bottom==0)
 61         {   top=top/bottom;
 62             bottom=1;}
 63         else
 64         {   m=top;
 65             n=bottom;
 66             t=0;
 67             r=0;
 68             if(m<n)
 69             {t=m;m=n;n=t;}
 70             r=m%n;
 71             while(r!=0)
 72             {m=n;n=r;r=m%n;}
 73             top=top/n;
 74             bottom=bottom/n;
 75         }
 76     }
 77     else
 78     {   top=top*y1;
 79         x1=x1*bottom;
 80         top=top-x1;
 81         bottom=bottom*y1;
 82         m=top;
 83         n=bottom;
 84         t=0;
 85         r=0;
 86         if(m<n)
 87         {t=m;m=n;n=t;}
 88         r=m%n;
 89         while(r!=0)
 90         {m=n;n=r;r=m%n;}
 91         top=top/n;
 92         bottom=bottom/n;
 93     }
 94     }
 95 }
 96 
 97 void Fraction::timeFraction(int x1,int y1){
 98     if(y1==0)                       //控制分母为零的情况
 99         cout<<"error!"<<endl;
100     else{
101     int m,n,r,t;
102         top=top*x1;
103         bottom=bottom*y1;
104     if(top%bottom==0)               //将分数化为最简形式
105     {   top=top/bottom;
106         bottom=1;}
107     else
108     {
109         m=top;
110         n=bottom;
111         t=0;
112         r=0;
113         if(m<n)
114         {t=m;m=n;n=t;}
115         r=m%n;
116         while(r!=0)
117         {m=n;n=r;r=m%n;}
118         top=top/n;
119         bottom=bottom/n;
120     }
121     }
122 }
123 
124 void Fraction::divideFraction(int x1,int y1){
125     int m,n,r,t;
126     if(y1==0)                        //控制分母为零的情况
127         cout<<"error!"<<endl;
128     else{
129     top=top*y1;
130     bottom=bottom*x1;
131     if(top%bottom==0)                //将分数化为最简形式
132     {   top=top/bottom;
133         bottom=1;}
134     else
135     {
136         m=top;
137         n=bottom;
138         t=0;
139         r=0;
140         if(m<n)
141         {t=m;m=n;n=t;}
142         r=m%n;
143         while(r!=0)
144         {m=n;n=r;r=m%n;}
145         top=top/n;
146         bottom=bottom/n;
147     }
148     }
149 }
150 
151 void Fraction::printFraction(){
152     if(bottom==0)                        //控制分母为零的情况
153         cout<<"error!"<<endl;
154     else
155     cout<<top<<"/"<<bottom<<endl;
156 }
157 
158 void Fraction::compareFraction(int x1,int y1){
159     double x,y;
160     if(y1==0)                            //控制分母为零的情况
161         cout<<"error!"<<endl;
162     else{
163     x=top*1.0/bottom;
164     y=x1*1.0/y1;
165     if(x>y)
166         cout<<top<<"/"<<bottom<<" > "<<x1<<"/"<<y1<<endl;
167     else if(x<y)
168         cout<<top<<"/"<<bottom<<" < "<<x1<<"/"<<y1<<endl;
169     else
170     
171         cout<<top<<"/"<<bottom<<" = "<<x1<<"/"<<y1<<endl;
172     }
173 }
fraction.cpp

运行截图

 

实验总结与体会

1.学会运用多文件结构,将类的定义、实现、使用部分分开,便于错误排查,使得程序更加便于管理。

2.了解了system("pause),system("color ××")函数的使用,但在Xcode中貌似有点问题,还在研究中,待完善。

3.C与C++的一些语法规则有些混淆了,说明对C++的基础语法规则掌握的还不熟练,需多敲多练。

 

实验二评论链接

https://www.cnblogs.com/zuiyankh/p/10587674.html#4219118

https://www.cnblogs.com/qsxsc/p/10583875.html#4219112

https://www.cnblogs.com/yfwg/p/10594280.html#4219099

 
 
posted @ 2019-04-22 21:46  糕点点  阅读(255)  评论(0编辑  收藏  举报