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

实验4

时间:2018-04-23 22:41:46      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ica   include   turn   return   div   inf   red   code   amp   

2.代码:

#ifndef GRAPH_H
#define GRAPH_H

 
class Graph {
    public:
        Graph(char ch, int n);   
        void draw();     
    private:
        char symbol;
        int size;
};


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

Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


void Graph::draw() {
    for (int i = 0; i < size; i++) {
        for (int j = i; j < (size-1); j++)        {cout << " ";}

        for (int j = 0; j < i; j++)                {cout << symbol;}

        cout << symbol;

        for (int j = 0; j < i; j++)                {cout << symbol;}

        for (int j = i; j < (size-1); j++)        {cout << " ";}

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


int main() {
    Graph graph1(*,5), graph2($,7) ;  
    graph1.draw(); 
    graph2.draw(); 
    int a;
    if (cin >> a) {
        return 0;
    }
} 

结果:

技术分享图片

3.代码:

class Fraction{
    public:
        Fraction(int a=0, int b=1);
        void compare(Fraction &p);
        void add(Fraction &p);
        void subtraction(Fraction &p);
        void multiplication(Fraction &p);
        void division(Fraction &p);
        void reduction(); 
        void show();
    private:
        int top;
        int bottom;
};
#include<iostream>
#include"fraction.h"
using namespace std;

Fraction::Fraction(int a, int b):top(a),bottom(b){
}

void Fraction::compare(Fraction &p){
    if(p.top>top){
        cout<<"前者<后者"<<endl; 
    }
    else if(p.top<=top){
        cout<<"前者>=后者"<<endl;
    }
}

void Fraction::add(Fraction &p){
    int b1=bottom,b2=p.bottom;
    top=top*b2;    bottom=bottom*b2;
    p.top=p.top*b1;    p.bottom=p.bottom*b1;
    top=top+p.top;
}

void Fraction::subtraction(Fraction &p){
    int b1=bottom,b2=p.bottom;
    top=top*b2;    bottom=bottom*b2;
    p.top=p.top*b1;    p.bottom=p.bottom*b1;
    top=top-p.top;
}

void Fraction::multiplication(Fraction &p){
    top=top*p.top;
    bottom=bottom*p.bottom;
}

void Fraction::division(Fraction &p){
    top=top*p.bottom;
    bottom=bottom*p.top;
}

void Fraction::reduction(){
    int a;
        if(top%bottom==0) a=bottom;
        else if(bottom%top==0) a=top;
        else 
        for(int i=1;i<(bottom/2)&&i<(top/2);i++)
        {
            if(bottom%i==0&&top%i==0) a=i;
        }
    top/=a;
    bottom/=a;
}

void Fraction::show(){
    cout<<"结果:"<<top<<"/"<<bottom<<endl;
}
#include<iostream>
#include"fraction.h"
using namespace std;

int main() {
    int m,n,x,y;
    cout<<"分数1:";
    cin>>m>>n;
    Fraction a(m,n);
    a.show();
    cout<<"分数2:"; 
    cin>>x>>y;
    Fraction b(x,y);
    b.show();
    a.compare(b);
    char input;
    cout<<"输入+ - * %:";
    cin>>input;
    switch(input){
        case +:
            a.add(b);
            break;
        case -:
            a.subtraction(b);
            break;
        case *:
            a.multiplication(b);
            break;
        case %:
            a.division(b);
            break;
    }
    a.reduction();
    a.show();
    return 0;
}

结果

 技术分享图片

技术分享图片

技术分享图片

技术分享图片

负数的计算没能想出来.

实验4

标签:ica   include   turn   return   div   inf   red   code   amp   

原文地址:https://www.cnblogs.com/krousar/p/8922048.html

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