Demo entry 6624789

1

   

Submitted by anonymous on Jun 18, 2017 at 08:14
Language: C++. Code size: 2.9 kB.

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <sstream>

using namespace std;

struct { int number; string str[10]; } keywords={3,"int","main","return"} ; //1关键词
struct { int number; string str[10]; } operators ={5,"+","*","=","+=","*="}; //2运算符
struct { int number; string str[10]; } boundaries ={6,"(",")","{","}",",",";"} ; //3分界符
struct { int number; string str[100];} identifieres={0}; //4标识符
struct { int number; string str[100];} Unsigned_integer={0}; //5无符号整数

string int2str(int num)
{
	stringstream ss;
    ss << num;
    return ss.str();
}

int main()
{
    string pro;
    getline(cin, pro, '#');
    
    vector<string> vec;
    vector<string> vecnum;
    
    int pos = 0;
    while(pos < pro.size())
    {
    	while(pos < pro.size() && (pro[pos] == '\n' || pro[pos] == '\t' || pro[pos] == ' '))
    		pos++;
    	if(pos >= pro.size()) break;
    	
    	bool next = false;
    	string s = "";
    	s += pro[pos];
    	
    	if(pro[pos] == '+' || pro[pos] == '*' || pro[pos] == '=')
    	{
    		if(pos + 1 < pro.size() && pro[pos + 1] == '=')
    			s += '=';
    		for(int i = 0; i < operators.number; ++i)
    			if(operators.str[i] == s)
    			{
    				cout << "<2," + int2str(i) + ">";
    				break;
				}
			pos += s.size();
			continue;
		}
		
		for(int i = 0; i < boundaries.number; ++i)
		{
			if(s == boundaries.str[i])
			{
				cout << "<3," + int2str(i) + ">";
				next = true;
				pos++;
				break;
			}
		}
		
		if(next) continue;
		
		if(isalpha(pro[pos]))
		{
			pos++;
			while(pos < pro.size() && isalnum(pro[pos]))
				s += pro[pos++];
			for(int i = 0; i < keywords.number; ++i)
				if(s == keywords.str[i])
				{
					cout << "<1," + int2str(i) + ">";
					next = true;
					break;
				}
			
			if(next) continue;
			
			for(int i = 0; i < vec.size(); ++i)
				if(vec[i] == s)
				{
					cout << "<4," + int2str(i) + ">";
					next = true;
					break;
				}
			
			if(next) continue;
			
			cout << "<4," + int2str(vec.size()) + ">";
			vec.push_back(s);
			
			continue;
		}
		
		if(isdigit(pro[pos]))
		{
			pos++;
			while(pos < pro.size() && isdigit(pro[pos]))
				s += pro[pos++];
				
			for(int i = 0; i < vecnum.size(); ++i)
				if(vecnum[i] == s)
				{
					cout << "<5," + int2str(i) + ">";
					next = true;
					break;
				}
			
			if(next) continue;
			
			cout << "<5," + int2str(vecnum.size()) + ">";
			vecnum.push_back(s);
			
			continue;
		}
	}
	cout << "\nidentifieres:";
	for(int i = 0; i < vec.size(); ++i)
		if(i != vec.size() - 1)
			cout << vec[i] << " ";
		else cout << vec[i] << endl;

	cout << "Unsigned_integer:";
	
	for(int i = 0; i < vecnum.size(); ++i)
		if(i != vecnum.size() - 1)
			cout << vecnum[i] << " ";
		else cout << vecnum[i] << endl;
}                                 

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).