实验目的

1. 掌握类的设计、定义、实现和测试

2. 掌握C++程序以项目文件组织的多文件结构编写形式

3. 深度理解面向对象编程与结构化编程在编程解决实际问题时思维方式的不同

实验内容

1.新建一个空项目,添加三个文件到项目中

  • graph.h (类Graph的声明)
  • graph.cpp (类Graph的实现)
  • main.cpp (类Graph的测试: 定义Graph类对象,调用绘图接口绘制图形) 

主函数中测试代码:

Graph graph1('*', 5);
graph1.draw();
 1 #ifndef GRAPH_H
 2 #define GRAPH_H
 3 
 4 // 类Graph的声明 
 5 class Graph {
 6     public:
 7         Graph(char ch = '*', int n = 1);  // 带有参数的构造函数 
 8         void draw();     // 绘制图形 
 9         void in();
10         private:
11         char symbol;
12         int size;
13 };
14 #endif
graph.h
 1 // 类graph的实现
 2  
 3 #include "graph.h" 
 4 #include <iostream>
 5 using namespace std;
 6 
 7 // 带参数的构造函数的实现 
 8 Graph::Graph(char ch, int n): symbol(ch), size(n) {
 9 }
10 
11 
12 // 成员函数draw()的实现
13 // 功能:绘制size行,显示字符为symbol的指定图形样式 
14 void Graph::draw() {
15     int i, j, k;
16     for ( i = 1; i <= size; i++)
17     {
18         for ( j = 1; j <= size-i; j++)
19         {
20             cout << ' ';
21         }
22         for ( k = 1; k <= 2*i-1 ; k++)
23         {
24             cout << symbol;
25         }
26         cout << endl;
27     }
28 }
29 
30 void Graph::in() {
31     cout << "Please input symbol" << endl;
32     cin >> symbol;
33     cout << "Please input size" << endl;
34     cin >> size;
35 }
graph.cpp
 1 #include <iostream>
 2 #include "graph.h"
 3 using namespace std;
 4 
 5 int main() {
 6     Graph graph1('*',5);
 7     graph1.draw();
 8     
 9     system("pause");
10     system("cls");
11     
12     Graph graph2('$',7);
13     graph2.draw();
14     
15     system("pause");
16     system("cls");
17 
18     Graph graph3;
19     graph3.in();
20     graph3.draw();
21 
22     system("pause");
23     system("cls");
24 
25     return 0; 
26 } 
main.cpp

