C 로 계산기 구현 (소수점 과 괄호 및 연산 우선 순위 포함)

5892 단어 데이터 구조
/*************************************************************************
  :              
  :        ,         ,         
  :566
**************************************************************************/
#include 
#include 
#include 
#define MAX 100

typedef struct stack1                      //   
{
	float num[MAX];
	int top;
}O_NUM;

typedef struct stack2                      //   
{
	char symbol[MAX];
	int top;
}O_SY;

/**********************************************************************
  :          
  :O_NUM *stacknum,O_SY *stacksymbol
   :void
***********************************************************************/
void init_O_NUM(O_NUM *stacknum)                //      
{
	stacknum->top = -1;
}

void init_O_SY(O_SY *stacksymbol)               //      
{
	stacksymbol->top = -1;
}
/*************************************************************************
  :           
  :O_NUM *stacknum,float num
   :int
**************************************************************************/
int PUSH_Stacknum(O_NUM *stacknum,float num)
{
	if(stacknum->top == MAX-1)
	{
		printf("        ,    !
"); return -1; } stacknum->top++; stacknum->num[stacknum->top] = num; return 0; } /*************************************************************************** : :O_SY *stacksymbol, char ch :int ***************************************************************************/ int PUSH_Stacksymbol(O_SY *stacksymbol,char ch) { if(stacksymbol->top == MAX-1) { printf(" , !
"); return -1; } stacksymbol->top++; stacksymbol->symbol[stacksymbol->top] = ch; return 0; } /*************************************************************************** : :O_NUM *stacknum :float ***************************************************************************/ float POP_Stacknum(O_NUM *stacknum) { float data = 0; data = stacknum->num[stacknum->top]; stacknum->top--; return data; } /************************************************************************** : :O_SY *stacksymbol :char **************************************************************************/ char POP_Stacksymbol(O_SY *stacksymbol) { char ch; ch = stacksymbol->symbol[stacksymbol->top]; stacksymbol->top--; return ch; } /************************************************************************* : judge_symbol :char ch :1( ),0( ) *************************************************************************/ int judge_symbol(char ch) { if(ch == '+'||ch == '-'||ch == '*'|| ch == '/'||ch == '('||ch == ')'||ch == '='||ch == '
') { return 1; } else { return 0; } } /************************************************************************* : :char t1,char t2 t1 ,t2 :char **************************************************************************/ char op_campare(char t1,char t2) { char ch; switch(t2) { case '+': case '-': { ch = (t1 == '('||t1 == '=')?''; break; } case '*': case '/': { ch = (t1 == '*'||t1 == '/' ||t1 == ')')?'>':''; } break; } case '=': { switch(t1) { case '=': { ch = '='; break; } case '(': { printf("error input!
"); return -1; } default:ch = '>'; } } } return ch; } /********************************************************************** : :float num1,char op,float num2 :float ***********************************************************************/ float calculate(float num1,char op,char num2) { float result; switch(op) { case '+': { result = num1 + num2; break; } case '-': { result = num1 - num2; break; } case '*': { result = num1 * num2; break; } case '/': { if(num2 == 0) { printf("error input!
"); return -1; } else { result = num1/num2; } } } return result; } /************************************************************************** , :void :float result ***************************************************************************/ float MainCalc() { O_NUM stacknum; // O_SY stacksymbol; // float num1,num2,result,num; char ch,sign; char *str = NULL; int count = 0; init_O_NUM(&stacknum); // init_O_SY(&stacksymbol); // '=' PUSH_Stacksymbol(&stacksymbol,'='); ch = getchar(); // while((ch != '=')||stacksymbol.symbol[stacksymbol.top] != '=') { if(judge_symbol(ch) == 0) { str = (char *)malloc(sizeof(char)*10); do { *str = ch; str++; count++; ch = getchar(); }while(judge_symbol(ch) == 0); *str = '\0'; str = str - count; num = atof(str); // PUSH_Stacknum(&stacknum,num); // str = NULL; count = 0; } else { switch(op_campare(stacksymbol.symbol[stacksymbol.top],ch)) { case '': { sign = POP_Stacksymbol(&stacksymbol); num2 = POP_Stacknum(&stacknum); num1 = POP_Stacknum(&stacknum); result = calculate(num1,sign,num2); PUSH_Stacknum(&stacknum,result); break; } } } } result = stacknum.num[stacknum.top]; return result; } int main() { float result; result = MainCalc(); printf("%g
",result); return 0; }

좋은 웹페이지 즐겨찾기