Home > Back-end >  Small white for help!!!!!! Polynomial arithmetic problem!!!!!
Small white for help!!!!!! Polynomial arithmetic problem!!!!!

Time:09-23

Really is pure white, then now requires complete polynomial algorithm with c + + (including brackets) of that kind of, but the program is written is a question! Create the stack in the Calculate will somehow call getpop method, and then complains, strives for the big help small white wow!

#include
# include
# include
#include
#include
Using namespace STD.

Typedef int ElemType;
Typedef int the Status;

//define symbols chain stack
Typedef struct SNode * Stack;
Struct SNode {
Char data;
Struct SNode * next;
};

//initialize
Stack InitList () {
Stack the L;
L=(Stack) malloc (sizeof (struct SNode));
L - & gt; Next=NULL;//create a head node
Return the L;
}

//into the stack
Void push (Stack s, ElemType x) {
Stack TmpCell;
TmpCell=(Stack) malloc (sizeof (struct SNode));
TmpCell - & gt; Data=https://bbs.csdn.net/topics/x;
TmpCell - & gt; Next=s - & gt; next;
S - & gt; Next=TmpCell;
}

//the stack
{char pop (Stack s)
Stack FirstCell;
ElemType temp.

FirstCell=s - & gt; next;
S - & gt; Next=FirstCell - & gt; next;
Temp=FirstCell - & gt; The data;
Free (FirstCell); Return temp.
}


//define priority stack elements
The Status of isps (char ch) {
The switch (ch) {
Case '#' : return 0;
Case '(' : return 1;
Case: '+' return 3;
Case '-' : return 3;
Case '*' : return 5;
Case '/' : return 5;
Case '%' : return 5;
Case '^' : return 7;
Case ') : return 8;
}
}

//get to the top of stack elements
{char getpop Stack (s)
char temp;
Temp=s - & gt; Next - & gt; The data;
Return temp.
}

//define the current element priority
The Status osp (char ch) {
The switch (ch) {
Case '#' : return 1;
Case '(' : return 8;
Case: '+' return 2;
Case '-' : return 2;
Case '*' : return 4;
Case '/' : return 4;
Case '%' : return 4;
Case '^' : return 6;
Case ') : return 1;
}
}


//define the method to calculate the
Void the count (Stack nuper, char topc) {
int result=0;
Int number1, number2;

Number2=pop (nuper) - 48;
Number1=pop (nuper) - 48;

The switch (topc) {
Case '+' : {result=number1 + number2; break; }
Case '-' : {result=number1 - number2; break; }
Case '*' : {result=number1 * number2; break; }
Case '/' : {result=number1/number2; break; }
Case '%' : {result=number1 % number2; break; }
Case '^' : {result=pow (number1, number2); break; }
}
Push (nuper, result);

}

//calculation process
Char Calculate (char * e) {
Char c, topc;
Stack nuper=InitList ();//create digital stack
Stack oper=InitList ();//create symbolic stack
int i;

Push (oper, '#');
I=0; C=e [0];
While (c!='#') {
If (c & gt;=48 & amp; & C & lt;=57)
Push (nuper, c);//will c input digital stack
The else
{
If (c==41)
{
Topc=pop (oper);
While (c! 40)={
Count (nuper topc);//throw Numbers stack the last two Numbers and operations, will result in the stack
Topc=pop (oper);
}
Pop (oper);//abandon the stack '('
}
The else {
Topc=getpop (oper);
While (osp (c) & lt; Isp (topc)) {//compare current symbols and the priority of the stack of symbol
Topc=pop (oper);
Count (nuper topc);//throw Numbers stack the last two Numbers and operations, will result in the stack
Topc=getpop (oper);
}
} push (oper, c);
} c=e (+ + I),
} topc=pop (oper); While (topc!='#') {count (nuper topc); Topc=pop (oper); }

Return (pop (nuper));
}



Int main () {
Cout<" Input polynomial: ";
Char e [256];
Cin> e;

Char total=Calculate (e);
Cout<" The end result is: "& lt; return 0;
}

CodePudding user response:

Fyi:
//list of one yuan polynomial addition subtraction multiplication 
#include
#include
Typedef struct node {
Float coef.//coefficient
Int expn;//index
Struct node * next;
}
PolyNode;//polynomial polynomial node node
Typedef PolyNode * Polynomial;
Polynomial createPolynomial () {//create a Polynomial
PolyNode * p * q, * head=(PolyNode *) malloc (sizeof (PolyNode));//head node
The head - & gt; Next=NULL;
Float coef.
Int expn;
Printf (" input the polynomial of each coefficient and index, and each row, enter 0 0 over! \n");
While (the scanf (" % f, % d ", & amp; Coef, & amp; Expn) & amp; & Coef) {//by default, according to the descending index
If (the head - & gt; Next) {
P=the head;
While (p - & gt; Next & amp; & Expn & lt; P - & gt; Next - & gt; Expn)
P=p - & gt; next;
If (p - & gt; Next) {
If (expn==p - & gt; Next - & gt; Expn) {//have the same index directly add to the original polynomial coefficient
P - & gt; Next - & gt; Coef +=coef;
If (p - & gt; Next - & gt; Coef & gt; 0.000001 & amp; & P - & gt; Next - & gt; Coef & lt; 0.000001) {//if the combined coefficient is 0, then abandon the node
Q=p - & gt; next;
P - & gt; Next=q - & gt; next;
Free (q);
}
} else {
Q=(PolyNode *) malloc (sizeof (PolyNode));
Q - & gt; Coef=coef;
Q - & gt; Expn=expn;
Q - & gt; Next=p - & gt; next;
P - & gt; Next=q;
}
} else {
P - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
P=p - & gt; next;
P - & gt; Coef=coef;
P - & gt; Expn=expn;
P - & gt; Next=NULL;
}
} else {
The head - & gt; Next=(PolyNode *) malloc (sizeof (PolyNode));
The head - & gt; Next - & gt; Coef=coef;
The head - & gt; Next - & gt; Expn=expn;
The head - & gt; Next - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related