# 一个指针的问题

jwb97148148 2008-03-29 08:34:22
#include <math.h>
#include <iostream.h>

//enum bool {false,true};

class rational {
int numerator,denominator;
void optimization();
public:
rational(int x=0, int y=1);
void print();
friend rational operator + (rational num1,rational num2);
friend rational operator - (rational num1,rational num2);
friend rational operator * (rational num1,rational num2);
friend rational operator / (rational num1,rational num2);
friend bool operator == (rational num1,rational num2);
friend double real(rational x);

};

void rational::optimization()
{
int gcd;
if(numerator==0)
{
denominator=1;
return;
}
gcd=(abs(numerator)>abs(denominator)? abs(numerator):abs(denominator));
if(gcd==0) return;
for(int i=gcd; i>1; i--)
if((numerator%i==0)&&(denominator%i==0) )break;
numerator/=i;
denominator/=i;
if(numerator<0 && denominator<0)
{
numerator = -numerator;
denominator = -denominator;
}
else if(numerator<0 || denominator<0)
{
numerator = -abs(numerator);
denominator = -abs(denominator);
}
}

rational::rational(int x,int y)
{
numerator = x;
denominator = y;
optimization();
}

void rational::print()
{
cout<<numerator;
if(numerator !=0 && denominator !=1)
cout<<"/"<<denominator<<"\n";
else cout<<"/n";
}

rational operator + (rational num1,rational num2)
{
rational * temp;
temp->denominator = num1.denominator * num2.denominator;
temp->numerator = num1.numerator * num2.denominator + num1.denominator * num2.numerator;
temp->optimization();
return * temp;
}

rational operator - (rational num1,rational num2)
{
rational * temp;
temp->denominator = num1.denominator * num2.denominator;
temp->numerator = num1.numerator * num2.denominator - num1.denominator * num2.numerator;
temp->optimization();
return * temp;
}

rational operator * (rational num1,rational num2)
{
rational * temp;
temp->denominator = num1.denominator * num2.denominator;
temp->numerator = num1.numerator * num2.numerator;
temp->optimization();
return * temp;
}

rational operator / (rational num1,rational num2)
{
rational * temp;
temp->denominator = num1.denominator * num2.numerator;
temp->numerator = num1.numerator * num2.denominator ;
temp->optimization();
return * temp;
}

bool operator == (rational num1,rational num2)
{
if(num1.numerator == num2.numerator && num1.denominator == num2.denominator)
return true;
else return false ;
}

double real(rational x)
{
return (double(x.numerator))/(double(x.denominator));
}

main()
{
rational ra1(3,8),ra2(2,4),ra3,ra4(8,16);
ra1.print();
ra2.print();
ra4.print();
ra3=ra1+ra2;
ra3.print();
ra3=ra1-ra2;
ra3.print();
ra3=ra1*ra2;
ra3.print();
ra3=ra1/ra2;
ra3.print();
cout<<"ra1="<<real(ra1)<<" "<<"ra2="<<real(ra2)<<" "
<<"ra3="<<real(ra3)<<"\n";
if(ra2 == ra4) cout<<"ra2 is equal to ra4. \n";
else cout<<"ra2 is not equal to ra4. \n";
return 1;
}

E:\vc++\j\有理数运算123.cpp(69) : warning C4700: local variable 'temp' used without having been initialized
E:\vc++\j\有理数运算123.cpp(79) : warning C4700: local variable 'temp' used without having been initialized
E:\vc++\j\有理数运算123.cpp(89) : warning C4700: local variable 'temp' used without having been initialized
E:\vc++\j\有理数运算123.cpp(98) : warning C4700: local variable 'temp' used without having been initialized

...全文
94 6 打赏 收藏 转发到动态 举报

6 条回复

yuwei2589 2008-03-29
• 打赏
• 举报

queshangxintou 2008-03-29
• 打赏
• 举报

jwb97148148 2008-03-29
• 打赏
• 举报

ttkk_2007 2008-03-29
• 打赏
• 举报

``````
rational operator + (rational num1,rational num2)
{
rational * temp;    //temp没有指向！，-，*,/都一样
temp->denominator = num1.denominator * num2.denominator;
temp->numerator = num1.numerator * num2.denominator + num1.denominator * num2.numerator;
temp->optimization();
return * temp;
}

``````
jieao111 2008-03-29
• 打赏
• 举报

``````rational * temp=new rational;
``````
Supper_Jerry 2008-03-29
• 打赏
• 举报

64,312

c++ 技术论坛（原bbs）

• 近7日
• 近30日
• 至今

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下