美文网首页
四则运算

四则运算

作者: Co_zy | 来源:发表于2018-07-21 21:38 被阅读0次

B. 四则运算
运行时间<1000 ms 单次测试运行时间限制: 1000 ms 内存限制: 64 MB
总提交: 2次 通过: 1次

题目描述

给定一个四则运算表达式,包括 + - * / ( ),计算结果

程序输入说明

有N行输入,每行有一个四则运算表达式,不超过10个数字ai( 1 < = i < = 10 )。
为了简便定 0 < = ai < = 9

程序输出说明

有N行输出,每行一个运算结果

程序输入样例

可见格式 带空格和换行符的格式 带空格和换行符的格式说明

1+1
5/2

程序输出样例
Original Transformed 带空格和换行符的格式说明

2
2.5

1

#include <stdio.h>
#include <string.h>
#define maxsize 100
int f(char ch, char sh)
{
    if(sh=='(') return 1;
    if( (ch=='*' || ch=='/' || ch=='%')&&( sh=='+' || sh=='-')  )
        return 1;
    else
        return -1;
}
int Op(int a,char op,int b)
{
    if(op == '+')
        return a+b;
    if(op == '-')
        return a-b;
    if(op == '*')
        return a*b;
    if(op == '/')
        return a/(b*1.0);
}
int com(char exp[])
{
    int i,a,b,c;
    int stack[maxsize];
    int top = -1;
    char op;
    for(i = 0;exp[i];i++)
    {
        if(exp[i]>='0' && exp[i]<='9')
            stack[++top] = exp[i] - '0';
        else
        {
            op = exp[i];
            b = stack[top--];
            a = stack[top--];
            c = Op(a,op,b);
            stack[++top] = c;
        }
    }
    return stack[top];
}
int main()
{
    int i = 0 , n , top = -1 ;
    char exp[1000] , str[1000] , ch ;
    while(scanf("%c", &ch) && ch != '#')
    {
        if(ch >= '0' && ch <='9')
            exp[i++] = ch ;
        else if(ch=='(')
            str[++top] = ch ;
        else if(ch==')')
        {
            while(top!=-1)
            {
                exp[i++] = str[top];
                top--;
                if(str[top]=='(')
                {
                    top--;
                    break;
                }
            }
        }
        else
        {
            if(top == -1 || f(ch,str[top]) > 0 )
                str[++top] = ch ;
            else
            {
                while(top >=0 && f(ch,str[top]) < 0  )
                {
                    exp[i++] = str[top--];
                }
                str[++top] = ch ;
            }
        }
    }
    while(top != -1)
    {
        exp[i++] = str[top--];
    }
    exp[i] = '\0';
    printf("%c\n",exp[i]);
    printf("%s\n", exp);
    printf("%d\n",com(exp));
    return 0;
}

2

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 100
int f(char ch, char sh)
{
    if(sh=='(') return 1;
    if((ch=='*' || ch=='/' || ch=='%')&&(sh=='+' || sh=='-'))
        return 1;
    else
        return -1;
}
double Op(double a,char op,double b)
{
    if(op == '+')
        return a+b;
    if(op == '-')
        return a-b;
    if(op == '*')
        return a*b;
    if(op == '/')
        //printf("%.1f",a/(b*1.0));
        return a/b;
}
double com(char exp[])
{
    int i;
    double a,b,c;
    double stack[maxsize];
    int top = -1;
    char op;
    for(i = 0; exp[i]; i++)
    {
        if(exp[i]>='0' && exp[i]<='9')
        {
            stack[++top] = (double)(exp[i] - '0');
//              stack[++top] = atof(exp[i]);
            //printf("h: %f\n",(double)(exp[i] - '0'));
        }

        else
        {
            op = exp[i];
            b = stack[top--];
            a = stack[top--];
            //printf("%d %d \n",a,b);
            c = Op(a,op,b);
            stack[++top] = c;
            //printf("hello: %f\n",c);
        }
    }
    return stack[top];
}
int main()
{
    int i = 0, n, top = -1 ;
    char exp[1000], str[1000], ch ;
    //exp存放要转成的后缀表达式 str存放字符乘除括号
    while(scanf("%c", &ch) && ch!='\n')
    {
        if(ch >= '0' && ch <='9')
            exp[i++] = ch ;
        else if(ch=='(')
            str[++top] = ch ;
        else if(ch==')')
        {
            while(top!=-1)
            {
                exp[i++] = str[top];
                top--;
                if(str[top]=='(')
                {
                    top--;
                    break;
                }
            }
        }
        else
        {
            if(top == -1 || f(ch,str[top]) > 0)
                str[++top] = ch ;
            else
            {
                while(top >=0 && f(ch,str[top]) < 0)
                {
                    exp[i++] = str[top--];
                }
                str[++top] = ch ;
            }
        }
    }

    while(top != -1)
    {
        exp[i++] = str[top--];
    }
    exp[i] = '\0';
    //printf("%.2f\n",Op(5,'/',2));
    //printf("%c\n",exp[i]);
    //printf("%s\n", exp);
    printf("%.2f\n",com(exp));
    return 0;
}
//5-(6+7)*8+9/4#    -96.75
// 5 6 7 + 8 * - 9 4 / +

