天天看點

第九周實驗報告3

實驗目的:接第八周任務3,定義分數類中的<<和>>運算符的重載,實作輸入和輸出,改造原程式中對結果的顯示方式,是程式度起來更自然。

實驗代碼:

#include<iostream> 

using namespace std;
  
class CFraction  
{  
private:  
    int nume; // 分子   
    int deno; // 分母   
public:      
    void Simplify(); //化簡(使分子分母沒有公因子) 
	CFraction operator + (CFraction  &c);
	CFraction operator - (CFraction  &c);
	CFraction operator * (CFraction  &c);
	CFraction operator / (CFraction  &c);

	CFraction operator + ();
	CFraction operator - ();

	bool operator > (CFraction &c);
	bool operator < (CFraction &c);
	bool operator >= (CFraction &c);
	bool operator <= (CFraction &c);
	bool operator == (CFraction &c);
	bool operator != (CFraction &c);

	friend ostream& operator << (ostream&, CFraction&);
    friend istream& operator >> (istream&, CFraction&);

};

ostream& operator << (ostream& output, CFraction& cf)
{
	cout << cf.nume << "/" << cf.deno << endl;
	return output;
}
istream& operator >> (istream& input, CFraction& cf)
{
	cout << "請輸入要置分數的分子和分母:";
	input >> cf.nume >> cf.deno;
	return input;
}
  
void main()  
{  
    CFraction cf1, cf2, cf3;  
    cin >> cf1 >> cf2;
	cout << "cf1 = " << cf1;
    cout << "cf2 = " << cf2;

	cf3 = cf1 + cf2;
	cout << "cf1 + cf2 = " ;
    cf3.Simplify();

	cf3 = cf1 - cf2;
	cout << "cf1 - cf2 = " ;
	cf3.Simplify();

	cf3 = cf1 * cf2;
	cout << "cf1 * cf2 = ";
	cf3.Simplify();

	cf3 = cf1 / cf2;
	cout << "cf1 / cf2 = ";
	cf3.Simplify();

	cout << "對cf1取正得: ";
	cf1 = + cf1;
	cout << "對cf2取反得: ";
	cf2 = - cf2;

	if(cf1 > cf2) cout << "cf1 > cf2" << endl;
	if(cf1 < cf2) cout << "cf1 < cf2" << endl;
	if(cf1 == cf2) cout << "cf1 = cf2" << endl; 
	if(cf1 != cf2) cout << "cf1 ≠ cf2" << endl;
	if(cf1 >= cf2) cout << "cf1 ≥ cf2" << endl;
	if(cf1 <= cf2) cout << "cf1 ≤ cf2" << endl;

	system("pause");
}   
  
void CFraction::Simplify()  
{  
    int a[10], j = 0;  
    for(int i = 1; i <= nume; i++)  
    {  
        if(nume % i == 0)  
        {  
            a[j] = i;  
            j++;  
        }  
    }  
    //定義a[10]數組用于存儲nume的因數   
    int b[10], m = 0;  
    for(int n = 1; n <= deno; n++)  
    {  
        if(deno % n == 0)  
        {  
            b[m] = n;  
            m++;  
        }  
    }  
    //定義b[10]數組用于存儲deno的因數   
    for(int k = 0; k <= (j - 1); k++)  
    {  
        for(int p = 0; p <= (m - 1); p++)  
        {  
            if(a[k] == b[p])  
            {  
                nume = nume / a[k];  
                deno = deno / a[k];  
            }  
        }  
    }  
        cout << nume << "/" << deno << endl;  
} 

CFraction CFraction::operator + (CFraction  &c)
{
	CFraction C;
	C.deno = deno * c.deno;
	C.nume = nume * c.deno + c.nume * deno;
	return C;
}

CFraction CFraction::operator - (CFraction  &c)
{
	CFraction C;
	C.deno = deno * c.deno;
	if(nume * c.deno < c.nume * deno)
	{
        C.nume = -(c.nume * deno - nume * c.deno);
	}
	else
	    C.nume = nume * c.deno - c.nume * deno;
	return C;
}

CFraction CFraction::operator * (CFraction  &c)
{
	CFraction C;
	C.deno = deno * c.deno;
	C.nume = nume * c.nume;
	return C;
}
CFraction CFraction::operator / (CFraction  &c)
{
	CFraction C;
	C.deno = deno * c.nume ;
	C.nume = nume * c.deno;
	return C;
}

CFraction CFraction::operator - ()
{
	CFraction C;
	C.deno = deno;
	C.nume = nume;
	cout << "-(" << C.nume << "/" << C.deno << ")" << endl;
	return C;
}

CFraction CFraction::operator + ()
{
	CFraction C;
	C.deno = deno;
	C.nume = nume;
	cout << "+(" << C.nume << "/" << C.deno << ")" << endl;
	return C;
}

bool CFraction::operator > (CFraction &c)
{
	deno = deno * c.deno;
	c.deno = deno;
	if((nume * c.deno) > (c.nume * deno))
		return true;
	else
		return false;
}
bool CFraction::operator < (CFraction &c)
{
	deno = deno * c.deno;
	c.deno = deno;
	if((nume * c.deno) < (c.nume * deno))
		return true;
	else
		return false;
}
bool CFraction::operator == (CFraction &c)
{
	deno = deno * c.deno;
	c.deno = deno;
	if((nume * c.deno) == (c.nume * deno))
		return true;
	else
		return false;
}
bool CFraction::operator != (CFraction &c)
{
	deno = deno * c.deno;
	c.deno = deno;
	if((nume * c.deno) == (c.nume * deno))
		return false;
	else
		return true;
}
bool CFraction::operator >= (CFraction &c)
{
	if((nume * c.deno) >= (c.nume * deno))
		return true;
	else
		return false;
}
bool CFraction::operator <= (CFraction &c)
{
	if((nume * c.deno) <= (c.nume * deno))
		return true;
	else
		return false;
}


           

實驗結果截圖:

第九周實驗報告3

實驗心得:

還是考察<<和>>運載符的重載,看來老師真的是太想我們掌握這一文法了,才會一而再,再而三的在實驗中反複考察我們,也好,程式設計本來就是枯燥的事情,很多時候是要重複工作的,現在提前鍛煉一下自己的耐性也是件好事。