实验四

Graph类:

//main函数
#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()在屏幕上绘制图形
    
Graph graph3;
 graph3.draw();
 
  while(true){
   graph3.input();
   graph3.draw();
  }//重绘,自定义 return 0; }
#ifndef GRAPH_H
#define GRAPH_H

// 类Graph的声明 
class Graph {
    public:
        Graph(char ch, int n);   // 带有参数的构造函数 
Graph();// 不带参数的构造函数
void input(); //自定义输入 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
// 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

// 带参数的构造函数的实现 
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}

Graph::Graph(){
}//定义空类
// 成员函数draw()的实现 // 功能:绘制size行,显示字符为symbol的指定图形样式 // size和symbol是类Graph的私有成员数据 void Graph::draw() { int i, j; for(j=1;j<=size;j++) { for(i=0;i<=size-1-j;i++) cout<<' '; for(;i<size-1+j;i++) cout<<symbol; while(i++<2*size-1) cout<<' '; cout<<endl; } // 补足代码,实现「实验4.pdf」文档中展示的图形样式 } 

void Graph::input() {
cin >> symbol >> size;
}//自定义输入

分数类的实现:

Fraction

-top:int

-bottom:int

+Fraction(top:x,bottom:y)

+Fraction(top:0,bottom:1)

+Fraction(top:x,bottom:1)

+pri():void

+show():void

+transform():void

+calculate(const Fraction &):void

+compare(const Fraction &):void

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

int main()
{
   Fraction a;
   Fraction b(3,4);
   Fraction c(5);
   cout<<"a=";
   a.show();
   cout<<"b=";
   b.show();
   cout<<"c=";
   c.show();
 
   Fraction d;
   d.pri();
   cout<<"d=";
   d.show();

   Fraction e;
   e.pri();
   cout<<"e=";
   e.show();
 
   cout<<"e=";
   e.transform();
   
   cout<<"d+e ";
   e.calculate(d);
   cout<<"d-e ";
   e.calculate(d);
   cout<<"d*e ";
   e.calculate(d);
   cout<<"d/e ";
   e.calculate(d);/*四次分别测试加减乘除*/
 
   e.compare(d);
 
   return 0;
}

//Fraction类的定义 
class Fraction {
    public:
        Fraction(int x, int y);
        Fraction();
        Fraction(int x);
        void pri();//输入 
        void show();//输出 
        void transform();//转换小数 
        void calculate(const Fraction &a);//计算 
        void compare(const Fraction &a);//比较 
        
       private:
           int top;
           int bottom;
};
//Fraction类的实现
#include <iostream>
#include <cmath>
#include "Fraction.h"
using namespace std;

Fraction::Fraction(int x, int y):top(x), bottom(y){
    }
    
Fraction::Fraction():top(0), bottom(1){}
Fraction::Fraction(int x):top(x), bottom(1){}

void Fraction::pri(){
    cout << "请输入分子与分母:";
    cin >> top >> bottom ;
}
    
void Fraction::show(){
        if(bottom<0)
        {
            bottom=fabs(bottom);
            top=-top;
        }
        if(bottom!=1)  
      cout << top << '/' << bottom << endl;
   else
      cout << top << endl;

}
void Fraction::transform(){ float i; i = (float)top/(float)bottom; cout << i << endl; } void Fraction::calculate (const Fraction &a){ char ch; cout << "输入计算的符号:"; cin >> ch; Fraction b; int s ,i ,j; for(s=a.bottom;!(s%a.bottom==0&&s%bottom==0);s++){ }; i = s / a.bottom; j = s / bottom;//通分 if(ch=='+') { b.top = a.top * i + top * j; b.bottom = s; } else if(ch=='-') { b.top = a.top * i - top * j; b.bottom = s; } else if(ch=='*') { b.top = a.top * top; b.bottom = a.bottom * bottom; } else if(ch=='/') { b.top = a.top * bottom; b.bottom = top * a.bottom; } for(;!(b.top%s==0&&b.bottom%s==0);s--){ }; b.bottom/=s; b.top/=s;//约分 if(b.bottom<0) { b.bottom=fabs(b.bottom); b.top=-b.top; }    if(b.bottom!=1) 
      cout << b.top << '/' << b.bottom << endl;
   else
      cout << b.top << endl;
} void Fraction::compare(const Fraction &a){ int s ,i ,j; for(s=a.bottom;!(s%a.bottom==0&&s%bottom==0);s++){ }; i = s / a.bottom; j = s / bottom;    if(a.bottom!=1&&bottom!=1)
   {
      if((a.top * i - top * j)>0)
         cout << a.top << '/' << a.bottom << " > " << top << '/' << bottom << endl;
      else if((a.top * i - top * j)==0)
               cout << a.top << '/' << a.bottom << " = " << top << '/' << bottom << endl;
      else
         cout << a.top << '/' << a.bottom << " < " << top << '/' << bottom << endl;   
        }  
   else if(a.bottom!=1&&bottom==1)
   {
      if((a.top * i - top * j)>0)
         cout << a.top << '/' << a.bottom << " > " << top << endl;
      else if((a.top * i - top * j)==0)
               cout << a.top << '/' << a.bottom << " = " << top << endl;
      else
         cout << a.top << '/' << a.bottom << " < " << top << endl;   
        }
        else if(a.bottom==1&&bottom!=1)
        {
          if((a.top * i - top * j)>0)
         cout << a.top << " > " << top << '/' << bottom << endl;
      else if((a.top * i - top * j)==0)
               cout << a.top << " = " << top << '/' << bottom << endl;
      else
         cout << a.top << " < " << top << '/' << bottom << endl;
        }
        else
   {
      if((a.top * i - top * j)>0)
         cout << a.top << " > " << top << endl;
      else if((a.top * i - top * j)==0)
               cout << a.top << " = " << top << endl;
      else
         cout << a.top << " < " << top << endl;   
        }
}//无非就是将两个分数相减,看结果正负     

 

总结:这次实验用的大部分都是基本的知识点,但是结合在一起,再加上类,构造函数,多文件组织,上手的时候明显感到了有些力不从心,感觉自己都懂,但代码打起来就不一样了。第一题几乎没有工作量,但第二题就是一个小项目,打起来还蛮费劲的,但是收获还有蛮大的,在不断的修正,找资料中,这方面的知识的掌握好了许多。

原文地址:https://www.cnblogs.com/zhaoluolong/p/8906243.html