实验三
Posted jiyuanxiangzhouziying
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验三相关的知识,希望对你有一定的参考价值。
1.基于已有信息,补足并扩充程序。 在graph文件夹里提供有三个文件: graph.h (类Graph的声明) graph.cpp (类Graph的实现) main.cpp (类Graph的测试: 定义Graph类对象,调用绘图接口绘制图形) 要求如下: 新建一个空项目,添加上述三个文件到项目中。
#ifndef GRAPH_H #define GRAPH_H #include<iostream> using namespace std; class Graph { public: Graph(char a, int b); void draw(); private: char symbol; int size; }; Graph::Graph(char a, int b) :symbol(a), size(b) { } void Graph::draw() { int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size - i - 1; j++) cout << " "; for (j = 0; j < 2 * i + 1; j++) cout << symbol; cout << endl; } } #endif
#include<iostream> #include"graph.h" using namespace std; int main() { Graph graph1(‘*‘, 5); graph1.draw(); Graph graph2(‘&‘, 7); graph2.draw(); system("pause"); }
2.基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。 具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值)
#ifndef FRACTION_H #define FRACTION_H #include<iostream> using namespace std; class fraction { public: fraction(int a = 0, int b = 1) :top(a), bottom(b) {} fraction(fraction &c) :top(c.top), bottom(c.bottom) {} void jia(fraction &d, fraction &e); void jian(fraction &d, fraction &e); void cheng(fraction &d, fraction &e); void chu(fraction &d, fraction &e); void compare(fraction &d, fraction &e); public: int top; int bottom; }; void yue(int, int); void yue(int x, int y) { int p, q, i = 0; if (x > y) { i = y; } else i = x; while (x%i != 0 || y % i != 0) { i--; } p = x / i; q = y / i; cout << p << "/" << q << endl; } void fraction::jia(fraction &d, fraction &e) { int x, y, k; x = d.top*e.bottom + e.top*d.bottom; y = d.bottom * e.bottom; k = x; if (x < 0) { x = x - 2 * k; cout << "-"; yue(x, y); } else if (x == 0) cout << ‘0‘ << endl; else yue(x, y); } void fraction::jian(fraction &d, fraction &e) { int x, y, k; x = d.top*e.bottom - e.top*d.bottom; y = d.bottom * e.bottom; k = x; if (x < 0) { x = x - 2 * k; cout << "-"; yue(x, y); } else if (x == 0) cout << ‘0‘ << endl; else yue(x, y); } void fraction::cheng(fraction &d, fraction &e) { int x, y, k; x = d.top*e.top; y = d.bottom*e.bottom; k = x; if (x < 0) { x = x - 2 * k; cout << "-"; yue(x, y); } else if (x == 0) cout << ‘0‘ << endl; else yue(x, y); } void fraction::chu(fraction &d, fraction &e) { int x, y, k; x = d.top*e.bottom; y = d.bottom*e.top; if (y < 0) { x = -x; y = -y; } k = x; if (x < 0) { x = x - 2 * k; cout << "-"; yue(x, y); } else if (x == 0) cout << ‘0‘ << endl; else yue(x, y); } void fraction::compare(fraction &d, fraction &e) { int x, y; x = d.top*e.bottom; y = e.top*d.bottom; cout << "大的数为:"; if (x > y) cout << d.top<<‘/‘<<d.bottom << endl; else if (x < y) cout << e.top<<‘/‘<<e.bottom<< endl; else cout << "一样大" << endl; } #endif
#include<iostream> #include"fraction.h" using namespace std; int main() { int t1, b1, t2, b2; cout << "请输入第一个分数" << endl; cin >> t1 >> b1; cout << "请输入第二个分数" << endl; cin >> t2 >> b2; if (b1 < 0) { b1 = -b1; t1 = -t1; } if (b2 < 0) { b2 = -b2; t2 = -t2; } while (b1 == 0 || b2 == 0) return 0; fraction a(t1, b1); fraction b(t2, b2); cout << "两数相加为"; a.jia(a, b); cout << "两数相减为"; a.jian(a, b); cout << "两数相乘为"; a.cheng(a, b); cout << "两数相除为"; a.chu(a, b); a.compare(a, b); system("pause"); return 0; }
实验感想:1.关于多文件结构一直不知道在.h文件中要大写头文件名,否则就调用出错。
2.第二个题目一开始是直接做的并没有思考如何去简化算法,只知道可能是循环加循环的做法,所以一开始尝试的时候多花了好长时间,最终改了几次发现只有几行。
3.第一个ball的程序完全是测试出来的,所以就不放出来了。
以上是关于实验三的主要内容,如果未能解决你的问题,请参考以下文章