列表

详情


BL3. 简单表达式计算

描述

给定一个合法的表达式字符串,其中只包含非负整数、加法、减法以及乘法符号(不会有括号),例如7+3*4*5+2+4-3-1,请写程序计算该表达式的结果并输出;

输入描述

输入有多行,每行是一个表达式,输入以END作为结束

输出描述

每行表达式的计算结果

示例1

输入:

7+3*4*5+2+4-3-1
2-3*1
END

输出:

69
-1

原站题解

C++ 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2020-07-07

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    while(cin>>s)
    {
        if(s=="END") break;
        int n=s.size();
        int ans=0;
        int cur=0;
        int p=1;
        int i=n-1;
        while(i>=0)
        {
            int j=i;
            while((s[j]!='*')&&(s[j]!='+')&&(s[j]!='-')){
                j--;
                if(j<0) break;
            }
            cur=stoi(s.substr(j+1,i-j));
            if(s[j]=='+')
            {
                ans+=cur*p;
                p=1;
            }
            if(s[j]=='-')
            {
                ans-=cur*p;
                p=1;
            }
            if(s[j]=='*')
            {
                p*=cur;
            }
            i=j-1;
        }
        //cur=stoi(s.substr(0,i+1));
        ans+=cur*p;
        cout<<ans<<endl;
    }
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 372KB, 提交时间: 2020-08-27

