| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3538 人关注过本帖
标题:[求助]C++分数计算器运行计算结果出现Process exited after 15.56 seconds ...
只看楼主 加入收藏
qq383225731
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2019-3-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
[求助]C++分数计算器运行计算结果出现Process exited after 15.56 seconds with return value 32212
//main
#include"first.h"
#include"second.h"
#include<iostream>
using namespace std;

int main()
{
    Integer dis;
    Fraction a, b, result, real;
    int m, z;
    char c;

    dis.display();
    cout << "请输入数字:";
    cin >> m;
    while (m != 0)
    {
        
        
        if (m == 1)
        {
            cout << endl;
            cout << "请输入一个分数:";
            cin >> a ;
            cout << "请输入运算符";
            cin >> c ;
            cout << "请输入一个分数:";
            cin >> b ;
            if (c == '+') result = a + b;
            else if (c == '-') result = a - b;
            else if (c == '*') result = a * b;
            else if (c == '/') result = a / b;

            real = result.simplify();
            cout << real << endl;
            cout << endl;
        }
        if (m == 2)
        {
            cout << endl;
            cout << "请输入一个分数:";
            cin >> a ;
            cout << "请输入运算符";
            cin >> c ;
            cout << "请输入一个整数:";
            cin >> z ;
            if (c == '+') result = a + z;
            if (c == '-') result = a - z;
            if (c == '*') result = a * z;
            if (c == '/') result = a / z;

            real = result.simplify();
            cout << real << endl;
            cout << endl;
        }
        if (m == 3)
        {
            cout << endl;
            cout << "请输入一个整数:";
            cin >> z ;
            cout << "请输入运算符";
            cin >> c ;
            cout << "请输入一个分数:";
            cin >> b ;
            if (c == '+') result = b + z;
            if (c == '-') result = b - z;
            if (c == '*') result = b * z;
            if (c == '/') result = b / z;

            real = result.simplify();
            cout << real << endl;
            cout << endl;
        }
    }
    return 0;
}

//first
#ifndef _First_H_
#define _First_H_

#include<iostream>
using namespace std;

class Integer                    //整数类
{
public:
    int numerator;           //分子
    int denominator;        //分母

    Integer(int a = 0, int b = 1) :numerator(a), denominator(b) {}
    ~Integer() {}
    void display();
};

class Fraction :public Integer    //分数类
{
public:
    Fraction(int a = 0, int b = 1) :Integer(a, b) {}

    friend istream &operator>>(istream &, Fraction &);    //重载流插入
    friend ostream &operator<<(ostream &, Fraction &);    //重载流读取

    Fraction operator+(Fraction &c1);    //分分
    Fraction operator+(int c2);            //分整

    Fraction operator-(Fraction &c1);    //分分
    Fraction operator-(int c2);            //分整
    friend Fraction operator-(int c2, Fraction &c1);

    Fraction operator*(Fraction &c1);    //分分
    Fraction operator*(int c2);            //分整

    Fraction operator/(Fraction &c1);    //分分
    Fraction operator/(int c2);            //分整
    friend Fraction operator/(int c2, Fraction &c1);

    Fraction simplify();                //化简函数

    void display();

};
#endif

//second
#ifndef _SECOND_H_
#define _SECOND_H_

#include<iostream>
using namespace std;

void Integer::display()
{
    cout << endl;
    cout << "------请输入数字0-3选择计算器的功能------" << endl;
    cout << "--------------0、退出计算器--------------" << endl;
    cout << "---------1、分数与分数的四则运算---------" << endl;
    cout << "---------2、分数与整数的四则运算---------" << endl;
    cout << "---------3、整数与分数的四则运算---------" << endl;
}

Fraction Fraction::operator+(Fraction &c1)
{
    Fraction c;
    c.numerator = numerator * c1.denominator + c1.numerator*denominator;
    c.denominator = denominator * c1.denominator;
    return c;
}

Fraction Fraction::operator+(int c2)
{
    Fraction c;
    c.numerator = denominator * c2 + numerator;
    c.denominator = denominator;
    return c;
}

