fork(1) download
  1. # include <iostream>
  2. # include <string>
  3. # include <string.h>
  4. # include <stdio.h>
  5. # include <stdlib.h>
  6. # include <sstream>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int testnum, input_count, output_count ; // count是index值
  12. char input[256], output[256] ;
  13.  
  14.  
  15. void CleanChar256( char targer[256] ) ;
  16. void GetLine() ;
  17. void AnalysisLine( int & skip );
  18. void SetSymbol() ;
  19. void SetNumber() ;
  20. void SetString() ;
  21. void ProceedQuote( int fq, int sq ) ;
  22.  
  23. int main()
  24. {
  25. cout << "Welcome to OurScheme!" << endl;
  26. cin >> testnum ;
  27. int iseof = 0 ;
  28. while( strcmp( output, "(exit)" ) != 0 ) { // 結束條件
  29. GetLine() ; // 讀一行進來
  30. int skip = 0 ; // 對付";"用的
  31. AnalysisLine( skip ) ; // 分析剛剛讀的那一行
  32. if ( strcmp( output, "" ) != 0 && strcmp( output, "(exit)" ) != 0 )
  33. cout << "> " << output << endl ; // 輸出
  34. } // while
  35.  
  36. cout << "Thanks for using OurScheme!" << endl;
  37. return 0;
  38. }
  39.  
  40. void CleanChar256( char targer[256] ){
  41. for ( int i = 0 ; i < 256 ; i++ )
  42. targer[i] = '\0' ;
  43. } // CleanChar256
  44.  
  45. void GetLine() {
  46. CleanChar256( input ) ;
  47. CleanChar256( output ) ;
  48. input_count = 0, output_count = 0 ;
  49.  
  50. char ch ;
  51. bool stop = 0 ;
  52. if ( scanf( "%c", &ch ) == EOF ) cout << "QQ" ;
  53. while( ch != EOF && stop == 0 ) { // 一個一個字元慢慢讀 湊成一行
  54. if ( ch != '\n' ) {
  55. input[input_count] = ch ;
  56. input_count++ ;
  57. scanf( "%c", &ch ) ;
  58. } // if
  59. else if ( ch == '\n' ) {
  60. stop = 1 ;
  61. } // if
  62. } // while()
  63. } // GetLine()
  64.  
  65. void AnalysisLine( int & sk ) {
  66. for( int i = 0 ; i < input_count ; i++ ) {
  67. if ( input[i] == ';' ) i = input_count+1, sk = 1 ; // 如果是";" 暫且無視此行
  68. else if ( input[i] == '"' ) { // 碰到字串的情況
  69. SetString() ;
  70. i = input_count ;
  71. } // if
  72. else if ( ( input[i] <= '9' && input[i] >= '0' ) || input[i] == '+' || input[i] == '-' || input[i] == '.' ) { // 數字的情況
  73. bool out01 = 0, isdig = 0 ;
  74. while ( i < input_count && out01 == 0 ) {
  75. if ( input[i] >= '0' && input[i] <= '9' ) isdig++ ; // 看會不會整行沒數字
  76. // 先看整行有沒有數字不該有的東西,出現的話就是symbol
  77. if ( ( ( ( input[i] >= 'A' && input[i] <= 'Z' )
  78. || ( input[i] >= 'a' && input[i] <= 'z' ) )
  79. && ( input[i] != '+' && input[i] != '-' && input[i] != '.' ) )
  80. || ( isdig == 0 && i == input_count-1 ) ) {
  81. SetSymbol() ;
  82. out01 = 1 ;
  83. } // if
  84.  
  85. i++ ;
  86. } // while
  87.  
  88. if ( !out01 ) SetNumber() ;// 確定是數字 開始處理
  89. i = input_count +1 ;
  90. } // if
  91. else { // 雜七雜八的應該是symbol
  92. i = input_count +1 ;
  93. SetSymbol() ;
  94. } // else
  95. } // for
  96. } // AnalysisLine()
  97.  
  98. void SetSymbol() {
  99. for ( int index = 0 ; index < input_count ; index++ ) {
  100. output[output_count] = input[index] ;
  101. output_count++ ;
  102. } // for
  103.  
  104. if ( strcmp( output, "nil" ) == 0 || strcmp( output, "#f" ) == 0 || strcmp( output, "()" ) == 0 ) strcpy( output, "nil" ) ; // nil的情況
  105. else if ( strcmp( output, "t" ) == 0 || strcmp( output, "#t" ) == 0 ) strcpy( output, "#t" ) ; // t的情況
  106. } // SetSymbol()
  107.  
  108. void SetNumber() {
  109. bool issymbol = 0 ;
  110. for ( int i = 0 ; i < input_count && issymbol == 0 ; i++ ) // +-連續出現 就當symbol
  111. if ( ( input[i] == '+' && input[i+1] == '-' ) || ( input[i] == '-' && input[i+1] == '+' ) ) {
  112. SetSymbol() ;
  113. issymbol = 1 ;
  114. } // if
  115.  
  116. if ( !issymbol ) {
  117. int beginsite = -1, pointsite = -1, specialsite = -1 ;
  118. for ( int i = 0 ; i < input_count ; i++ ) { // 抓正負號和小數點的位置
  119. if ( input[i] == '+' || input[i] == '-' ) beginsite = i ;
  120. if ( input[i] == '.' ) pointsite = i ;
  121. } // for
  122.  
  123. if ( pointsite - 1 >= 0 ) {
  124. if ( input[pointsite-1] == '+' || input[pointsite-1] == '-' ) { // 看.和+-之間是不是沒東西 是的話補0
  125. input_count++ ;
  126. for ( int i = input_count-1 ; i > pointsite ; i-- )
  127. input[i] = input[i-1] ;
  128.  
  129. input[pointsite] = '0' ;
  130. pointsite++ ;
  131. } // if
  132. } // if
  133. else if ( pointsite == 0 ) { // 看.和+-之間是不是沒東西 是的話補0
  134. input_count++ ;
  135. for ( int i = input_count-1 ; i > pointsite ; i-- )
  136. input[i] = input[i-1] ;
  137.  
  138. input[pointsite] = '0' ;
  139. pointsite++ ;
  140. } // if
  141.  
  142. if ( pointsite != -1 ) {
  143. if ( input_count - 1 - pointsite == 2 ) input[input_count] = '0', input_count++ ; // 處理小數點(後面補0)
  144. else if ( input_count - 1 - pointsite == 1 ) {
  145. input[input_count] = '0' ;
  146. input[input_count+1] = '0' ;
  147. input_count += 2 ;
  148. }
  149. else if ( input_count - 1 - pointsite == 0 ) {
  150. input[input_count] = '0' ;
  151. input[input_count+1] = '0' ;
  152. input[input_count+2] = '0' ;
  153. input_count += 3 ;
  154. }
  155. } // if
  156.  
  157. if ( input[0] == '+' ) { // 正數把+弄掉 看到好煩
  158. for ( int i = 0 ; i < input_count ; i++ )
  159. input[i] = input[i+1] ;
  160.  
  161. input_count-- ; // 消掉+所以長度-1
  162. if ( pointsite != -1 ) pointsite-- ;
  163. } // if
  164.  
  165. double d_temp01 ; // 取小數後第三位
  166. if ( pointsite + 4 < input_count && pointsite != -1 ) {
  167. d_temp01 = atof( input ) ;
  168. if ( input[pointsite+4] >= '5' && input[pointsite+4] <= '9' ) {
  169. if ( d_temp01 > 0 ) d_temp01 += 0.001 ;
  170. else if ( d_temp01 < 0 ) d_temp01 -= 0.001 ;
  171. } // if
  172.  
  173. input_count = pointsite + 4 ;
  174. d_temp01 = (double)((int)(d_temp01*1000)*0.001) ;
  175. sprintf(input, "%f", d_temp01); // 轉回output
  176. } // if
  177.  
  178. for ( int i = 0 ; i < input_count ; i++ )
  179. output[i] = input[i] ;
  180. } // if
  181. } // SetNumber()
  182.  
  183. void SetString() {
  184. for ( int i = 1 ; i < input_count ; i++ ) { // 先抓整個字串
  185. if ( input[i] == '"' )
  186. if ( input[i-1] != '\\' ) input_count = i+1 ;
  187. } // for
  188.  
  189. int fq = -1, sq = -1 ; // first quote and second quote
  190. for ( int i = 0 ; i < input_count ; i++ ) {
  191. if ( fq == -1 || sq == -1 ) { // 抓看看兩個'
  192. if ( input[i] == '\'' && fq == -1 ) fq = i ;
  193. else if ( input[i] == '\'' && fq != -1 ) sq = i ;
  194. else if ( ( input[i] == '\\' && input[i+1] == 'n' && fq == -1 && sq == -1 ) ||
  195. ( input[i] == '\\' && input[i+1] == '\\' && fq == -1 && sq == -1 ) ||
  196. ( input[i] == '\\' && input[i+1] == '"' && fq == -1 && sq == -1 ) ) { // 有\n的情況(以及其他)
  197. if ( input[i+1] != 'n' ) input[i] = input[i+1] ;
  198. else if ( input[i+1] == 'n' ) input[i] = '\n' ;
  199. int j = i+1 ;
  200. while ( j < input_count ) {
  201. input[j] = input[j+1] ;
  202. j++ ;
  203. } // while
  204.  
  205. input[input_count] = '\0' ;
  206. input_count--, i-- ;
  207. } // if
  208. } // if
  209. else { // fq和sq都有值 開始處理兩個'之間的情況
  210. ProceedQuote( fq, sq ) ;
  211. i--, fq = -1, sq = -1 ; // Refresh
  212. } // else
  213. } // for
  214.  
  215. for ( int i = 0 ; i < input_count ; i++ ) {
  216. output[i] = input[i] ;
  217. } // for
  218. } // SetString()
  219.  
  220. void ProceedQuote( int fq, int sq ) {
  221. for ( int i = fq ; i < sq ; i++ ) {
  222. if ( ( input[i] == '\\' && input[i+1] == '"' ) ||
  223. ( input[i] == '\\' && input[i+1] == '\\' ) ) {
  224. input[i] = input[i+1] ;
  225. int j = i+1 ;
  226. while ( j < input_count ) {
  227. input[j] = input[j+1] ;
  228. j++ ;
  229. } // while
  230.  
  231. input[input_count] = '\0' ;
  232. input_count-- ;
  233. } // if
  234. } // for
  235. } // ProceedQuote
  236.  
