zoukankan      html  css  js  c++  java
  • c++实验4 类 2(有点复杂,注释写不过来)

    #include<iostream>
    #include<vector>
    #include"fraction.h"
    using namespace std;
    int main()
    {
        int k,m,n;
        char q;
        Fraction a;
        Fraction b(3,1);  
        Fraction c(5);
        cout<<"make sure of the number of the fraction:";
        cin>>k;    
        cout<<endl;
        vector<Fraction> p(k);
        for(int i=0;i<k;i++){
            cout<<"set top and bottom:";
            cin>>m>>n;
            if(n!=0){
                p[i].gettb(m,n);
                p[i].legal();
            }
            else{
                cout<<"wrong value";
                return 0;
            } 
        }
        cout<<"choose two out of them from 1 to k:\n";
        cin>>m>>n;
        Fraction l;
        cout<<"please decide add,compare,mutiply,subtract,divide,output:(A,C,M,S,D,O)\n";
        while(cin>>q){
        switch(q){
            case 'A':l=p[m-1]+p[n-1];l.puttb();break;
            case 'S':l=p[m-1]-p[n-1];l.puttb();break;
            case 'D':l=p[m-1]/p[n-1];l.puttb();break;
            case 'M':l=p[m-1]*p[n-1];l.puttb();break;
            case 'C':
            {
                if(compare(p[m-1],p[n-1])>0){
                    cout<<"the bigger one is:";
                    p[m-1].puttb();
                }        
                else if(compare(p[m-1],p[n-1])<0){
                    cout<<"the bigger one is:";
                    p[n-1].puttb();
                }
                else 
                    cout<<"they are equal"<<endl;
                    break;
            }
            case 'O':cout<<p[m-1].val()<<" "<<p[n-1].val()<<endl;break;
        }
        }
        return 0;
    }
    #include<iostream>
    #include<cstdio>
    #include"fraction.h"
    #include<cmath>
    int _gcd_(int a,int b)
    {
        int max,min,k;
        max=a>b?a:b;
        min=a>b?b:a;
        if(min!=0){
            while(min!=0)
            {
                k=max%min;
                max=min;
                min=k;
            }
        return max;    
        }
        else
        return 1;    
    } 
    using namespace std;
    Fraction::Fraction(){
        top=0;
        bottom=1;
    } 
    Fraction::Fraction(int a,int b){   
        top=a;
        if(b!=0) bottom=b;
        else cout<<"wrong value b";
    }
    void Fraction::gettb(int a,int b){
        top=a;
        bottom=b;
    }
    void Fraction::puttb(){
        cout<<top<<"/"<<bottom<<endl;
    }
    void Fraction::legal(){
        if(top<0&&bottom<0){
                top=-top;
                bottom=-bottom;
        }
        if(top>0&&bottom<0){
                top=-top;
                bottom=-bottom;
        }
        int k;
        k=_gcd_(abs(top),abs(bottom));
        top=top/k;// gcc 编译器 可以用 _gcd_(a,b) 
        bottom=bottom/k;    
    }
    Fraction Fraction::operator+(const Fraction &a)
    {
        Fraction b;
        b.top=top*a.bottom+a.top*bottom;
        b.bottom=a.bottom*bottom;
        b.legal();
        return b;
    }
    Fraction Fraction::operator-(const Fraction &a)
    {
        Fraction b;
        b.top=top*a.bottom-a.top*bottom;
        b.bottom=a.bottom*b.bottom;
        b.legal();
        return b;
    }
    Fraction Fraction::operator/(const Fraction &a)
    {
        Fraction b;
        b.top=top*a.bottom;
        b.bottom=bottom*a.top;
        b.legal();
        return b;
    }
    Fraction Fraction::operator*(const Fraction &a)
    {
        Fraction b;
        b.top=top*a.top;
        b.bottom=bottom*a.bottom;
        b.legal();
        return b;
    }
    double Fraction::val()
    {
        double k;
        k=double(top)/bottom;
        return k;
    }
    int compare(Fraction &a,Fraction &b)
    {
        double t;
        t=(a-b).val();
        if(t>0)return 1;
        else if(t<0)return -1;
        else return 0;
    }
    class Fraction{
        private:
            int top;
            int bottom;
        public:
            Fraction();                            
            Fraction(int a,int b=1);    
            void gettb(int a,int b);
            void puttb();
            void legal();
            double val();
            Fraction operator+(const Fraction &a);
            Fraction operator-(const Fraction &a);
            Fraction operator/(const Fraction &a);
            Fraction operator*(const Fraction &a);
            friend int compare(Fraction &a,Fraction &b);    
    };        

    上面是对Fraction的实现

    #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()在屏幕上绘制图形
        char ch;int n;
        cout<<"reset symbol:";
        cin>>ch;
        cout<<"and your size:";
        cin>>n;
        graph1.getype(ch,n);
        graph1.draw();
        return 0; 
    } 
    #ifndef GRAPH_H
    #define GRAPH_H
    
    // 类Graph的声明 
    class Graph {
        public:
            Graph(char ch, int n);   // 带有参数的构造函数 
            void draw();     // 绘制图形 
            void getype(char ch,int n);
        private:
            char symbol;
            int size;
    };
    
    
    #end// 类graph的实现
     
    #include "graph.h" 
    #include <iostream>
    using namespace std;
    
    // 带参数的构造函数的实现 
    Graph::Graph(char ch, int n): symbol(ch), size(n) {
    }
    void Graph::getype(char ch,int n)
    {
        symbol=ch;size=n;    
    }
    // 成员函数draw()的实现
    // 功能:绘制size行,显示字符为symbol的指定图形样式 
    //       size和symbol是类Graph的私有成员数据 
    void Graph::draw() {
        // 补足代码,实现「实验4.pdf」文档中展示的图形样式 
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<2*size-1;j++)        
            {                                
                if(j>=size-1-i&&j<size+i)    
                cout<<symbol;               
                else                       
                cout<<" ";                   
            }                                
            cout<<endl;                        
        }                                     
    }

    以上是对类graph的实现

  • 相关阅读:
    Android 项目结构图
    MySQL的简单使用
    MySql简易配置
    SQL Server 2008 R2 错误代码:233
    C#设置输入框只输入数字
    【学习笔记】JAva编程思想之多态
    【Java之对象清理】finalize()的用途
    【错误总结】java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
    【学习笔记】Struts2之配置处理结果
    【学习笔记】Struts2之一个Action包含多个控制处理逻辑
  • 原文地址:https://www.cnblogs.com/tacore/p/8922386.html
Copyright © 2011-2022 走看看