资源简介
用类来定义单项式和多项式,再利用符号重载实现多项式的计算,包含详细代码,可参考。
代码片段和文件信息
#include
#include
#include
#include
using namespace std;
//辅助函数
bool isDigit(const char& a){
if (a >= 48 && a <= 57) return true;
else return false;
}
int toDigit(const string& c){
stringstream a;
int b;
a< a>>b;
return b;
}
string getexpression(const string& expression){
int i=0len=expression.length();
while( expression[i] != ‘=‘ ){
i++;
}
return expression.substr(i+1len-i);
}
string getName(const string& expression){
int i=0;
while( expression[i] != ‘=‘ ){
i++;
}
return expression.substr(0i);
}
bool isRightfull(const string& expression){
int len=expression.length()-1;
int index=-1;
for(int i=len; i>=0;){
int a=0;
if( expression[i] != ‘)‘ ) return false;
else {
if( !isDigit(expression[i-1]) )
return false;
else{
while( isDigit(expression[i-a-1]) ){
a++;
}
}
if( index >= toDigit( expression.substr(i-aa) ) )
return false;
else index = toDigit( expression.substr(i-aa) );
if( expression[i-a-1] != ‘‘ ) return false;
else {
if( !isDigit(expression[i-a-2]) )
return false;
while( isDigit(expression[i-a-2]) ){
a++;
}
if ( toDigit( expression.substr(i-a-1a) ) == 0 )
return false;
if ( expression[i-a-2] == ‘-‘ ) a++;
if ( expression[i-a-2] != ‘(‘ ) return false;
}
}
i=i-a-3;
}
return true;
}
//单项式类
class Item{
private:
int coefficient;
int index;
public:
Item();
int getIndex() const;
int getCoefficient() const;
void setCoefficient(int a);
void setIndex(int a);
Item operator+(const Item& a);
Item operator-(const Item& a);
Item operator*(const int& a);
Item operator*(const Item& a);
int getValue(const int& a);
void show_Item();
};
Item::Item(){
index=0; coefficient=0;
}
int Item::getIndex() const{
return index;
}
int Item::getCoefficient() const{
return coefficient;
}
void Item::setCoefficient(int a){
coefficient=a;
}
void Item::setIndex(int a){
index=a;
}
Item Item::operator+(const Item& a){
Item b;
b.coefficient = a.coefficient + coefficient;
b.index = index;
return b;
}
Item Item::operator-(const Item& a){
Item b;
b.coefficient = coefficient - a.coefficient;
b.index = a.index;
return b;
}
Item Item::operator*(const int& a){
Item b;
b.coefficient = coefficient * a;
b.index = index;
return b;
}
Item Item::operator*(const Item& a){
Item b;
b.coefficient = coefficient * a.coefficient;
b.index = index + a.index;
return b;
}
int Item::getValue(const int& a){
return coefficient * pow( aindex );
}
void Item::show_Item(){
if( index == 0 ) cout< else {
if( index != 1 ){
if( coefficient == 1 ) cout<<“x^“< else {
if( coefficient == -1 ) cout<<“-x^“< else cout< }
}
else
- 上一篇:实序列FFT算法的C语言实现
- 下一篇:icp C++实现包含测试数据
相关资源
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- 学校超市选址问题(数据结构C语言版
- VC++MFC小游戏实例教程(实例)+MFC类库
- 数据结构,迷宫问题C语言版源代码
- DSDEMO-C演示(数据结构C语言版 严蔚敏
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- 数据结构 图的遍历源代码
- 数据结构实验源代码集
- 实验报告:数据结构长整数四则运算
- c++ 邮件多附件群发
评论
共有 条评论