Demo entry 6339325

1

   

Submitted by wangl on Dec 21, 2016 at 13:52
Language: C++. Code size: 4.1 kB.

#include<stdio.h>
#include<string.h>


char prog[200], token[20];
char ch;
int type, p, m = 0, n = 0;
char *rwtab[14] = { "int", "char", "void", "if", "else", "switch","case","default","while","do","for","break","continue","return" };

void scaner()
{
	//初始化token数组
	for (n = 0; n<20; n++) token[n] = NULL;
	ch = prog[p++];		//读入第一个字符
	while (ch == ' ')		//遇到空格读入下一个字符
	{
		ch = prog[p];
		p++;
	}
	if ((ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z'))	//字符开头
	{
		m = 0;
		while ((ch >= '0'&&ch <= '9') || (ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z'))
		{
			token[m++] = ch;
			ch = prog[p++];
		}
		token[m++] = '\0';		//字符串结束时给token加上字符串结束标记

		//遇到错误回退一个字符
		p--;

		//循环检查是否与关键字数组有冲突
		type = 1;
		for (n = 0; n<14; n++)
		{
			if (strcmp(token, rwtab[n]) == 0)
			{
				type = 3;
				break;
			}
		}
	}

	
	else if ((ch >= '1'&&ch <= '9'))		//数字开头十进制
	{
		m = 0;
		while ((ch >= '0'&&ch <= '9'))
		{
			token[m++] = ch;
			ch = prog[p++];
		}
		token[m++] = '\0';
		p--;
		type = 2;
	}

	else if (ch == '0')		//数字开头八、十六进制
	{
		m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == 'x')
		{
			token[m++] = ch;
			ch = prog[p++];
			while ((ch >= '0'&&ch <= '9') || (ch >= 'a'&&ch <= 'f') || (ch >= 'A'&&ch <= 'F'))
			{
				token[m++] = ch;
				ch = prog[p++];
			}
		}
		else
			while (ch >= '0'&&ch <= '9')
			{
				token[m++] = ch;
				ch = prog[p++];
			}

		token[m++] = '\0';
		p--;
		type = 2;
	}
		
	
	else switch (ch)
	{
	case'\'':m = 0;
		ch = prog[p++];
		token[m] = ch;
		ch = prog[p++];
		if (ch == '\'')
		{
			type = 2;
		}
		else
		{
			type = -1;

		}
		break;
	case'\"':m = 0;
		ch = prog[p++];
		while (ch != '\"')
		{ 
			token[m++] = ch;
			ch = prog[p++];
		}
		type = 2;
		break;
	case'+':m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == '+')
		{
			type = 4;
			token[m++] = ch;
		}
		else
		{
			type = 4;
			p--;
		}
		break;
	case'-':m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == '-')
		{
			type = 4;
			token[m++] = ch;
		}
		else
		{
			type = 4;
			p--;
		}
		break;
	case'!':m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == '=')
		{
			type = 4;
			token[m++] = ch;
		}
		else
		{
			type = 4;
			p--;
		}
		break;
	case'&':m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == '&')
		{
			type = 4;
			token[m++] = ch;
		}
		else
		{
			type = -1;
			p--;
		}
		break;
	case'|':m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == '|')
		{
			type = 4;
			token[m++] = ch;
		}
		else
		{
			type = -1;
			p--;
		}
		break;
	case'<':m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == '=')
		{
			type = 4;
			token[m++] = ch;
		}
		else
		{
			type = 4;
			p--;
		}
		break;
	case'>':m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == '=')
		{
			type = 4;
			token[m++] = ch;
		}
		else
		{
			type = 4;
			p--;
		}
		break;
	case'=':m = 0;
		token[m++] = ch;
		ch = prog[p++];
		if (ch == '=')
		{
			type = 4;
			token[m++] = ch;
		}
		else
		{
			type = 4;
			p--;
		}
		break;
	case'*':type = 4; token[0] = ch; break;
	case'/':type = 4; token[0] = ch; break;
	case';':type = 5; token[0] = ch; break;
	case'(':type = 5; token[0] = ch; break;
	case')':type = 5; token[0] = ch; break;
	case',':type = 5; token[0] = ch; break;
	case':':type = 5; token[0] = ch; break;
	case'{':type = 5; token[0] = ch; break;
	case'}':type = 5; token[0] = ch; break;
	case'#':type = 0; break;
	case '\n': type = 11; break;
	default: type = -1; break;
	}
}
int main()
{
	while (1)
	{
		p = 0;
		printf("请输入字符串以#结束:");
		do
		{
			//getchar();
			scanf("%c", &ch);
			prog[p++] = ch;
			//getchar();
		} while (ch != '#');
		p = 0;
		do
		{
			scaner();
			switch (type)
			{
			case -1:printf("输入格式有误"); break;
			case 0:
			case 11:
				break;
			default: printf("(%d,%s)", type, token); break;
			}
			printf("\n");
		} while (type != 0);
		getchar();
	}
	
	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).