實驗4 類與對象2)


#include<iostream> 
using namespace std;

// 類Graph的聲明 
class Graph {
	public:
		Graph(char ch, int n);   // 帶有參數的構造函數 
		void draw(); 	// 繪制圖形 
	private:
		char symbol;
		int size;
};

// 類graph的實現


// 帶參數的構造函數的實現
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成員函數draw()的實現
// 功能:繪制size行,顯示字符為symbol的指定圖形樣式
//       size和symbol是類Graph的私有成員數據
void Graph::draw() {
	// 補足代碼,實現「實驗4.pdf」文檔中展示的圖形樣式
	for(int j=1;j<=size;j++)  //j控制行 
	{
	   for(int i=1;i<=size-j+1;i++) cout<<' '; //i控制每行中的每個數 
	   for(int i=1;i<=2*j-1;i++) cout<<symbol;
	cout<<endl;
    }
}



int main() {
	Graph graph1('*',5), graph2('$',7) ;  // 定義Graph類對象graph1, graph2 
	graph1.draw(); // 通過對象graph1調用公共接口draw()在屏幕上繪制圖形 
	graph2.draw(); // 通過對象graph2調用公共接口draw()在屏幕上繪制圖形
	
	return 0; 
} 

//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
// 類graph的實現,graph.cpp

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

// 帶參數的構造函數的實現
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}


// 成員函數draw()的實現
// 功能:繪制size行,顯示字符為symbol的指定圖形樣式
//       size和symbol是類Graph的私有成員數據
void Graph::draw() {
	// 補足代碼,實現「實驗4.pdf」文檔中展示的圖形樣式
	for(int j=1;j<=size;j++)
	{
	for(int i=size;i>=1;i--) cout<<' ';
	for(int i=1;i<=2*size-1;i++) cout<<symbol;
	cout<<endl;
    }
}
//主函數,main.cpp
#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()在屏幕上繪制圖形

	return 0;
}

2,

#include<iostream> 
using namespace std;
class Fraction{
	public:
		Fraction();
		Fraction(int t,int b);
		Fraction(int b);
		Fraction(Fraction &f0);
		void show();
		void add(Fraction &f0);
		void min(Fraction &f0);
		void mul(Fraction &f0);
		void div(Fraction &f0);
		void comp(Fraction &f0);
		
	private:
		int top;
		int bottom;
};
Fraction::Fraction():top(0),bottom(1){}
Fraction::Fraction(int t,int b):top(t),bottom(b){}
Fraction::Fraction(int b):top(1),bottom(b){}
void Fraction::show(){
	cout<<top<<"/"<<bottom<<endl;
}
void Fraction::add(Fraction &f0){
	top=top*f0.bottom+f0.top*bottom;
	bottom=bottom*f0.bottom;
	show();
}
void Fraction::min(Fraction &f0){
	top=top*f0.bottom-f0.top*bottom;
	bottom=bottom*f0.bottom;
	show();
}
void Fraction::mul(Fraction &f0){
	top=top*f0.top;
	bottom=bottom*f0.bottom;
	show();
}
void Fraction::div(Fraction &f0){
	top=top*f0.bottom;
	bottom=bottom*f0.top;
	show();
}
void Fraction::comp(Fraction &f0){
	double i,j;
	i=top/bottom;
	j=f0.top/f0.bottom;
	if(i>j) cout<<i<<">"<<j;
	else if(i<j) cout<<i<<"<"<<j;
    else cout<<i<<"="<<j; 
}
int main(){
	Fraction a;
	Fraction b(3,4);
	Fraction c(5);
	a.show();
	b.show();
	c.show();
	b.add(c);
	b.min(c);
	b.mul(c);
	b.div(c);
	b.comp(c);
	return 0;
}

我的運行結果,,,前面都正常,但是后面的減法用的是上面那個加法的結果,乘法用的是減法的,除法用的是乘法的,求教大佬,如何讓上一函數的結果不影響到下面?
還有一種《我不寫參量對象f0,在各類函數中具體化對象運算,應該就不會這么煩吧。。。》

對上面的錯誤進行修正(1.題目看錯,要求Fraction c(5);出來的結果是5/1,我搞成1/5了。 2.comp比較的話,之前總展示5>0,發現是我直接展示分數值大小,而沒有用show()顯示的格式

#include<iostream>
using namespace std;
int g,h;    //用來存放初始值,並且起到賦值更新的用法
class Fraction{     //類的定義
    public:          //外部接口
        Fraction();    //默認構造函數
        Fraction(int t,int b);  //構造函數
        Fraction(int t);        //同上,但是類似於上面的重載。
        Fraction(Fraction &f0);  //復制構造函數--對象的應用方面
        void show();             //show函數--顯示分數(格式)
        void add(Fraction &f0);  //以下都是用對象作為形參
        void min(Fraction &f0);
        void mul(Fraction &f0);
        void div(Fraction &f0);
        void comp(Fraction &f0);

    private:
        int top;
        int bottom;
};
//類的實現,加上函數變量賦初值,以下
Fraction::Fraction():top(0),bottom(1){}
Fraction::Fraction(int t,int b):top(t),bottom(b){}
Fraction::Fraction(int t):top(t),bottom(1){}
void Fraction::show(){
    cout<<top<<"/"<<bottom<<endl;
}
void Fraction::add(Fraction &f0){
 g=top;h=bottom;  //把top值中的3賦給g,bottom=4賦給h--用於下一函數運算前,把變量更新一下
    top=top*f0.bottom+f0.top*bottom; //通分--列個式子就知道
    bottom=bottom*f0.bottom;
    show();  //以分數形式輸出

}

void Fraction::min(Fraction &f0){
	top=g;bottom=h; //更新一下,就是top重新變回3,bottom變回4

    top=top*f0.bottom-f0.top*bottom;
    bottom=bottom*f0.bottom;
    show();
}
void Fraction::mul(Fraction &f0){
	top=g;bottom=h;    //下面都是如此,不多說。
    top=top*f0.top;
    bottom=bottom*f0.bottom;
    show();
}
void Fraction::div(Fraction &f0){
	top=g;bottom=h;
    top=top*f0.bottom;
    bottom=bottom*f0.top;
    show();
}
void Fraction::comp(Fraction &f0){
	top=g;bottom=h;
    double i,j;  //為了比較兩個數,設置i,j存放分數值 
    i=f0.top/f0.bottom;
    j=top/bottom;
    if(i>j) cout<<f0.top<<"/"<<f0.bottom<<">"<<top<<"/"<<bottom; //顯示出格式。 
    else if(i<j) cout<<f0.top<<"/"<<f0.bottom<<"<"<<top<<"/"<<bottom;
    else cout<<f0.top<<"/"<<f0.bottom<<"="<<top<<"/"<<bottom;
}
int main(){
    Fraction a;
    Fraction b(3,4);
    Fraction c(5);
    a.show();
    b.show();
    c.show();
    b.add(c);
    b.min(c);
    b.mul(c);
    b.div(c);
    b.comp(c);
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM