📄 cpp1.cpp
字号:
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include <iostream.h>
#include <fstream.h>
#define BUFFMAXLEN 100
FILE *out;
char token[20],s[20],filename[25],outfile[25],ch;
char blank[34][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed","sizeof",
"static","struct","switch","typedef","union","unsigned",
"void","volatile","while","main"};
int flag=1;
void getbe()
{int readchar(char *ch);
while(isspace(ch))
ch=readchar(filename);
}
void concatenation(char ch)
{
strcat(token,&ch);
}
void error()
{
if(ch!=-1)
fputs("\nyour input a data ERROR!",out);
}
int reserve()
{int i,m=0;
for(i=0;i<=34;i++)
{ if(strcmp(token,blank[i])==0)m=1;
}return(m);
}
int readchar(char * filename)
{
static FILE *fp = NULL;
static char buff[BUFFMAXLEN];
static int bufflen = 0;
static int buffpos = -1;
if (fp == NULL)
{
if ((fp = fopen(filename,"r")) == NULL)
{
printf("File open error!\n");
return 0;
}
}
if (buffpos == -1)
{
if (feof(fp))
{
fclose(fp);
return -1;
}
bufflen = fread(buff,sizeof(char),BUFFMAXLEN,fp);
buffpos = 0;
}
if (buffpos < bufflen)
ch = buff[buffpos++];
if (buffpos >= bufflen)
buffpos = -1;
if(ch=='\n')
flag++;
return ch;
}
void main()
{
int c,k=0;
char b=' ';
printf("\nPlease input filename:");
scanf("%s",filename);
printf("\nPlease input outfile name:");
scanf("%s",outfile);
if((out=fopen(outfile,"w"))==NULL)
{printf("\ncannot open outfile file!");
}
ch = b;
while(ch != -1 && ch != 0)
{ strcpy(token,s);
if(k==0)ch=readchar(filename);
else {ch=b;k=0;}
getbe();
if(isalpha(ch))
{while(isalnum(ch)||ch=='.')
{
concatenation(ch);
b=ch=readchar(filename);k=1;
}
c=reserve();
if(c==1)fprintf(out,"\n %d: reserved word:%s ",flag, token);
else
fprintf(out,"\n %d:ID, name= %s",flag,token);
}
else
{if(isdigit(ch))
{while(isdigit(ch))
{
concatenation(ch);
b=ch=readchar(filename);k=1;
}
fprintf(out,"\n %d:INT,val=%s",flag,token); }
else
switch(ch)
{ case '+': b=ch=readchar(filename);
if(ch=='+')fprintf(out,"\n %d: ++",flag,token);
else {fprintf(out,"\n %d: +",flag,token);k=1;}
break;
case '-': b=ch=readchar(filename);
if(ch=='-')fprintf(out,"\n %d: --",flag,token);
else {fprintf(out,"\n %d: -",flag,token);k=1;}
break;
case '*':fprintf(out,"\n %d: *",flag,token);break;
case '<': b=ch=readchar(filename);
if(ch=='=')fprintf(out,"\n %d: <=",flag,token);
else {fprintf(out,"\n %d: <",flag,token);k=1;}
break;
case '=': b=ch=readchar(filename);
if(ch=='=')fprintf(out,"\n %d: ==",flag,token);
else {fprintf(out,"\n %d: =",flag,token);k=1;}
break;
case '>': b=ch=readchar(filename);
if(ch=='=')fprintf(out,"\n %d: >=",flag,token);
else {fprintf(out,"\n %d: >",flag,token);k=1;}
case '&': b=ch=readchar(filename);
if(ch=='&')fprintf(out,"\n %d: &&",flag,token);
else {fprintf(out,"\n %d: &",flag,token);k=1;}
break;
//case '|': b=ch=readchar(filename);
// if(ch=='|')fprintf(out,"\n %d: ||",flag,token);
// else { fprintf(out,"\n %d: |",flag,token);break;k=1;}
// break;
case '!': b=ch=readchar(filename);
if(ch=='=')fprintf(out,"\n %d: !=",flag,token);
else { fprintf(out,"\n %d: !",flag,token);break;k=1;}
break;
case ':': fprintf(out,"\n %d: :",flag,token);break;
case ',': fprintf(out,"\n %d: ,",flag,token);break;
case ';': fprintf(out,"\n %d: ;",flag,token);break;
case '(': fprintf(out,"\n %d: (",flag,token);break;
case ')': fprintf(out,"\n %d: )",flag,token);break;
case '{': fprintf(out,"\n %d: {",flag,token);break;
case '}': fprintf(out,"\n %d: }",flag,token);break;
case '[': fprintf(out,"\n %d: [",flag,token);break;
case ']': fprintf(out,"\n %d: ]",flag,token);break;
case '"': fprintf(out,"\n %d: \"",flag,token);break;
case '\\': fprintf(out,"\n %d: \\",flag,token);break;
case '.': fprintf(out,"\n %d: .",flag,token);break;
case '\'': fprintf(out,"\n %d: \'",flag,token);break;
case EOF: fprintf(out,"\n %d: EOF",flag);break;
default:
error();
}
}
}
fclose(out);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -