运算符-类的重载

Operator- Overloading for a class

本文关键字:重载 -类 运算符      更新时间:2023-10-16

在我的项目中,用户输入2个浮点数(整数部分和小数部分,用(.)分隔)。(+)运算符成功重载。但在(-)中,没有错误,但程序似乎无法正常工作。答案是错误的,例如,当ob1=216.20和ob2=213.45时,输出为:3.-35(这是错误的)或者甚至当ob1=213.45且ob2=216.20时,输出为999999999999997.25。

谢谢

#include <conio.h>
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <string.h>
using namespace std ;
class Google
{
private:
int integer[20];
int decimal[40];
public:
friend istream &operator>>(istream &z,Google &t);
Google operator-(Google T);
friend ostream &operator<<(ostream &r,Google &p);
};// End Of Google Class
istream &operator>>(istream &z,Google &t)
{
char a[100];
cout<<"n"<<" Please Enter The Google Number, Sepatare with (.)"<<"n";
z.get(a ,99);
z.get();
char  *x , *y;
x = strtok(a,".");
//cout<<x<<"n";
y = strtok(''," ");
//cout<<y<<"n";
int k1=strlen(x);
//cout<<k1<<"n";
int k2=strlen(y);
//cout<<k2<<"n";
for( int i = 0; i < 20-k1; i++)
{
t.integer[i] = 0 ;
}
int j = 0;
for( int i = 20-k1; i < 20; i ++ )
{
t.integer[i] = x[j] - 48 ;
j ++ ;
}
for( int i = k2 ; i < 40 ; i ++ )
{
t.decimal[i] = 0 ;
}
for( int i = 0 ; i < k2 ; i ++ )
{
t.decimal[i] = y[i] - 48 ;
}
// for (int i =0; i< k2 ; i++) {
//cout<<"n"<<t.decimal[i]<<"n";
//} Checking Out Outcome
return z ;
}
Google Google::operator-(Google T)   {
Google M1;
for(int k = 0 ; k < 40 ; k++ )
M1.decimal[k] = 0 ;
for(int p = 0 ; p < 20 ; p ++ )
M1.integer[p] = 0 ;
for(int j = 39 ; j > 0 ; j -- )
{
if (this->decimal[j]<T.decimal[j])
{
this->decimal[j-1] -= 1 ;
this->decimal[j] += 10 ; 
}
M1.decimal[j] = this->decimal[j] - T.decimal[j] ;
}
if (this->decimal[0]<T.decimal[0])
{
M1.integer[19] -= 1  ;
M1.decimal[0]  +=  10 ;
}
M1.decimal[0] = this->decimal[0]- T.decimal[0] ;
for(int i=19 ; i> 0 ; i--)
{
if (this->integer[i] < T.integer[i])
{
this->integer[i-1] -= 1  ;
this->integer[i] +=  10 ;
}
M1.integer[i] = this->integer[i] - T.integer[i] ;
}
return M1;
} //end of Operator 
ostream & operator <<(ostream &r ,Google &p )
{
int  k1 = 0 , k2 = 0 ;
for( int  i = 0 ; p.integer[i] == 0 ; i ++ )
k1 ++ ;
for( int  i = k1 ; i < 20 ; i ++ ) 
r << p.integer[i] ;
if( !p.integer[19] )
r << "0"  ;
for( int  i = 39 ; p.decimal[i] == 0 ; i -- )
k2 ++ ;
cout<<"."  ;
for( int  i = 0 ; i < 40 - k2 ; i ++ )
r << p.decimal[i] ;
if( !p.decimal[0] )
r << "0"  ;
return  r ;
}
void main () {
Google ob1;
Google ob2;
cin>>ob1;
cin>>ob2;
cout<<"ob1 = " <<ob1<<"n"<< "ob2 = " <<ob2<<"n" <<endl;
Google ob4;
ob4 = ob1-ob2;
cout<<"n"<<"Result of (-) Will Be:"<<"n";
cout<<ob4;
cout<< "n";
getch();
} 

我发现的一些问题。当您对decimal[0]进行减法运算时,您将从M1中减去1,而对其他小数使用this。它们可能应该是相同的(可能是M1,因为当你从中减去一些东西时,你不想修改存储在this中的数字)。以后用新数字覆盖-1时,请注意不要丢失它。

你正在做的另一件事是没有正确处理负数。如果你从0中减去1,你的代码将填充integer的第9部分。这种格式被称为9的补码,基本上要得到负数,你需要为每个数字(包括小数部分)提取9整数[x]。另一种处理方法是首先比较两个数字,如果结果为负,则交换操作数(现在结果为正)并更改符号。

可能还有其他问题。举几个非常简单的例子来调试代码。

基本上,您在这里执行不动点运算。看起来你遇到了一个问题,进位/借位没有正确地从小数部分传播到整数部分。(例如5.222-0.223)你考虑过这个案子吗?

三个提示给你:

  • 在类中添加一个构造函数,用于清除这两个数组。这将避免您必须在"-"运算符或其他任何位置显式地执行此操作
  • 考虑使用一个数字数组而不是两个,并且在输入/输出数字时只插入/删除小数点。这将使您的算法更简单,也使代码更易于维护
  • 不要在整个代码中使用像20这样的"幻数"。将static const int IntDigits=20;定义为私有类成员,并使用它