首页 > 其他 > 详细

实验三

时间:2019-04-21 23:01:34      阅读:220      评论:0      收藏:0      [点我收藏+]
// 类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,m,k;
    for(i=1;i<=5;i++){
        for(j=1;j<=size-i;j++){
            cout<<ends;
        }
        for(k=1;k<=2*i-1;k++){
            cout<<symbol;
        }
        cout<<endl;
    }
    
    
    
    
    
    // 补足代码
    // ...
}
#ifndef GRAPH_H
#define GRAPH_H

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


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

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

技术分享图片技术分享图片

#ifndef FRACTION_H
#define FRACTION_H
class Fraction {
    public:
        Fraction(int top0=0,int bottom0=1);
        void polish();
        void add(Fraction a,Fraction b);
        void minus(Fraction a,Fraction b);
        void multiply(Fraction a,Fraction b);
        void division(Fraction a,Fraction b);
        void compare(Fraction a,Fraction b);
        void show();
        
    
    private:
        int top;//分子 
        int bottom;//分母 
};
#endif
#include"Fraction.h"
#include<iostream>
#include<cmath>
using namespace std;
Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){
}
//讨论分母
void Fraction::show()
{
    //分母为零
    if(bottom==0)
    cout<<"分母为零,无意义"<<endl;
    else
    {
        if(bottom==1)
        cout<<" "<<top<<endl;
        else
        {
            cout<<top<<"/"<<bottom<<endl;
        }
     } 
 } 
//最大公约数
int gongyue(int a,int b)
{
    int t;
    if(a<b)
    {
        t=a;
        a=b;
        b=t;
    }
    while(t=a%b)
    {
        a=b;
        b=t;
    }
    return b;
 } 
 //最小公倍数
 int gongbei(int a,int b)
 {
     int t=gongyue(a,b);
     return a*b/t;
  } 
void Fraction::polish()
{
    if(top>0&&bottom<0)
    {
        top=0-top;
        bottom=0-bottom;
    }
    else if(top<0&&bottom<0)
    {
        top=0-top;
        bottom=0-bottom;
    }
    else if(bottom==0)
    {
        cout<<"无意义"<<endl;
    }
    
}
//加法
void Fraction::add(Fraction a,Fraction b)
{
    bottom=gongbei(a.bottom,b.bottom);
    top=(bottom/a.bottom)*a.top+(bottom/b.bottom)*b.top;
    polish();
    show();
 } 
 //减法
 void Fraction::minus(Fraction a,Fraction b)
 {
     bottom=gongbei(a.bottom,b.bottom);
    top=(bottom/a.bottom)*a.top-(bottom/b.bottom)*b.top;
    polish();
    show();
     
 }
 //乘法
 void  Fraction::multiply(Fraction a,Fraction b)
 {
     top=a.top*b.top;
     bottom=a.bottom*b.bottom;
     polish();
     show();
 }
 //除法
 void Fraction::division(Fraction a,Fraction b) 
 {
     top=a.top*b.bottom;
     bottom=a.bottom*b.top;
     polish();
     show();
 }
 //比较
 void Fraction::compare(Fraction a,Fraction b)
 {
     double m,n;
     m=a.top/a.bottom;
     n=b.top/b.bottom;
     if(m>n)
     {
         cout<<"较大的数是"<<a.top<<"/"<<a.bottom;
         cout<<"较小的数是"<<b.top<<"/"<<b.bottom;
         
     }
     else if(m<n)
     {
         cout<<"较大的数是"<<b.top<<"/"<<b.bottom;
         cout<<"较小的数是"<<a.top<<"/"<<a.bottom;
     }
     else if(m=n)
     {
         cout<<"两个数相等"<<endl;
     }
 }
#include<iostream>
#include"Fraction.h"
using namespace std;
int main()
{
    //输入初始值
     Fraction a;
     cout<<"a:"<<endl;
     a.show();
     Fraction b(3,4);
     cout<<"b:"<<endl;
     b.show();
     Fraction c(5);
     cout<<"c:"<<endl;
     c.show();
     Fraction d;
     cout<<"+:"<<endl;
     d.add(a,b);
     cout<<"-:"<<endl;
     d.minus(a,c);
     cout<<"*:"<<endl;
     d.multiply(b,c);
     cout<<"/:"<<endl;
     d.division(c,b);
     cout<<"?:"<<endl;
     d.compare(b,c);
     return 0; 
}

技术分享图片

 

1.一开始在第二个程序中,就符号前应该空几个空格并不是很清楚,可能也是因为没有梳理算法,没有思考完整就直接开始写代码,思路并不是很清晰只是凭借感觉在写,在乱改,

后来老师指出这个问题后重新想好并写好。

2.第二个程序在自己写的过程中,出现了许许多多的错误,从单词拼错这种小错误,到程序逻辑上错误这种严重的错误都有,就连一开始在创建项目时也出现一些错误,在自己反复查找

和舍友的提点下完成了该程序。

实验三

原文:https://www.cnblogs.com/agsjg/p/10747398.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!