1.实验内容2 graph类 /*附加拓展1和2*/
draw()函数的话,因为每一行输出的是空格和规定字符,输出空格数等于总行数-当前行数,输出字符数等于当前行数*2-1。
graph.h
1 #ifndef GRAPH_H
2 #define GRAPH_H
3
4 // 类Graph的声明
5 class Graph {
6 public:
7 Graph(char ch, int n); // 带有参数的构造函数
8 void draw(); // 绘制图形
9 void redraw(); //重绘图形
10 void changecolor(); //改变颜色
11 private:
12 char symbol;
13 int size;
14 };
15
16
17 #endif
graph.cpp
1 #include "graph.h"
2 #include <iostream>
3 #include<cstdlib>
4 using namespace std;
5
6 // 带参数的构造函数的实现
7 Graph::Graph(char ch, int n): symbol(ch), size(n) {
8 }
9
10
11 // 成员函数draw()的实现
12 // 功能:绘制size行,显示字符为symbol的指定图形样式
13 // size和symbol是类Graph的私有成员数据
14 void Graph::draw() {
15 for (int i = 1; i <= size; i++)
16 {
17 for (int j = 1; j <= (size - i); j++) //输出空格数等于 总行数-当前行数
18 cout << ' ';
19 for (int j = 1; j <= (2 * i - 1); j++) //输出字符数等于 当前行数*2-1
20 cout << symbol;
21 cout << endl;
22 }
23
24 }
25 void Graph::redraw() {
26 system("cls"); //清屏
27 for (int i = 1; i <= size; i++) //重绘
28 {
29 for (int j = 1; j <= (size - i); j++)
30 cout << ' ';
31 for (int j = 1; j <= (2 * i - 1); j++)
32 cout << symbol;
33 cout << endl;
34 }
35 }
36 void Graph::changecolor() {
37 char input[10] = { 0 };
38 char colorname[2] = { 0 };
39 cout << "请选择颜色,先输入前景色后输入背景色:" << endl << "0 = 黑色 1 = 蓝色 2 = 绿色 3 = 湖蓝色" << endl << "4 = 红色 5 = 紫色 6 = 黄色 7 = 白色"
40 << endl << "8 = 灰色 9 = 淡蓝色 A = 淡绿色 B = 淡浅绿色" << endl << "C = 淡红色 D = 淡紫色 E = 淡黄色 F = 亮白色" << endl;
41 cin >> colorname;
42 sprintf(input, "color %s", colorname); //利用sprintf函数,对system进行输入
43 system(input);
44 }
main.cpp
1 #include <iostream>
2 #include "graph.h"
3 using namespace std;
4
5
6 int main() {
7 Graph graph1('*',5), graph2('$',7) ; // 定义Graph类对象graph1, graph2
8 graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形
9 graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
10 while(1) //构造循环
11 {
12 cout << "是否需要额外操作? 不需要请输入N" << endl;
13 char determine;
14 int choice;
15 cin >> determine;
16 if (determine == 'N')
17 break;
18 else {
19 cout << "需要什么操作?" << endl << "输入1:重新设置显示的字符、尺寸 输入2:图形的前景色、背景色设置和调整"<< endl;
20 cin >> choice;
21 if (choice == 1)
22 {
23 char changec;
24 int changei;
25 cout << "输入改变的字符和尺寸:";
26 cin >> changec >> changei;
27 Graph graph3(changec, changei);
28 graph3.redraw();
29 continue;
30 }
31 if (choice == 2)
32 {
33 graph1.changecolor();
34 continue;
35 }
36 }
37 }
38 return 0;
39 }




这里有bug,不知道怎么改……请后面同学指教……

