实验三

实验三

实践性实验:

Part2:

基于已有信息,补足并扩充程序。 在graph文件夹里提供有三个文件: graph.h (类Graph的声明) graph.cpp (类Graph的实现) main.cpp (类Graph的测试): 定义Graph类对象,调用绘图接口绘制图形)

要求如下: 新建一个空项目,添加上述三个文件到项目中。 补足graph.h中类的成员函数draw()的实现,使得在main()中对类的测试能够实现以下效果:

主函数中测试代码:

Graph graph1('*', 5);

graph1.draw();

 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 #include <iostream>
 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     
12     Graph graph2('$',7);
13     graph2.draw();
14     
15     return 0; 
16 } 
main.cpp
 1 // 类graph的实现
 2  
 3 #include "graph.h" 
 4 #include <iostream>
 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 
16 int rank,row,a,t;
17 t=size;
18     for(row=1;row<=t;row++)
19     {
20     for(rank=1;rank<=size-1;rank++)
21     {
22         cout<<ends;
23     }
24     for(a=1;a<=2*row-1;a++)
25     {
26     cout<<symbol;
27     }
28     cout<<endl;
29     size=size-1;
30     }
31     }
graph.cpp

实验运行结果:

 


 

Part3 基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。

具体要求如下: 设计一个分数类 Fraction描述分数(两个整数的比值) 设计并实现接口,实现以下要求: 分数类Fraction的基本功能列表

定义Fraction类对象时,支持如下形式:

Fraction a; // 默认没有提供任何初始化数据时,分数对象的默认值为0/1

Fraction b(3,4); // 定义分数对象时,如果提供两个初始化参数,代表分数3/4

Fraction b(5); // 定义分数对象时,如果提供两个初始化参数,代表分数5/1

提示:通过编写构造函数实现,合理使用带有默认值的构造函数,同时,注意对分母为0的边界情况进判断

Fraction类对象能够进行如下操作: 加、减、乘、除运算 对两个分数值进行比较运算,返回其大小关系 分数的输入、输出

提示:通过定义成员函数实现。设计每一个成员函数时,从以下几个方面考虑:

成员函数的功能 是否需要形参和返回值?

如果需要,需要几个参数,参数的类型是什么?

返回值类型是什么?

充分考虑分母为0的边界情况处理。

