主程序 yang95.c 如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define null 0
int i,j,flag,status,count,id,row=1;
char ch,*tmpStr='\0';
char words[10] = {" "};
char program[1000];
FILE *input,*output,*fpReserver,*fpCal,*fpSep;
int Scan(char program[]){
flag=-1;
j=0;
ch=program[i++];
if((ch==' ')||(ch=='\n'||(ch=='\t'))&&ch!=EOF) {
if(ch=='\n')
row++;
Scan(program);
}
else{
/*判断是否保留字*/
if((ch>='a')&&(ch<='z')){
while(((ch>='a')&&(ch<='z'))||((ch>='0')&&(ch<='9'))){
words[j++]=ch;
ch=program[i++];
}
i--;
words[j++]='\0';
/*打开关键字文件*/
if((fpReserver=fopen("keywords.txt","r"))==NULL){
printf("file open in readonly mode,but an error generate!\n");
exit(0);
}
while(fscanf(fpReserver,"%s",tmpStr)!=EOF)
if(strcmp(words,tmpStr)==0){
fclose(fpReserver);
id++;
flag=1;
break;
}
fclose(fpReserver);
if(flag==-1){
id++;
flag=2;
}
}/*end:判断是否保留字*/
/*识别数字*/
else if(isdigit(ch)){
while(isdigit(ch)){/*都为数字时*/
words[j++]=ch;
ch=program[i++];
}
if(ch=='.'){ /*第一个小数点时 加入 且读取下一个字符*/
words[j++]=ch;
ch=program[i++];
if(isdigit(ch)){
while(isdigit(ch)){/*小数点后是数字 则加入,读取下一个*/
words[j++]=ch;
ch=program[i++];
}
if(isalpha(ch)||(ch=='.')){/*如:22.2后面又出现小数点或字母*/
words[j++]=ch;
ch=program[i++];
while(isalpha(ch)||isdigit(ch)||ch=='.'){
words[j++]=ch;
ch=program[i++];
}
words[j]='\0';
id++;
flag=0;
}
else{/*数字、其他字符或结束符时,则为数据*/
words[j]='\0';
id++;
flag=3;
}
}
else{/*小数点后不是数字*/
while((ch!=' '&&ch!='\t'&&ch!='\n')&&ch!=EOF){
words[j++]=ch;
ch=program[i++];
}
words[j]='\0';
id++;
flag=0;
}
}
else if(isalpha(ch)){ /*数字后是字母*/
while(isdigit(ch)||isalpha(ch)||ch=='.'){
words[j++]=ch;
ch=program[i++];
}
words[j]='\0';
id++;
flag=0;
}
else if(!isalpha(ch)||ch!='.'){/*都为数字*/
words[j]='\0';
id++;
flag=3;
}
i--;
}/*end判断是否数字*/
/*判断是否运算符*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define null 0
int i,j,flag,status,count,id,row=1;
char ch,*tmpStr='\0';
char words[10] = {" "};
char program[1000];
FILE *input,*output,*fpReserver,*fpCal,*fpSep;
int Scan(char program[]){
flag=-1;
j=0;
ch=program[i++];
if((ch==' ')||(ch=='\n'||(ch=='\t'))&&ch!=EOF) {
if(ch=='\n')
row++;
Scan(program);
}
else{
/*判断是否保留字*/
if((ch>='a')&&(ch<='z')){
while(((ch>='a')&&(ch<='z'))||((ch>='0')&&(ch<='9'))){
words[j++]=ch;
ch=program[i++];
}
i--;
words[j++]='\0';
/*打开关键字文件*/
if((fpReserver=fopen("keywords.txt","r"))==NULL){
printf("file open in readonly mode,but an error generate!\n");
exit(0);
}
while(fscanf(fpReserver,"%s",tmpStr)!=EOF)
if(strcmp(words,tmpStr)==0){
fclose(fpReserver);
id++;
flag=1;
break;
}
fclose(fpReserver);
if(flag==-1){
id++;
flag=2;
}
}/*end:判断是否保留字*/
/*识别数字*/
else if(isdigit(ch)){
while(isdigit(ch)){/*都为数字时*/
words[j++]=ch;
ch=program[i++];
}
if(ch=='.'){ /*第一个小数点时 加入 且读取下一个字符*/
words[j++]=ch;
ch=program[i++];
if(isdigit(ch)){
while(isdigit(ch)){/*小数点后是数字 则加入,读取下一个*/
words[j++]=ch;
ch=program[i++];
}
if(isalpha(ch)||(ch=='.')){/*如:22.2后面又出现小数点或字母*/
words[j++]=ch;
ch=program[i++];
while(isalpha(ch)||isdigit(ch)||ch=='.'){
words[j++]=ch;
ch=program[i++];
}
words[j]='\0';
id++;
flag=0;
}
else{/*数字、其他字符或结束符时,则为数据*/
words[j]='\0';
id++;
flag=3;
}
}
else{/*小数点后不是数字*/
while((ch!=' '&&ch!='\t'&&ch!='\n')&&ch!=EOF){
words[j++]=ch;
ch=program[i++];
}
words[j]='\0';
id++;
flag=0;
}
}
else if(isalpha(ch)){ /*数字后是字母*/
while(isdigit(ch)||isalpha(ch)||ch=='.'){
words[j++]=ch;
ch=program[i++];
}
words[j]='\0';
id++;
flag=0;
}
else if(!isalpha(ch)||ch!='.'){/*都为数字*/
words[j]='\0';
id++;
flag=3;
}
i--;
}/*end判断是否数字*/
/*判断是否运算符*/