拓展内容说明:1.使用了system("cls")指令清屏
2.从网上找到改变颜色的函数是system("color **"),于是使用sprintf进行输入
3.能力有限 -_-|| 实在不会,但是BUluGuy(http://www.cnblogs.com/BuluGuy/p/8890472.html)写出来了,正在学习。还有他在颜色改变的时候使用了哈希函数,觉得应该学习一个。
注意:使用vs的同学在运行我的代码时可能会出现c4996错误,可参考:https://jingyan.baidu.com/article/ce436649fd61543773afd32e.html 解决
2.实验内容3 Fraction类 /*选做1.2*/
Fraction.h
class Fraction {
public:
Fraction();
Fraction(int t, int b);
Fraction(int t);
void show();
void add(Fraction &f0);//加法函数
void min(Fraction &f0);//减法函数
void mul(Fraction &f0);//乘法函数
void div(Fraction &f0);//除法函数
void compare(Fraction &f1);//比较函数
int gongyinshu(int a,int b);//求公因数
void transform();//转换函数
private:
int top; //分子
int bottom; //分母
};
fraction.cpp
1 #include <iostream>
2 #include "fraction.h"
3 using namespace std;
4 Fraction::Fraction() :top(0), bottom(1){} //初始化
5 Fraction::Fraction(int t, int b) : top(t), bottom(b) {}
6 Fraction::Fraction(int t) : top(t), bottom(1) {}
7
8 void Fraction::show(){ //输出分数
9 int a, b, c, d, e, f, g;
10 a = top;
11 b = bottom;
12 c = 0; d = 0; f = 0;
13 do {
14 c++; //判断分子分母的位数
15 a /= 10;
16 } while (a > 0);
17 do {
18 d++;
19 b /= 10;
20 } while (b > 0);
21 e = (c > d ? c : d);
22 g = gongyinshu(abs(top), abs(bottom)); //求最大公因数
23 top /= g; bottom /= g; //化简
24 if (bottom <= 0) { //规范分数
25 bottom = -bottom;
26 top = -top;
27 }
28 cout << top << endl; //输出分子
29 do{
30 f++;
31 cout << "-" ; //根据位数输出横线长度
32 } while (f < e);
33 cout <<endl<< bottom; //输出分母
34 }
35
36 void Fraction::mul(Fraction &f0) { //乘法函数的实现
37 cout << "相乘得:" << endl;
38 Fraction f1;
39 f1.top = top *f0.top;
40 f1.bottom = bottom*f0.bottom;
41 f1.show();
42 }
43
44 void Fraction::div(Fraction &f0) { //除法函数的实现
45 cout << "相除得:" << endl;
46 Fraction f1;
47 f1.top = top *f0.bottom;
48 f1.bottom = bottom*f0.top;
49 f1.show();
50 }
51
52 void Fraction::add(Fraction &f0) { //加法函数的实现
53 cout << "相加得:" << endl;
54 Fraction f1;
55 f1.top = top *f0.bottom+ f0.top*bottom;
56 f1.bottom = bottom*f0.bottom;
57 f1.show();
58 }
59
60 void Fraction::min(Fraction &f0) { //减法函数的实现
61 cout << "相减得:" << endl;
62 Fraction f1;
63 f1.top = top *f0.bottom - f0.top*bottom;
64 f1.bottom = bottom*f0.bottom;
65 f1.show();
66 }
67
68 void Fraction::compare(Fraction &f1) {
69 double real0 = double(top) / double(bottom); //强制转换为double类型,换成小数比较
70 double real1 = double(f1.top) / double(f1.bottom);
71 if (real0 > real1)
72 cout << "第一个数大于第二个数";
73 else if (real0 == real1)
74 cout << "相等";
75 else
76 cout << "第一个数小于第二个数";
77 }
78
79 int Fraction::gongyinshu(int a,int b) { //求最大公因数
80 int t; //假设a>b,如果a不能被b整除,则将b赋值给a,
81 if (a < b) //余数赋值给b,重复执行a%b,直到a能够被b整除。此时返回b的值,则为最大公约数。
82 {
83 t = a;
84 a = b;
85 b = t;
86 }
87 while (b != 0)
88 {
89 t = a % b;
90 a = b;
91 b = t;
92 }
93 return a; //返回最大公因数
94 }
95
96 void Fraction::transform() { //转换为十进制
97 double real0 = double(top) / double(bottom);
98 cout << "转换为十进制为:" << real0;
99 }
mian.cpp
1 #include<iostream>
2 #include"fraction.h"
3 using namespace std;
4 int main()
5 {
6 Fraction a(5, 2);
7 Fraction b(5429, -21);
8 Fraction c(8, 2);
9 Fraction d(9);
10 a.show();
11 cout << " 这是分数a"<<endl;
12 b.show();
13 cout << " 这是分数b" << endl;
14 c.show();
15 cout << " 这是分数c" << endl;
16 d.show();
17 cout << " 这是分数d" << endl;
18 cout << endl << "a,b相加,";
19 b.add(a);
20 cout << endl << "a,b相减,";
21 b.min(a);
22 cout << endl << "a,b相乘,";
23 b.mul(a);
24 cout << endl << "a,b相除,";
25 b.div(a);
26 cout << endl<<"ab相比";
27 a.compare(b);
28 cout << endl<<"a";
29 a.transform();
30 cout << endl;
31 return 0;
32 }

实验总结与体会
在这次的实验中,因为有了拓展题的加入,所以能够更直观的认识到自己和其他同学之间的差距。
来源:https://www.cnblogs.com/zhibifenli/p/8906291.html