滿意答案
tigerlalala
推薦于 2018.05.06
采納率:56% 等級:13
已幫助:14843人
||我有,這是這學期剛4102做的,
#include
#include
#include
#include
#include
#include
using namespace std;
bool isLetter(char ch){
if ((ch>='A' && ch<='Z') ||1653 (ch>='a' && ch<='z')) return true;
else return false;
}
bool isDigit(char ch){
if (ch>='0' && ch<='9') return true;
else return false;
}
bool isP(char ch){
if(ch=='+'||ch=='*'||ch=='-'||ch=='/') return true;
//ch==':'||ch==','||ch=='='||ch==';'||ch=='('||ch==')'
else return false;
}
bool isJ(char ch){
if(ch==','||ch==';'||ch=='.'||ch=='('||ch==')'||ch=='['||ch==']'||ch=='='||ch==':'||ch==''||ch=='{'||ch=='}'||ch=='#') return true;
//
else return false;
}
bool isBlank(char ch){
if(ch==' '||ch=='\t') return true;
else return false;
}
int main(){
string src,ste,s;
char ch0,ch,ch1[2];
char ktt[48][20]={"and","begin","const","div","do","else","end","function","if","integer",
"not","or","procedure","program","read","real","then","type","var","while","write","辨別符","無符号數",
",",";",":",".","(",")","[","]","..","++","--","+","-","*","/","=","","<>","<="
,">=",":=","{","}","#"};
int pos=0;
FILE *fp;
fp=fopen("d:\\in.txt","r");
ch0=fgetc(fp);
while(ch0!=EOF)
{
//if(ch0!='\t'){src+=ch0;}
src+=ch0;
ch0=fgetc(fp);
}
src+='#';
cout<
ch=src[pos++];
ste=" ";
for(int j=0;j<47;j++){cout<
cout<
while(ch!='#')
{
char str[20];
if(ch!='\n')
{
if(isDigit(ch))
{ //判斷常數
int i=0;
while(isDigit(ch)||ch=='.')
{
str[i++]=ch;
//i++;
ch=src[pos++];
}
str[i]='\0';
ste=ste+"|"+"22";
cout<
continue;
}
else if(isLetter(ch))
{ //判斷字元
int i=0,j;
while(isLetter(ch)||isDigit(ch))
{
str[i++]=ch;
//i++;
ch=src[pos++];
}
str[i]='\0';
for(j=0;j<21;j++){ //判斷是否關鍵字
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<
ss>>ste;
break;
}
}
if(j==21){ste=ste+"|"+"21";}
// cout<
cout<
continue;
}
else if(isP(ch)){ ///判斷是否運算符
int i=0,j;
str[i++]=ch;
str[i]='\0';
for(j=34;j<38;j++){
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<
ss>>ste;
break;
}
}
cout<
ch=src[pos++];
continue;
}
else if(isJ(ch)) //判斷是否界符
{
int i=0,j;
while(isJ(ch))
{
str[i++]=ch;
ch=src[pos++];
}
str[i]='\0';
for(j=23;j<47;j++){
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<
ss>>ste;
break;
}
}
cout<
continue;
}
else if(isBlank(ch))
{
cout<
ch=src[pos++];
continue;
}
}
else{
cout<
ste=" ";
}
ch=src[pos++];
}
return 0;
}
還有運作效果圖,和實驗報告 ,你要的話留下郵箱
162分享舉報