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