实验4

实验二:这个函数的编写部分要用到循环的嵌套,但是发现并不满足要求,发现自己没有注意空格,然后自己又加入了一个循环才符合要求

#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明 
class Graph {
    public:
        Graph(char ch, int n);   // 带有参数的构造函数 
        void draw();     // 绘制图形 
    private:
        char symbol;
        int size;
};


#endif


/ 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
//       size和symbol是类Graph的私有成员数据 
void Graph::draw()
{  cin>>symbol>>size;
  for(int i=1;i<=size;i++)
  { 
   for(int k=size-i;k>=0;k--)
    {
        cout<<" ";
    }//设置空格 
    for(int j=1;j<=(2*i-1);j++)
        {
          cout<<symbol;
      }
         cout<<"\n";
  } //输出每一行的字符 
}


#include <iostream>
#include "graph.h"
using namespace std;


int main() {
    Graph graph1('*',5), graph2('$',7) ;  // 定义Graph类对象graph1, graph2 
    graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 
    graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    
    return 0; 
} 

实验三:这一题我做的不是太好,非常的繁琐,只是勉强完成了要求,此外还有就在的过程中最后main函数的实现过程中自己定义一个plus函数无法运行成功,我将plus换成fun1他就能运行了。。基础较差,不是太懂,希望大佬们解答下

#ifndef  Fraction_H 
#define  Fraction_H 
class Fraction
{
    public: Fraction(int t=0,int b=1);
            Fraction(Fraction&f);
            ~Fraction();
            void set_fraction(int t,int b);
            void show_fraction();
            double result();
    private:double top,bottom;
};



#endif



include "Fraction.h" 
#include <iostream>
using namespace std;
Fraction::Fraction(int t,int b):top(t),bottom(b)
{
    cout<<"the Fraction constructor is called..."
    <<endl;
}
Fraction::Fraction(Fraction&f):top(f.top),bottom(f.bottom)
{
    cout<<"the Fraction copyconstructor is called..."
    <<endl;
}
Fraction::~Fraction()
{
    cout<<"the Fraction destructor is called..."
    <<endl;
}
void Fraction::set_fraction(int t,int b) 
{    top=t;
     bottom=b;
    
}
void Fraction::show_fraction()
{
cout<<top<<"\n" <<"-"<<
"\n"<<bottom<<endl;    
}
double Fraction::result()
{
    return (top/bottom);
}

#include<iostream>
#include"Fraction.h"
using namespace std;
double fun1(Fraction x,Fraction y);
double fun2(Fraction x,Fraction y);
double fun3(Fraction x,Fraction y);
double fun4(Fraction x,Fraction y);
double fun5(Fraction x,Fraction y);
int main()
{
    Fraction a;
    Fraction b(5);
    Fraction c(1,5);
     a.show_fraction();
     b.show_fraction();
     c.show_fraction();
     cout<<fun1(b,c)<<endl;
     cout<<fun2(b,c)<<endl;
     cout<<fun3(b,c)<<endl;
     cout<<fun4(b,c)<<endl;
     cout<<fun5(b,c)<<endl;
    return 0;
}
double fun1(Fraction x,Fraction y)
{
    return(x.result()+y.result());
}
double fun2(Fraction x,Fraction y)
{
    return(x.result()-y.result());
}
double fun3(Fraction x,Fraction y)
{
    return(x.result()*y.result());
}
double fun4(Fraction x,Fraction y)
{
    return(x.result()/y.result());
}
double fun5(Fraction x,Fraction y)
{
    return(x.result()>y.result()?x.result():y.result());
}

运行结果如图

 

posted @ 2018-04-23 13:50  宇神  阅读(139)  评论(2)    收藏  举报