current position:Home>About the problem of C language: input 12 + 3 with DEVC + + to get 12.000000. I don't know what's wrong, include & lt; stdio. h>

About the problem of C language: input 12 + 3 with DEVC + + to get 12.000000. I don't know what's wrong, include & lt; stdio. h>

2022-02-02 17:35:22 CSDN Q & A

With devc++ Input 12+3# obtain 12.000000 I don't know what's wrong

#include <stdio.h> #include <stdlib.h> #include <string.h> #define NULL 0 #define OK 1#define ERROR -1 #define STACK_INIT_SIZE 100#define STACKINCREMENT 20 /*  Define character type stack  */ typedef struct{ int stacksize; char *base; char *top; } Stack;/*  Define integer stack  */ typedef struct{ int stacksize; float *base; float *top; } Stack2;/* -----------------  Global variables --------------- */ Stack OPTR;/*  Define the operator stack */Stack2 OPND; /*  Define the operand stack  */ char expr[255] = ""; /*  Store expression string  */ char *ptr = expr; int InitStack(Stack *s) // Construct operator stack { s->base=(char *)malloc(STACK_INIT_SIZE*sizeof(char)); if(!s->base) return ERROR;  s->top=s->base; s->stacksize=STACK_INIT_SIZE;return OK; } int InitStack2(Stack2 *s) // Construct operand stack { s->base=(float *)malloc(STACK_INIT_SIZE*sizeof(float)); if(!s->base) return ERROR; s->stacksize=STACK_INIT_SIZE; s->top=s->base; return OK; } int In(char ch) // Determine whether the character is an operator , Operator returns 1{ return(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='#'); } int Push(Stack *s,char ch) // Operator stack insert ch For the new stack top element {  *s->top=ch; s->top++; return 0; } int Push2(Stack2 *s,float ch)// Operand stack insertion ch For the new stack top element  { *s->top=ch; s->top++; return 0; }char Pop(Stack *s) // Delete operator stack s Top element of , use p Return its value { char p;s->top--; p=*s->top; return p;} float Pop2(Stack2 *s)// Delete operand stack s Top element of , use p Return its value  { float p;s->top--; p=*s->top; return p;}char GetTop(Stack s)// use p Returns the operator stack s Top element of { char p=*(s.top-1); return p; }float GetTop2(Stack2 s) // use p Returns the operand stack s Top element of { float p=*(s.top-1); return p; }/*  Judgment operator priority , Returns the high priority  */ char Precede(char c1,char c2) { int i=0,j=0; static char array[49]={'>', '>', '<', '<', '<', '>', '>', '>', '>', '<', '<', '<', '>', '>', '>', '>', '>', '>', '<', '>', '>', '>', '>', '>', '>', '<', '>', '>', '<', '<', '<', '<', '<', '=', '!', '>', '>', '>', '>', '!', '>', '>', '<', '<', '<', '<', '<', '!', '='}; switch(c1) { /* i For the following array The banner of  */ case '+' : i=0;break; case '-' : i=1;break; case '*' : i=2;break; case '/' : i=3;break; case '(' : i=4;break; case ')' : i=5;break; case '#' : i=6;break; } switch(c2) { /* j For the following array Vertical standard of  */ case '+' : j=0;break; case '-' : j=1;break; case '*' : j=2;break; case '/' : j=3;break; case '(' : j=4;break; case ')' : j=5;break; case '#' : j=6;break; }return (array[7*i+j]); /*  Return operator  */ } /* Operation function  */ float Operate(float a,char op,float b) { switch(op) { case '+' : return (a+b); case '-' : return (a-b); case '*' : return (a*b); case '/' : return (a/b); }  return 0;} int num(float n)// Returns the length of the operand {      char p[10];    int pl;    itoa((int)n,p,10);// Convert integer to string     pl=2*strlen(p);    return pl;}float EvalExpr()// Main operation functions  { char c,theta,x; int n;float m;float a,b; c = *ptr++; while(c!='#'||GetTop(OPTR)!='#') {if(!In(c)) { if(!In(*(ptr-1))) ptr=ptr-1;m=atoi(ptr);// Take the number field in front of the string n=num(m); Push2(&OPND,m); ptr=ptr+n;c=*ptr++;} else switch(Precede(GetTop(OPTR),c)) { case '<': Push(&OPTR,c); c = *ptr++; break; case '=': x=Pop(&OPTR); c = *ptr++; break; case '>': theta=Pop(&OPTR); b=Pop2(&OPND); a=Pop2(&OPND); Push2(&OPND,Operate(a,theta,b));break;} }return GetTop2(OPND); } int main( ) { printf(" Please enter the correct expression to '#' ending :"); do{ gets(expr); }while(!*expr); InitStack(&OPTR); /*  Initialize the operator stack  */ Push(&OPTR,'#'); /*  take # Push into the operator stack  */ InitStack2(&OPND); /*  Initializes the operand stack  */ printf(" The expression result is :%f\n", EvalExpr());getchar();return 0; }



Refer to the answer 1:

printf(" The expression result is :%f\n", EvalExpr());// The output format is %f, Floating point output , The decimal point is reserved by default 6 position




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021735201353.html

Random recommended