實驗三:類的實現
阿新 • • 發佈:2019-04-20
none spa eba efi n) names 小球 == esp
1.小球測試
2.畫布
#ifndef GRAPH_H #define GRAPH_H // 類Graph的聲明 class Graph { public: Graph(char ch, int n); // 帶有參數的構造函數 void draw(); // 繪制圖形 private: char symbol; int size; }; #endifgraph.h
#include "graph.h" #include <iostream> usinggraph.cppnamespace std; // 帶參數的構造函數的實現 Graph::Graph(char ch, int n): symbol(ch), size(n) { } // 成員函數draw()的實現 // 功能:繪制size行,顯示字符為symbol的指定圖形樣式 void Graph::draw() { int i, j, k; for(i=1;i<=size;i++) { for (j = 1; j <= size - i; j++) cout << " ";for (k = 1; k <= 2 * i - 1; k++) cout << symbol; cout << endl; } }
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1(‘*‘,5); graph1.draw(); system("pause"); system(main"cls"); Graph graph2(‘$‘,7); graph2.draw(); system("pause"); return 0; }
3.分數
#ifndef FRACTION_H #define FRACTION_H class Fraction { public: Fraction(int t = 0, int b = 1) : top(t), bottom(b) { } Fraction(const Fraction &fr) : top(fr.top), bottom(fr.bottom) { } void fractionadd(Fraction &f, Fraction &p); void fractionmin(Fraction &f, Fraction &p); void fractionmul(Fraction &f, Fraction &p); void fractiondiv(Fraction &f, Fraction &p); void fractioncom(Fraction &f, Fraction &p); void show(); private: int top; int bottom; }; #endif // !FRACTION_Hfraction.h
#include"fraction.h" #include<iostream> using namespace std; void Fraction::show() { if (top == 0) cout << 0<<endl; else if (bottom == 1) cout << top << endl; else if (top / bottom < 0) cout << "-" << top << "/" << bottom << endl; else cout << top << "/" << bottom << endl; } void Fraction::fractionadd(Fraction &f, Fraction &p) { int t1, b1, t2, b2, m, n, temp, x, y ,i; t1 = f.top; t2 = p.top; b1 = f.bottom; b2 = p.bottom; y = b1 * b2; x = t1 * b2 + t2 * b1; m = x; n = y; if(m<n) { temp = m; m = n; n = temp; } for(i=n;i>=1;i--) { if (x%i == 0 && y%i == 0) break; } x = x / i; y = y / i; cout << x << "/" << y << endl; } void Fraction::fractionmin(Fraction &f, Fraction &p) { int t1, t2, b1, b2, x, y, m, n, temp, i; t1 = f.top; t2 = p.top; b1 = f.bottom; b2 = p.bottom; y = b1 * b2; x= t1 * b2 - t2 * b1; m = x; n = y; if (m < n) { temp = m; m = n; n = temp; } for (i = n; i >= 1; i--) { if (x%i == 0 && y%i == 0) break; } x = x / i; y = y / i; cout << x << "/" << y << endl; } void Fraction::fractionmul(Fraction &f, Fraction &p) { int t1, t2, b1, b2, x, y, m, n, temp, i; t1 = f.top; t2 = p.top; b1 = f.bottom; b2 = p.bottom; y = b1 * b2; x = t1 * t2; m = x; n = y; if (m < n) { temp = m; m = n; n = temp; } for (i = n; i >= 1; i--) { if (x%i == 0 && y%i == 0) break; } x = x / i; y = y / i; cout << x << "/" << y << endl; } void Fraction::fractiondiv(Fraction &f, Fraction &p) { int t1, t2, b1, b2, x, y, m, n, temp, i; t1 = f.top; t2 = p.bottom; b1 = f.bottom; b2 = p.top; y = b1 * b2; x = t1 * t2; m = x; n = y; if (m < n) { temp = m; m = n; n = temp; } for (i = n; i >= 1; i--) { if (x%i == 0 && y%i == 0) break; } x = x / i; y = y / i; cout << x << "/" << y << endl; } void Fraction::fractioncom(Fraction &f, Fraction &p) { int t1, t2, b1, b2, x, y; t1 = f.top; t2 = p.top; b1 = f.bottom; b2 = p.bottom; y = b1 * b2; x = t1 * b2 - t2 * b1; if (x < 0) cout << f.top << "/" << f.bottom << "<" << p.top << "/" << p.bottom << endl; else if (x > 0) cout << f.top << "/" << f.bottom << ">" << p.top << "/" << p.bottom << endl; else if (x == 0) cout << f.top << "/" << f.bottom << "=" << p.top << "/" << p.bottom << endl; }fraction.cpp
#include"fraction.h" #include<iostream> using namespace std; int main() { Fraction a; a.show(); Fraction b(3, 4); b.show(); Fraction c(5); c.show(); int x, y; cin >> x >> y; Fraction d(x, y); d.show(); a.fractionadd(b, d); a.fractionmin(b, d); a.fractionmul(b, d); a.fractiondiv(b, d); a.fractioncom(b, d); system("pause"); }main
實驗三:類的實現