3 输入多行

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 100
int f(char ch, char sh)
{
    if(sh=='(') return 1;
    if((ch=='*' || ch=='/' || ch=='%')&&(sh=='+' || sh=='-'))
        return 1;
    else
        return -1;
}
double Op(double a,char op,double b)
{
    if(op == '+')
        return a+b;
    if(op == '-')
        return a-b;
    if(op == '*')
        return a*b;
    if(op == '/')
        //printf("%.1f",a/(b*1.0));
        return a/b;
}
double com(char exp[])
{
    int i;
    double a,b,c;
    double stack[maxsize];
    int top = -1;
    char op;
    for(i = 0; exp[i]; i++)
    {
        if(exp[i]>='0' && exp[i]<='9')
        {
            stack[++top] = (double)(exp[i] - '0');
//              stack[++top] = atof(exp[i]);
            //printf("h: %f\n",(double)(exp[i] - '0'));
        }
        else
        {
            op = exp[i];
            b = stack[top--];
            a = stack[top--];
            //printf("%d %d \n",a,b);
            c = Op(a,op,b);
            stack[++top] = c;
            //printf("hello: %f\n",c);
        }
    }
    return stack[top];
}
int main()
{
    int i = 0, n, top = -1 ;
    int k;
    char exp[1000], str[1000] ;
    char ch[1000];
    //exp存放要转成的后缀表达式 str存放字符乘除括号
    while(~scanf("%s", ch))
    {
        for(k=0; ch[k]; k++)
        {
            if(ch[k] >= '0' && ch[k] <='9')
                exp[i++] = ch[k] ;
            else if(ch[k]=='(')
                str[++top] = ch[k] ;
            else if(ch[k]==')')
            {
                while(top!=-1)
                {
                    exp[i++] = str[top];
                    top--;
                    if(str[top]=='(')
                    {
                        top--;
                        break;
                    }
                }
            }
            else
            {
                if(top == -1 || f(ch[k],str[top]) > 0)
                    str[++top] = ch[k] ;
                else
                {
                    while(top >=0 && f(ch[k],str[top]) < 0)
                    {
                        exp[i++] = str[top--];
                    }
                    str[++top] = ch[k] ;
                }
            }
        }
        while(top != -1)
        {
            exp[i++] = str[top--];
        }
        exp[i] = '\0';
        //printf("%.2f\n",Op(5,'/',2));
        //printf("%c\n",exp[i]);
        //printf("%s\n", exp);
        printf("%.1f\n",com(exp));
    }
    return 0;
}
//5-(6+7)*8+9/4#    -96.75
// 5 6 7 + 8 * - 9 4 / +

最终版,但未通过

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define maxsize 100
int f(char ch, char sh)
{
    if(sh=='(') return 1;
    if((ch=='*' || ch=='/' || ch=='%')&&(sh=='+' || sh=='-'))
        return 1;
    else
        return -1;
}
double Op(double a,char op,double b)
{
    if(op == '+')
        return a+b;
    if(op == '-')
        return a-b;
    if(op == '*')
        return a*b;
    if(op == '/')
        //printf("%.1f",a/(b*1.0));
        return a/b;
}
double com(char exp[])
{
    int i;
    double a,b,c;
    double stack[maxsize];
    int top = -1;
    char op;
    for(i = 0; exp[i]; i++)
    {
        if(exp[i]>='0' && exp[i]<='9')
        {
            stack[++top] = (double)(exp[i] - '0');
//              stack[++top] = atof(exp[i]);
            //printf("h: %f\n",(double)(exp[i] - '0'));
        }
        else
        {
            op = exp[i];
            b = stack[top--];
            a = stack[top--];
            //printf("%d %d \n",a,b);
            c = Op(a,op,b);
            stack[++top] = c;
            //printf("hello: %f\n",c);
        }
    }
    return stack[top];
}
int main()
{
    int i = 0, n, top = -1 ;
    int k;
    char exp[1000], str[1000] ;
    char ch[1000];
    //exp存放要转成的后缀表达式 str存放字符乘除括号
    while(~scanf("%s", ch))
    {
        for(k=0; ch[k]; k++)
        {
            if(ch[k] >= '0' && ch[k] <='9')
                exp[i++] = ch[k] ;
            else if(ch[k]=='(')
                str[++top] = ch[k] ;
            else if(ch[k]==')')
            {
                while(top!=-1)
                {
                    exp[i++] = str[top];
                    top--;
                    if(str[top]=='(')
                    {
                        top--;
                        break;
                    }
                }
            }
            else
            {
                if(top == -1 || f(ch[k],str[top]) > 0)
                    str[++top] = ch[k] ;
                else
                {
                    while(top >=0 && f(ch[k],str[top]) < 0)
                    {
                        exp[i++] = str[top--];
                    }
                    str[++top] = ch[k] ;
                }
            }
        }
        while(top != -1)
        {
            exp[i++] = str[top--];
        }
        exp[i] = '\0';
        //printf("%.2f\n",Op(5,'/',2));
        //printf("%c\n",exp[i]);
        //printf("%s\n", exp);
        if((com(exp)-floor(com(exp))) == 0)
            printf("%d\n",(int)com(exp));
        else
            printf("%.1f\n",com(exp));
    }
    return 0;
}
//5-(6+7)*8+9/4#    -96.75
// 5 6 7 + 8 * - 9 4 / +

