C语言写个计算器

Posted 勇二郎

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言写个计算器相关的知识,希望对你有一定的参考价值。

原创内容:转载请注明出处:https://blog.csdn.net/ndzjx/article/details/122027916

整体思路:

1. 格式化:(1)在符号前加0  (2)去掉空格
2. 检查     (1)检测左右括号的两边是否合法
3. 解析     (1)将输入的字符串分解到结构体数组中
4. 计算      遍历结构体数组,数字入数字栈,符号入符号栈,按照优先级表进行运算。

stack.h

#ifndef STACK_H
#define STACK_H

typedef float eletype;
typedef struct

    eletype *buf;
    int top;
    int max;
stack;
stack *create(int im);
int push(stack *ss,eletype data);
int pop(stack *ss,eletype *data);
int gettop(stack *ss,eletype *data);
int isfull(stack *ss);
int isempty(stack *ss);
void destroy(stack **ss);

stack *create(int im)

    stack *ss;
    ss=(stack *)malloc(sizeof(stack));
    if(!ss)
    
        return 0;
    
    ss->buf=(eletype *)malloc(im*sizeof(eletype));
    if(!ss->buf)
    
        free(ss);
        return 0;
    
    ss->top=-1;
    ss->max=im;
    return ss;


void destroy(stack **ss) 
    if (!*ss) 
        if ((*ss)->buf) 
            free((*ss)->buf);
        
        free((*ss));
        *ss=0;
    


int push(stack *ss,eletype data)

    if(isfull(ss)) return 0;
    ss->buf[++ss->top]=data;
    return 1;

int pop(stack *ss,eletype *data)

    if(isempty(ss)) return 0;
    *data=ss->buf[ss->top--];
    return 1;

int gettop(stack *ss,eletype *data)

    if(isempty(ss)) return 0;
    *data=ss->buf[ss->top];
    return 1;

int isfull(stack *ss)

    return ss->top==ss->max-1;

int isempty(stack *ss)

    return ss->top==-1;



#endif // STACK_H

main.c

#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include "stack.h"

#define MAXBUF 100

char *symbol="+-*/^()#";
char *symnum="0123456789.";
char table[8][8]='>','>','<','<','<','<','>','>',
                  '>','>','<','<','<','<','>','>',
                  '>','>','>','>','<','<','>','>',
                  '>','>','>','>','<','<','>','>',
                  '>','>','>','>','>','<','>','>',
                  '<','<','<','<','<','<','=','?',
                  '-','-','-','-','-','-','-','-',
                  '<','<','<','<','<','<','-','='
                 ;
typedef struct

    float data;
    int flag;
newtype;

int search(char *str,char a);
int checknum(char *str);
int format(char *input);
int check(char *input);
int analysis(char *input,newtype *node,int *nnode);
int calc(newtype *node,int nnode,float *result);

float add(float f1,float f2) return f1+f2;
float sub(float f1,float f2) return f1-f2;
float mul(float f1,float f2) return f1*f2;
float div1(float f1,float f2) return f1/f2;
float fmi(float f1,float f2)

    return (float)pow(f1,f2);
    /*
    // 考虑到指数有小数的情况,先调用库函数pow比较简单。
    int i;
    int num=(int)f2;
    float result = f1;
    for (i=1;i<num;i++)
    
        result = result * f1;
    
    return result;
    */

float (*fun[])(float,float)=add,sub,mul,div1,fmi;

int main()

    char input[MAXBUF];
    float result;
    newtype node[MAXBUF];
    int nnode;

    printf("please input the expression:\\n");
    gets(input);

    if(!format(input))
    
        printf("format error.\\n");
        return 0;
    
    else if(!check(input))
    
        printf("check error.\\n");
        return 0;
    
    else if(!analysis(input,node,&nnode))
    
        printf("analysis error.\\n");
        return 0;
    
    else if(!calc(node,nnode,&result))
    
        printf("calc error.\\n");
        return 0;
    
    else
    
        printf("result = %6.3f\\n",result);
        return 1;
    

int search(char *str,char a)

    int i;
    for(i=0;str[i];i++)
    
        if(str[i]==a)
        
            return i;
        
    
    return -1;

int checknum(char *str)

    int i,nc=0;
    for(i=0;str[i];i++)
    
        if(str[i]=='.')
        
            nc++;
        
    
    return (nc<=1);

int format(char *input)

    int i,j=0;
    char temp[MAXBUF] = 0;
    memcpy(temp, input, strlen(input));

    for(i=0;temp[i];i++)
    
        if((temp[i]=='-'||temp[i]=='+')&&(i==0||(i>0&&temp[i-1]=='(')))
        
            input[j++]='0';
        
        if(temp[i]!=' '&&temp[i]!='\\t')
        
            input[j++]=temp[i];
        
    
    input[j]='\\0';
    return 1;

