In this problem、we will define a simple interpreter、and you are asked to write
a program to implement your algorithm.
The basic parsing rules of the simple interpreter are defined as follows:
1. <digit> ::= 0 1 2 3 4 5 6 7 8 9
2. <int> ::= <digit> <int><digit>
3. <op> ::= + -
4. <stm> ::= SUM = SUM <op> <int>; SUM = SUM <op> X;
5. support the function to interpret the following program paragraph.
BEGIN
X= <int>;
SUM=0;
While(X != 0){
<stm>
X=X-1;
}
END
Your program can input the program paragraph accepted by above definitions、and
output the value of SUM.
Input Format:
Input any program paragraph.
Output Format:
Report the value of SUM or the undefined messages.
Sample Input:
(1)
BEGIN
X= 10;
SUM=0;
While(X != 0){
SUM=SUM+X;
X=X-1;
}
END
(2)
BEGIN
X= 10;
SUM=0;
While(X != 0){
SUM=X+1;
X=X-1;
}
END
(3)
BEGIN
X= 10;
SUM=0;
While(X != 0){
SUM=SUM+X;
X=X+1;
}
END
Sample Output:
(1) SUM=55
(2) <undefined>
(3) <undefined>
困難度:**
時間複雜度:O(n)
程式語言:C++
預估時間:4 小時
解題原理:
1. 以字串讀入所有內容
2. 進行正規化去除空白、換行等字元
3. 分解以分號為基準的單元
4. 判斷何時進行 While 迴圈內的運算
沒有留言:
張貼留言