zoukankan      html  css  js  c++  java
  • 实验四

    #include<iostream>
    using namespace std;
    // 类Graph的声明 
    class Graph {
    	public:
    		Graph(char ch, int n);   // 带有参数的构造函数 
    		void draw(); 	// 绘制图形 
    	private:
    		char symbol;
    		int size;
    };
    // 带参数的构造函数的实现 
    Graph::Graph(char ch, int n): symbol(ch), size(n) {}
    // 成员函数draw()的实现
    // 功能:绘制size行,显示字符为symbol的指定图形样式 
    //       size和symbol是类Graph的私有成员数据 
    void Graph::draw() 
    {
    	int i,j;
    	for(i=0;i<=size;i++)
    	{
    	for(j=0;j<=size-i;j++)
    	cout<<" ";	
    	for(int j=0;j<2*i-1;j++)
                cout<<symbol;
            cout<<endl;
    }
    }
    int main() {
    	Graph graph1('*',5), graph2('$',7) ;  // 定义Graph类对象graph1, graph2 
    	graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形 
    	graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
    	
    	return 0; 
    } 
    

    #include<iostream>
    using namespace std;
    class Fraction
    {
    	public:
    		Fraction():top(0),bottom(1) {};
    		Fraction(int a,int b):top(a),bottom(b) {};
    		Fraction(int c):top(c),bottom(1){};
    		void add(Fraction &p);     
            void sub(Fraction &p);      
            void mul(Fraction &p);      
            void div(Fraction &p);
    		void display()
    		{
    			cout<<top<<"/"<<bottom<<endl; 
    		};  
    		void op();    
    		private:
    			int top;
    			int bottom;
    };
    void Fraction:: add(Fraction &p)
    {
    	top=top*p.bottom+p.top*bottom;
        bottom=bottom*p.bottom;
        void display();
        
    }
    void Fraction::sub(Fraction &p)
    {
    top=top*p.bottom-p.top*bottom;
        bottom=bottom*p.bottom;
    	void display();	
    	
    }
    void Fraction::mul(Fraction &p)
    {
    	top=top*p.top;
        bottom=bottom*p.bottom;
        void display();
        
    }
    void Fraction::div(Fraction &p)
    {
    	top=top*p.bottom;
        bottom=p.top*bottom;
        void display();
        
    }
    void Fraction::op()
    {
        cout<<"输入分子"<<endl;
        cin>>top;
        cout<<"输入分母"<<endl;
        cin>>bottom;
    }
    int main()
    {
    Fraction a;
    Fraction b(3,4);
    Fraction c(5);
    a.display();
    b.display();
    c.display();
    a.add(c);
    a.display();
    b.mul(c);
    b.display();
    return 0;
    }
    
    
    

    第一个代码还蛮简单的,第二个是真的有点懵,交作业前不习惯参考别人的代码,交上去之后我会参考一下别的同学的思路再找一下我第二个代码的错误,然后完善一下我代码的功能

  • 相关阅读:
    SA练习题总结-篇一
    树上距离(树形DP)
    Codeforces Round #633(Div.2) E. Perfect Triples
    Codeforces Round #633 (Div. 2) D.Edge Weight Assignment
    问题 B: FZB(树形DP+边记忆化)
    【Matlab】自学笔记——基础知识篇
    【Python科学计算】Numpy——ndarry
    退役总结
    [树的遍历]树的遍历(PTA)
    [stl]集合相似度(PTA)
  • 原文地址:https://www.cnblogs.com/Nicholastwo/p/8922647.html
Copyright © 2011-2022 走看看