📄 term.cpp
字号:
#include<iostream.h>
#include<stdlib.h>
#include<strstrea.h>
#include<math.h>
#include"term.hpp"
term::term(double c,int p)
{
coefficient = c;
power = p;
}
//////////////////////////////////////////////////////////////////////////
term::term(const term& x)
{
coefficient = x.coefficient;
power = x.power;
}
//////////////////////////////////////////////////////////////////////////
double term::getCoefficient()
{
return coefficient;
}
//////////////////////////////////////////////////////////////////////////
int term::getPower()
{
return power;
}
//////////////////////////////////////////////////////////////////////////
void term::setCoefficient(double c)
{
coefficient = c;
}
//////////////////////////////////////////////////////////////////////////
void term::setPower(int p)
{
power = p;
}
//////////////////////////////////////////////////////////////////////////
term term::operator =(const term& right)
{
coefficient = right.coefficient;
power = right.power;
return *this;
}
//////////////////////////////////////////////////////////////////////////
term operator * (const term& left, const term& right)
{
term result;
result.coefficient=right.coefficient * left.coefficient;
result.power = right.power + left.power;
return result;
}
//////////////////////////////////////////////////////////////////////////
term operator + (const term& left, const term& right)
{
term result;
if(left.power == right.power)//仅当两个项的指数相同时才将两个项相加
{
result.coefficient = left.coefficient + right.coefficient;
result.power = left.power;
return result;
}
else//否则中止程序
{
cerr<<"Error!The power of the two terms are not equal!"<<endl;
exit(1);
}
}
//////////////////////////////////////////////////////////////////////////
term operator - (const term& t)//求负函数,将项的系数变为原来的相反数
{
term result;
result.coefficient = -t.coefficient;
result.power = t.power;
return result;
}
//////////////////////////////////////////////////////////////////////////
term operator - (const term& left, const term& right)
{
term result;
//调用已定义的两个项的相加函数和求负函数实现减法
result= left + ( -right );
return result;
}
//////////////////////////////////////////////////////////////////////////
bool operator == (const term& l, const term& r)
{
//如果两个项的系数和指数都相同则返回true
if((l.coefficient == r.coefficient) && (l.power == r.power))
return true;
else
return false;
}
//////////////////////////////////////////////////////////////////////////
bool operator != (const term& l, const term& r)//比较两个项是否相等,要系数和指数都完全一样才认为两个项相同
{
if((l.coefficient != r.coefficient) || (l.power != r.power))
return true;
else
return false;
}
//////////////////////////////////////////////////////////////////////////
bool operator < (const term& l, const term& r)//比较两个项的指数的大小
{
if(l.power < r.power)
return true;
else
return false;
}
//////////////////////////////////////////////////////////////////////////
bool operator > (const term& l, const term& r)//功能同上
{
if(l.power > r.power)
return true;
else
return false;
}
//////////////////////////////////////////////////////////////////////////
double term::evalAt(double x)//求项在x处的值
{
double result=pow(x,power);
result*=coefficient;
return result;
}
//////////////////////////////////////////////////////////////////////////
istream & operator >>(istream &termin,term & val)
{
int isCoefficient = 2; //输入系数时为>=1(系数未改变时为2,改变后为1)
//输入指数时=-1,-2
int isMinus = 1; //系数为负数时为-1
char inputChar;
//初始化
val.power=0;
val.coefficient =1; //未输入系数而直接输入指数时系数为1
while(termin>>inputChar)
{
switch(inputChar)
{
case '0':case '1':case '2':case '3':
case '4':case '5':case '6':case '7':
case '8':case '9':case '.':
termin.putback(inputChar); //将字符送回输入流栈中
if (isCoefficient>0)
{
termin>>val.coefficient; //将输入流栈中的数(系数)读出
val.coefficient=val.coefficient*isMinus;
isCoefficient=1;
}
if(isCoefficient<0)
termin>>val.power; //将输入流栈中的数(指数)读出
if(isCoefficient==0)
{ // *后未输入X
cout<<"错误!"<<"丢失'X'!"<<endl;
return termin;
}
break;
case '*':
if(isCoefficient>0)
{
isCoefficient=0;val.power=0;
break;
}
cout<<"多余的'"<<inputChar<<"'"<<"被输入,错误!"<<endl;
return termin;
case 'X':
case 'x':
if(isCoefficient>=0)
{
isCoefficient=-1;val.power=1;
break;
}
cout<<"多余的'"<<inputChar<<"'"<<"被输入,错误!"<<endl;
return termin;
case '^':
if(isCoefficient==-1)
{
isCoefficient=-2;
val.power=0;
break;
}
cout<<"多余的'"<<inputChar<<"'"<<"被输入,错误!"<<endl;
return termin;
case '-':
if (isCoefficient==2)
{ //在系数前输入-
isMinus=-1;
val.coefficient=-1;
isCoefficient=1; //系数已改变
break;
}
termin.putback(inputChar);
return termin;
case '+':
if (isCoefficient==2)
{ //在系数前输入+
isMinus=1;
val.coefficient=1;
isCoefficient=1; //系数已改变
break;
}
termin.putback(inputChar);
return termin;
case '=':
case '#':
if(isCoefficient>-1 && isCoefficient<1)
cout<<"输入了错误的字符'"<<inputChar<<"'!"<<endl;
if(isCoefficient==2)
{
val.power=0;
val.coefficient =0;
break;
}
else
{
termin.putback(inputChar);
}
return termin;
default: /* 非法输入指示一个term输入结束,须将结束符返回 */
if(isCoefficient>-1 && isCoefficient<1)
cout<<"输入了错误的字符'"<<inputChar<<"'!"<<endl;
if(isCoefficient==2)
{
val.power=0;
val.coefficient =0;
}
else
{
cout<<"输入了错误的字符'"<<inputChar<<"'!"<<endl;
}
break;
return termin;
}
}
return termin;
}
//////////////////////////////////////////////////////////////////////////
ostream & operator<<(ostream & out,const term & val)
{
if(val.coefficient==0) return out; /* 系数为0时不输出 */
if(val.coefficient==-1 && val.power!=0) out<<"-"; /* 系数为-1,指数不为0时系数输出的“-” */
/* 其他情况 */
if((val.coefficient!=1 && val.coefficient!=-1)||(val.coefficient==1 && val.power==0) || ((val.coefficient==-1) && (val.power==0))) out<<val.coefficient;
switch(val.power){
case 0:
break;
case 1:
out<<"X";
break;
default:
out<<"X^"<<val.power;
break;
}
return out;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -