/*programed by:alex shoal
edit date:2009-3-22, Sunday
revision:2
****************
*Program Objective:将C程序中的除注释外的所有保留字全部大写
****************
*int,long,float,double,bool,char,
if,while,switch,case,continue,break,else,
void,unsigned,extern,global,default,
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char kw[][18]={"int","long","float","double","bool","char","if","while","switch","case","continue","break","else","void","unsigned","extern","global","default"};
int main(int argc, char *argv[])
{
char token[32];
int i;
int j;
int check=0;
int sz=0;
int state=0;
int ch; // place to store each character as read
FILE *fp; // "file pointer"
if (argc != 2){
printf("Usage: %s filename/n", argv[0]);
exit(1);
}
if ((fp = fopen(argv[1], "r")) == NULL){
printf("Can't open %s/n", argv[1]);
exit(1);
}
while (((ch = getc(fp)) != EOF)){
i=0;
j=0;
sz=0;
check=0;
switch(state){
case 0:{ if(ch=='/') state=1; break; }
case 1:{ switch(ch){
case '*': state=2;break;
case '/': state=4;break;
default : state=0;break;
}
break;
}
case 2:{ if(ch=='*') state=3;break; }
case 3:{ if(ch=='/') state=0;else state=2;}
case 4:{ if(ch==10 ) state=0;} //if ch == '/n', exit
}
if((!isalpha(ch))||state==2||state==4) putc(ch,stdout);
else{
for(i=0;i<32;i++) token[i]='/0';
i=0;
do{
token[i++]=ch;
ch=getc(fp);} while(isalpha(ch)&&ch!=EOF);
for(i=0;i<18;i++)
if (strcmp(token,kw[i])==0) {
check=1;
break;}
//sz=sizeof(token);
for(j=0;token[j]!='/0';j++){
if(check==1) putc(token[j]-32,stdout);
else putc(token[j],stdout); }
putc(ch,stdout);
}
}
fclose(fp);
return 0;
}
编译器练习:将C程序中的除注释外的所有保留字全部大写
最新推荐文章于 2024-04-16 21:29:54 发布