int check(char *input)

    int i;
    for(i=0;input[i];i++)
    
        if(input[i]=='(')
        
            if(search(symnum,input[i-1])!=-1)
            
                printf("before '(' can't be num.\\n");
                return 0;
            
            if(input[i-1]==')')
            
                printf("before '(' can't be ')'.\\n");
                return 0;
            
            if(input[i+1]&&(search(symbol,input[i+1])!=-1)&&input[i+1]!='(')
            
                printf("behind of '(' can't be other symbol.\\n");
                return 0;
            
        
        if(input[i]==')')
        
            if((search(symbol,input[i-1])!=-1)&&input[i-1]!=')')
            
                printf("before ')' can't be other symbol.\\n");
                return 0;
            
            if(input[i+1]&&search(symnum,input[i+1])!=-1)
            
                printf("behind of ')' can't be num.\\n");
                return 0;
            
        
    
    return i;

int analysis(char *input,newtype *node,int *nnode)

    int i,inode=0,j=0;
    char temp[MAXBUF];
    for(i=0;input[i];i++)
    
        if(search(symbol,input[i])!=-1)
        
            node[inode].data=(float)input[i];
            node[inode++].flag=0;
        
        else if(search(symnum,input[i])!=-1)
        
            j=0;
            while(search(symnum,input[i])!=-1)
            
                temp[j++]=input[i++];
            
            temp[j]='\\0';
            i--;
            if(j>9)
            
                printf("the %d's num is too long than 9.\\n",i-j+2);
                return 0;
            
            if(!checknum(temp))
            
                printf("the %d's num have many '.'.\\n",i-j+2);
                return 0;
            
            node[inode].data=atof(temp);
            node[inode++].flag=1;
        
        else
        
            printf("the %d's have other symbol.\\n",i+1);
            return 0;
        
    
    node[inode].data=(float)'#';
    node[inode++].flag=0;
    *nnode=inode;
    return 1;

int calc(newtype *node,int nnode,float *result)

    stack *ss1,*ss2;
    int i,row,col;
    float f1,f2,f3;
    ss1=create(MAXBUF);
    if(!ss1)return 0;
    ss2=create(MAXBUF);
    if(!ss2) destroy(&ss1);return 0;
    push(ss2,(float)'#');

    for(i=0;i<nnode;i++)
    
        if(node[i].flag==1)
        
            if(!push(ss1,node[i].data))
            
                printf("%6.3f can't push",node[i].data);
                return 0;
            
        
        else
        
            if(!gettop(ss2,&f1))
            
                printf("%c can't gettop of char stack.\\n",(char)node[i].data);
                return 0;
            
            row=search(symbol,(char)f1);
            col=search(symbol,(char)node[i].data);
            switch(table[row][col])
            
                case '<':
                    if(!push(ss2,(char)node[i].data))
                    
                        printf("%c can't push.\\n",(char)node[i].data);
                        return 0;
                    
                    break;
                case '=':
                    if(!pop(ss2,&f1))
                    
                        printf("no happen!.\\n");
                        return 0;
                    
                    break;
                case '>':
                    if(!pop(ss2,&f2))
                    
                        printf("no happen!.\\n");
                        return 0;
                    
                    if(!pop(ss1,&f3))
                    
                        printf("%c not have two operator.\\n",(char)f2);
                        return 0;
                    
                    if(!pop(ss1,&f1))
                    
                        printf("%c not have two operator.\\n",(char)f2);
                        return 0;
                    
                    if(!push(ss1,fun[row](f1,f3)))
                    
                        printf("no happen!.\\n");
                        return 0;
                    
                    i--;
                    break;
                case '?':
                    printf("the '(' is not match.\\n");
                    return 0;
                case '-':
                    printf("the ')' is not match.\\n");
                    return 0;
            
        
    
    if(!isempty(ss2))
    
        printf("the char stack have other symbol.\\n");
        return 0;
    
    else if(!pop(ss1,&f1))
    
        printf("the num stack is empty.no result.\\n");
        return 0;
    
    else if(isempty(ss1))
    
        *result=f1;
        return 1;
    
    else
    
        printf("the num stack have other num.\\n");
        return 0;
    


以上是关于C语言写个计算器的主要内容,如果未能解决你的问题,请参考以下文章

C语言写个计算器

C语言写个计算器

用C语言写个计算矩形的面积

请给我写个,用C语言做的,10进制转换ASCII的程序。需要验证过的

学了C语言想装x能干点啥?手把手教你写个聊天软件来玩玩

学了C语言想装x能干点啥?手把手教你写个聊天软件来玩玩