Runtime error #stdin #stdout 0s 16064KB
stdin
2
v
Hello
a.b
-.
#t
t
#tt
nil
"OurScheme allows the use of '\\n', '\\t' and  '\\\"' in a string."
"Please enter YES\NO below this line >\n" 
NIL
()
#f
-.375
-.12499
#fa
exit
"Hi"
"  (1 . 2   . 3)  "
"   ( 1 2 3 . 4 5 6 . 7 . nil ) "
"What's up?"
"Use '\"' to start and close a string."
"You need to handle >>\\<<"      
"You also need to handle >>\"<<"
325
-325
3.25
-3.25
+3.25
there!
@$%--3
3.25a
325.
+325.
-325.
.375
+.3751
-.3751
-3.25
-325.
-.12500
"There is an ENTER HERE>>\nSee?!"
+.31499
+5
+-3.25
exit
stdout
Welcome to OurScheme!
> v
> Hello
> a.b
> -.
> #t
> #t
> #tt
> nil
> "OurScheme allows the use of '\n', '\t' and  '\"' in a string."
> "Please enter YES\NO below this line >
"
> NIL
> nil
> nil
> -0.375
> -0.125
> #fa
> exit
> "Hi"
> "  (1 . 2   . 3)  "
> "   ( 1 2 3 . 4 5 6 . 7 . nil ) "
> "What's up?"
> "Use '"' to start and close a string."
> "You need to handle >>\<<"
> "You also need to handle >>"<<"
> 325
> -325
> 3.250
> -3.250
> 3.250
> there!
> @$%--3
> 3.25a
> 325.000
> 325.000
> -325.000
> 0.375
> 0.375
> -0.375
> -3.250
> -325.000
> -0.125
> "There is an ENTER HERE>>
See?!"
> 0.315
> 5
> +-3.25
> exit