qq_43919400
threecat.up
采纳率61.5%
2019-05-26 20:11

大佬帮我看看我的程序为什么不正确

程序一直不对,好难受啊谢谢大家

#include
#include
#include

using namespace std;

class Num
{
public:
Num(int num, int denom); //初始化数据
friend ostream & operator<<(ostream &cout, Num &n); //重载<<运算符
friend istream & operator>>(istream &cin, Num &n); //重载>>运算符
friend Num operator + (Num n1, Num n2); //重载+运算符
friend Num operator - (Num n1, Num n2); //重载-运算符
friend Num operator * (Num n1, Num n2); //重载*运算符
friend Num operator / (Num n1, Num n2); //重载/运算符
friend bool operator == (Num n1, Num n2); //重载真假运算符,c++中用bool代表真假
Num huajian(); //定义化简函数
Num huajian1();
friend void realPrintf(Num &n); //定义了一个将有理数转换为实数的函数

private:
int denom;
int num;
};

//Num(int num, int denom);

void realPrintf(Num &n) //将有理数输出为实数
{
double m = double(n.num) / double(n.denom); //强制转换为实型数,再进行除法运算
cout << m << endl;
}

Num::Num(int num, int denom)
{

if (denom != 0)      //分母不能为0
{
    this->denom = denom;    // 指向被调用成员函数所属对象
}
else denom = 1;
huajian();

}

Num Num::huajian() //化简有理数
{
if (denom < 0) //确保分母为正数
{
num = -num;
denom =- denom;
}
int x = abs(num); //用欧几里得算法求出分子分母的最大公约数
int y = abs(denom);
while (y > 0)
{
int temp = x % y;
x = y;
y = temp;
}
num /= x;
denom /= x;
//Num N(num /= x, denom /= x);
//return N;
}

Num Num::huajian1()
{
if (denom < 0) //确保分母为正数
{
num = -num;
denom =- denom;
}
int x = abs(num); //用欧几里得算法求出分子分母的最大公约数
int y = abs(denom);
while (y > 0)
{
int temp = x % y;
x = y;
y = temp;
}
Num N(num /= x, denom /= x);
return N;
}

Num operator + (Num n1, Num n2)
{
int a = n1.num;
int b = n1.denom;
int c = n2.num;
int d = n2.denom;
int e = a * d + b * c;
int f = b * d;
Num N(e, f);
return N;
}

Num operator - (Num n1, Num n2)
{
n2.num = -n2.num;
return operator + (n1, n2);
}

Num operator * (Num n1, Num n2)
{
int a = n1.num;
int b = n1.denom;
int c = n2.num;
int d = n2.denom;
int e = a * c;
int f = b * d;
Num N(e, f);
return N;
}

Num operator / (Num n1, Num n2)
{
int temp = n2.num;
n2.num = n2.denom;
n2.denom = temp;
return operator * (n1, n2);
}

ostream & operator<<(ostream &cout, Num &n)
{
if (n.num / n.denom == 0)
{
cout << n.num / n.denom;
}
else
{
cout << n.num << "\" << n.denom;
}
return cout;
}

istream & operator>>(istream &cin, Num &n)
{
cin >> n.num >> n.denom;
if (n.denom != 0)
{
return cin;
}
else
{
cout << "输入有误";
exit(0);
}
}

bool operator == (Num n1, Num n2) //判断两个数是否相等,c++中用bool代表真和假
{

//return double(Num.n1)  == (double)N.n2 ? true : false;
if ( n1.num == n2.num &&  n2.denom == n1.denom)
{
    //cout << "两数相等" << endl;
    return true;
}
else
{
    //cout << "两个有理数不相等" << endl;
    return false;
}

}

int main()
{
Num n1(1, 9);
Num n2(1, 2);
Num n3(5, 8);
char m;
char p;
int k;
cout << "***************有理数计算器***************" << endl;
cout << "***********是否进入系统?(Y / N)**********" << endl; //提示是否进入有理数计算器的系统
cin >> m;
if ( m == 'N' && m == 'n')
{
cout << "不进入系统" << endl;
}
else
{
cout << "请输入两个有理数" << endl;
}
cout << "请输入第一个数:" << endl;
cin >> n1;
cout << "请输入第二个数:" << endl;
cin >> n2;
do{
cout << "************ 1、有理数加法运算************" << endl;
cout << "************ 2、有理数减法运算************" << endl;
cout << "************ 3、有理数乘法运算************" << endl;
cout << "************ 4、有理数除法运算************" << endl;
cout << "************ 5、有理数的判断**************" << endl;
cout << "************ 6、有理数的化简**************" << endl;
cout << "************ 7、有理数变成实数************" << endl;
cout << "************ 8、重新输入有理数************" << endl;
cout << "************ 9、程序结束******************" << endl;
cout << "************ 请输入选择:" ;

    cin >> k;
    switch (k)
    {
    case 1:
        n3 = n1 + n2;
        cout << n3 << endl;
        break;
    case 2:
        n3 = n1 - n2;
        cout << n3 << endl;
        break;
    case 3:
        n3 = n1 * n2;
        cout << n3 << endl;
        break;
    case 4:
        n3 = n1 / n2;
        cout << n3 << endl;
        break;
    case 5:
        if (n1 == n2)
        {
            cout << "两个有理数相等" << endl;
        }
        else
        {
            cout << "两个有理数不相等" << endl;
        }
        break;
    case 6:
        n1 = n1.huajian1();
        n2 = n2.huajian1();
        cout << n1 << endl;
        cout << n2 << endl;
        break;
    case 7:
        cout << "n1 = " ;
        realPrintf(n1);
        cout << "n2 = " ;
        realPrintf(n2);
        break;
    case 8:
        cout << "请输入第一个数:" << endl;
        cin >> n1;
        cout << "请输入第二个数:" << endl;
        cin >> n2;
        break;
    case 9:
        break;
    default:
        if (k != 1, 2, 3, 4, 5, 6, 7, 8, 9)
        {
            cout << "输入有误" << endl;
            break;
        }
        else
        {
            cout << "谢谢使用有理数计算器" << endl;
            break;
        }

    }
    cout << "**********是否继续选择步骤操作**********" << endl;
    do {
        cin >> p;
    } while (p != 'y'&& p != 'Y' && p != 'n' && p != 'N');
} while (m == 'y' || m == 'Y');
//system("pause");

//return 0;

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • qtchen_1988 qtchen_1988 2年前

    你这程序两个地方错了,1.friend Num operator + (Num n1, Num n2); //重载+运算符(其他减、乘、除等一样道理),这个返回的是对象栈内存,应该返回对象堆内存;
    2.friend ostream & operator<<(ostream &cout, Num &n); //重载<<运算符 这个函数实现不对,if (n.num / n.denom == 0),假如我输入1/3,这个结果为真,就会输出0;
    前面你已经判断分母不为零,你可以改为if (n.num == 0);
    这样,应该就可以输出你想要的结果

    点赞 评论 复制链接分享
  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 2年前

    Num operator + (Num n1, Num n2)
    ->
    Num& operator + (Num n1, Num n2)

    别的类似

    在public:下添加一个拷贝构造函数
    Num(Num& n)
    {
    denom = n.denom;
    num = n.num;
    }

    点赞 评论 复制链接分享

为你推荐