zoukankan      html  css  js  c++  java
  • akoj-1162-计算表达式

    计算表达式

    Time Limit:1000MS  Memory Limit:65536K
    Total Submit:14 Accepted:7

    Description

    对于一个不存在括号的表达式进行计算

    Input

    存在多种数据,每组数据一行,表达式不存在空格

    Output

    输出结果

    Sample Input

    6/2+3+3*4

    Sample Output

    18

    Source

    [Submit]   [Go Back]   [Status]   [Discuss]

    #include <stdio.h>
    #include <string.h>
    
    /*此题关键对数的处理,遇到加减就增加个变量储存 ,
     *遇到‘-’还需要把这个数转换为其相反数储存 
     */
    
    int main()
    {
    	char temp;
    	int x, a[200], i, sum = 0;
    	memset(a, 0, sizeof(a));
    	while (~scanf("%d", &x))
    	{
    		sum = 0;
    		i = 0;
    		a[i] = x;
    		while ( ~scanf("%c", &temp) && temp != '
    ' )
    		{
    			scanf("%d", &x);
    			switch(temp)
    			{
    				case '*': a[i] *= x; break;//遇到乘除就当成一个数处理 
    				case '/': a[i] /= x; break;
    				case '+': a[++i] = x; break; 
    				case '-': a[++i] = -x; break; 
    			}
    		}
    		for ( i; i>=0; i--) {
    			sum += a[i];
    		}
    		printf("%d
    ", sum);
        }
    }


  • 相关阅读:
    Nginx配置文件详解
    Mycat概述
    日志切割之Logrotate
    js数组(二)
    js数组(一)
    sass颜色
    scss
    HTML5新属性
    HTML5新元素
    Bootstrap
  • 原文地址:https://www.cnblogs.com/fayne/p/7224820.html
Copyright © 2011-2022 走看看