如何在不使用字符串或push / pop(stack)的情况下用C为Postfix编写代码?

时间:2019-05-24 19:25:29

标签: c

到目前为止,我已经编写了这段代码,但没有通过运行prgram得到结果

即我扫描了两个数组的操作和操作数

我希望最后打印结果(我认为主要的问题是我想通过循环运行它的switch命令)

//only want to use + - * 

#include<stdio.h>
#include<conio.h>
#define size 100
# define size_1 99
int main()
{
    int array[size] = { 0 };//array of numbers
    char opera[size_1];// array for operations + - * 
    int  n, i,j;

    printf("Enter number of operands(2-100)");
    scanf_s("%d", &n);

    if (n < 2 || n>100) {//condition minimum numbers 2 max 100
        printf("Wrong number of operands");
    }
    else if (n >= 2 || n <= 100) {
        for (i = 0; i < n; i++) {//to put the numbers in array
            printf("Enter operand (%d):", i);
            scanf("%d", &array[i]);
        }
        n--;
        for (j = 0; j < n; j++) {
            printf("press operator (+ - *):");
            scanf("%s", &opera[j]);// to the operations
        }
    }
    n++;

    switch (opera[size_1])//this switch to calculate with the postfix
    {
    case '+':
        array[n - 1] = array[n] + array[n - 1];
        printf("%d", array[n - 1]);
        break;

    default:
        printf("unknown operation");
    }
    _getch();
}

enter image description here

0 个答案:

没有答案