// Unary polynomial addition and subtraction Program
// Program: Zhang jianbo
// Time: 2005/7/12 pm: 20-08
// Function:
// 1: 1 + 2 + 3-1 + 2-5 + 6 + 3 (addition and subtraction are acceptable)
// 2: 2x + 3X + 5x-x ^ 2 + x ^ 3 + 4x ^ 7 + 9
// 3: You can perform combined computing for a = 1 + 2 + x ^ 2 B = x + x ^ 2 a + B = 3 + 2x + 2x ^ 2
// Note: Except for the index, the value cannot be negative !! You can enter a negative number for all others.
# Include <iostream. h>
# Include <string. h>
# Include <math. h>
# Include "menu. H"
# Include "key. H"
Typedef struct polyn // item Structure
{
Int e; // Index
Int C; // Coefficient
} Polynelem;
/// Function declaration
Char * midstr (char * P, int F1, int F2); // string
Int sval (char * s); // string, converted to integer variable
Int FX (char * F, polynelem * Arg); // separates items from expressions
Void sppolyn (char * polynstring, int * C, int * E); // Add: separation coefficient, Exponent
Void polynelemsub (polynelem * s, Int & NN); // merge similar items
Void addpolynelem (polynelem * arga, Int & Na, polynelem * argb, int Nb, int isadd );
// Merge expression A and expression B. The result is in expression.
Void outputfx (polynelem * Arg, int N); // output expression
Void orderfx (polynelem * Arg, int N); // sort by expression
Void test_fx (); // test the program
///~
Int _ f2_main (){
Menu M [3];
M [1]. Name = "unary polynomial addition and subtraction ";
M [2]. Name = "return ";
Int T = 1, ID;
While (t)
{
Showmenu ("unary polynomial addition and subtraction of Data Structure", m, 2); // display menu
Cout <"/T Description: 1-each expression cannot exceed 300 characters! /N ";
Cout <"/T 2-expression can be composed of one item containing x (n times of ax and x (x ^ N), constant term (c) composition/N ";
Cout <"/T 3-expression can contain addition and subtraction operations +/-/N ";
Cout <"/T example: 8x + 2x ^ 3 + 3x ^ 4 + 60-4x ^ 2-9 + x/N ";
Id = selectmenuid ();
Switch (ID)
{
Case 1: test_fx (); initkey (); break;
Case 2: T = 0; break;
}
}
Return 0;
}
Void test_fx () {// Test Program
Polynelem A [600], B [300];
Char F1 [300], F2 [300];
Cout <"Enter the mathematical expression A:/NF (x) = ";
Cin> F1;
Cout <"Enter the mathematical expression B:/NF (x) = ";
Cin> F2;
Cout <"/n the expression you entered/NA =" <F1 <Endl;
Cout <"B =" <F2 <Endl;
Int Na, NB;
/// Addition demonstration
NA = FX (F1, a); // recognition expression, and separation coefficient, stored in array
NB = FX (F2, B );
Polynelemsub (A, Na); // Add the same index in F1
Polynelemsub (B, Nb); // Add up the same index in F1
Cout <"A + B = ";
Addpolynelem (A, Na, B, Nb, 1); // addition merge
Polynelemsub (A, Na); // computing
Orderfx (A, Na); // sort
Outputfx (A, Na); // output result
/// Subtraction demonstration
NA = FX (F1, a); // recognition expression, and separation coefficient, stored in array
NB = FX (F2, B );
Polynelemsub (A, Na); // Add the same index in F1
Polynelemsub (B, Nb); // Add up the same index in F1
Cout <"A-B = ";
Addpolynelem (A, Na, B, Nb, 0); // subtraction merge
Polynelemsub (A, Na); // computing
Orderfx (A, Na); // sort
Outputfx (A, Na); // output result
}
Char * midstr (char * P, int F1, int F2) {// string
Char * Buf = new char [F2-F1 + 1]; // open a temporary array to save characters
Int K = 0;
For (INT I = F1; I <= F2; I ++) // retrieves a string from F1 until F2 ends.
{
Buf [k ++] = P [I]; // Save the string in Buf
}
Buf [k] = '/0 ';
Return Buf; // return the first address of the Buf.
}
Int sval (char * s) {// string, converted to integer variable
Int L = strlen (s );
Char * H, * P;
H = P = s;
Int TMP = 0;
Int K = 0;
Int PW = 0;
While (* P)
{
Switch (* P ){
Case '0': TMP = 0; break;
Case '1': TMP = 1; break;
Case '2': TMP = 2; break;
Case '3': TMP = 3; break;
Case '4': TMP = 4; break;
Case '5': TMP = 5; break;
Case '6': TMP = 6; break;
Case '7': TMP = 7; break;
Case '8': TMP = 8; break;
Case '9': TMP = 9; break;
}
PW = (INT) (POW (10, L-1 ));
TMP = TMP * PW;
K = K + TMP;
TMP = 0;
L --;
P ++;
}
Return K;
}
Void sppolyn (char * polynstring, int * C, int * E) {// Add entry: separation coefficient, Exponent
Char * H, * P, * fc;
Int F1 = 0, f2 = 0;
Char * CC, * EE;
Fc = H = P = polynstring; // point to polynstring
// If there is no processing coefficient, if there is no input coefficient, 1 is automatically added.
If (FC [0] = 'X '){
Int Len = strlen (FC );
Char * Ss = new char [Len + 2];
Ss [0] = '1 ';
Ss [1] = '/0 ';
Ss = strcat (SS, FC );
Fc = H = P = polynstring = SS; // point to new character data
}
Int TMP = 0;
If (strchr (FC, 'x ')! = NULL | strchr (FC, 'x ')! = NULL)
{
If (strchr (FC, '^ ')! = NULL)
{
While (* P)
{
If (* P = 'X' | * P = 'X ')
{
Cc = midstr (H, F1, F2-1); // coefficient string
F1 = F2;
}
P ++;
F2 ++;
}
EE = midstr (H, F1 + 2, strlen (h); // exponential string
* C = sval (CC );
* E = sval (EE );
}
Else
{
// One item
TMP = sval (midstr (H, 0, strlen (H)-2 ));
* C = TMP;
* E = 1;
}
}
Else
{
// Constant
TMP = sval (h); // convert the number
* C = TMP; // retention coefficient
* E = 0; // The index is set to 0.
}
}
Int FX (char * F, polynelem * Arg) // separates items from expressions
{
Int F1 = 0, f2 = 0;
Int I = 0;
Int J = 0;
Int n = 0;
Int C, E;
Char * P, * h;
Char * TMP;
H = P = f; // Save the expression
Int flag = 1; // symbol + /-
Int L1 = strlen (f); // error prevention
F [L1] = '#';
F [L1 + 1] = '/0 ';
While (* P ){
If (* P = '+' | * P = '-' | * P = '#')
{
If (j = 0) {// the first character to be processed is + or-
If (* P = '-') Flag =-1;
Else
Flag = 1;
J ++;
P ++;
Continue;
}
F2 = J-1;
TMP = midstr (H, F1, F2); // obtain the string
F1 = J + 1; // re-mark F1
Sppolyn (TMP, & C, & E); // Separation Coefficient
Arg [N]. C = C * flag;
Arg [N]. E = E;
N ++;
If (* P = '-') Flag =-1;
Else
Flag = 1;
}
J ++;
P ++;
}
Return N;
}
Void polynelemsub (polynelem * s, Int & NN) {// merge similar items
Int I, J;
Int K;
Int Pn = 0;
Int N;
N = nn;
Polynelem TMP [1000]; // temporary array, save the addition result
For (I = 0; I <n; I ++) // Add
For (j = I + 1; j <n; j ++)
{
If (s [I]. E = s [J]. e) // returns the same exponential value as the base number.
{
S [J]. c = s [J]. C + s [I]. C; // Add two numbers
S [I]. c = 0; // set the number of Integers to 0, indicating that the number has been added.
}
}
For (k = 0; k <n; k ++) // useless item with a picking coefficient of 0
{
If (s [K]. c = 0) continue;
Else
{
TMP [pn]. c = s [K]. C;
TMP [pn]. E = s [K]. E;
Pn ++;
}
}
For (k = 0; k <PN; k ++) // Save the result
{
S [K]. c = TMP [K]. C;
S [K]. E = TMP [K]. E;
}
Nn = pN; // return the number of joined items
}
Void addpolynelem (polynelem * arga, Int & Na, polynelem * argb, int Nb, int isadd) {// merges expression A and expression B, and the result is in expression.
Int I, J, K;
I = Na;
J = Nb;
For (k = 0; k <NB; k ++)
{
If (isadd = 1) arga [Na + K]. c = argb [K]. C; // addition merge
Else arga [Na + K]. c =-(argb [K]. c); // perform a +-processing when merging subtraction.
Arga [Na + K]. E = argb [K]. E;
}
NA = Na + NB;
}
Void outputfx (polynelem * Arg, int N) {// output expression
If (n = 0) cout <"0 ";
For (INT I = 0; I <n; I ++)
{
Cout <Arg [I]. C;
If (ARG [I]. e! = 0)
{
Cout <"X ";
If (ARG [I]. e! = 1) cout <"^" <Arg [I]. E;
}
If (ARG [I + 1]. c> = 0 & I + 1 <n) cout <"+ ";
}
Cout <"/N ";
}
Void orderfx (polynelem * Arg, int N) {// sort by expression
Int I, J;
Polynelem TMP;
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
{
If (ARG [J]. e <Arg [I]. e)
{
TMP. c = Arg [I]. C;
TMP. E = Arg [I]. E;
Arg [I]. c = Arg [J]. C;
Arg [I]. E = Arg [J]. E;
Arg [J]. c = TMP. C;
Arg [J]. E = TMP. E;
}
}
}