第5周实验报告任务2(修改)

#include<iostream>   
using namespace std; 	
int gcd(int,int);  
class CFraction  
{  
private:  
    int nume;  // 分子   
    int deno; // 分母       
public:  
    CFraction(int nu=0,int de=1);   //构造函数,初始化用   
    void Set(int nu=0,int de=1);    //置值,改变值时用   
    void input();               //按照"nu/de"的格式,如"5/2"的形式输入   
    void Simplify();            //化简(使分子分母没有公因子)   
    void amplify(int n);            //放大n倍,如2/3放大5倍为10/3   
    void output(int style=0);//输出:以8/6为例,style为0时,输出8/6;   
    //style为1时,输出4/3;   
	                    //style为2时,输出1(1/3),表示一又三分之一;
  //不给出参数和非1、2,认为是方式0 
		
};  
CFraction::CFraction(int nu,int de)  
{  
	if(de!=0)
	{
		nume=nu;  
		deno=de;  
	}
	else
	{
		cerr<<"初始化中发生错误,程序退出\n";  
		exit(0);  
	}
}  
void CFraction::Set(int nu,int de)  
{ 
	if(deno!=0)
	{
		nume=nu;  
		deno=de;
	}
}  
void CFraction::input()  
{  
	/*int nu,de;
	char c='/';
	while(1)
	{
		cout<<"请输入分数n/d:"<<endl;
		cin>>nu>>de;
		if(c!='/')
			cout<<"输入格式不对,请重新输入!"<<endl;
		else
			if(de==0)
				cout<<"分母不能为零"<<endl;
			else
				break;
	}*/
	cout<<nume<<"/"<<deno;
	//nume=nu;
	//deno=de;
} 
//化简 
void CFraction::Simplify()  
{ 
	int n=gcd(nume,deno);
	nume=nume/n;
	deno=deno/n;    
} 
int gcd(int m,int n)
{
	int r;  
    if (m<n){r=m;m=n;n=r;}  
    while(r=m%n)  // 求m,n的最大公约数 (原来求公约数可以这样求啊)  
    {  
        m=n;  
        n=r;  
    }  
    return n; 
}


void CFraction::amplify(int n)  
{  
	cout<<"放大"<<n<<"倍后为:"<<nume*n<<"/"<<deno<<endl;  
}  
void CFraction::output(int style)  
{  
	int n,nu,de;
	
    
    if(style==1)  
    {  
		n=gcd(nume,deno);
        
		cout<<nume/n<<"/"<<deno/n<<endl;  
    }  
    else  
        if(style==2) 
		{   	
		n=gcd(deno, nume);  
        nu=nume/n;   
        de=deno/n;  
        cout<<nu/de<<'('<<nu%de<<'/'<<de<<')'<<endl;    
        }  
        else  
            cout<<nume<<"/"<<deno<<endl;  
}  

int main()  
{  
    CFraction cf;  
    cf.Set(24,72);  
    cf.input(); 
    //cf.output(0); 	
    cf.amplify(5);  
    cout<<"style(0)"<<endl;  
	cf.output(0);  
    cout<<"style(1):"<<endl;  
	cf.output(1);  
    cout<<"style(2):"<<endl;   
    cf.output(2);  
    return 0;  
}  

#include<iostream>   
using namespace std; 	
int gcd(int,int);  
class CFraction  
{  
private:  
    int nume;  // 分子   
    int deno; // 分母       
public:  
    CFraction(int nu=0,int de=1);   //构造函数,初始化用   
    void Set(int nu=0,int de=1);    //置值,改变值时用   
    void input();               //按照"nu/de"的格式,如"5/2"的形式输入   
    void Simplify();            //化简(使分子分母没有公因子)   
    void amplify(int n);            //放大n倍,如2/3放大5倍为10/3   
    void output(int style=0);//输出:以8/6为例,style为0时,输出8/6;   
    //style为1时,输出4/3;   
	                    //style为2时,输出1(1/3),表示一又三分之一;
  //不给出参数和非1、2,认为是方式0 
		
};  
CFraction::CFraction(int nu,int de)  
{  
	if(de!=0)
	{
		nume=nu;  
		deno=de;  
	}
	else
	{
		cerr<<"初始化中发生错误,程序退出\n";  
		exit(0);  
	}
}  
void CFraction::Set(int nu,int de)  
{ 
	if(deno!=0)
	{
		nume=nu;  
		deno=de;
	}
}  
void CFraction::input()  
{  
	int nu,de;
	char c='/';
	while(1)
	{
		cout<<"请输入分数n/d:"<<endl;
		cin>>nu>>de;
		if(c!='/')
			cout<<"输入格式不对,请重新输入!"<<endl;
		else
			if(de==0)
				cout<<"分母不能为零"<<endl;
			else
				break;
	}
	cout<<nume<<"/"<<deno;
	nume=nu;
	deno=de;
} 
//化简 
void CFraction::Simplify()  
{ 
	int n=gcd(nume,deno);
	nume=nume/n;
	deno=deno/n;    
} 
int gcd(int m,int n)
{
	int r;  
    if (m<n){r=m;m=n;n=r;}  
    while(r=m%n)  // 求m,n的最大公约数 (原来求公约数可以这样求啊)  
    {  
        m=n;  
        n=r;  
    }  
    return n; 
}


void CFraction::amplify(int n)  
{  
	cout<<"放大"<<n<<"倍后为:"<<nume*n<<"/"<<deno<<endl;  
}  
void CFraction::output(int style)  
{  
	int n,nu,de;
	
    
    if(style==1)  
    {  
		n=gcd(nume,deno);
        
		cout<<nume/n<<"/"<<deno/n<<endl;  
    }  
    else  
        if(style==2) 
		{   	
		n=gcd(deno, nume);  
        nu=nume/n;   
        de=deno/n;  
        cout<<nu/de<<'('<<nu%de<<'/'<<de<<')'<<endl;    
        }  
        else  
            cout<<nume<<"/"<<deno<<endl;  
}  

int main()  
{  
    CFraction cf;  
    cf.Set(24,72);  
    cf.input(); 
    //cf.output(0); 	
    cf.amplify(5);  
    cout<<"style(0)"<<endl;  
	cf.output(0);  
    cout<<"style(1):"<<endl;  
	cf.output(1);  
    cout<<"style(2):"<<endl;   
    cf.output(2);  
    return 0;  
}  


 

 

老师,这个分母为什么是随机数啊????? 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值