# include <iostream>
# include <string>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <sstream>
using namespace std;
int testnum, input_count, output_count ; // count是index值
char input[256], output[256] ;
void CleanChar256( char targer[256] ) ;
void GetLine() ;
void AnalysisLine( int & skip );
void SetSymbol() ;
void SetNumber() ;
void SetString() ;
void ProceedQuote( int fq, int sq ) ;
int main()
{
cout << "Welcome to OurScheme!" << endl;
cin >> testnum ;
int iseof = 0 ;
while( strcmp( output, "(exit)" ) != 0 ) { // 結束條件
GetLine() ; // 讀一行進來
int skip = 0 ; // 對付";"用的
AnalysisLine( skip ) ; // 分析剛剛讀的那一行
if ( strcmp( output, "" ) != 0 && strcmp( output, "(exit)" ) != 0 )
cout << "> " << output << endl ; // 輸出
} // while
cout << "Thanks for using OurScheme!" << endl;
return 0;
}
void CleanChar256( char targer[256] ){
for ( int i = 0 ; i < 256 ; i++ )
targer[i] = '\0' ;
} // CleanChar256
void GetLine() {
CleanChar256( input ) ;
CleanChar256( output ) ;
input_count = 0, output_count = 0 ;
char ch ;
bool stop = 0 ;
if ( scanf( "%c", &ch ) == EOF ) cout << "QQ" ;
while( ch != EOF && stop == 0 ) { // 一個一個字元慢慢讀 湊成一行
if ( ch != '\n' ) {
input[input_count] = ch ;
input_count++ ;
scanf( "%c", &ch ) ;
} // if
else if ( ch == '\n' ) {
stop = 1 ;
} // if
} // while()
} // GetLine()
void AnalysisLine( int & sk ) {
for( int i = 0 ; i < input_count ; i++ ) {
if ( input[i] == ';' ) i = input_count+1, sk = 1 ; // 如果是";" 暫且無視此行
else if ( input[i] == '"' ) { // 碰到字串的情況
SetString() ;
i = input_count ;
} // if
else if ( ( input[i] <= '9' && input[i] >= '0' ) || input[i] == '+' || input[i] == '-' || input[i] == '.' ) { // 數字的情況
bool out01 = 0, isdig = 0 ;
while ( i < input_count && out01 == 0 ) {
if ( input[i] >= '0' && input[i] <= '9' ) isdig++ ; // 看會不會整行沒數字
// 先看整行有沒有數字不該有的東西,出現的話就是symbol
if ( ( ( ( input[i] >= 'A' && input[i] <= 'Z' )
|| ( input[i] >= 'a' && input[i] <= 'z' ) )
&& ( input[i] != '+' && input[i] != '-' && input[i] != '.' ) )
|| ( isdig == 0 && i == input_count-1 ) ) {
SetSymbol() ;
out01 = 1 ;
} // if
i++ ;
} // while
if ( !out01 ) SetNumber() ;// 確定是數字 開始處理
i = input_count +1 ;
} // if
else { // 雜七雜八的應該是symbol
i = input_count +1 ;
SetSymbol() ;
} // else
} // for
} // AnalysisLine()
void SetSymbol() {
for ( int index = 0 ; index < input_count ; index++ ) {
output[output_count] = input[index] ;
output_count++ ;
} // for
if ( strcmp( output, "nil" ) == 0 || strcmp( output, "#f" ) == 0 || strcmp( output, "()" ) == 0 ) strcpy( output, "nil" ) ; // nil的情況
else if ( strcmp( output, "t" ) == 0 || strcmp( output, "#t" ) == 0 ) strcpy( output, "#t" ) ; // t的情況
} // SetSymbol()
void SetNumber() {
bool issymbol = 0 ;
for ( int i = 0 ; i < input_count && issymbol == 0 ; i++ ) // +-連續出現 就當symbol
if ( ( input[i] == '+' && input[i+1] == '-' ) || ( input[i] == '-' && input[i+1] == '+' ) ) {
SetSymbol() ;
issymbol = 1 ;
} // if
if ( !issymbol ) {
int beginsite = -1, pointsite = -1, specialsite = -1 ;
for ( int i = 0 ; i < input_count ; i++ ) { // 抓正負號和小數點的位置
if ( input[i] == '+' || input[i] == '-' ) beginsite = i ;
if ( input[i] == '.' ) pointsite = i ;
} // for
if ( pointsite - 1 >= 0 ) {
if ( input[pointsite-1] == '+' || input[pointsite-1] == '-' ) { // 看.和+-之間是不是沒東西 是的話補0
input_count++ ;
for ( int i = input_count-1 ; i > pointsite ; i-- )
input[i] = input[i-1] ;
input[pointsite] = '0' ;
pointsite++ ;
} // if
} // if
else if ( pointsite == 0 ) { // 看.和+-之間是不是沒東西 是的話補0
input_count++ ;
for ( int i = input_count-1 ; i > pointsite ; i-- )
input[i] = input[i-1] ;
input[pointsite] = '0' ;
pointsite++ ;
} // if
if ( pointsite != -1 ) {
if ( input_count - 1 - pointsite == 2 ) input[input_count] = '0', input_count++ ; // 處理小數點(後面補0)
else if ( input_count - 1 - pointsite == 1 ) {
input[input_count] = '0' ;
input[input_count+1] = '0' ;
input_count += 2 ;
}
else if ( input_count - 1 - pointsite == 0 ) {
input[input_count] = '0' ;
input[input_count+1] = '0' ;
input[input_count+2] = '0' ;
input_count += 3 ;
}
} // if
if ( input[0] == '+' ) { // 正數把+弄掉 看到好煩
for ( int i = 0 ; i < input_count ; i++ )
input[i] = input[i+1] ;
input_count-- ; // 消掉+所以長度-1
if ( pointsite != -1 ) pointsite-- ;
} // if
double d_temp01 ; // 取小數後第三位
if ( pointsite + 4 < input_count && pointsite != -1 ) {
d_temp01 = atof( input ) ;
if ( input[pointsite+4] >= '5' && input[pointsite+4] <= '9' ) {
if ( d_temp01 > 0 ) d_temp01 += 0.001 ;
else if ( d_temp01 < 0 ) d_temp01 -= 0.001 ;
} // if
input_count = pointsite + 4 ;
d_temp01 = (double)((int)(d_temp01*1000)*0.001) ;
sprintf(input, "%f", d_temp01); // 轉回output
} // if
for ( int i = 0 ; i < input_count ; i++ )
output[i] = input[i] ;
} // if
} // SetNumber()
void SetString() {
for ( int i = 1 ; i < input_count ; i++ ) { // 先抓整個字串
if ( input[i] == '"' )
if ( input[i-1] != '\\' ) input_count = i+1 ;
} // for
int fq = -1, sq = -1 ; // first quote and second quote
for ( int i = 0 ; i < input_count ; i++ ) {
if ( fq == -1 || sq == -1 ) { // 抓看看兩個'
if ( input[i] == '\'' && fq == -1 ) fq = i ;
else if ( input[i] == '\'' && fq != -1 ) sq = i ;
else if ( ( input[i] == '\\' && input[i+1] == 'n' && fq == -1 && sq == -1 ) ||
( input[i] == '\\' && input[i+1] == '\\' && fq == -1 && sq == -1 ) ||
( input[i] == '\\' && input[i+1] == '"' && fq == -1 && sq == -1 ) ) { // 有\n的情況(以及其他)
if ( input[i+1] != 'n' ) input[i] = input[i+1] ;
else if ( input[i+1] == 'n' ) input[i] = '\n' ;
int j = i+1 ;
while ( j < input_count ) {
input[j] = input[j+1] ;
j++ ;
} // while
input[input_count] = '\0' ;
input_count--, i-- ;
} // if
} // if
else { // fq和sq都有值 開始處理兩個'之間的情況
ProceedQuote( fq, sq ) ;
i--, fq = -1, sq = -1 ; // Refresh
} // else
} // for
for ( int i = 0 ; i < input_count ; i++ ) {
output[i] = input[i] ;
} // for
} // SetString()
void ProceedQuote( int fq, int sq ) {
for ( int i = fq ; i < sq ; i++ ) {
if ( ( input[i] == '\\' && input[i+1] == '"' ) ||
( input[i] == '\\' && input[i+1] == '\\' ) ) {
input[i] = input[i+1] ;
int j = i+1 ;
while ( j < input_count ) {
input[j] = input[j+1] ;
j++ ;
} // while
input[input_count] = '\0' ;
input_count-- ;
} // if
} // for
} // ProceedQuote