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

实验3

时间:2019-04-24 00:12:37      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:gif   bsp   action   图形   for   ace   closed   eve   image   

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
技术图片
#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
技术图片
// 类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;j++)
     cout<<" ";
  for( k=0;k<2*i-1;k++)
     cout<<symbol;
  cout<<endl;
 }
}
graph.cpp

 

技术图片

技术图片

part3

技术图片
#include <iostream>
#include "fraction.h"
using namespace std;
int main()
{
 Fraction a;
 cout<<"a="; 
 a.show();
 Fraction b(3,4);
 cout<<"b=";
 b.show();
 Fraction c(5);
 cout<<"c=";
 c.show();
 Fraction d;
 cout<<"+:"<<endl;
    d.add(a,b);
    cout<<"-:"<<endl;
    d.sub(a,c);
    cout<<"*:"<<endl;
    d.mul(b,c);
    cout<<"/:"<<endl;
    d.div(c,b);
    c.com(a);

 return 0;
}
main.cpp
技术图片
#include<iostream>
#include"fraction.h"
using namespace std;
void Fraction::show(){
  cout << top << "/" << bottom << endl;
}
void Fraction::add(Fraction &a, Fraction &b){
    fraction c;
    c.top=a.top*b.bottom+b.top*a.bottom;
    c.bottom=a.bottom*b.bottom;
    cout<<"add: "<<c.top<<"/"<<c.bottom<<endl;    
}
void Fraction::sub(Fraction &a, Fraction &b){
    int comm,t,r,f1,f2;
    if(a.bottom<b.bottom)
    {
      t=a.bottom;
      a.bottom=b.bottom;
      b.bottom=t;
      }
    r=a.bottom%b.bottom;
    while(r!=0)
    {
      a.bottom=b.bottom;
      b.bottom=r;
      r=a.bottom%b.bottom;
      }
    comm=a.bottom*b.bottom/b.bottom;
    f1=a.top*(comm/a.bottom)-b.top*(comm/b.bottom);
    f2=comm;
    cout<<"sub: "<<f1<<"/"<<f2<<endl;    
}
void Fraction::mul(Fraction &a, Fraction &b){
    int f1,f2;
    f1=a.top*b.top;
    f2=a.bottom*b.bottom;
    cout<<"mul: "<<f1<<"/"<<f2<<endl;
}
void Fraction::div(Fraction &a, Fraction &b){
    int f1,f2;
    f1=a.top*b.bottom;
    f2=b.top*a.bottom;
    cout<<"div: "<<f1<<"/"<<f2<<endl;
}

void Fraction::com(Fraction &a, Fraction &b){
    int comm,t,r,f1,f2;
    if(a.bottom<b.bottom)
    {
      t=a.bottom;
      a.bottom=b.bottom;
      b.bottom=t;
      }
    r=a.bottom%b.bottom;
    while(r!=0)
    {
      a.bottom=b.bottom;
      b.bottom=r;
      r=a.bottom%b.bottom;
      }
    comm=a.bottom*b.bottom/b.bottom;
    f1=a.top*(comm/a.bottom)-b.top*(comm/b.bottom);
    if(f1<0)
      cout<<"com: "<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
    else if(f1>0)
            cout<<"com: "<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
         else
            cout<<"com: "<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl;
}
fraction.cpp
技术图片
#ifndef Fraction_H
#define Fraction_H
class Fraction {
    public:
        Fraction(int i=0, int j=1){
            top=i;
            bottom=j;
        }
        void add(Fraction &a, Fraction &b);
        void sub(Fraction &a, Fraction &b);
        void mul(Fraction &a, Fraction &b);
        void div(Fraction &a, Fraction &b); 
        void com(Fraction &a, Fraction &b);
        void show();
    private:
        int top;
        int bottom;
};
#endif
fraction.h

出不了结果

实验3

标签:gif   bsp   action   图形   for   ace   closed   eve   image   

原文地址:https://www.cnblogs.com/AliceMaestra/p/10759864.html

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