首页 > 其他 > 详细

实验3 :类和对象

时间:2019-04-21 15:12:10      阅读:140      评论:0      收藏:0      [点我收藏+]

实验结果:

Part1 验证性内容:以多文件结构组织的项目文件示例:在画布上可以上下左右移动的小球

动态图截取如下,和我的预想差距有点大,www。想弄的好看一些,无奈我是个菜鸡,而且时间紧迫。。

技术分享图片

/*选做部分*/

/*更改为自我设置画布前景色与背景色

技术分享图片
 1 #include "pch.h"
 2 #include <iostream>
 3 #include "canvas.h"
 4 #include "Ball.h"
 5 int main() {
 6     Canvas canvas; //创建默认画布,黑底绿色
 7     Ball ball1(10, 10);
 8     string s1,s2;
 9     system("pause");
10     ball1.left(5);
11     system("pause");
12     ball1.up(20);
13     system("pause");
14     std::cout << "0 = 黑色  8 = 灰色;1 = 蓝色  9 = 淡蓝色;2 = 绿色   A = 淡绿色;3 = 浅绿色  B = 淡浅绿色;4 = 红色  C = 淡红色;5 = 紫色   D = 淡紫色;6 = 黄色   E = 淡黄色;7 = 白色   F = 亮白色" << std::endl << "请更新设置画布前景色" << std::endl;
15     std::cin >> s1;
16     canvas.changeCanvasFg(s1); // 更新画布前景色
17     system("pause");
18     std::cout << "请更新设置画布背景色" << std::endl;
19     std::cin >> s2;
20     canvas.changeCanvasBg(s2); // 更新画布背景色
21     system("pause");
22     system("pause");
23     return 0;
24 }
moving ball main.cpp

 /*优化小球移动部分的代码,用了switch选择语句

技术分享图片
 1 #include"pch.h"
 2 #include "ball.h"
 3 #include <iostream>
 4 #include <cstdlib> // 因为使用了system("cls"); 所以需要包含这个头文件
 5 using std::cout;
 6 using std::endl;
 7 const int SIZE_X = 50; // 小球x轴移动范围0~SIZE_X
 8 const int SIZE_Y = 50; // 小球y轴移动范围0~SIZE_Y
 9 Ball::Ball(int x0, int y0) :x(x0), y(y0) {  // 打印y0-1行空行
10     for(int line=1; line <= y0-1; line++)
11 cout << endl;// 打印x0-1个空格
12 for(int col=1; col <= x0-1; col++)
13 cout << " ";// 打印小球
14 cout << "O" << endl;
15 }
16 
17 void Ball::moving(char s, int step){
18     while (s != 0) {
19         switch (s)
20         {
21         case w:{
22             y = y - step;
23             if (y <= 0)
24                 y = 0;// 清屏
25          }
26         case a: {
27             x = x - step;
28             if (x <= 0)
29                 x = 0;// 清屏
30         }
31         case s:{
32             y = y + step;
33             if (y >= SIZE_Y)
34                 y = SIZE_Y;// 清屏
35         }
36         case d:{
37             x = x + step;
38             if (x >= SIZE_X)
39                 x = SIZE_X;// 清屏
40          }
41         default:
42             break;
43         }
44         system("cls");// 打印y-1行空行
45         for (int line = 1; line <= y - 1; line++)
46             cout << endl;// 打印x-1个空格
47         for (int col = 1; col <= x - 1; col++)
48             cout << " ";// 打印小球
49         cout << "O" << endl;
50     }
51 }
ball.cpp

Part2 基于已有信息,补足并扩充程序。

在graph文件夹里提供有三个文件: graph.h (类Graph的声明) graph.cpp (类Graph的实现) main.cpp (类Graph的测试: 定义Graph类对象,调用绘图接口绘制图形)

基本代码如下:

技术分享图片
 1 #pragma once
 2 #ifndef GRAPH_H
 3 #define GRAPH_H
 4 // 类Graph的声明
 5 class Graph {
 6 public:
 7     Graph(char ch, int n);// 带有参数的构造函数
 8     void draw(); // 绘制图形
 9     Graph(int x0 = 0, int y0 = 0); // 在坐标(x,y)处构造一个符号
10 private:
11     char symbol;
12     int size;
13     int x, y;
14 };
15 #endif
graph.h
技术分享图片
 1 #include"pch.h"
 2 // 类graph的实现
 3 #include "graph.h"
 4 #include <iostream>
 5 #include<cstdlib>
 6 using namespace std;
 7 // 带参数的构造函数的实现
 8 Graph::Graph(char ch, int n) : symbol(ch), size(n) {
 9 }
10 // 成员函数draw()的实现
11 // 功能:绘制size行,显示字符为symbol的指定图形样式
12 void Graph::draw() {
13     for (int i = 1; i <= size; i++) {
14         
15             for (int x = 1; x <= size - i; x++){
16                 if (size-i> 0)
17                     cout << " ";
18             }//循环1,用于输出左边空格
19             
20             for (int j = 1; j <= 2 * i - 1; j++) {
21             cout << symbol;
22             }//循环2,用于输出符号
23 
24             for (int x = 1; x <= size - i; x++) {
25                 if (size-i> 0)
26                     cout << " ";
27             }//循环3,用于输出左边空格
28         cout << endl;
29     }
30 }
graph.cpp
技术分享图片
 1 // 简单graph.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
 2 //
 3 
 4 #include "pch.h"
 5 #include <iostream>
 6 #include "graph.h"
 7 #include"canvas.h"
 8 using namespace std;
 9 int main() {
10     char s;
11     int size0;
12     Canvas canvas;
13     Graph graph1(*, 5);
14     system("pause");
15     Graph graph2($, 7);
16     graph2.draw();
17     system("pause");
18     cout <<endl<< "请您设置新的符号及大小" << endl;
19     cin >> s >> size0;
20     Graph graph3(s, size0);//重新设置符号和大小
21     graph3.draw();
22     system("pause");
23     canvas.changeCanvasFg("A"); // 更新画布前景色
24     system("pause");
25     canvas.changeCanvasBg("E"); // 更新画布背景色
26     system("pause");
27     return 0;
28 }
main.cpp

/*部分选做如下*/

