内存:128  时间:1

题目描述

输入

输出

样例输入

样例输出

提示

代码如下

#ifndef CALC_H_INCLUDED
#define CALC_H_INCLUDED
#include<stdio.h>
#include<stdlib.h>
int getOperand(char c);
char getOperator(char c);
int cmpPriority(char ope1,char oper2);
double  calculate(double num1,char oper,double num2);

#endif // CALC_H_INCLUDED

typedef char Operator;
Operator add='+',subtract='-',multiply='*',divide='/';
int is_shuzi(char c)
{
    if(c<='9'&&c>='0')
        return 1;
    else
        return 0;
}
int is_operator(char c)
{
    if(c=='+'||c=='-'||c=='*'||c=='/')
        return 1;
    else
        return 0;
}
int is_high_operator(char c)
{
    if(c=='*'||c=='/')
        return 1;
    else
        return 0;
}
int main()
{
    FILE *fin,*fout;
    fin=fopen("input.dat","r");
    if(fin==NULL)
    {
        printf("文件打开失败
");
        exit(-1);
    }
    fout=fopen("output.dat","w");
    if(fout==NULL)
    {
        printf("文件打开失败
");
        exit(-2);
    }
    while(!feof(fin))
    {
        double result;
        char oper,ope1,oper2,num1,num2,c;
        result=calculate(num1,oper,num2);
        oper=cmpPriority(ope1,oper2);
        num1= getOperand(c);
        num2=getOperand(c);
        ope1=getOperator(c);
        oper2=getOperator(c);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}
int getOperand(char c)
{
    int num;
    num=c-'0';
    return num;
}
char getOperator(char c)
{
    if(c==add)
        return add;
    if(c==subtract)
        return subtract;
    if(c==multiply)
        return multiply;
    if(c==divide)
        return divide;
}
int cmpPriority(char ope1,char oper2)
{
    if((ope1=='+'||ope1=='-')&&(oper2=='*'||oper2=='/'))
        return 1;
    if((oper2=='+'||oper2=='-')&&(ope1=='+'||ope1=='-'))
        return 0;
    if((ope1=='*'||ope1=='/')&&(oper2=='*'||oper2=='/'))
        return 2;
    if((ope1=='*'||ope1=='/')&&(oper2=='+'||oper2=='-'))
        return -1;
}
double calculate(double num1,char oper,double num2)
{
    double result;
    if(oper==add)
        result=num1+num2;
    else
    {
        if(oper==subtract)
            result=num1-num2;
        else
        {
            if(oper==multiply)
                result=num1*num2;
            else
            {
                if(num2==0)
                    result=-10000000;
                else
                {
                    if(oper==divide)
                        result=num1/num2;
                }
            }
        }
    }
    return result;
}

代码来源于互联网,仅供参考!