2.基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。

 具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值)

 1 #ifndef FACTION_H
 2 #define FACTION_H
 3 class Fraction
 4 {
 5 public:
 6     Fraction(int t = 0,int b = 1);
 7     Fraction Fjia (Fraction num);
 8     Fraction Fjian (Fraction num);
 9     Fraction Fchen (Fraction num);
10     Fraction Fchu (Fraction num);
11     void Fbi (Fraction num);
12     void in();
13     void out();
14 
15 private:
16     int top;
17     int bottom;
18 };
19 #endif // !FACTION_H
fraction.h
 1 #include "fraction.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 Fraction::Fraction(int t, int b) :top(t), bottom(b) {
 6 }
 7 
 8 Fraction Fraction::Fjia(Fraction num) {
 9     Fraction s;
10     if (top == 0) { s.top = num.top; s.bottom = num.bottom;}
11     else if (num.top == 0) { s.top = top; s.bottom = bottom; }
12     else
13     {
14         s.top = top * num.bottom + num.top*bottom;
15         s.bottom = num.bottom*bottom;
16     }
17     return s;
18 }
19 
20 Fraction Fraction::Fjian(Fraction num) {
21     Fraction s;
22     if (top == 0) { s.top = - num.top; s.bottom = num.bottom; }
23     else if (num.top == 0) { s.top = top; s.bottom = bottom; }
24     else
25     {
26         s.top = top * num.bottom - num.top*bottom;
27         s.bottom = num.bottom*bottom;
28     }
29     return s;
30 }
31 
32 Fraction Fraction::Fchen(Fraction num) {
33     Fraction s;
34     if (top == 0 || num.top == 0) { s.top = 0; s.bottom = 1; }
35     else
36     {
37         s.top = top * num.top;
38         s.bottom = num.bottom*bottom;
39     }
40     return s;
41 }
42 
43 Fraction Fraction::Fchu(Fraction num) {
44     Fraction s;
45     if (top == 0 || num.top == 0) { s.top = 0; s.bottom = 1; }
46     else
47     {
48         s.top = top * num.bottom;
49         s.bottom = bottom * num.top;
50     }
51     return s;
52 }
53 
54 void Fraction::Fbi(Fraction num) {
55     double bi, bi2;
56     bi = static_cast<double>(top) / static_cast<double>(bottom);
57     bi2 = static_cast<double>(num.top) / static_cast<double>(num.bottom);
58     if (bi > bi2) cout << " > " << endl;
59     else if (bi == bi2) cout << " = " << endl;
60     else cout << " < " << endl;
61 }
62 
63 void Fraction::in() {
64     cout << "Please input numbers:" << endl;
65     cin >> top;
66     cin >> bottom;
67 }
68 
69 void Fraction::out() {
70     int f, i;
71     if (top == 0 && bottom != 0) cout << '0' << endl;
72     else {
73         if (top*bottom < 0) f = -1;
74         else f = 1;
75         top = abs(top);
76         bottom = abs(bottom);
77         i = top < bottom ? top : bottom;
78         for (; i > 1; i--) {
79             if (top%i == 0 && bottom%i == 0) break;
80         }
81         top = f * top / i;
82         bottom = bottom / i;
83         cout << top << '/' << bottom << endl;
84     }
85 }
fraction.cpp
 1 #include "fraction.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main() {
 6     Fraction a;
 7     Fraction b(3,4);
 8     Fraction c(5);
 9     Fraction s;
10     cout << " a = "; a.out();
11     cout << " b = "; b.out();
12     cout << " c = "; c.out();
13     a.in();
14     cout << " a = "; a.out();
15     printf("\n");
16     b.in();
17     cout << " b = "; b.out();
18     printf("\n");
19     c.in();
20     cout << " c = "; c.out();
21     printf("\n");
22     s = a.Fjia(b);
23     cout << " a + b = "; s.out();
24     printf("\n");
25     s = a.Fjian(b);
26     cout << " a - b = "; s.out();
27     printf("\n");
28     s = a.Fchen(b);
29     cout << " a * b = "; s.out();
30     printf("\n");
31     s = a.Fchu(b);
32     cout << " a / b = "; s.out();
33     printf("\n");
34     cout << " a ? b = "; a.Fbi(b);
35     printf("\n");
36     system("pause");
37 }
main.cpp

实验总结与体会 

1.两个程序的功能都只完善了一部分,全部完善感觉还是有一定难度的

2.两个程序的完善中第一个是构造函数那边出了问题,尝试了几次才弄出来;

 第二个程序的问题就比较多了,首先运算里面没考虑到分子为'0'的情况,着实让我忙活了好久;

  然后就是数据存放的问题,处理的很不熟练,原本的程序是直接存在运算数据里面的,然后就改变了数据,没发现问题的我算了很多次发现结果算出来根本不对,后来改成了有返回值的形式;

 之后又有比较大小的问题,我原本的处理方式是将分数转换为小数,运行的时候结果不对了,埋了一堆输出语句像比较大小后输出分数a、b的小数值啊之类的,才知道自己忽略了这个是整除;

 总体来说第二道题的程序非常的冗长看起来很不舒服。

3.处理问题的速度很慢,写完代码问题很多啊。

posted on 2019-04-21 22:43  Nibelungenlied  阅读(123)  评论(0编辑  收藏  举报