rendered paste body#include<stdlib.h>#include<string>#include <iostream>using namespace std;inline string StringShift( string *str, int from, int distance ){ string tmp = *str; *str = tmp.substr(0,from); *str+= tmp.substr(from+distance); return *str;}class C_expr{public: C_expr (); C_expr ( string str ); C_expr ( unsigned int deg, char c ); // ~C_expr (); string C_expr::OutputStr( std::string *str ); C_expr operator+ ( string str_ ); C_expr operator+=( string str_ ); C_expr operator+ ( C_expr expr ); C_expr operator+=( C_expr expr );private: string coefs; unsigned int degree; char symbolX; string ArrangeExprStr ( string *str ); int DistanceToNextMark ( string str); void DeleteChars ( string *str, char c); int GetDegreeFromStr ( string str);};C_expr::C_expr(){ degree = 2; symbolX = 'x';}C_expr::C_expr( unsigned int deg, char c ){ degree = deg; symbolX = c; coefs = "0x^2+0x^1+0";}C_expr::C_expr( string str ){ degree = GetDegreeFromStr( str ); symbolX = 'x'; // todo coefs = str;}// C_expr::~C_expr(){ free( coef ); }string C_expr::ArrangeExprStr( string *str ){ int index; unsigned int i, n; unsigned int istart, iend; int num; char cnum[30]; string tmp, tmp2, str_xdeg; C_expr ChildExpr(degree, symbolX); const unsigned int len = str->length(); int deg; DeleteChars( str, ' ' ); // -- ()処理 -- // TODO for( i = 0; i < len; i++ ) { istart = str->substr(i).find('('); iend = str->substr(i).find(')'); if( istart == string::npos || iend == string::npos ) break; istart += i; iend += i; } // -- 掛け算系 -- // while( ( index = str->find('*') ) != (signed)string::npos ) { if( index != (signed)string::npos ) // 掛け算記号が見つかった { if( (*str).substr(index-1,1).find_first_of("0123456789") == 0 ) // num* { if( (*str).substr(index+1,1).find_first_of("0123456789") == 0 ) // num*num { for( i = index-1; ( i > 0 ) && ( !(*str).substr(i-1,1).find_first_of("0123456789") ); i-- ); for( n = index+1; (n < len) && ( !(*str).substr(n+1,1).find_first_of("0123456789") ); n++ ); num = atoi( (*str).substr(i, index-i).c_str() ) * atoi( (*str).substr(index+1).c_str() ); // can remove ",index-i" _itoa_s( num, cnum, 10 ); // 数字 tmp = *str; *str = (str->substr(0,i) + cnum) +str->substr( n+1 ); } else // TODO { break; } } } } // -- 足し算処理 -- // tmp = *str; *str = ""; str_xdeg = ""; for( deg = degree; deg >= 0; deg-- ) { num = 0; // -- deg乗の項を抜き出す -- // if( deg != 0) { // -- str_xdeg = x^deg -- // _itoa_s( deg, cnum, 10 ); str_xdeg = symbolX; str_xdeg+= '^'; str_xdeg+= cnum; while( tmp.find(str_xdeg) != -1 ) { index = tmp.find(str_xdeg); if( tmp.substr(index-1,1).find_first_of("0123456789") == 0 ) { for( i = index-1; ( i > 0 ) && ( tmp.substr(i-1,1).find_first_of("0123456789") != -1); i-- ); num += atoi( tmp.substr(i, index-i).c_str() ); } else // 係数 = 1 だと思う { num++; } tmp2 = tmp; if( i != 0 ) tmp = tmp2.substr(0, i-1) + tmp2.substr(index+str_xdeg.length()); else tmp = tmp2.substr(index+str_xdeg.length()); } _itoa_s( num, cnum, 10 ); *str += cnum; *str += str_xdeg + " + "; } else // 定数項 { num = 0; while( tmp.find_first_of("0123456789") != string::npos ) { num += atoi( tmp.substr( tmp.find_first_of("0123456789") ).c_str() ); for( n = tmp.find_first_of("0123456789"); (n < len) && ( !tmp.substr(n,1).find_first_of("0123456789") ); n++ ); tmp = tmp.substr(n); } _itoa_s( num, cnum, 10 ); *str += cnum; } } return *str;}void C_expr::DeleteChars( string *str, char c ){ unsigned int i; for( i = 0; (*str)[i] != '\0'; i++ ) { if( (*str)[i] == c ) { StringShift( str, i, 1 ); i--; } }}int C_expr::DistanceToNextMark( string str ) // ret: find: distance, not find: -1{ const unsigned int i = str.find_first_of("+-"); return ( i < str.length() )? i: -1 ;}string C_expr::OutputStr( string *str ){ return *str = coefs;}int C_expr::GetDegreeFromStr( string str ){ int p; const int distance2nextmark = DistanceToNextMark(str); p = str.find('^'); if( p != -1 && ( p < distance2nextmark || distance2nextmark == -1 ) ) return atoi( str.substr(p+1).c_str() ); else return 0;}C_expr C_expr::operator+ ( string str ){ // -- 宣言 -- // C_expr expr_tmp(degree, symbolX); expr_tmp.coefs = ( coefs + "+" ) + str; ArrangeExprStr( &expr_tmp.coefs ); return expr_tmp;}C_expr C_expr::operator+=( string str ){ // -- 宣言 -- // string copystr; // -- 初期化 -- // coefs += "+"; coefs += str; ArrangeExprStr( &coefs); return *this;}C_expr C_expr::operator+ ( C_expr expr ){ string str; return *this+expr.OutputStr(&str);}C_expr C_expr::operator+=( C_expr expr ){ string str; return *this+=expr.OutputStr(&str);}int main(void){ string str; C_expr expr1(2,'x'); C_expr expr2(2,'x'); C_expr expr3(2,'x'); // -- 初期 0 0 0 -- // cout << expr1.OutputStr(&str) << endl; cout << expr2.OutputStr(&str) << endl; // -- 多項式を足す -- // expr1 += "3*5x^2+ 3x^1 + 2*4 +6x^2"; expr2 += "3x^2 + 2"; expr3 = expr1+expr2; // -- 変更後の結果 --// cout << expr1.OutputStr(&str) << endl; cout << expr2.OutputStr(&str) << endl; cout << expr3.OutputStr(&str) << endl; return 0;}