Fraction Fraction::operator-(Fraction &c1)
{
    Fraction c;
    c.numerator = numerator * c1.denominator - c1.numerator*denominator;
    c.denominator = denominator * c1.denominator;
    return c;
}

Fraction Fraction::operator-(int c2)
{
    Fraction c;
    c.numerator = denominator * c2 - numerator;
    c.denominator = denominator;
    return c;
}

Fraction operator-(int c2, Fraction &c1)
{
    Fraction c;
    c.numerator = c1.denominator*c2 - c1.numerator;
    c.denominator = c1.denominator;
    return c;
}

Fraction Fraction::operator*(Fraction &c1)
{
    Fraction c;
    c.numerator = numerator * c1.numerator;
    c.denominator = denominator * c1.denominator;
    return c;
}

Fraction Fraction::operator*(int c2)
{
    Fraction c;
    c.numerator = numerator * c2;
    c.denominator = denominator;
    return c;
}

Fraction Fraction::operator/(Fraction &c1)
{
    Fraction c;
    if (c1.numerator == 0) return *this;
    c.numerator = numerator * c1.denominator;
    c.denominator = denominator * c1.numerator;
    return c;
}

Fraction Fraction::operator/(int c2)
{
    Fraction c;
    c.numerator = numerator;
    c.denominator = denominator * c2;
    return c;
}

Fraction operator/(int c2, Fraction &c1)
{
    Fraction c;
    c.numerator = c2 * c1.denominator;
    c.denominator = c1.numerator;
    return c;
}

istream &operator>>(istream &input, Fraction &x)//重载输入符
{
    int a, b;
    char t;
    input >> a >> t >> b;
    if (t == '/'&&b != 0)
    {
        x.numerator = a;
        x.denominator = b;
    }
    else
    {
        cout << "分数格式错误或分母为零!请重新输入:";
    }
    return input;
}

ostream &operator<<(ostream &output, Fraction &x)//重载输出符
{
    int a, b;
    a = x.numerator;
    b = x.denominator;
    if (a == 0) cout << "0" << endl;
    if (b == 0) cout << "计算错误!" << endl;
    else output << x.numerator << "/" << x.denominator;
    return output;
}

Fraction Fraction::simplify()//化简函数
{
    int nume, deno, r, t;
    int s = 1;
    Fraction c;
    if (numerator < 0 && denominator < 0)
    {
        numerator = -numerator;
        denominator = -denominator;
    }
    if (numerator > 0 && denominator < 0)
    {
        denominator = -denominator;
        s = -1;
    }
    if (numerator < 0 && denominator>0)
    {
        numerator = -numerator;
        s = -1;
    }
    nume = numerator;
    deno = denominator;

    if (nume < deno)
    {
        t = nume;
        nume = deno;
        deno = t;
    }
    while (deno != 0)
    {
        r = nume % deno;
        nume = deno;
        deno = r;
    }

    c.numerator = s * (numerator / deno);
    c.denominator = s * (denominator / deno);
    return c;
}
#endif
搜索更多相关主题的帖子: return int result cout operator 
2019-03-28 16:39
qq383225731
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2019-3-28
收藏
得分:0 
刚学不久看不出哪里有问题
2019-03-28 16:42
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:20 
代码太长了,你具体说说遇到什么问题了吧
a. 你输入了什么?
b. 你期待输出什么?
c. 程序实际输出什么?

下班了,明天来回答

2019-03-28 17:05
qq383225731
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2019-3-28
收藏
得分:0 
回复 3楼 rjsp
输入1进入分数与分数四则运算,之后输入1/2-1/2就得不出结果
2019-03-28 17:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:0 
在 Fraction Fraction::simplify() 函数中
    while (deno != 0)
    {
        r = nume % deno;
        nume = deno;
        deno = r;
    }

    c.numerator = s * (numerator / deno);
    c.denominator = s * (denominator / deno);
while (deno != 0) 执行完毕后 deno 肯定是 0 了吧,那下面 numerator / deno 肯定是允许错误了。

C++ 标准库有求最大公约数的函数,详见 https://en.

2019-03-29 08:47
快速回复:[求助]C++分数计算器运行计算结果出现Process exited after 15.56 sec ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018396 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved