Understanding Stack Structure in c++

Asked 2 years ago, Updated 2 years ago, 75 views

When I tried to implement reverse Poland, after receiving input in while(scanf("%s",s)!=EOF), the conditional branch begins in if(s[0]=='+'). Why do I keep the index of s[0] and array 0 fixed?
I imagine that s[0] is followed by s[1], and then s[2], and eventually s[n] ends if s[n] is EOF. What's the difference?

source code

#include<bits/stdc++.h>
using namespace std;

int top, S[1000];

void push(intx){
    S[++top] = x;
}

int pop(){
    top--;
    return S[top+1];
}

int main() {
    inta,b;
    top = 0;
    chars [100];

    while(scanf("%s",s)!=EOF){
        if(s[0]=='+'){
            a = pop();
            b = pop();
            push(a+b);
        } else if(s[0]=='-'){
            b = pop();
            a = pop();
            push(a-b);
        } else if(s[0]=='*'){
            a = pop();
            b = pop();
            push(a*b);
        } else{
            push(atoi(s));
        }
    }

    printf("%d\n", pop());

    return 0;
}

Also,

int main(){
    inta,b;
    top = 0;
    chars;

    while(scanf("%c",s)!=EOF){
        if(s=='+'){
            a = pop();
            b = pop();
            push(a+b);
        } else if(s=='-'){
            b = pop();
            a = pop();
            push(a-b);
        } else if(s=='*'){
            a = pop();
            b = pop();
            push(a*b);
        } else{
            push(atoi(s));
        }
    }

    printf("%d\n", pop());

    return 0;
}

Also, if you change chars[100] to chars, while(scanf("%s",s)!=EOF) to while(scanf("%c",s)!=EOF) and if(s[0]=='+') to if(s=='+') as above, it doesn't work, but I don't know why.
The number you enter, such as 1+1 , is limited to one digit.

c++ algorithm stack

2022-09-30 20:20

1 Answers

The %s specifier for scanf is a blank character (half-width space or new line) separated by a string to the standard input.
(It is not typed in bulk into chars[100])

For example, if there is an input of "1112+",

The first s contains "11"
For the second s,
12
"+"

for the third s

Each contains

In the case of each symbol, there is no problem even if you only look at the first character for each input. I think you specified s[0].

By the way, isn't it probably an example from a book?
If you have something like an input example, it might be easy to understand if you check the operation while inserting a debugger or printf.
(The following is an example.)

int main(){
    inta,b;
    top = 0;
    chars [100];
    int cnt = 1;

    while(scanf("%s",s)!=EOF){
        printf("%dth time:%s\n", cnt,s);
        if(s[0]=='+'){
            a = pop();
            b = pop();
            push(a+b);
        } else if(s[0]=='-'){
            b = pop();
            a = pop();
            push(a-b);
        } else if(s[0]=='*'){
            a = pop();
            b = pop();
            push(a*b);
        } else{
            push(atoi(s));
        }
        ++cnt;
    }

    printf("%d\n", pop());

    return 0;
}

*Additional questions
There seems to be a problem with the following three points.
(I wrote the correction code at the end of the sentence, so please check it while comparing it.)

1. You did not specify a pointer for the second argument of scanf
scanf must be a pointer to the stored object.
We have changed from array type to element type (char[100]→char), so in this case, we need to modify it to take a pointer.

2. No pointer to string is specified for atoi
Atoi is a function that converts strings into int-type numbers, so you cannot convert characters

If you want the characters to be numeric, you must write
Interesting characters - '0'
(The language standard indirectly guarantees that this operation will work properly on any platform.)
Example:
chars='3'
int value = s - '0';

3. Enter new line characters and blank characters as numeric values
Unlike %s, they don't skip blank characters, so
When blank spaces or line breaks are entered, they are recognized as numeric values.

 while(scanf("%c",s)!=EOF) {←1. When blank is entered
        if(s=='+'){←2.+ not
            a = pop();
            b = pop();
            push(a+b);
        } else if(s=='-'){←3.-not even
            b = pop();
            a = pop();
            push(a-b);
        } else if(s=='*'){←4.* not even
            a = pop();
            b = pop();
            push(a*b);
        } else {←5. It's not a number, but it's entered as a number.
            push(atoi(s));
        }
    }

I corrected the above problem and rewritten it a little.

#include<bits/stdc++.h>
using namespace std;

int top, S[1000];

void push(intx){
    S[++top] = x;
}

int pop(){
    top--;
    return S[top+1];
}

int main() {
    inta,b;
    top = 0;
    chars;

    while(scanf("%c", &s)!=EOF) {//1.scanf second argument to pointer
        if(s=='+'){
            a = pop();
            b = pop();
            push(a+b);
        } else if(s=='-'){
            b = pop();
            a = pop();
            push(a-b);
        } else if(s=='*'){
            a = pop();
            b = pop();
            push(a*b);
        } else if(isspace(s)) {//3. Next without doing anything when new line or blank characters are in it
            /* do nothing*/
        } else{
            Corrected to use s-'0' as push(s-'0');//2.atoi is not available
        }
    }

    printf("%d\n", pop());

    return 0;
}

Check against the answers above and the original source code


2022-09-30 20:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.