AC

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define maxsize 100
int f(char ch, char sh)
{
    if(sh=='(') return 1;
    if((ch=='*' || ch=='/' || ch=='%')&&(sh=='+' || sh=='-'))
        return 1;
    else
        return -1;
}
double Op(double a,char op,double b)
{
    if(op == '+')
        return a+b;
    if(op == '-')
        return a-b;
    if(op == '*')
        return a*b;
    if(op == '/')
        //printf("%.1f",a/(b*1.0));
        return a/b;
}
double com(char exp[])
{
    int i;
    double a,b,c;
    double stack[maxsize];
    int top = -1;
    char op;
    for(i = 0; exp[i]; i++)
    {
        if(exp[i]>='0' && exp[i]<='9')
        {
            stack[++top] = (double)(exp[i] - '0');
//              stack[++top] = atof(exp[i]);
            //printf("h: %f\n",(double)(exp[i] - '0'));
        }
        else
        {
            op = exp[i];
            b = stack[top--];
            a = stack[top--];
            //printf("%d %d \n",a,b);
            c = Op(a,op,b);
            stack[++top] = c;
            //printf("hello: %f\n",c);
        }
    }
    return stack[top];
}
int main()
{
    int i = 0, n, top = -1 ;
    int k;
    char exp[1000], str[1000] ;
    char ch[1000];
    //exp存放要转成的后缀表达式 str存放字符乘除括号
    while(~scanf("%s", ch))
    {
        for(k=0; ch[k]; k++)
        {
            if(ch[k] >= '0' && ch[k] <='9')
                exp[i++] = ch[k] ;
            else if(ch[k]=='(')
                str[++top] = ch[k] ;
            else if(ch[k]==')')
            {
                while(top!=-1)
                {
                    exp[i++] = str[top];
                    top--;
                    if(str[top]=='(')
                    {
                        top--;
                        break;
                    }
                }
            }
            else
            {
                if(top == -1 || f(ch[k],str[top]) > 0)
                    str[++top] = ch[k] ;
                else
                {
                    while(top >=0 && f(ch[k],str[top]) < 0)
                    {
                        exp[i++] = str[top--];
                    }
                    str[++top] = ch[k] ;
                }
            }
        }
        while(top != -1)
        {
            exp[i++] = str[top--];
        }
        exp[i] = '\0';
        //printf("%.2f\n",Op(5,'/',2));
        //printf("%c\n",exp[i]);
        //printf("%s\n", exp);
        printf("%g\n",com(exp));
    }
    return 0;
}
//5-(6+7)*8+9/4#    -96.75
// 5 6 7 + 8 * - 9 4 / +

相关文章

  • 1.1 整数运算

    1. 四则运算 Java的整数运算遵循四则运算规则,可以使用任意嵌套的小括号。四则运算规则和初等数学一致。例如: ...

  • 树的应用举例——四则计算

    计算四则运算:

  • 任意四则运算

    任意四则运算

  • 估算(四)

    今天聊一聊四则运算在估算中的应用。 常用的四则运算规律,是小学计算的基础。所谓的四则运算规律应用于估算,实际上还包...

  • shell运算符

    四则运算简单四则运算:awk、expr 注意 val=.. 中间不能有空格 $a + $b 中间有空格,否则会使...

  • 算法(3)简单四则运算

    1.0 问题描述 实现10以内四则运算(只包含数字,+-*/和小括号) 2.0 问题分析 四则运算使用“后缀表达式...

  • 数学运算

    Python 提供的基本数据类型 int、float 可以做整数和浮点的四则运算以及乘方等运算。 但是,四则运算不...

  • Python 简单实现数学四则运算

    一、题目描述 (1)能自动生成小学四则运算题目; (2)能支持真分数的四则运算; 二、实现环境 PyCharm、P...

  • 41-python中数学运算

    Python 提供的基本数据类型int、float可以做整数和浮点的四则运算以及乘方等运算。 但是,四则运算不局限...

  • 19-数学运算符

        在Java中数学运算都提供了标准的支持。包括四则运算都是支持的。 范例:实现一个简单的四则运算     在...

网友评论

      本文标题:四则运算

      本文链接:https://www.haomeiwen.com/subject/fqskmftx.html