會寫程式的貓
2008年12月30日
判斷出關鍵字-隨便應付版
User:
MY BLOG
Class:
c++
,
c++ 疑難雜症
程式說明: 1. 讀入文字檔並分析檔案內的程式語言 2. 判斷出關鍵字,並輸出結果 關鍵字: #include < > int, char, float, double for, do while, while cout, cin (printf, scanf) 3. 例如
#include
void main(void) { int x; for( x=0 ; x < 10; x++ ) cout << x; }
輸出: -2 載入標頭檔 iostream.h -4 宣告整數變數 x -5 執行for迴圈 -6 輸出 x
//讀取檔案 #include
#include
#include
using namespace std; void f(string str){ string bS[str.size()]; int n=0; for( int x=0 ; x < str.size() ; x++ ){ if( str[x] != '\n' ){ bS[n]+=str[x]; }else n++; } n=0; for( int x=0 ; x < sizeof(bS)/sizeof(bS[0]) ; x++ ){ if( (bS[x] != "") && (bS[x] != "\n") ){ n++; } } string s[n]; n=0; for( int x=0 ; x < sizeof(bS)/sizeof(bS[0]) ; x++ ){ if( (bS[x] != "") && (bS[x] != "\n") ){ s[n]=" "; s[n]+=bS[x]; s[n].erase(0,s[n].find_first_not_of(" "));//去除前面的空白 s[n].erase(s[n].find_last_not_of(" ") + 1);//去除後面的空白 s[n]=" "+s[n]; n++; } } for( int x=0 ; x < sizeof(s)/sizeof(s[0]) ; x++ ){ cout << s[x] << endl; } for( int i=0 ; i < sizeof(s)/sizeof(s[0]) ; i++ ){ if( s[i].find("#include") == true ){ cout << i << "匯入標頭檔"; for( int j=s[i].find_first_of('e')+1 ; j < s[i].size() ; j++ ){ cout << s[i][j]; } cout << endl; }else if( s[i].find("int") == true ){ cout << i <<"宣告 整數 型態變數"; for( int j=s[i].find_first_of('t')+1 ; j < s[i].size() ; j++ ){ cout << s[i][j]; } cout << endl; }else if( s[i].find("char") == true ){ cout << i << "宣告 字元 型態變數"; for( int j=s[i].find_first_of('r')+1 ; j < s[i].size() ; j++ ){ cout << s[i][j]; } cout << endl; }else if( s[i].find("float") == true ){ cout << i << "宣告 字元 型態變數"; for( int j=s[i].find_first_of('t')+1 ; j < s[i].size() ; j++ ){ cout << s[i][j]; } cout << endl; }else if( s[i].find("double") == true ){ cout << i << "宣告 字元 型態變數"; for( int j=s[i].find_first_of('e')+1 ; j < s[i].size() ; j++ ){ cout << s[i][j]; } cout << endl; }else if( s[i].find("for") == true ){ cout << i << "執行 for 迴圈" << endl; }else if( s[i].find("while") == true ){ cout << i << "執行 while 迴圈" << endl; }else if( s[i].find("do") == true ){ cout << i << "執行 do while 迴圈" << endl; }else if( s[i].find("cout") == true ){ cout << i << "輸出"; for( int j=s[i].find_first_of('t')+1 ; j < s[i].size() ; j++ ){ if( s[i][j] != '<') cout << s[i][j]; } cout << endl; }else if( s[i].find("cin") == true ){ cout << i << "輸入"; for( int j=s[i].find_first_of('n')+1 ; j < s[i].size() ; j++ ){ if( s[i][j] != '>') cout << s[i][j]; } cout << endl; } } } int main(){ ifstream in("file02.cpp");//讀取檔案 string fs; string fstr=""; while(getline(in,fs))//若是有文字將儲存至字串S中 fstr += fs+"\n"; f(fstr); }
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言