/*main.cpp中,添加代码,可以自由设置符号及大小*/

/*结合part1,改变画布的前景色与背景色*/

ps:本来还想通过使用二维数组来实现symbol阵整体移动,然后想了想,发现实现的话应该没有什么问题,加上时间比较紧张,就偷个懒没有写,嘻嘻嘻。

技术分享图片
 1 #pragma once
 2 #ifndef CANVAS_H
 3 #define CANVAS
 4 #include <string>
 5 using std::string;
 6 class Canvas {
 7 public:
 8     Canvas(string bg0 = "0", string fg0 = "A");
 9     void changeCanvasBg(string bg0);
10     void changeCanvasFg(string fg0);
11     void changeCanvasColor(string bg0, string fg0);
12 private:
13     string bg; // background color
14     string fg; // foreground color
15 };
16 #endif
canvas.h
技术分享图片
 1 #include"pch.h"
 2 #include "canvas.h"
 3 #include <cstdlib>
 4 Canvas::Canvas(string bg0, string fg0) :bg(bg0), fg(fg0) {
 5     string color = "color ";
 6     color += bg0;
 7     color += fg0;
 8     system(color.c_str());
 9 }
10 void Canvas::changeCanvasBg(string bg0) {
11     bg = bg0; // 更新画布背景色
12     string color = "color ";
13     color += bg;
14     color += fg;
15     system(color.c_str());
16 }
17 void Canvas::changeCanvasFg(string fg0) {
18     fg = fg0; // 更新画布前景色
19     string color = "color ";
20     color += bg;
21     color += fg;
22     system(color.c_str());
23 }
24 void Canvas::changeCanvasColor(string bg0, string fg0) {
25     bg = bg0; // 更新画布背景色
26     fg = fg0; // 更新画布前景色
27 }
cavas.cpp

/*运行效果截图如下*/

技术分享图片

 

Part3 基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。 具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值)

基本代码如下:

技术分享图片
 1 #pragma once
 2 #ifndef FRACYION_H
 3 #define FRACTION_H
 4 
 5 class Fraction {
 6 public:
 7     class Fraction(int top0 = 0, int bottom0 = 1) {
 8         top = top0;
 9         bottom = bottom0;
10     }
11     void print();//第三个参数是分数整数部分,输出前判断是否为0,规范负号位置。
12     friend Fraction calculation(Fraction &F1, Fraction &F2,char operators0);//返回计算出的分数值,未化简。
13     friend Fraction compare(Fraction &F1, Fraction &F2);//返回分子作差比较后的结果,>,=,<的各种判断
14     friend  double change(Fraction F1);//实现分数和小数的互相转换
15 private:
16     int top; 
17     int bottom;
18 };
19 
20 #endif // !FRACYION_H
Fraction.h
技术分享图片
 1 #include"pch.h"
 2 #include"Fraction.h"
 3 #include<iostream>
 4 using namespace std;
 5 
 6 Fraction calculation(Fraction &F1, Fraction &F2,  char operators0) {
 7     int r,gcd;
 8     int x = F1.bottom, y = F2.bottom;
 9     Fraction F3;
10     do {
11         r = x % y;
12         x = y;
13         y = r;
14     } while (r != 0);//r为最大公因数
15     gcd = F1.bottom *F2.bottom / x;//gcd为通分后的分母
16     if (operators0 == +) {
17         F3.top = F1.top * (gcd / F1.bottom) + F2.top * (gcd / F2.bottom);
18         F3.bottom = gcd;
19     }
20         
21     else if (operators0 == -) {
22         F3.top= F1.top * (gcd / F1.bottom) - F2.top * (gcd / F2.bottom);
23         F3.bottom = gcd;
24     }
25     else if (operators0 == *) {
26         F3.top = F1.top * F2.top;
27         F3.bottom = F1.bottom * F2.bottom;
28     }
29     else {
30         F3.top = F1.top*F2.bottom;
31         F3.bottom = F1.bottom*F2.top;
32     }
33     return F3;
34 }//返回计算出的分数值,未化简。
35 
36  Fraction compare(Fraction &F1, Fraction &F2) {
37     int r,gcd;
38     int x = F1.bottom, y = F2.bottom;
39     do {
40         r = x % y;
41         x = y;
42         y = r;
43     } while (r != 0);//r为最大公因数
44     gcd = F1.bottom*F2.bottom / x;
45     if (F1.top*(gcd / F1.bottom) - F2.top*(gcd / F2.bottom) >= 0)
46         return F1;
47     else
48         return F2;
49 }//比较输出较大的分数。
50 
51 void Fraction::print() {
52     if (top == 0) {
53         cout << 0 << endl;
54     }
55     else if (bottom == 0 )
56         cout << "数据错误,分母不能为0" << endl;
57     else if (bottom < 0) {
58         top *= -1; 
59     }
60     else if (bottom == 1) {
61         cout << top << endl;
62         
63     }
64     else {
65         int rr, a = top, b = bottom;
66         do {
67             rr = a % b;
68             a = b;
69             b = rr;
70         } while (rr != 0);
71         cout << top / a << "/" << bottom / a << endl;
72     }
73 }//输出最简分数。
74 
75 double change(Fraction F1) {
76     double temp;
77     temp = (double)F1.top /(double) F1.bottom;
78     return temp;
79 }//返回分数对应的小数。
Fraction.cpp
技术分享图片
 1 // 简单Fraction的实现.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
 2 //
 3 
 4 #include "pch.h"
 5 #include"Fraction.h"
 6 #include <iostream>
 7 using namespace std;
 8 
 9 int main() {
10     Fraction f;
11     f.print();
12     char s=+,s1,s2;
13     int top1, top2, bottom1, bottom2;
14     cout << "请分别输入两个分数" << endl;
15     cin >> top1 >> s1 >> bottom1 >> top2 >> s2 >> bottom2;
16     Fraction F1(top1, bottom1), F2(top2, bottom2), F3;
17     cout << "请输入运算符+-*/,按Q退出计算" << endl;
18     while (s!= Q) {
19         cin >> s;
20         calculation(F1, F2, s).print();
21     }//输出两个分数加减乘除后计算的结果。
22     cout << "输出较大的分数";
23     compare(F1, F2).print();//输出F1,F2中较大的分数。
24     cout << "分数转为小数";
25     cout << change(F1);//分数转化为小数
26     return 0;
27 }
main.cpp

运行截图如下:

技术分享图片

/*程序基本解释*/

设置了用户自己输入分数,并且用户自己输入运算符。

/*选做部分*/

进行了分数最简化;进行了负号统一到分母;进行了分数转换为小数。

/*存在未修改的问题*/

1、在输入Q表示运算结束时会再次输出一个分数,时间紧迫,没能回去检查了。

2、其实最开始自己设计的算法过程非常友好,但是编写过程中出现了一个小问题,就是在求公因数时无意的把0置于分母,结果程序一直不出结果,然后我就疯狂地找bug,结果把很好的框架改乱掉了。预期的框架是类似于用户自己在计算器上对于分数进行运算,每一组出一次结果,直到用户自己终止计算。emmm,现在写成这样,只能算是一个半成品,我准备在最近各种期中考试的浪潮过了之后重新改一改,争取达到预期。

实验感悟:

这次完全由自己设计了一个分数运算的类,虽然有未达预期的地方,但是感觉很不错,计划在这段时间忙完之后再次修改,并且尝试加入更多的数据形式来写一个更加像计算的计算程序。

 

/*点评地址*/

1、https://www.cnblogs.com/changtingzao/p/10645725.html

2、https://www.cnblogs.com/fifi1224/p/10639198.html

 

实验3 :类和对象

原文:https://www.cnblogs.com/shenqidetao/p/10742384.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!