C++ 在C+中实现istream+;分数计算器
我正在尝试学习面向对象编程,制作一个简单的分数计算器,它可以对任意数量的函数进行加法或减法运算,并将答案写成一个简化分数 示例:输入= 3/2 + 4/ 8. ,输出= 二, 我正在尝试重载操作符以实现这一点 在这个程序中,我试图开发一个由分数组成的表达式,分数由操作符“+”或“-”分隔 表达式中的分数数是任意的 以下6行中的每一行都是有效输入表达式的示例:C++ 在C+中实现istream+;分数计算器,c++,istream,fractions,C++,Istream,Fractions,我正在尝试学习面向对象编程,制作一个简单的分数计算器,它可以对任意数量的函数进行加法或减法运算,并将答案写成一个简化分数 示例:输入= 3/2 + 4/ 8. ,输出= 二, 我正在尝试重载操作符以实现这一点 在这个程序中,我试图开发一个由分数组成的表达式,分数由操作符“+”或“-”分隔 表达式中的分数数是任意的 以下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
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)
{
}
}
}
};
#包括
使用名称空间std;
#包括“分数.h”
#包括
类分数
{
公众:
分数:分数(inta,intb);
int find_gcd(int n1,int n2);
空减分数(int*nump,int*denomp)
{
int gcd;
gcd=find_gcd(*nump,*denomp);
*nump=*nump/gcd;
*denomp=*denomp/gcd;
if((*denomp0){
*denomp*=-1;
}
如果(*denomp==0){
抛出无效的_参数(“错误:零分母”);
}
}
分数和分数::运算符+(常数分数和n){
int denom=*denomp*n.denom;
整数=(*nump*n.numera)+(n.denom*n.nump);
返回分数(数值、分母);
}
分数和分数::运算符-(常数分数和n){
int denom=*denomp*n.denom;
整数=(*nump*n.numera)-(n.denom*n.nump);
返回分数(数值、分母);
}
friend ostream&operator提取运算符>
,应该读取一个分数。
使用其他代码读取完整的表达式并计算其值
它看起来是这样的(没有错误检查,只是假设输入的形式正确):
提示1:你不需要用一个指针来解决这个问题——你只会让事情变得更难
提示2:您正在从运算符返回引用,但分配给加法结果(例如(1/2+3/4)=7/3
)没有多大意义
提示3:永远不要键入字符“eof”。它的含义与您认为的不一样。返回对临时变量的引用(如操作符+
和操作符-
)是不好的(这是未定义的行为,如果到目前为止没有崩溃,您是不幸运的)这是值得一看的Stroustrup的桌面计算器的例子从C++编程语言。示例代码在这里(第6章):它确实有一个更复杂的语法比你所需要的,所以你可以管理一个简单的解析器。
#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
istream& operator >> (istream& is, Fraction& f)
{
char slash = 0;
return is >> f.numerator >> slash >> f.denominator;
}