下記のc++のコードをxcodeで実行したところ、入力を受け付けた後、何も反応を返しません。
しかし、
while(scanf("%s", s)
のところにブレイクポイントを置くと正常に結果を出力しました。
プログラムの内容はデータ構造のスタックを再現したものです。
期待される動作は逆ポーランド記法の数式を入力した後に、結果を返します。
下記のサイトでは正常の動きました。
AOJ競技プログラミングコンテスト
このことからxcodeに何か不具合があるかと思うのですが、自分では解決できずにいます。
c++
1 2#include <stdio.h> 3#include <stdlib.h> 4#include <string.h> 5 6int top, S[1000]; 7 8void push(int x){ 9 S[++top] = x; 10} 11 12int pop(){ 13 top--; 14 return S[top+1]; 15} 16 17int main(){ 18 int a, b; 19 top = 0; 20 char s[100]; 21 while(scanf("%s", s) != EOF){ 22 if(s[0] == '+'){ 23 a = pop(); 24 b = pop(); 25 push(a+b); 26 }else if(s[0] == '-'){ 27 b = pop(); 28 a = pop(); 29 push(a-b); 30 } else if(s[0] == '*'){ 31 a = pop(); 32 b = pop(); 33 push(a * b); 34 } else{ 35 push(atoi(s)); 36 } 37 } 38 printf("%d¥n", pop()); 39 return 0; 40} 41
環境
MacBook Pro (Retina, 15-inch, Mid 2015)
OS High Sierra
xcode 9.3
回答2件
あなたの回答
tips
プレビュー