实验四

#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;
}


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

posted @ 2018-04-23 23:18  雪卿狂  阅读(167)  评论(3编辑  收藏  举报