码迷,mamicode.com
首页 > 其他好文 > 详细

实验4

时间:2018-04-22 16:07:26      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:rbo   draw   div   实验   pause   源码   技术   oid   private   

1.Graph文件

(1)graph.h

源码:

1 class Graph{
2     public:
3         Graph(char a,int l);
4         void draw();
5     private:
6         char appoint;
7         int linage;
8 };

(2)类的实现.cpp

源码:

#include"Graph.h"
#include<iostream>
using namespace std;
Graph::Graph(char a,int l):appoint(a),linage(l){
}
void Graph::draw()
{
    for(int i=1;i<=linage;i++)
    {
        for(int j=linage-i;j>0;j--)
        {
            cout<<" ";
        }
        for(int j=1;j<=2*i-1;j++)
        {
            cout<<appoint;
        }
        cout<<endl;
    }
}

(3)main.cpp

源码:

#include <iostream>
#include"Graph.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
    Graph graph1(*,5);
    graph1.draw();
    Graph graph2($,7);
    graph2.draw();
    return 0;
    
}

 

运行结果:

技术分享图片

 

关于draw的算法:

1.输入的数字代表有几行

2.第n行字符前的空格数为linage-1,输出空格

3.输出字符,第n行的字符个数为2*n-1个,换行。

 

 

 

 

 

 

2.Fraction文件

(1)Fraction.h

源码:

 1 class Fraction{
 2     public:
 3         Fraction();
 4         Fraction(int t);
 5         Fraction(int t,int b);
 6         int gettop(){return top;}
 7         int getbottom(){return bottom;}
 8         void plus(Fraction);
 9         void minus(Fraction);
10         void multiply(Fraction);
11         void divide(Fraction);
12         void output();
13     private:
14         int top;
15         int bottom;
16 };

(2)类的实现.cpp

源码:

 1 #include"Fraction.h"
 2 #include<iostream>
 3 using namespace std;
 4 Fraction::Fraction():top(0),bottom(1){
 5 }
 6 Fraction::Fraction(int t):top(t),bottom(1){
 7 }
 8 Fraction::Fraction(int t,int b):top(t),bottom(b){
 9 }
10 void Fraction::output(){
11     cout<<top<<"/"<<"("<<bottom<<")";
12 }
13 void Fraction::plus(Fraction f){
14     int rtop,rbottom;
15     rtop=top*f.getbottom()+f.gettop()*bottom;
16     rbottom=bottom*f.getbottom();
17     Fraction result(rtop,rbottom);
18     result.output();
19 }
20 void Fraction::minus(Fraction f){
21     int rtop,rbottom;
22     rtop=top*f.getbottom()-f.gettop()*bottom;
23     rbottom=bottom*f.getbottom();
24     Fraction result(rtop,rbottom);
25     result.output();
26 }
27 void Fraction::multiply(Fraction f){
28     int rtop,rbottom;
29     rtop=top*f.gettop();
30     rbottom=bottom*f.getbottom();
31     Fraction result(rtop,rbottom);
32     result.output();
33 }
34 void Fraction::divide(Fraction f){
35     int rtop,rbottom;
36     rtop=top*f.getbottom();
37     rbottom=bottom*f.gettop();
38     Fraction result(rtop,rbottom);
39     result.output();
40 }

(3)main.cpp

源码:

#include <iostream>
#include"Fraction.h" 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
    Fraction a;
    Fraction b(3,4);
    Fraction c(5);
    b.plus(c);
    cout<<endl;
    b.minus(c);
    cout<<endl;
    b.multiply(c);
    cout<<endl;
    b.divide(c);  
    return 0;
}

 

运行结果:

技术分享图片

 

实验4

标签:rbo   draw   div   实验   pause   源码   技术   oid   private   

原文地址:https://www.cnblogs.com/ditongwoshang/p/8907223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!