Live2d Test Env

实验三

1.Graph类的实现:

代码:

“Graph.h”

#ifndef GRAPH_H
#define GRAPH_H
// 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
private:
char symbol;
int size;
};
#endif
#include "graph.h"//Graph.cpp
#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;
}
}

main.cpp

#include <iostream>
#include "graph.h"
using namespace std;
int main() {
Graph graph1('*',5);
graph1.draw();
system("pause");
Graph graph2('$',7);
graph2.draw();
return 0;
}

效果图:

2. 基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。 具体要求如下: 设计一个分数类Fraction描述分数(两个整数的比值):

代码:

Graph.h

#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
class Fraction{
public:
Fraction();
Fraction(int t,int b);
Fraction(int t);
        void show();
        void add(Fraction &f);
        void min(Fraction &f);
        void mul(Fraction &f);
        void chu(Fraction &f);
        void com(Fraction &f);
        
        private:
            int top;
            int bot;
};
#endif

Graph.cpp:

#incldue<iostream>
#include"Fraction.h"
using namespace std;
Fraction::Fraction():top(0),bot(1){}
Fraction::Fraction(int t,int b):top(t),bot(b){}
Fraction::Fraction(int t):top(t),bot(1){} 

void Fraction::show(){
    if(top!=0)
    cout<<top<<"/"<<bot<<endl;
    else
    cout<<"0"<<endl; 
}
void Fraction::add(Fraction &f)
{
    Fraction re;
    re.bot=bot*f.bot;
    re.top=top*f.bot+f.top*bot;
    re.show();
}
void Fraction::min(Fraction &f)
{
    Fraction re;
    re.bot=bot*f.bot;
    re.top=top*f.bot-f.top*bot;
    re.show();
}
void Fraction::mul(Fraction &f)
{
    Fraction re;
    re.bot=bot*f.bot;
    re.top=top*f.top;
    re.show();
}
void Fraction::chu(Fraction &f)
{
    Fraction re;
    re.bot=bot*f.top;
    re.top=top*f.bot;
    re.show();
}
void Fraction::com(Fraction &f)
{
    f.top=top*f.bot-f.top*bot;
    if(f.top>0)
    cout<<"第一个数大于第二个数。"<<endl;
    else if(f.top==0)
    cout<<"两个数一样大。"<<endl;
    else
    cout<<"第一个数小于第二个数。"<<endl; 
}

mian.cpp:

int main()
{
    Fraction a;
    a.show();
    Fraction b(3,4);
    b.show();
    Fraction c(5);
    c.show();
    int d=1000;
    while(d--){
    cout<<"请输入第一个数的分子和分母:"<<endl; 
    int m,n,r,s;
    cin>>m>>n;
 Fraction f1(m,n);
 cout<<"请输入第二个数的分子和分母:"<<endl; 
 cin>>r>>s;
 Fraction f2(r,s);
 char p;
  cout<<"选择一种运算:"<<endl;
 cin>>p;

 switch(p)
 {
     case'+':f1.add(f2);break;
     case'-':f1.min(f2);break;
     case'*':f1.mul(f2);break;
     case'/':f1.chu(f2);break;
     case'c':f1.com(f2);break;
 }
}
 return 0;
}

效果图:

实验发现:

在之前的complex类中函数add:

 

void Complex::add(Complex c)
{
    real+=c.real;
    imaginary+=c.imaginary;
}

而现在的add:

 

void Fraction::add(Fraction &f)
{
    Fraction re;
    re.bot=bot*f.bot;
    re.top=top*f.bot+f.top*bot;
    re.show();
}

 

 

 

写的时候忘了上一次的写法,觉得加法的实现应该与复制构造函数一样,用&f,但突然想到之前的complex类中并没有用到&,于是试着去掉&,发现并不影响程序的实现,但不知道为什么会这样。

实验结论

1.没有实现数的化简,因为在类中嵌套自定义函数没能成功。

2.构造函数与要求一一对应即可。

3.定义的变量应与其充当的角色相互对应,以免造成不必要的误解。

 

posted @ 2019-04-18 09:15  lszz  阅读(229)  评论(2)    收藏  举报