All pastes #1590541 Raw Edit

ExprClass

public cpp v1 · immutable
#1590541 ·published 2009-10-03 13:20 UTC
rendered paste body
#include<stdio.h>#include<stdlib.h>#include<string.h>#include <iostream>class C_expr{public:	C_expr	();	C_expr	( unsigned int deg, char c );	// ~C_expr	();	inline int  GetCoef	( unsigned int deg ) const;	char* OutputStr	( char *str );	C_expr operator+( char *str );	template<class T>C_expr operator^( const T& a )	// for test	{ return *this; }	C_expr operator+=( char *str );private:	int *coef;	unsigned int degree;	char symbolX;	inline void GetMemory	( unsigned int deg );	void InitZero	();	inline void CopyData( C_expr *Dest );	void CopyCoefs( C_expr *Dest );	inline void SetCoef	( int num, unsigned int deg ) const;	int DistanceToNextMark( char *str);	void DeleteChars( char *str, char c);	int GetDegreeFromStr( char *str);};C_expr::C_expr(){	degree = 2;	symbolX = 'x';	GetMemory( degree );	InitZero();}C_expr::C_expr( unsigned int deg, char c ){	degree = deg;	symbolX = c;	GetMemory( degree );	InitZero();}// C_expr::~C_expr(){ free( coef ); }void C_expr::DeleteChars( char *str, char c ){	unsigned int i;	const unsigned int len = strlen(str);	for( i = 0; str[i] != '\0'; i++ )	{		if( str[i] == c )			strcpy_s( str+i, len, str+i+1 );	}}int C_expr::DistanceToNextMark( char *str )	// ret: find: distance, not find: -1{	const unsigned int i = strcspn( str, "+-" );	return ( i < strlen(str) )? i: -1 ;}inline void C_expr::GetMemory( unsigned int deg ){	coef = (int*)malloc( sizeof(int)* (deg+1) );}void C_expr::InitZero(){	unsigned int i;	for( i = 0; i <= degree; i++ )		coef[i] = 0;}char* C_expr::OutputStr( char *str ){	int i;	char tmp[ 100]; // TO DO	strcpy_s( str, degree,  "" );	for( i = degree; i >= 0 ; i-- )	{		if( i == 0 )	sprintf_s( tmp, 100, "%d",coef[i] );		else			sprintf_s( tmp, 100, "%dx^%d + ",coef[i] ,i );		strcat_s( str, 100, tmp );	}	return str;}inline void C_expr::CopyData( C_expr *Dest ){	Dest->symbolX = symbolX;	// mp	CopyCoefs( Dest );			// p}void C_expr::CopyCoefs( C_expr *Dest ){	unsigned int i;	for( i = 0; i <= degree; i++ )		Dest->coef[i] = coef[i];}inline void C_expr::SetCoef( int num, unsigned int deg ) const{	coef[deg] = num;}inline int C_expr::GetCoef( unsigned int deg ) const{	return coef[deg];}int C_expr::GetDegreeFromStr( char *str ){	char *p;	const int distance2nextmark = DistanceToNextMark(str);		p = strchr( str, '^');		if( p != NULL && ( p < str+distance2nextmark || distance2nextmark == -1 ) )		return atoi(p+1);	else		return 0;}C_expr C_expr::operator+( char *str ){	// --  -- //	int num;	unsigned int deg;	const unsigned int len = strlen(str);	char *copystr; 	bool Flag_continue;	int distance2nextmark;	C_expr expr_tmp(degree, symbolX);	// --  -- //	CopyData( &expr_tmp );	copystr = new char( len+1 );		// obt@m	strcpy_s( copystr, len+1, str );	// Rs[	DeleteChars( copystr, ' ' );	DeleteChars( copystr, '*' );	// -- (O) -- //	do	{		// --  -- //		num = (copystr[0] == symbolX)? 1: atoi( &copystr[0] ) ;	// W( 1,atoi )		deg = GetDegreeFromStr( copystr );					// 		// --  -- //		expr_tmp.SetCoef( expr_tmp.GetCoef(deg) + num, deg );		// --  -- //		distance2nextmark = DistanceToNextMark(copystr);		Flag_continue = ( distance2nextmark == -1)? false: true ;		// --  -- //		if( Flag_continue )		{			strcpy_s( copystr, len, copystr+distance2nextmark+1 );	// Ll.		}	}while( Flag_continue );	return expr_tmp;}C_expr C_expr::operator+=( char *str ){	// --  -- //	int num;	unsigned int deg;	const unsigned int len = strlen(str);	char *copystr; 	bool Flag_continue;	int distance2nextmark;	// --  -- //	copystr = new char( len+1 );		// obt@m	strcpy_s( copystr, len+1, str );	// Rs[	DeleteChars( copystr, ' ' );	DeleteChars( copystr, '*' );	// -- (O) -- //	do	{		// --  -- //		num = (copystr[0] == 'x')? 1: atoi( &copystr[0] ) ;	// W( 1,atoi )		deg = GetDegreeFromStr( copystr );					// 		// --  -- //		SetCoef( GetCoef(deg) + num, deg );		// --  -- //		distance2nextmark = DistanceToNextMark(copystr);		Flag_continue = ( distance2nextmark == -1)? false: true ;		// --  -- //		if( Flag_continue )		{			strcpy_s( copystr, len, copystr+distance2nextmark+1 );	// Ll.		}	}while( Flag_continue );	return *this;}//C_expr::template<class T>C_expr operator^( const T& a );int main(void){	char str[100];	C_expr expr(2,'x');	// --  0 0 0 -- //	printf( "before : %s\n", expr.OutputStr(str) );	// --  -- //	expr += "2*x^2+ 3x^1 + 3 +6x^2";	expr^x;	// -- X --//	printf( "after  : %s\n", expr.OutputStr(str) );	return 0;}