满意答案
wanggq222
2018.03.14
采纳率:42% 等级:7
已帮助:57人
#pragma once
#include
#include
#include
#include
using namespace std;
typedef struct SymKeyVal
{
string key;
int val;
};
static SymKeyVal KeyValTable[]=
{
{"+",0},
{"-",0},
{"*",1},
{"/",1},
{"++",2},
{"--",2}
};
enum ComPareenum{
BIG,
EQU,
LIT,
UN
};
class CampareTwoSym
{
public:
CampareTwoSym();
~CampareTwoSym();
ComPareenum ComTwoSymbol(string &sym1, string &sym2);
void InsertSymbol_Key_Value(string & key, int & val);
void OutPutResult(ComPareenum result, string &sym1, string &sym2);
private:
map storesym;
};
//CampareTwoSym.h
#include "CampareTwoSym.h"
CampareTwoSym::CampareTwoSym()
{
int Datalen = sizeof(KeyValTable) / sizeof(SymKeyVal);
for (int i = 0; i
{
InsertSymbol_Key_Value(KeyValTable[i].key, KeyValTable[i].val);
}
}
CampareTwoSym::~CampareTwoSym()
{
}
ComPareenum CampareTwoSym::ComTwoSymbol(string &sym1, string &sym2) {
int valsym1 = 0;
int valsym2 = 0;
map::iterator iter;
iter = storesym.find(sym1);
if (iter == storesym.end())
{
return UN;
}
valsym1 = iter->second;
iter = storesym.find(sym2);
if (iter == storesym.end())
{
return UN;
}
valsym2 = iter->second;
return (valsym1 > valsym2) ? BIG : (valsym1 == valsym2) ? EQU : LIT;
}
void CampareTwoSym::InsertSymbol_Key_Value(string &key, int &val) {
storesym.insert(pair(key,val));
}
void CampareTwoSym::OutPutResult(ComPareenum result, string &sym1, string &sym2)
{
string outsym;
switch (result)
{
case BIG:
outsym = string(">");
break;
case EQU:
outsym = string("=");
break;
case LIT:
outsym = string("
break;
case UN:
outsym = string("error");
break;
default:
outsym = string("unexpect");
break;
}
cout<
}
int main(int argc,char **argv)
{
CampareTwoSym* comtwostr =new CampareTwoSym();
string sym1 = "+";
string sym2 = "*";
comtwostr->OutPutResult(comtwostr->ComTwoSymbol(sym1, sym2), sym1, sym2);
getchar();
getchar();
return 0;
}
//CampareTwoSym.cpp追问: 问题已经解决了,谢谢
00分享举报