www.pudn.com > DesignII.rar > main.cpp
#include#include #include #include //一些定义-------------------------------------------------------------------- typedef struct { char ID[2]; char num[11]; }Code; char* KeyWord[]={ "bool","break","case","char","continue","do","double",//7 "else","float","for","if","int","main","return","struct","switch",//8 "typedef","union",//2 "void","while"//2 };//19 //----------------------------------------------------------------------------- //所定义函数------------------------------------------------------------------- bool isDigital(char ch) { if(ch>='0' && ch<='9') return true; else return false; } bool isLetter(char ch) { if( (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') ) return true; else return false; } bool isDivide(char ch) { if(ch=='{' || ch=='}' || ch=='(' || ch==')' || ch=='\'' || ch=='\"' || ch==',' || ch==';' || ch==':') return true; else return false; } bool isOperator(char ch) { if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='=' /*|| ch=='>' || ch=='<'*/) return true; else return false; } bool isKeyword(char* str) { int i=0; for(i=0;i<19;i++) { if(strcmp(str,KeyWord[i])==0) { return true; } } return false; } //----------------------------------------------------------------------------- //全局变量--------------------------------------------------------------------- char file[2000]; Code myCode[100]; int count=0; bool strFlag=false; //------------------------------------------------------------------------------ int main() { int i=0; FILE* fp; fp=fopen("s.txt","r"); while( !feof(fp) ) { file[i++]=fgetc(fp); } file[i-1]='\0'; for(i=0;file[i]!='\0';i++) { if(file[i]==' ' || file[i]=='\t' || file[i]=='\r' || file[i]=='\n') file[i]='#'; } printf("%s\n",file); printf("-----------------------------------------\n"); //----------------------------------------------------------------------------- i=0; while(file[i]!='\0') { if(file[i]=='#') { i++; continue; }//end if file[i]=='#' if( isLetter(file[i]) ) { char tmp[10]; int begin=i; // int end; int pos=0; /////////////////////////////////////////////////// while( isDigital(file[i]) || isLetter(file[i]) ) { tmp[pos]=file[i]; i++; pos++; } tmp[pos]='\0'; pos=0; // printf("%s",tmp); // end=i; if( isKeyword(tmp) ) { int k; for(k=0;k<19;k++) { if(strcmp(tmp,KeyWord[k])==0) { strcpy(myCode[count].ID,"00"); strcpy(myCode[count].num,tmp); printf("%4s%10s\n",myCode[count].ID,myCode[count].num); count++; } } // printf("%d",myCode[count-1].Num); } else { int len=strlen(tmp); int sum=0; int t; for(t=0;t ' || file[i]=='<') { char tmp[10]; int iTmp=i; i++; if(file[i]=='=') { tmp[0]=file[iTmp]; tmp[1]=file[i]; tmp[2]='\0'; strcpy(myCode[count].num,tmp); strcpy(myCode[count].ID,"01"); printf("%4s%10s\n",myCode[count].ID,myCode[count].num); i++; count++; } else { i=iTmp; tmp[0]=file[i]; tmp[1]='\0'; strcpy(myCode[count].num,tmp); strcpy(myCode[count].ID,"01"); printf("%-4s%-10s\n",myCode[count].ID,myCode[count].num); i++; count++; } } }//end while printf("\n----------------------------------------------------\n"); // int h; // for(h=0;h