/*平成25年度春期応用情報技術者試験
午後問題問2 逆ポーランド表記法
written by ◆g3C4s2T.1c
*/
#include <stdio.h>
/*---関数プロトタイプ宣言---*/
char GetElement( int index );
int GetPriority( char element );
/*---グローバル変数---*/
char inputArray[] = {'1','+','2','*','3'};
int main(){
char result[100];
int elementCount = 5;
int resultCount = 0;
int stackCount = 0;
char stack[100];
stack[0] = NULL;
int i=1;
while(i <= elementCount ){
int elementPriority = GetPriority(GetElement(i));
char stackTop = stack[stackCount];
//演算要素をスタックからポップし、変換後配列の末尾に追加する。
while( elementPriority <= GetPriority(stackTop) && stackTop != '(' ){
resultCount++;
result[resultCount] = stackTop;
stackCount--;
stackTop = stack[stackCount];
}
//変換前配列を参照し、演算要素を処理する。
if( elementPriority == 1 ){
stackCount--;
}else{
stackCount++;
stack[stackCount] = GetElement(i);
}
i++;
}
//スタックに残った演算要素を、変換後配列の末尾に追加する。
while( stack[stackCount] != NULL ){
resultCount++;
result[resultCount] = stack[stackCount];
stackCount--;
}
for(i=1; i<=resultCount; ++i)
return 0;
}
char GetElement( int index ){
return inputArray[index-1];
}
int GetPriority( char element ){
if(element == NULL)
return 0;
switch( element ){
case '(': return 5;
case '*': return 3;
case '/': return 3;
case '+': return 2;
case '-': return 2;
case ')': return 1;
//case NULL: return 0;
default: return 4; //上記以外は数値と判断
}
}
LyrlubPmiJAyNeW5tOW6puaYpeacn+W/nOeUqOaDheWgseaKgOihk+iAheippumokwrljYjlvozllY/poYzllY8yIOmAhuODneODvOODqeODs+ODieihqOiomOazlQp3cml0dGVuIGJ5IOKXhmczQzRzMlQuMWMKKi8KCiNpbmNsdWRlIDxzdGRpby5oPgoKLyotLS3plqLmlbDjg5fjg63jg4jjgr/jgqTjg5flrqPoqIAtLS0qLwpjaGFyIEdldEVsZW1lbnQoIGludCBpbmRleCApOwppbnQgR2V0UHJpb3JpdHkoIGNoYXIgZWxlbWVudCApOwoKLyotLS3jgrDjg63jg7zjg5Djg6vlpInmlbAtLS0qLwpjaGFyI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