实验三

Part2

#ifndef GRAPH_H
#define GRAPH_H

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


#endif
graph.h
// 类graph的实现
 
#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 i,j,k;
    for(i=0;i<size;i++)
    { for(j=0;j<=size-i-1;j++)
       cout<<" ";
      for(k=0;k<2*i+1;k++)
        cout<<symbol;
      cout<<endl;
    }
}
graph.cpp
#include <iostream>
#include "graph.h"
using namespace std;

int main() {
    Graph graph1('*',5);
    graph1.draw();
    
    system("pause");
    system("cls");
    
    Graph graph2('$',7);
    graph2.draw();

    system("pause");
    
    return 0; 
} 
main.cpp

 

Part3

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

 
 void Fraction::add(Fraction x,Fraction y){ 
     Fraction s;
    s.bottom=x.bottom*y.bottom;
    s.top=x.top*y.bottom+x.bottom*y.top;
    cout<<s.top<<"/"<<s.bottom<<endl; 
     
 }
 
 void Fraction::jian(Fraction x,Fraction y){
    Fraction s;
    s.bottom=x.bottom*y.bottom;
    s.top=x.top*y.bottom-x.bottom*y.top;
    cout<<s.top<<"/"<<s.bottom<<endl; 
    
}
 
 
 void Fraction::cheng(Fraction x,Fraction y){
    Fraction s;
    s.bottom=x.bottom*y.bottom;
    s.top=x.top*y.top;
    cout<<s.top<<"/"<<s.bottom<<endl; 
    
 }

void Fraction::chu(Fraction x,Fraction y){
    Fraction s;
    s.bottom=x.bottom*y.top;
    s.top=x.top*y.bottom;
    cout<<s.top<<"/"<<s.bottom<<endl; 
    
 }
 
 void Fraction::bj(Fraction x,Fraction y){
    int m;
    m=x.top*y.bottom-x.bottom*y.top;
    if(m<0)
        cout<<x.top<<"/"<<x.bottom<<"<"<<y.top<<"/"<<y.bottom;
    else if(m>0)
        cout<<x.top<<"/"<<x.bottom<<">"<<y.top<<"/"<<y.bottom;
        else
            cout<<x.top<<"/"<<x.bottom<<"="<<y.top<<"/"<<y.bottom;
    
 }
 
 void Fraction::show(){
    cout<<top<<"/"<<bottom<<endl;
}
fraction.cpp
#ifndef FRACTION_H
#define FRACTION_H
 
class Fraction{
    public:
        Fraction(int top0=0,int bottom0=1);
        void add(Fraction x,Fraction y);
        void jian(Fraction x,Fraction y);
        void cheng(Fraction x,Fraction y);
        void chu(Fraction x,Fraction y);
        void bj(Fraction x,Fraction y);
        void show();
    private:
        int top;
        int bottom;
 };


#endif
fraction.h
#include <iostream>
#include "fraction.h"
using namespace std;
int main(){
    Fraction a;
    cout<<"f1=";
    Fraction f1(4,5);
    cout<<"f2=";
    Fraction f2(3);
    cout<<"f3=";
    Fraction f3(2,0);
    a.bj(f1,f2);
    cout<<"f1+f2=";
    a.add(f1,f2);
    cout<<"f1-f2=";
    a.jian(f1,f2);
    cout<<"f1*f2=";
    a.cheng(f1,f2);
    cout<<"f1/f2=";
    a.chu(f1,f2);
    return 0;
}
main.cpp

 

 

 编程期间有出现这种错误,百度了还是看不懂,求解

 https://www.cnblogs.com/qiuqiuwr/p/10739483.html

https://www.cnblogs.com/mxueyyqx/p/10740652.html

https://www.cnblogs.com/knight04/p/10744911.html

posted @ 2019-04-21 17:43  黎黎0202  阅读(138)  评论(0编辑  收藏  举报