c++ 实验三
发布日期:2022-03-30 20:19:30 浏览次数:25 分类:博客文章

本文共 3667 字,大约阅读时间需要 12 分钟。

实验三    类和对象

graph类框架

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
1 // 类graph的实现 2   3 #include "graph.h"  4 #include 
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
1 #include 
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

 

 

fraction类框架

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
1 #include
2 #include
3 #include
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<
c2.top)132 {133 c6.output();134 cout << ">";135 c7.output();136 }137 else138 {139 c6.output();140 cout << "<";141 c7.output();142 }143 144 }
fraction.cpp
1 #include"fraction.h" 2 #include
3 #include
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

 

实验总结:

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

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

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

 

转载地址:https://www.cnblogs.com/aiwenzhuo/p/10750212.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Aspose.cell.dll中EXECL的使用
下一篇:c++ 实验四 类的继承丶派生和多态

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年03月20日 19时11分41秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

mysql+err+1067_MySQL 5.7 Invalid default value for 'CREATE_TIME'报错的解决方法 2019-04-21
程序中mysql添加用户_MySQL添加用户的两种方法 2019-04-21
简述mysql安装过程_mysql安装的过程 2019-04-21
后端接口重定向_不用再等后端的接口啦!这个开源项目花 2 分钟就能模拟出后端接口... 2019-04-21
学mysql需要英语水平多高_大学英语专业挂科率高吗 2019-04-21
mysql group by实现_SQL数据分析之 group by 的实现原理 2019-04-21
mysql数据库大小如何查看器_如何用SQL命令查看Mysql数据库大小 2019-04-21
python 红黑树_红黑树-Python实现 | 学步园 2019-04-21
java string 日期格式_JAVA中String.format的用法 格式化字符串,格式化数字,日期时间格式化,... 2019-04-21
php显示json,使用 PHP 获取并解析 JSON 显示在页面中 2019-04-21
js php排序表格,javascript实现对表格元素进行排序操作_javascript技巧 2019-04-21
php sspi,php 内置的 web 服务器 php -s 2019-04-21
java生成结果集向量,如何解释H2o深度学习输出向量? 2019-04-21
matlab 曲线拟合求导,如何对matlab cftool拟合得到的cfit函数求导数 2019-04-21
matlab 50hzquchu,新手求消除50HZ工频干扰陷波滤波器源程序 2019-04-21
laravel没有route.php,Laravel中的RouteCollection.php中的NotFoundHttpException 2019-04-21
php服务端开启socket,php socket服务端能不能在网页端开启?而不是只能用CLI模式开启... 2019-04-21
php不需要也能输出,php 如何只输出最后生成的那个值?? 2019-04-21
php正则过滤sql关键字,使用正则表达式屏蔽关键字的方法 2019-04-21
php取整v,php取整方式分享 2019-04-21