编译器练习:找出C程序中除注释外的数字并将其值翻倍
2009-03-22 13:56
309 查看
/*
programed by:alex shoal
edit date:2009-3-22, Sunday
revision:1
****************
*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>
int main(int argc, char *argv[])
{
char token[32];
int i;
int j;
int sz=0;
int temp=0;
int state=0;
char 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;
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((!isdigit(ch))||state==2||state==4) putc(ch,stdout); //extern int isdigit(int c); 当c为数字0-9时,返回非零值,否则返回零
else{
for(i=0;i<32;i++) token[i]='/0';
i=0;
do{
token[i++]=ch;
ch=getc(fp);} while(isdigit(ch)&&ch!=EOF);
temp=atoi(token);
temp=temp*2;
sprintf(token,"%d",temp);
sz=strlen(token);
for(j=0;j<sz;j++){
putc(token[j],stdout);
}
putc(ch,stdout);
}
}
fclose(fp);
return 0;
}
programed by:alex shoal
edit date:2009-3-22, Sunday
revision:1
****************
*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>
int main(int argc, char *argv[])
{
char token[32];
int i;
int j;
int sz=0;
int temp=0;
int state=0;
char 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;
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((!isdigit(ch))||state==2||state==4) putc(ch,stdout); //extern int isdigit(int c); 当c为数字0-9时,返回非零值,否则返回零
else{
for(i=0;i<32;i++) token[i]='/0';
i=0;
do{
token[i++]=ch;
ch=getc(fp);} while(isdigit(ch)&&ch!=EOF);
temp=atoi(token);
temp=temp*2;
sprintf(token,"%d",temp);
sz=strlen(token);
for(j=0;j<sz;j++){
putc(token[j],stdout);
}
putc(ch,stdout);
}
}
fclose(fp);
return 0;
}
相关文章推荐
- 编译器练习:找出C程序中声明的变量,关键字和自然数
- 编译器练习:将C程序中的注释中的小写字母转换为大写字母
- 编译器练习:将C程序中的除注释外的所有保留字全部大写
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
- 华为练习3 在字符串中找出连续最长的数字串
- C#练习——统计整型数组中不重复数字的个数、将普通日期格式转换成汉字日期格式、在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
- Java程序练习-数字问题
- 有一组数字,从1到n,从中减少了3个数,顺序也被打乱,放在一个n-3的数组里,请找出丢失的数字,最好能有程序,最好算法比较快
- 排序练习——找出前m大的数字
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。 请写程序找出这两个只出现一次的数字。
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
- 【算法练习】找出数组中仅出现一次的数字
- 一个整数数组里面,除了两个数之外,其他的数字都出现了两次,写一个程序找出这两个数
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
- 实验课程序练习:猜数字.java
- 分支结构练习-5.数字Game程序
- 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字