程序如下
#include <stdio.h>
#include <string>
#include <map>
#include <vector>
#include <conio.h>
using namespace std;
FILE * g_fp = NULL;
int g_total = 0;
int g_event_count = 0;
map<string,int> g_Values;
struct s_cmd
{
string cmd_name;
string name;
int value;
};
vector<s_cmd> g_vecCmd;
char getNextChar(string &s,int &pos);
bool isSpace(char c)
{
return (c=='\x20' || c=='\t' || c=='\r' ||c=='\n');
}
char jumpSpace(string &s,int pos,char c)
{
while(isSpace(c))
{
c = getNextChar(s,pos);
if(c==-1)
{
break;
}
}
return c;
}
bool isEOF()
{
return feof(g_fp);
}
char getNextChar(string &s,int &pos)
{
return s[pos++];
}
bool isNumber(char c)
{
return c>='0' && c<='9';
}
bool isAlphabet(char c)
{
return (c>='a' && c<='z')||(c>='A' && c<='Z');
}
bool scan(string s)
{
int state = 0;
string result ;
int len = s.size();
int pos = 0;
while(pos<len)
{
result.clear();
char c = getNextChar(s, pos);
c=jumpSpace(s,pos,c);
if (isAlphabet(c))
{
while (isAlphabet(c))
{
result += c;
c = getNextChar(s, pos);
}
string cmd = result;
result.clear();
if (cmd == "Add")
{
//get name
c = getNextChar(s, pos);
if (isAlphabet(c))
{
while (isAlphabet(c))
{
result += c;
c = getNextChar(s, pos);
}
if (!isSpace(c))
{
printf("输入的名称中包含非法字母\n");
exit(1);
}
string name = result;
result.clear();
c = getNextChar(s, pos);
if (isNumber(c))
{
while (isNumber(c))
{
result += c;
c = getNextChar(s, pos);
}
int value = atoi(result.c_str());
result.clear();
s_cmd tmp;
tmp.cmd_name = "Add";
tmp.name = name;
tmp.value = value;
g_vecCmd.push_back(tmp);
}
else
{
printf("input value is invalid\n");
exit(1);
}
}
else
{
printf("input name is invalid\n");
exit(1);
}
}
else if (cmd == "Find")
{
c = getNextChar(s, pos);
if (isNumber(c))
{
while (isNumber(c))
{
result += c;
c = getNextChar(s, pos);
}
int value = atoi(result.c_str());
result.clear();
s_cmd tmp;
tmp.cmd_name = "Find";
tmp.name = "";
tmp.value = value;
g_vecCmd.push_back(tmp);
}
else
{
printf("input value is invalid\n");
exit(1);
}
}
}
}
}
int testFromFile()
{
FILE* fp = fopen("d:\\test\\input.txt","rb");
if(!fp)
{
printf("open file failed\n");
return 1;
}
g_fp = fp;
fclose(fp);
return 0;
}
bool GetValue(int value,string &ret_name)
{
int result = 0;
map<string, int>::iterator itfind;
map<string,int>::iterator it = g_Values.begin();
for (; it != g_Values.end(); it++)
{
if (it->second >=value)
{
if (result == 0)
{
ret_name = it->first;
result = it->second;
itfind = it;
}
else
{
if (result > it->second)
{
ret_name = it->first;
result = it->second;
itfind = it;
}
}
}
}
if (result != 0)
{
g_Values.erase(itfind);
}
return result != 0;
}
void parse()
{
vector<s_cmd>::iterator it = g_vecCmd.begin();
for (; it != g_vecCmd.end(); it++)
{
if (it->cmd_name == "Add")
{
g_Values[it->name] = it->value;
printf("%d\n", g_Values.size());
}
else if (it->cmd_name == "Find")
{
string name;
if (GetValue(it->value, name))
{
printf("%s\n", name.c_str());
}
else
{
printf("%s\n", "WAIT...");
}
}
}
}
int main(int argc,char** argv)
{
int stat = 0;
int sub_stat = 0;
string name;
string value;
printf("%s\n", "$>");
g_total = 1;
while (stat!=-1)
{
char buff[256] = {0};
if (g_total == 0)
{
break;
}
gets(buff);
switch (stat)
{
case 0:
g_total = atoi(buff);
stat = 1;
break;
case 1:
g_event_count = atoi(buff);
stat = 2;
break;
case 2:
{
if (g_event_count > 0)
{
g_event_count -= 1;
scan(string(buff));
if (g_event_count == 0)
{
printf("%s\n", "Sample Output");
parse();
stat = 1;
if (g_total > 0)
{
g_total -= 1;
}
else
{
stat = -1;
}
}
}
break;
}
default:
break;
}
}
printf("press any key exit...\n");
getch();
return 0;
}
输入:fcwr
$>
1
5
Add lcy 1
Add lyd 19
Find 11
Find 13
Add zxs 10
Sample Output
1
2
lyd
WAIT...
2
press any key exit...
第二个例子:fcwr
$>
2
5
Add lcy 1
Add lyd 19
Find 11
Find 13
Add zxs 10
Sample Output
1
2
lyd
WAIT...
2
5
Add sxl 19
Add xsw 11
Add xxx 13
Find 10
Find 18
Sample Output
2
3
lyd
WAIT...
2
3
4
5
zxs
sxl
press any key exit...