实验四

内容一

:graph

如图为题目

#include<iostream>   /*声明*/
class graph
{
    public :                      /*外部接口*/
        graph(char ch,int l);
        void draw();
    private :
        char c;
        int line;
};

 文件graph.cpp

#include"graph.h"
#include<iostream>
using namespace std;
graph::graph(char ch,int l){
    c=ch;
    line=l;
}
void graph::draw(){
    int i,j;
    for(i=0;i<line;i++)          /*第i-1行*/
    {
        for(j=0;(j<line-i-1);j++)   /*输出空格*/
        {
            cout<<' ';
        }
        for(j=0;(j<2*i+1);j++)  /*输出字符*/
        {
            cout<<c;
        }
        cout<<endl;
    }
}

main.cpp

#include"graph.h"
#include<iostream>
#include<stdlib.h> 
using namespace std;
int main()
{
    cout<<"决定控制台颜色"<<endl;
    cout<<"0 = 黑色       8 = 灰色"<<endl;
    cout<<"1 = 蓝色       9 = 淡蓝色"<<endl;
    cout<<"2 = 绿色       A = 淡绿色"<<endl;
    cout<<"3 = 湖蓝色     B = 淡浅绿色"<<endl;
    cout<<"4 = 红色       C = 淡红色"<<endl;
    cout<<"5 = 紫色       D = 淡紫色"<<endl;
    cout<<"6 = 黄色       E = 淡黄色"<<endl;
    cout<<"7 = 白色       F = 亮白色"<<endl; 
    char se[10];
    char c[2];
    cin>>c;
    sprintf(se ,"color %s",c); 
    system(se);
    char m;          /*输入内容*/
    int n,a=1;           /*输入行数*/ 
    while(a){
    cout<<"请输入字符和行数"<<endl;
    cin>>m>>n;
    graph g1(m,n);
    g1.draw();
    cout<<"继续进行,输入1,关闭程序,输入0"<<endl;
    cin>>a;
    }

    return 0;
}

拓展3我就做不出来了。在做这次实验的时候我是不停的百度,逛了各个贴吧才学会了拓展的问题,如果有什么错误的,还请在评论的时候指正。

fraction.h

class fraction{
    public :              /*外部接口*/
        fraction();       /*构造函数*/
        fraction(int t,int b);  /*函数重载*/
        fraction(int t);        /*函数重载*/
        void show();            /*展示*/
        add(fraction &f1);        /**/
        sub(fraction &f1);        /**/
        mul(fraction &f1);        /**/
        div(fraction &f1);        /**/
        compare(fraction &f1); 
    private :
        int top,bottom;
}; 

fraction.cpp

#include<iostream>
using namespace std;
fraction::fraction(){
    top=0;
    bottom=1;
}
fraction::fraction(int t){
    top=t;
    bottom=1;
}
fraction::fraction(int t,int b){
    top=t;
    bottom=b;
}
void fraction::add(fraction &f1)            /**/
{
    cout << top * f1.bottom + f1.top * bottom
        << "/" << bottom * f1.bottom << endl;
}

void fraction::sub(fraction &f1)       /**/
{
    cout << top * f1.bottom - f1.top * bottom
        << "/" << bottom * f1.bottom << endl;
}

void fraction::mul(fraction &f1)       /**/
{
    cout << top * f1.top << "/"
        << bottom * f1.bottom << endl;
}

void fraction::div(fraction &f1)     /**/
{
    cout << top * f1.bottom << "/"
        << bottom * f1.top << endl;
}

void fraction::compare(fraction &f1)        /*比较大小*/
{
    if (top * f1.bottom > bottom * f1.top)
        cout << top << "/" << bottom << endl;
    else if (top * f1.bottom < bottom * f1.top)
        cout << f1.top << "/" << f1.bottom << endl;
    else if (top * f1.bottom == bottom * f1.top)
        cout << "一样大" << endl;
}
void fraction::show()
{
    cout << top << "/" << bottom << endl;
}

main.cpp

#include <iostream>
#include"fraction.h"
using namespace std;
int main() {
    fraction a;
    fraction b(3,4);
    fraction c(5);
    cout<<"输出测试"<<endl;
    cout<<"分数a是:";  a.show();  cout<<endl;
    cout<<"分数b是:";  b.show();  cout<<endl;
    cout<<"分数c是:";  c.show();  cout<<endl;
    cout<<"计算测试"<<endl;
    cout<<"a+b="<<endl;a.add(b);
    cout<<"a-b="<<endl;a.sub(b);
    cout<<"a*b="<<endl;a.mul(b);
    cout<<"a/b="<<endl;a.div(b);
    cout<<"比较测试"<<endl;
    cout<<"a和b"<<a.compare(b)<<endl;
    return 0;
}

 

原文地址:https://www.cnblogs.com/spring-winds/p/8922440.html