Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类的运算符重载_C++ - Fatal编程技术网

C++ 类的运算符重载

C++ 类的运算符重载,c++,C++,在我的项目中,用户输入2个浮点数(整数部分和小数部分,用(.)分隔)。(+)运算符成功重载。但在(-)中,没有错误,但程序似乎无法正常工作。答案将是错误的,例如,当ob1=216.20和ob2=213.45时,输出为:3.-35(这是错误的) 甚至当ob1=213.45和ob2=216.20时,输出也是999999999997.25 多谢各位 #include <conio.h> #include <iostream> #include <stdlib.h>

在我的项目中,用户输入2个浮点数(整数部分和小数部分,用(.)分隔)。(+)运算符成功重载。但在(-)中,没有错误,但程序似乎无法正常工作。答案将是错误的,例如,当ob1=216.20和ob2=213.45时,输出为:3.-35(这是错误的) 甚至当ob1=213.45和ob2=216.20时,输出也是999999999997.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('\0'," ");
    //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();
} 
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类谷歌
{
私人:
整数[20];
整数十进制[40];
公众:
friend istream&operator>>(istream&z、谷歌&t);
谷歌运营商-(谷歌T);
friend ostream&operator(istream&z、Google&t)
{
chara[100];

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

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


可能还有其他问题。举一些非常简单的例子并调试代码。

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

给你三个提示:

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

请将代码缩减到再现问题所需的最小值-这确实是太多的代码。此外,请正确设置代码格式。有关更多信息,请参阅(重点放在简短部分)。接下来,处理缩进,删除多余的空行(不是全部,但一行中的4行是太多的空格),使用一致的样式,而不是每两行更改一次大括号的位置。此代码几乎是可修改的。+1用于良好的SSCCE尝试!+1用于倾听注释并实际努力重新格式化代码:)我建议您将此发布以了解如何改进代码。