C++ 实验三
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ 实验三,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3729字,纯文字阅读大概需要6分钟。
内容图文
![C++ 实验三](/upload/InfoBanner/zyjiaocheng/832/9339786c69bc4d10a548e8326a3404ab.jpg)
part2
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328029.jpg)
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328063.jpg)
#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endifGraph.h
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328029.jpg)
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328063.jpg)
#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 line, j, k; for (line=1;line<= size;line++) { for (j=1; j<=size-line;j++) cout << " "; for (k=1;k<=2*line-1;k++) cout<<symbol; cout<<endl; } }Graph.cpp
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328029.jpg)
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328063.jpg)
#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; }main.cpp
part3
基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。 具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值)
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328029.jpg)
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328063.jpg)
#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); a.plus(b, d); a.minus(b, d); a.times(b, d); a.divide(b, d); a.compare(b, d); system("pause"); }main.cpp
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328029.jpg)
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328063.jpg)
#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::plus(Fraction &a, Fraction &b) { int t1, b1, t2, b2, m, n, temp, x, y, i; t1 = a.top; t2 = b.top; b1 = a.bottom; b2 = b.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::minus(Fraction &a, Fraction &b) { int t1, t2, b1, b2, x, y, m, n, temp, i; t1 = a.top; t2 = b.top; b1 = a.bottom; b2 = b.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::times(Fraction &a, Fraction &b) { int t1, t2, b1, b2, x, y, m, n, temp, i; t1 = a.top; t2 = b.top; b1 = a.bottom; b2 = b.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::divide(Fraction &a, Fraction &b) { int t1, t2, b1, b2, x, y, m, n, temp, i; t1 = a.top; t2 = b.top; b1 = a.bottom; b2 = b.bottom; y = b1 * t2; x = t1 * b2; 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::compare(Fraction &a, Fraction &b) { int t1, t2, b1, b2, x, y; t1 = a.top; t2 = b.top; b1 = a.bottom; b2 = b.bottom; y = b1 * b2; x = t1 * b2 - t2 * b1; if (x < 0) cout << a.top << "/" << a.bottom << "<" << b.top << "/" << b.bottom << endl; else if (x > 0) cout << a.top << "/" << a.bottom << ">" << b.top << "/" << b.bottom << endl; else if (x == 0) cout << a.top << "/" << a.bottom << "=" << b.top << "/" << b.bottom << endl; }fraction.cpp
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328029.jpg)
![C++ 实验三 - 文章图片](/upload/getfiles/0001/2021/5/6/20210506013328063.jpg)
#ifndef FRACTION_H #define TRACTION_H class Fraction { public: Fraction(int a = 0, int b = 1) :top(a), bottom(b) {} Fraction(const Fraction &a) :top(a.top), bottom(a.bottom) {} void plus(Fraction &x, Fraction &y); void minus(Fraction &x, Fraction &y); void times(Fraction &x, Fraction &y); void divide(Fraction &x, Fraction &y); void compare(Fraction &x, Fraction &y); void show(); private: int top; int bottom; }; #endiffraction.h
一开始第一题漏写了一个system("pause"),导致窗口闪了一下就消失了。后来检查后发现并解决了问题,以后写代码一定要细心,不要漏写这个少写那个。
内容总结
以上是互联网集市为您收集整理的C++ 实验三全部内容,希望文章能够帮你解决C++ 实验三所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】