数组元素ch[j]=='('报错,我是想作比较不是赋值,编译器报错->'=' : left operand must be l-value (请帮忙看看哪里有问题,附上源码和报错)。
源码:
#include <iostream>
#include <string.h>
// #include <stdio.h>
// #include <cmath>
#include <string>
using namespace std;
class Rational
{
protected:
string str;
int integer; //整数部分
int point[2]; //小数部分,又分为非循环与循环部分
public:
Rational();
Rational(string st);
void countInteger(); //取整数部分
void countPoint(); //取小数部分,计算作为小数部分时分母和分子
int countSimplify(); //当只有小数部分时,化简
void countAdd(); //,化简后整合两部分,相加
void integerTransfDecimal(); //show
friend istream &operator>>(istream &input, Rational &Ra);
};
Rational::Rational()
{
this->str = "0";
this->integer = 0;
this->point[0] = 0;
this->point[1] = 0;
}
Rational::Rational(string st)
{
this->str = st;
this->integer = 0;
this->point[0] = 0;
this->point[1] = 0;
}
//取整数部分
void Rational::countInteger()
{
char ch[15];
for (int i = 0; i < str.length(); i++)
{
if (ch[i] != '.')
{
ch[i] = str[i];
this->point[0]++; //临时计数
}
else
break;
}
}
//取小数部分,计算作为小数部分时分母和分子
void Rational::countPoint()
{
char ch[15];
int te = this->point[0];
for (int i = te + 1; i < str.length(); i++)
{
ch[i] = str[i];
}
int px = 0, py = 0, pm = 0;
//计算位数
for (int j = 0; j < str.size(); j++)
{
if (ch[j] != '(' &&pm = 0)
{
this->point[0] *= 10;
this->point[0] += ch[j] - '0'; //得到非循环数值
px++; //记非循环位数
}
if (ch[j] == '(' &&pm = 0)
{
pm = 1;
j++; //记循环内位数
}
if (ch[j] != ')' &&pm = 1)
{
this->point[1] *= 10;
this->point[1] += ch[j] - '0';
py++;
}
}
//判断类型,计算分母分子最终值的大小
if (pm == 1) //如果为非循环小数(例:0.5(0))
{
int pw = px - 1;
int pn = this->point[0];
this->point[1] = 9;
while (pw--)
{
this->point[0] *= 10;
}
pw = px;
while (pw--)
{
this->point[1] *= 10;
}
this->point[0] -= pn;
}
if (pm == 0 && px == 1) //如果为纯循环小数(例:0.(3))
{
int pw = py;
int pn = this->point[0];
this->point[1] = 9;
while (pw--)
{
this->point[0] *= 10;
this->point[1] *= 10;
this->point[1] += 9;
}
this->point[0] -= pn;
}
if (pm == 0 && px == 0) //如果为非纯循环小数(例:0.52(3))
{
int pw = px;
int pu = py;
int pn = this->point[0];
this->point[1] = 9;
while (pu--)
{
this->point[0] *= 10;
}
while (pw--)
{
this->point[1] *= 10;
}
this->point[0] -= pn;
}
}
//当只有小数部分时,化简
int Rational::countSimplify()
{
int te = 0;
for (int i = 1; i <= this->point[1]; i++)
{
if (this->point[0] / i == this->point[1] / i)
{
te = i; //i最小值应该为1
}
}
return te;
}
//,化简后整合两部分,相加
void Rational::countAdd()
{
this->integer *= this->point[1];
this->point[0] *= this->integer;
}
//show
void Rational::integerTransfDecimal()
{
this->countInteger();
this->countPoint();
this->countSimplify();
this->countAdd();
cout << "\ninteger transform decimal is :" << endl;
}
istream &operator>>(istream &input, Rational &Ra)
{
cout << "Please input a point(小数)" << endl;
input >> Ra.str;
return input;
}
int main()
{
Rational ra1;
cin >> ra1;
ra1.integerTransfDecimal();
return 0;
编译错误提示:
: error C2106: '=' : left operand must be l-value
: error C2106: '=' : left operand must be l-value
: error C2106: '=' : left operand must be l-value
: error C2248: 'str' : cannot access protected member declared in class 'Rational'
: see declaration of 'str'
: error C2593: 'operator >>' is ambiguous
执行 cl.exe 时出错.
测试八.exe - 1 error(s), 0 warning(s)
谢谢谢谢!