Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <limits.h>
- #include <stdbool.h>
- #define N 10000
- int count = 0;
- int idx = 0; /* スタックが次に入れるところを指す */
- int stack[N];
- int push(){
- count++;
- stack[idx] = count;
- idx++;
- return stack[idx - 1];
- }
- bool stack_is_empty() {
- return (idx == 0) ? true : false;
- }
- int pop(){
- if(stack_is_empty() == true) return INT_MIN;
- int ret = stack[idx - 1];
- idx--;
- return ret;
- }
- int main(void)
- {
- char buff[N];
- int i;
- printf("括弧文字列を入力して下さい : ");
- fgets(buff, sizeof(buff), stdin);
- buff[strlen(buff) - 1] = '\0'; /* 改行コードを消す */
- for (i = 0;i < strlen(buff);i++){
- if(buff[i] == '['){
- printf("[");
- int push_val = push();
- printf("%d ", push_val);
- } else if(buff[i] == ']'){
- int pop_val = pop();
- if(pop_val == INT_MIN){
- printf("\n括弧が整合していません。");
- return -1;
- }
- printf("]");
- printf("%d ", pop_val);
- }
- }
- if(stack_is_empty() == false){
- printf("\n括弧が整合していません。");
- return -1;
- }
- printf("\n括弧は整合していました。");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment