TEXT.C
上传用户:ziyourenyl
上传日期:2013-06-02
资源大小:2k
文件大小:3k
- #include"stdio.h"
- #include"string.h"
- #include"ctype.h"
- int operation(char ch);
- int keyword(char c[]);
- int symbol(char ch);
- spy(char m[],char n[],int i,int j);
- char a[200];
- char t;
- int main()
- {
- char sourcename[10];
- char objectname[10];
- FILE *fp;
- char ch;
- int i=0,j;
- char store[30];
- printf("please input sourcefile:");
- scanf("%s",sourcename);
- if((fp=fopen(sourcename,"r"))==NULL)
- {
- printf("file error! please input again.n");
- exit(0);
- }
- printf("n");
- do
- {
- ch=fgetc(fp);
- if(ch=='n') continue;
- a[i]=ch;
- i++;
- }while(!feof(fp));
- fclose(fp);
- printf("Please input objectfile name:");
- scanf("%s",objectname);
- printf("n");
- if((fp=fopen(objectname,"w"))==NULL)
- {
- printf("Open Objectfile failure!n");
- exit(0);
- }
- j=0;
- for(i=0;i<(strlen(a)-1);i++)
- {
- if(a[i]==' ') continue;
- if(isdigit(a[i])) {t='5';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);continue;}
- if(operation(a[i])) {t='3';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);continue;}
- if(symbol(a[i])) {t='2';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);continue;}
- if(isalpha(a[i]))
- {
- j=i+1;
- while(1)
- {
- if(isalpha(a[j])) {j++;continue;}
- break;
- }
- spy(store,a,i,j);
- if(keyword(store)) {t='1';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputs(store,fp);fputc('"',fp);fputc('n',fp);i=j-1;continue;}
- {t='6';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputs(store,fp);fputc('"',fp);fputc('n',fp);i=j-1;continue;}
- }
- if((a[i]=='<')&&(a[i+1]!='=')||(a[i]=='>'&&a[i+1]!='=')) {t='4';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);continue;}
- if((a[i]=='<'&&a[i+1]=='=')||(a[i]=='>'&&a[i+1]=='=')||(a[i]=='='&&a[i+1]=='=')) {t='4';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc(a[i+1],fp);fputc('"',fp);fputc('n',fp);i++;continue;}
- {t='6';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);}
- }
- }
- int operation(char ch)
- {
- if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
- return 1;
- else return 0;
- }
- int keyword(char c[])
- {
- char key[][8]={"int","float","char","double","long","if","else","while","for"};
- int j=0,k=0;
- for(j=0;j<9;j++)
- {
- if(!strcmp(c,key[j])) {k=1;break;}
- }
- if(k==1) return 1;
- else return 0;
- }
- int symbol(char ch)
- {
- if(ch=='{'||ch=='}'||ch==','||ch==';'||ch=='('||ch==')'||ch=='"'||ch=='=')
- return 1;
- else return 0;
- }
- spy(char m[],char n[],int i,int j)
- {
- int z=0,k=0;
- for(z=i;z<j;z++)
- {m[k]=n[z];k++;}
- m[k]=' ';
- }