⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 015750t.c

📁 编译的作业
💻 C
字号:
#include<stdio.h>
#include<ctype.h>
#include<string.h>

int LINENUM = 1;  /* 程序出错的行号 */
char ch = '\0';
char buffer[50];
int i = 0; /* 数组下标 */
int ID = 0; /* 类型编码 */
FILE *infp = NULL;
FILE *outfp = NULL;
FILE *errfp = NULL;

/* 以下为子函数定义 */

int iskeyword()		/* 判断是否是保留字 */
{
    const char *KEYWORD[] ={ /* 保留字列表 */
        "if",  
        "else",
        "do",
        "while"
        "break",
        "double",
        "return",
        "case",
        "int",
        "short",
        "void",
        "static",
        "char",
        "long",
        "const",
        "for",
        "continue",
        "goto",
        "include",
        "switch",
        "typedef",
        "begin",
        "end",
        "then"
        };
    int n;
    i++;
    buffer[i] = '\0';
    for(n = 0; n < 24; n++)
    {
        if(strcmp(KEYWORD[n], buffer) == 0)
            return(1);
    }
    i--;
    return(0);  
}
   


int isoperator(char c) /* 是否是运算符字母表内的字母。*/
{                      /* 返回值大于1:是;返回值小于1:不是 */
    int s;				/* 返回值等于2:是单操作符序列 */
    switch(c)
    {
        case '-': s = 1; break;
        case '+': s = 1; break;
        case '=': s = 1; break;
        case '*': s = 1; break;
        case '/': s = 1; break;
        case '|': s = 1; break;
        case '>': s = 1; break;
        case '<': s = 1; break;
        case '&': s = 1; break;
        case '!': s = 1; break;
        case '~': s = 1; break;
        case ':': s = 2; break;
        case ',': s = 2; break;
        case '?': s = 2; break;
        default : s = 0;
    }
    return(s);
}



int ispairoperator()
{
    int s = 0;
    char *PAIR[] ={
        "+=",
        "++",
        "-=",
        "--",
        "*=",
        "/=",
        "%=",
        "==",
        "<=",
        ">=",
        "!=",
        "&=",
        "&&",
        "||"
        };
    int n;
    i++;
    buffer[i] = '\0';
    for(n = 0; n < 14; n++)
    {
        if(strcmp(PAIR[n], buffer) == 0)
        {
            s = 1;
            break;
        }
    }
    i--;
    return(s);    
}



int isseparator(char c)		/* 判断是否是分隔符 */
{
    int s;
    switch(c)
    {
        case '(': s = 1;break;
        case ')': s = 1;break;
        case '{': s = 1;break;
        case '}': s = 1;break;
        case '[': s = 1;break;
        case ']': s = 1;break;
        case ';': s = 1;break;
        default : s = 0;
    }
     return(s);
}



int isdigitordot(char c)	/* 判断是否是数字或"." */
{
    if(isdigit(c) | c == '.')
        return(1);
    else 
        return(0);
}






int ispartofesc(char c) /* 判断"\"之后的字符*/
{
    int s;
    switch(c)
    {
        case 'n' : s = 1;break;
        case 't' : s = 1;break;
        case 'b' : s = 1;break;
        case 'f' : s = 1;break;
        case 'r' : s = 1;break;
        case 96 : s = 1;break;  /* "'" */
        case '"' : s = 1;break;
        case 92 : s = 1;break;  /* "\" */
        default  : s = 0;
    }
    return(s);
}

void RIGHT(int id)  /* 正确的单词的输出 */
{
    int j;
    fprintf(outfp,"(%d,",id);
    for(j = 0; j <= i; j++)
    {
        fprintf(outfp,"%c",buffer[j]);
    }
    fprintf(outfp,")\n");
    i = 0;
}
  
  
    
void ERROR()  /* 错误的单词的输出 */
{
    int j;
    fprintf(errfp,"There is an ERROR in line%d:",LINENUM);
    for(j = 0; j <= i; j++)
    {
        fprintf(errfp,"%c",buffer[j]);
    }
    fprintf(errfp,"\n");
    i = 0;
}   


/* 主程序 */
void main(void)
{
    if((infp = fopen("015750.txt","r")) == NULL)
    {
        printf("error opening the file: 015750text.txt\n");
        exit(0);
    }
    if((outfp = fopen("output.txt","wa")) == NULL)
    {
        printf("error opening the file: output.txt\n");
        exit(0);
    }
    if((errfp = fopen("erroutput.txt","wa")) == NULL)
    {
        printf("error opening the file: erroutput.txt\n");
        exit(0);
    }
    while((ch = getc(infp)) != EOF)
    {
	if(isalpha(ch)) /* ch是否为大小写字母 */
        {
            buffer[i] = ch;
            while(isalnum(ch = getc(infp)))
            {
                i++;
                buffer[i] = ch;
            }
            if(iskeyword())
            {
                ID = 1;
            } 
            else
            {
                ID = 7;
            }
            RIGHT(ID);
            ungetc(ch,infp);
            continue;
        }	
        if(isdigit(ch)) /* 判断ch是否为数字 */
        {
            buffer[i] = ch;
            while(isdigitordot(ch = getc(infp)))
            {
                if(ch == '.')
                {
                    ID = 3;
                }
                else
                {
                    ID = 2;
                }
                i++;
                buffer[i] = ch;
            }
            if(isalpha(ch))
            {
                i++;
                buffer[i] = ch;
                while(isalnum(ch = getc(infp)))
                {
                    i++;
                    buffer[i] = ch;
                }
                ERROR();
                continue;
            }
            RIGHT(ID);
            ungetc(ch,infp);
            continue;
                
        }

        if(isoperator(ch)) /* ch是否为操作符字母表中的元素 */
        {
            buffer[i] = ch;
            ID = 4;
            if(isoperator(ch) != 2)
            {
                if(isoperator(ch = getc(infp)))
                {
                    i++;
                    buffer[i] = ch;
                    if(ispairoperator())
                    {
                        ;
                    }
                    else
                    {
                        ERROR();
                        continue;
                    }
                }
            }
            RIGHT(ID);
            continue;   
        }
        if(isseparator(ch)) /* ch是否为分隔符 */
        {
            buffer[i] = ch;
            ID = 6;
            RIGHT(ID);
            continue;   
        }
        if(ch == 92)  /*ch是否为转义序列其始字符'\'*/
        {
            buffer[i] = ch;
            if(ispartofesc(ch = getc(infp)))
            {
                ID = 5;
                i++;
                buffer[i] = ch;
            }
            else
            {
                i++;
                buffer[i] = ch;
                ERROR();
                continue;
            }
            RIGHT(ID);
            continue;   
        }
        if(ch == '\n') /* ch是否为回车换行符 */
        {
            LINENUM++;
            continue;
        }
        if(ch == ' ' | ch == '  ') /* ch是否为空格或TAB符 */
        {
            continue;
        }
    }
    fclose(infp);
    fclose(outfp);
    fclose(errfp);
}   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -