一个指针的问题

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


有理数运算123.cpp
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
Linking...

有理数运算123.exe - 0 error(s), 0 warning(s)

程序没有问题,可就是不能运行,那个“我不是已经定义了吗?希望大家给下指点吧!
...全文
94 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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
  • 打赏
  • 举报
回复
你定义了temp但是没有初始化就使用了。

64,312

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