UVA Solution 327 - Evaluating Simple C Expressions - Solution in C, C++ | Volume 3 - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Friday, May 5, 2017

UVA Solution 327 - Evaluating Simple C Expressions - Solution in C, C++ | Volume 3

UVA Solution 327 - Evaluating Simple C Expressions - Solution in C, C++ | Volume 3


UVA Online Judge Solution 327 - Evaluating Simple C Expressions | Volume 3
UVA Problem Link -

Problem Name: 327 - Evaluating Simple C Expressions
Problem Number : UVA - 327 - Evaluating Simple C Expressions
Online Judge : UVA Online Judge Solution
Volume: 3
Solution Language : C, C plus plus

UVA Solution 327 - Evaluating Simple C Expressions - Solution in C, C++ | Volume 3

UVA Solution 327 - Evaluating Simple C Expressions Code in C, CPP:


#include <stdio.h>

int main() {
    char s[1024];
    int i, j;
    while(gets(s)) {
        printf("Expression: %s\n", s);
        int val[26];
        int pos_val[26] = {}, used[26] = {};
        int pre_val = -1;
        for(i = 0; i < 26; i++)
            val[i] = i+1;
        for(i = 0, j = 0; s[i]; i++)
            if(s[i] != ' ')
                s[j++] = s[i];
        s[j] = s[j+1] = s[j+2] = '\0';
        for(i = 0; s[i]; i++) {
            if(s[i] >= 'a' && s[i] <= 'z') { // [a-z]
                used[s[i]-'a'] = 1;
                if(s[i+1] == '+' && s[i+2] == '+') { // i++
                    pos_val[s[i]-'a']++;
                    s[i+1] = s[i+2] = ' ', i += 2;
                } else if(s[i+1] == '-' && s[i+2] == '-') { // i--
                    pos_val[s[i]-'a']--;
                    s[i+1] = s[i+2] = ' ', i += 2;
                } else if(i-2 >= 0 && s[i-1] == '+' && s[i-2] == '+') { // ++i
                    val[s[i]-'a']++;
                    s[i-1] = s[i-2] = ' ';
                } else if(i-2 >= 0 && s[i-1] == '-' && s[i-2] == '-') { // --i
                    val[s[i]-'a']--;
                    s[i-1] = s[i-2] = ' ';
                }
            }
        }
        int plus = 1, value = 0;
        for(i = 0; s[i]; i++) {
            if(plus && s[i] >= 'a' && s[i] <= 'z')
                value += val[s[i]-'a'];
            if(!plus && s[i] >= 'a' && s[i] <= 'z')
                value -= val[s[i]-'a'];
            if(s[i] == '+') plus = 1;
            if(s[i] == '-') plus = 0;
        }
        printf("    value = %d\n", value);
        for(i = 0; i < 26; i++) {
            if(used[i]) {
                printf("    %c = %d\n", i+'a', val[i]+pos_val[i]);
            }
        }
    }
    return 0;
}

No comments:

Post a Comment