#include <stdio.h>
int main()
{
	int b = 0;
	int sum = 0, mul = 1;
	char s;
	while (1)
	{
		scanf("%c", &s);
		if (s == 69)  //以E开头
			break;
		while (s >= 48 && s < 58)
		{
			b = b * 10 + s - 48;
			scanf("%c", &s);
		}
		if (s == 43)  // +
		{
			sum += (mul * b);
			mul = 1;
		}
		else if (s == 45)  // -
		{
			sum += (mul * b);
			mul = -1;
		}
		else if (s == 42)  // *
		{
			mul *= b;
		}
		if (s == 13 || s == 10)  //换行符
		{
			printf("%d\n", sum + (mul * b));
			sum = 0;
			mul = 1;
		}
        b = 0;
	}
	return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-05-20

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
    int i, len, top_s, top_n, cnt , res;
    char s[10000];
    char ch[12];                                   //临时存储
    char sign_stack[1000];
    int num_stack[2000];
    while (scanf("%s", s) != EOF)
    {
        len = strlen(s);
        if (!strcmp(s, "END"))
            break;
        for (i = 0, cnt = 0, res = 0, top_s = -1, top_n = -1; i<len; i++)
        {
            if (isdigit(s[i]))
            {
                int l=0;
                top_n++;
                for (l = 0; isdigit(s[i]); i++,l++)
                    ch[l]=s[i];
                ch[l] = '\0';
                num_stack[top_n] = atoi(ch);       //数据入栈
                i--;
    
                //判断符号栈顶元素是否是表达式中优先级最高的元素
                if(top_s<0)
                    continue;
                if (sign_stack[top_s] == '*')
                {
                    num_stack[top_n - 1] = num_stack[top_n] * num_stack[top_n - 1];
                    top_n--;
                    top_s--;
                }
            }
            else
            {
                if(sign_stack[top_s] == ')')
                {
                    //先处理,在把当前符号放入
                    top_s--;
                    cnt = 0;
                    while(sign_stack[top_s] !='(' )
                    {
                        if(sign_stack[top_s] == '-')
                            cnt = cnt - num_stack[top_n];
                        else
                            cnt = cnt + num_stack[top_n];
                        top_n--;
                        top_s--;
                    }
                    num_stack[top_n]  += cnt;
                    sign_stack[top_s] = s[i];
                }
                else
                    sign_stack[++top_s] = s[i];        //符号入栈
            }
        }
        while (top_s >= 0)
        {
            if (sign_stack[top_s] == '-')
                res = res - num_stack[top_n];
            else
                res = res + num_stack[top_n];
            top_s--;
            top_n--;
        }
        printf("%d\n", num_stack[0]+res);
    }
    
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-05-14

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
    int i, len, top_s, top_n, cnt , res;
    char s[10000];
    char ch[12];                                   //临时存储
    char sign_stack[1000];
    int num_stack[2000];
    while (scanf("%s", s) != EOF)
    {
        len = strlen(s);
        if (!strcmp(s, "END"))
            break;
        for (i = 0, cnt = 0, res = 0, top_s = -1, top_n = -1; i<len; i++)
        {
            if (isdigit(s[i]))
            {
                int l=0;
                top_n++;
                for (l = 0; isdigit(s[i]); i++,l++)
                    ch[l]=s[i];
                ch[l] = '\0';
                num_stack[top_n] = atoi(ch);       //数据入栈
                i--;
   
                //判断符号栈顶元素是否是表达式中优先级最高的元素
                if(top_s<0)
                    continue;
                if (sign_stack[top_s] == '*')
                {
                    num_stack[top_n - 1] = num_stack[top_n] * num_stack[top_n - 1];
                    top_n--;
                    top_s--;
                }
            }
            else
            {
                if(sign_stack[top_s] == ')')
                {
                    //先处理,在把当前符号放入
                    top_s--;
                    cnt = 0;
                    while(sign_stack[top_s] !='(' )
                    {
                        if(sign_stack[top_s] == '-')
                            cnt = cnt - num_stack[top_n];
                        else
                            cnt = cnt + num_stack[top_n];
                        top_n--;
                        top_s--;
                    }
                    num_stack[top_n]  += cnt;
                    sign_stack[top_s] = s[i];
                }
                else
                    sign_stack[++top_s] = s[i];        //符号入栈
            }
        }
        while (top_s >= 0)
        {
            if (sign_stack[top_s] == '-')
                res = res - num_stack[top_n];
            else
                res = res + num_stack[top_n];
            top_s--;
            top_n--;
        }
        printf("%d\n", num_stack[0]+res);
    }
   
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-05-04

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
    int i, len, top_s, top_n, cnt , res;
    char s[10000];
    char ch[12];                                   //临时存储
    char sign_stack[1000];
    int num_stack[2000];
    while (scanf("%s", s) != EOF)
    {
        len = strlen(s);
        if (!strcmp(s, "END"))
            break;
        for (i = 0, cnt = 0, res = 0, top_s = -1, top_n = -1; i<len; i++)
        {
            if (isdigit(s[i]))
            {
                int l=0;
                top_n++;
                for (l = 0; isdigit(s[i]); i++,l++)
                    ch[l]=s[i];
                ch[l] = '\0';
                num_stack[top_n] = atoi(ch);       //数据入栈
                i--;
  
                //判断符号栈顶元素是否是表达式中优先级最高的元素
                if(top_s<0)
                    continue;
                if (sign_stack[top_s] == '*')
                {
                    num_stack[top_n - 1] = num_stack[top_n] * num_stack[top_n - 1];
                    top_n--;
                    top_s--;
                }
            }
            else
            {
                if(sign_stack[top_s] == ')')
                {
                    //先处理,在把当前符号放入
                    top_s--;
                    cnt = 0;
                    while(sign_stack[top_s] !='(' )
                    {
                        if(sign_stack[top_s] == '-')
                            cnt = cnt - num_stack[top_n];
                        else
                            cnt = cnt + num_stack[top_n];
                        top_n--;
                        top_s--;
                    }
                    num_stack[top_n]  += cnt;
                    sign_stack[top_s] = s[i];
                }
                else
                    sign_stack[++top_s] = s[i];        //符号入栈
            }
        }
        while (top_s >= 0)
        {
            if (sign_stack[top_s] == '-')
                res = res - num_stack[top_n];
            else
                res = res + num_stack[top_n];
            top_s--;
            top_n--;
        }
        printf("%d\n", num_stack[0]+res);
    }
  
    return 0;
}

上一题