程序如下:

  1 // 类Fraction的实现
  2  
  3 #include "Fraction.h" 
  4 #include <iostream>
  5 #include<cmath>
  6 using namespace std;
  7 
  8 //求最大公约数的函数,这样可以使分数计算后的分数约到最简 
  9 int reduction(int x,int y){
 10     int z = y;
 11     while(x%y!=0)
 12     {
 13         z = x%y;
 14         x = y;
 15         y = z;    
 16     }
 17     return z;
 18 }
 19 
 20 // 带参数的构造函数的实现 
 21 Fraction::Fraction(int Top, int Bottom): top(Top), bottom(Bottom) {
 22 }
 23 
 24 // 成员函数show()的实现
 25 // 功能:显示分数 
 26 void Fraction::show( ) {
 27 cout<<top<<"/"<<bottom<<endl; 
 28 }
 29 
 30 //判断“-”号位置,并作改进 
 31 int judge(int top1,int bottom1){
 32     if(top1>0&&bottom1>0)
 33         cout<<top1<<"/"<<bottom1<<endl;
 34     else if((top1<0&&bottom1>0)||(top1>0&&bottom1<0))
 35         cout<<"-"<<fabs(top1)<<"/"<<fabs(bottom1)<<endl;
 36     else if(top1==0)
 37         cout<<"0"<<endl;
 38 }
 39 
 40 //以下是实现加减乘除功能的成员函数内容 
 41 void Fraction::plus(Fraction fra1,Fraction fra2){
 42     int a,b,c;
 43     a = fra1.bottom;
 44     b = fra2.bottom;
 45     c = a%b;
 46     while ( c>0 )
 47     {
 48     a = b;
 49     b = c;
 50     c = a%b;
 51     }//求最大公倍数 
 52     a = fra1.bottom*fra2.bottom/b;
 53     int top1=a/fra1.bottom*fra1.top+a/fra2.bottom*fra2.top;
 54     int bottom1=a;
 55     int reduce=reduction(bottom1,top1);//约分 
 56     top1=top1/reduce;
 57     bottom1=bottom1/reduce;
 58     judge(top1,bottom1);
 59 }
 60 
 61 void Fraction::minus(Fraction fra1,Fraction fra2){
 62     int a,b,c;
 63     a = fra1.bottom;
 64     b = fra2.bottom;
 65     c = a%b;
 66     while ( c>0 )
 67     {
 68     a = b;
 69     b = c;
 70     c = a%b;
 71     }//求最小公倍数 
 72     a = fra1.bottom*fra2.bottom/b;
 73     int top1=a/fra1.bottom*fra1.top-a/fra2.bottom*fra2.top;
 74     int bottom1=a;
 75     if(top1==0)
 76     cout<<"0"<<endl;
 77     else
 78     {
 79     int reduce=reduction(bottom1,top1);//约分
 80     top1=top1/reduce;
 81     bottom1=bottom1/reduce; 
 82     judge(top1,bottom1);
 83     }
 84 }
 85 
 86 void Fraction::multiply(Fraction fra1,Fraction fra2){
 87     int top1=fra1.top*fra2.top;
 88     int bottom1=fra1.bottom*fra2.bottom;
 89     int reduce=reduction(bottom1,top1);//约分
 90     top1=top1/reduce;
 91     bottom1=bottom1/reduce; 
 92     judge(top1,bottom1);
 93     }
 94     
 95 void Fraction::divide(Fraction fra1,Fraction fra2){
 96     int top1=fra1.top*fra2.bottom;
 97     int bottom1=fra1.bottom*fra2.top;
 98     int reduce=reduction(bottom1,top1);//约分 
 99     top1=top1/reduce;
100     bottom1=bottom1/reduce;
101     judge(top1,bottom1);
102     }
103     
104 //分数比较大小
105 void compare(Fraction fra1,Fraction fra2){
106     int a,b,c;
107     a = fra1.bottom;
108     b = fra2.bottom;
109     c = a%b;
110     while ( c>0 )
111     {
112     a = b;
113     b = c;
114     c = a%b;
115     }//求最小公倍数 
116     a = fra1.bottom*fra2.bottom/b;
117     int top1=a/fra1.bottom*fra1.top-a/fra2.bottom*fra2.top;
118     int bottom1=a;
119     if(top1<0&&bottom1<0)
120     cout<<fra1.top<<"/"<<fra1.bottom<<"大于"<<fra2.top<<"/"<<fra2.bottom<<endl;
121     else if((top1<0&&bottom1>0)||(top1>0&&bottom1<0)) 
122     cout<<fra1.top<<"/"<<fra1.bottom<<"小于"<<fra2.top<<"/"<<fra2.bottom<<endl;
123     else if(top1==0)
124     cout<<fra1.top<<"/"<<fra1.bottom<<"等于"<<fra2.top<<"/"<<fra2.bottom<<endl;
125 }
Fraction.cpp
 1 #ifndef FRACTION_H
 2 #define FRACTION_H
 3 
 4 // 类Fraction的声明 
 5 class Fraction {
 6     public:
 7         Fraction(int Top=0,int Bottom=1);// 带有参数的构造函数 
 8         void plus(Fraction fra1,Fraction fra2);
 9         void minus(Fraction fra1,Fraction fra2);
10         void multiply(Fraction fra1,Fraction fra2);
11         void divide(Fraction fra1,Fraction fra2);
12         void show();     // 输出分数
13         friend void compare(Fraction fra1,Fraction fra2);
14         
15     private:
16         int top;
17         int bottom;
18 };
19 
20 
21 #endif
Fraction.h
 1 #include <iostream>
 2 #include "Fraction.h"
 3 using namespace std;
 4 
 5 int main() {
 6     //Fraction
 7     Fraction f1;
 8     cout<<"分数的初始值为:";
 9     f1.show();//输出分数的初始化值 
10     Fraction f2(3,4);
11     Fraction f3(5);
12     cout<<"给出两个未经简化运算的分数:" <<endl; 
13     f2.show();
14     f3.show(); 
15     Fraction f4;
16     //将两个分数的加减乘除运算后的结果赋给e并在每次运算后都显示出结果 
17     cout<<"这两个分数相加为:";
18     f4.plus(f2,f3);
19     cout<<"这两个分数相减为:"; 
20     f4.minus(f2,f3);
21     cout<<"这两个分数相乘为:";
22     f4.multiply(f2,f3);
23     cout<<"这两个分数相除为:";
24     f4.divide(f2,f3);
25     cout<<"比较两个数的大小:";
26     compare(f2,f3);
27     Fraction f7;
28     Fraction f5(4,1);
29     Fraction f6(4);
30     cout<<"给出两个未经简化运算的分数:" <<endl;
31     f5.show();
32     f6.show(); 
33     //将两个分数的加减乘除运算后的结果赋给e并在每次运算后都显示出结果 
34     cout<<"这两个分数相加为:";
35     f7.plus(f5,f6);
36     cout<<"这两个分数相减为:"; 
37     f7.minus(f5,f6);
38     cout<<"这两个分数相乘为:";
39     f7.multiply(f5,f6);
40     cout<<"这两个分数相除为:";
41     f7.divide(f5,f6);
42     cout<<"比较两个数的大小:";
43     compare(f5,f6);
44     
45     return 0; 
46 }
main.cpp

实验运行结果如下:

 

 


实验结论:

对于类中成员函数与主函数中的对应关系存在不明确的地方,很多时候错误都显示主函数中要执行的操作无法准确对应到类的成员函数。

对于第三个实验,还有可以完善的地方,比如说当显示的分数的分母为1时,可以进行省略操作,我尝试了用if语句来判断分数的分母是否为1,但是不知道是哪里的问题,到最后输出的分数即使分子为1,也无法省去分母,直接显示出一个整数。同时第三个程序的过程写得过于繁琐了。

 

实验中参考的个把网站:

https://blog.csdn.net/jian_yun_rui/article/details/61916594    关于c_str()

https://zhidao.baidu.com/question/421481811.html   关于system(“pause”)

https://zhidao.baidu.com/question/72684822.html    关于system(”color xx”)

 

互评地址:

https://www.cnblogs.com/jyf13/

https://www.cnblogs.com/aitxy899/

https://www.cnblogs.com/wsggwsc/

 

转载于:https://www.cnblogs.com/DADABu-21/p/10732017.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值