实验三
Posted jackyayue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验三相关的知识,希望对你有一定的参考价值。
part1
Part2
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1(‘*‘, 5); graph1.draw(); system("pause"); Graph graph2(‘$‘, 7); graph2.draw(); system("pause"); return 0; }
#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
#include "graph.h" #include <iostream> using namespace 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; } }
part3
#include<iostream> #include"Fraction.h" using namespace std; int main() { Fraction a; a.show(); Fraction b(3, 4); b.show(); Fraction c(5); c.show(); Fraction e; e.add(b, c); e.minus(b, c); e.multiply(b, c); e.division(b, c); e.compare(b, c); }
#include"Fraction.h" #include<iostream> #include<cmath> using namespace std; Fraction::Fraction(int top0, int bottom0) :top(top0), bottom(bottom0) { } int gongyueshu(int m, int n) { int x, y, i, t; x = abs(m); y = abs(n); for (i = 1;i <= x;i++) if (x%i == 0 && y%i == 0) t = i; return t; } int gongbeishu(int m, int n) { int x, y, i, t; x = abs(m); y = abs(n); for (i = 1;i <= x;i++) if (x%i == 0 && y%i == 0) t = i; return m * n / t; } void Fraction::judge() { if (bottom < 0 && top>0) { bottom = 0 - bottom; top = 0 - top; } else if (bottom == 0) { cout << "This score makes no sense." << endl; } else if (bottom < 0 && top < 0) { bottom = 0 - bottom; top = 0 - top; } } void Fraction::add(Fraction a, Fraction b) { bottom = gongbeishu(a.bottom, b.bottom); top = (bottom / a.bottom)*a.top + (bottom / b.bottom)*b.top; judge(); cout << "相加:"; cout << top / gongyueshu(abs(top), abs(bottom)) << "/" << bottom / gongyueshu(abs(top), abs(bottom)) << endl; } void Fraction::minus(Fraction a, Fraction b) { bottom = gongbeishu(a.bottom, b.bottom); top = (bottom / a.bottom)*a.top - (bottom / b.bottom)*b.top; judge(); cout << "相减:"; cout << top / gongyueshu(abs(top), abs(bottom)) << "/" << bottom / gongyueshu(abs(top), abs(bottom)) << endl; } void Fraction::multiply(Fraction a, Fraction b) { top = a.top*b.top; bottom = a.bottom*b.bottom; judge(); cout << "相乘:"; cout << top / gongyueshu(abs(top), abs(bottom)) << "/" << bottom / gongyueshu(abs(top), abs(bottom)) << endl; } void Fraction::division(Fraction a, Fraction b) { top = a.top*b.bottom; bottom = a.bottom*b.top; judge(); cout << "相除:"; cout << top / gongyueshu(abs(top), abs(bottom)) << "/" << bottom / gongyueshu(abs(top), abs(bottom)) << endl; } void Fraction::compare(Fraction a, Fraction b) { double m, n; m = a.top / a.bottom; n = b.top / b.bottom; if (m > n) { cout<<"比较大小:"<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl; } else if (m < n) { cout<<"比较大小:"<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl; } else if (m == n) { cout<<"比较大小:"<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl; } } void Fraction::show() { judge(); cout << top << "/" << bottom << endl; }
#ifndef FRACTION_H #define FRACTION_H class Fraction { public: Fraction(int top0 = 0, int bottom0 = 1); void judge(); void add(Fraction a, Fraction b); void minus(Fraction a, Fraction b); void multiply(Fraction a, Fraction b); void division(Fraction a, Fraction b); void compare(Fraction a, Fraction b); void show(); private: int top; int bottom; }; #endif
总结:程序总是出错,可能是dev或者vs的问题,也有可能是我写的程序不够严谨出错了。第三题花了很多时间也请教了别人,
总感觉不够细心老是漏掉条件,感觉类的知识掌握得不是很好。
以上是关于实验三的主要内容,如果未能解决你的问题,请参考以下文章