就像前面说过的,InputParser 就是用来分析原始用户输入的公式字符串,检查出哪些是变量。下面列出它的头文件接口。(InputParser.h)
#if !defined(AFX_INPUTPARSER_H__EB32D411_1F82_4EB3_A482_12E90D8F02F5__INCLUDED_)
#define AFX_INPUTPARSER_H__EB32D411_1F82_4EB3_A482_12E90D8F02F5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include 
<vector>

class InputParser  
{
public:
    InputParser(
char* input, char* szOptrTable);
    
virtual ~InputParser();
public:
    std::vector
<char> GetValNames();
    
char* GetCharArr();
private:
    
char* input;
    
char* szOptrTable;
    
int* prioTable;
    
private:
    
// get index of ch in the szOptrTable.
    
// on error return -1.
    inline int GetIndex(char ch)
    {
        
int i = 0;
        
while (szOptrTable[i] != 0)
        {
            
if (szOptrTable[i] == ch)
            {
                
return i;
            }
            i
++;
        }
        
return -1;
    }
    
};

#endif // !defined(AFX_INPUTPARSER_H__EB32D411_1F82_4EB3_A482_12E90D8F02F5__INCLUDED_)

GetValNames() 用来返回字符串中的变量,并把这些变量放在一个 vector 中。此时,这些变量已经被排好序了。szOptrTable 就像前面所介绍的,是操作符表。它是一个以 zero 结尾的字符串。prioTable 是操作符优先级表的 int 型数组。它们通过构造函数被调用者传递过来。这样可以使,当这些表发生变化时,只需要在定义它们的地方修改就可以了。以后的功能类里也用类似的方法传递必要的表,就不再作说明了。GetIndex(char ch) 是在操作符表里检查 ch 是否在其中。也就是说,如果 ch 是操作符,那么这个方法返回 ch 在操作符表中的索引。如果不是,返回 -1。

下面给出 InputParser 的实现:
#include "stdafx.h"
#include 
"InputParser.h"
#include 
<cstdlib>
#include 
<algorithm>

using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

InputParser::InputParser(
char* input, char* szOptrTable)
: input(input), szOptrTable(szOptrTable)
{

}

InputParser::
~InputParser()
{

}

vector
<char> InputParser::GetValNames()
{
    vector
<char> vec;
    
int i = 0;
    
while (this->input[i] != 0)
    {
        
// judge if input[i] is a letter
        if (
                
!('A' <= input[i] && input[i] <= 'Z'&&
                
!('a' <= input[i] && input[i] <= 'z')
            )
        {
            i
++;
            
continue;
        }


        
// judge if input[i] is not a operator
        if (GetIndex(input[i]) != -1)
        {
            i
++;
            
continue;
        }

        
// insert the letter.
        bool flag = false;
        
for (int j = 0; j < vec.size(); j++)
        {
            
if (vec[j] == input[i])
            {
                flag 
= true;
                
break;
            }
        }
        
if (!flag) vec.push_back(input[i]);
        
// sort the vector.
        sort(vec.begin(), vec.end());
        
// increment.
        i++;
    }
    
return vec;
}

char* InputParser::GetCharArr()
{
    
return this->input;
}

这样,InputParser 就算完成了。