实验三    类和对象

graph类框架

c++ 实验三 随笔 第1张
 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     private:
10         char symbol;
11         int size;
12 };
13 
14 
15 #endif
graph.h c++ 实验三 随笔 第3张
 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,n=1;
16     while (n  <=size)
17     {
18         for (i = 0; i < size - n; i++)
19             cout <<" "<< ends;
20         for (i = 0; i < 2 * n - 1; i++)
21             cout << symbol << ends;
22         n++;
23         cout << endl;
24     }
25     // 补足代码
26     // ...
27 }
graph.cpp c++ 实验三 随笔 第5张
 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     system("pause");
12     Graph graph2('$',7);
13     graph2.draw();
14     system("pause");
15     
16     return 0; 
17 } 
main.cpp

 

c++ 实验三 随笔 第7张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

fraction类框架

c++ 实验三 随笔 第8张
 1 #ifndef FRACTION_H
 2 #define FRACTION_H
 3 
 4 class Fraction{
 5 public:
 6     Fraction (int x1=0, int y1=1);
 7     void add(Fraction c2);
 8     void sub(Fraction c3);
 9     void mult(Fraction c4);
10     void div(Fraction c5);
11     void compare(Fraction c6,Fraction c7);
12     void input();
13     void output();
14 private:
15     int top;
16     int bottom;
17 };
18 #endif 
fraction.h c++ 实验三 随笔 第10张
  1 #include<iostream>
  2 #include<cmath>
  3 #include<cstdlib>
  4 int simt(int ,int );
  5 int simb(int, int);
  6 #include "fraction.h"
  7 using std::cout;
  8 using std::endl;
  9 using std::ends;
 10 using std::cin;
 11 int simt(int x, int y)
 12 {
 13     int fx = x;
 14     if (x == 0 || y == 0)
 15         exit(0);
 16     if (x < 0)
 17         x = abs(x);
 18     if (y < 0)
 19         y = abs(y);
 20     while (x != y)
 21     {
 22         if (x > y)
 23             x = x - y;
 24         else
 25             y = y - x;
 26     }
 27     return fx / x;
 28 }
 29 int simb(int x, int y)
 30 {
 31     int fy = y;
 32     if (x == 0 || y == 0)
 33         exit(0);
 34     if (x < 0)
 35         x = abs(x);
 36     if (y < 0)
 37         y = abs(y);
 38     while (x != y)
 39     {
 40         if (x > y)
 41             x = x - y;
 42         else
 43             y = y - x;
 44     }
 45     return fy / x;
 46 }
 47 void Fraction::input()
 48 {
 49     cin >> top >> bottom;
 50     if (bottom == 0)
 51     {
 52         cout << "wrong input" << endl;
 53         system("pause");
 54         exit(0);
 55     }
 56 }
 57 void Fraction::output()
 58 {
 59     cout << top << "/" << bottom<<ends;
 60 }
 61 Fraction::Fraction(int x1 , int y1 ) :top(x1), bottom(y1)
 62 {
 63     if (bottom == 0)
 64     {
 65         cout << "wrong input" << endl;
 66         system("pause");
 67         exit(0);
 68     }
 69 }
 70 void Fraction::add(Fraction c2)
 71 {
 72     if (c2.bottom == 0)
 73     {
 74         cout << "wrong input" << endl;
 75         system("pause");
 76         exit(0);
 77     }
 78     top = top * c2.bottom;
 79     c2.top = c2.top*bottom;
 80     bottom = bottom * c2.bottom;
 81     c2.bottom = bottom;
 82     cout << "add complete" << endl;
 83     cout <<  simt((top+c2.top),bottom) << "/" << simb((top+c2.top),bottom) << endl;     
 84 }
 85 void Fraction::sub(Fraction c3)
 86 {
 87     if (bottom==0&&c3.bottom == 0)
 88     {
 89         cout << "wrong input" << endl;
 90         system("pause");
 91         exit(0);
 92     }
 93     top = top * c3.bottom;
 94     c3.top = c3.top*bottom;
 95     bottom = bottom * c3.bottom;
 96     c3.bottom = bottom;
 97     cout << "sub complete" << endl;
 98     cout << simt((top - c3.top),bottom) << "/" <<simb((top-c3.top),bottom) << endl;            
 99 }
100 void Fraction::mult(Fraction c4)
101 {
102     if (bottom == 0 && c4.bottom == 0)
103     {
104         cout << "wrong input" << endl;
105         system("pause");
106         exit(0);
107     }
108     top = top * c4.top;
109     bottom = bottom * c4.bottom;
110     cout << "mult complete" << endl;
111     cout <<simt(top,bottom)<<"/"<<simb(top,bottom)<< endl;                                        
112 }
113 void Fraction::div(Fraction c5)
114 {
115     if (bottom == 0 && c5.bottom == 0)
116     {
117         cout << "wrong input" << endl;
118         system("pause");
119         exit(0);
120     }
121     top = top * c5.bottom;
122     bottom = bottom * c5.top;
123     cout << "div complete" << endl;
124     cout << simt(top,bottom)<<"/"<<simb(top,bottom)<<endl;                                    
125 }
126  void Fraction::compare(Fraction c6,Fraction c7)
127 {
128     Fraction c1, c2;
129     c1.top = c6.top * c7.bottom;
130     c2.top = c7.top*c6.bottom;
131     if (c1.top > c2.top)
132     {
133         c6.output();
134         cout << ">";
135         c7.output();
136     }
137     else
138     {
139         c6.output();
140         cout << "<";
141         c7.output();
142     }
143 
144 }
fraction.cpp c++ 实验三 随笔 第12张
 1 #include"fraction.h"
 2 #include<iostream>
 3 #include<cstdlib>
 4 int main()
 5 {
 6     Fraction a,d;
 7     Fraction b(20,40),e(-4,6),f(7,8);
 8     Fraction c(5);
 9     b.compare(b, c);
10     system("pause");
11     b.add(c);
12     system("pause");
13     b.sub(c);
14     system("pause");
15     b.mult(c);
16     system("pause");
17     e.div(f);
18     system("pause");
19     a.output();
20     system("pause");
21     d.input();
22     system("pause");
23     d.output();
24     system("pause");
25     d.input();
26     return 0;
27 }
main.cpp

 

c++ 实验三 随笔 第14张

实验总结:

fraction类的实验写的比较粗糙,化简的函数写的有点啰嗦,分别化简了分子和分母(因为想写在类外面)。给后面的输出带来了一些麻烦。

compare没有化简,因为想要直观的展现原分数的大小关系。(写完之后发现可以用bool类型……555

然后肯定还有很多不足,希望大家指出,谢谢~

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