【问题标题】:Implementing istream in a C++ Fraction Calculator在 C++ 分数计算器中实现 istream
【发布时间】:2014-04-24 04:58:43
【问题描述】:

我正在尝试学习面向对象的编程并制作简单的分数计算器,而不是可以添加或减去任意数量的函数并将答案写为减少的分数。

示例:输入= 3/2 + 4/ 8 , 输出 = 2

我正在尝试重载运算符以完成此操作。

所以在程序中,我试图开发输入由一个由运算符'+'或'-'分隔的分数组成的表达式组成。

表达式中分数的数量是任意的。

以下 6 行中的每一行都是有效输入表达式的示例:

1/2 + 3/4
1/2 -5/7+3/5
355/113
3    /9-21/    -7
4/7-5/-8
-2/-3+7/5

作为输入给出的分数的分子和/或分母可能是负数。 我正在尝试使输入由单行上的单个表达式组成,并且应通过检测文件结尾来检测输入的结尾。

我的不完整分数类在下面的源文件中定义:

#include <iostream>
using namespace std;
#include "Fraction.h"
#include <stdexcept>

class Fraction
{
public: 
    Fraction::Fraction(int a, int b);
    int find_gcd(int n1, int n2); 

    void reduce_fraction(int *nump,  int *denomp) 
    {
      int gcd;   
      gcd = find_gcd(*nump, *denomp);
      *nump = *nump / gcd;
      *denomp = *denomp / gcd;

      if ((*denomp<0 && *nump < 0 ))
    {
        *denomp*=-1;
        *nump*=-1;
    }
    else if (*denomp < 0 &&  *nump >0){
        *denomp*=-1;

    }
if ( *denomp ==0) {
        throw invalid_argument( "Error: zero denominator" );
    }
    }



Fraction& Fraction::operator+(const Fraction& n) {
    int denom = *denomp * n.denom;
    int numera = (*nump * n.numera) + (n.denom * n.nump);
    return Fraction(numera,denom);
}

Fraction& Fraction::operator-(const Fraction& n) {
    int denom = *denomp * n.denom;
    int numera = (*nump * n.numera) - (n.denom* n.nump);
    return Fraction(numera, denom);
}

friend ostream& operator<<(ostream &os, Fraction& n)
{
    if (n.numera == 0)
    {
        cout << 0 << endl;
        return os;
    }
    else if (n.numera == n.denom)
    {
        cout << 1 << endl;
        return os
    }
    else
    {
        cout << n.numera << '/' << n.denom << endl;
        return os;
    }
}

friend istream& operator>>(istream &os, Fraction& n)
{
    while(!cin.EOF)
    {


    }
    }
}



};

基本上我遇到的问题是遍历文件并获取字符以分配给作为参数传递的函数的数据成员。

我试图在我的 istream 成员函数中实现它,但在这种情况下我无法实现它

我的头文件也在下面:

#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;


class Fraction{

    public: 
    Fraction(int , int );
    int fraction(int,int);
    void reduce_fraction(int *,  int *);
    Fraction& operator+(const Fraction&);
    Fraction& operator-(const Fraction&);
    friend ostream& operator<<(ostream &os, const  Fraction& n);
    friend istream& operator>>(istream &is, const Fraction& n);


};

#endif

【问题讨论】:

  • 返回对临时对象的引用(如在operator+operator- 中)是不好的(这是未定义的行为,如果它到目前为止还没有崩溃,那么你很不幸)。
  • 看看 Stroustrup 的 C++ 编程语言的桌面计算器示例值得一看。示例代码在这里(第 6 章):stroustrup.com/3rd_code.html 它确实比您需要的语法更复杂,因此您可以管理一个更简单的解析器。

标签: c++ istream fractions


【解决方案1】:

提取运算符&gt;&gt; 应为one 分数。
使用其他代码读取完整的表达式并计算其值。

它看起来像这样(没有错误检查,只是假设输入的格式正确):

istream& operator >> (istream& is, Fraction& f)
{
   char slash = 0;
   return is >> f.numerator >> slash >> f.denominator;
}

提示 1:你不需要使用单个指针来解决这个问题 - 你只会让事情变得更难。

提示 2:您从运算符返回引用,但分配给加法的结果,例如(1/2 + 3/4) = 7/3,没有多大意义。

提示 3:切勿输入字符“eof”。它并不意味着你认为它的作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2014-06-29
    • 2014-04-17
    • 1970-01-01
    • 2017-03-29
    相关资源
    最近更新 更多