例如,有一个CString 类型的串s,s的内容是"a=b"。
怎样才能在程序中将a=b提取出来,并且执行它,从而将b的值赋给a?
希望各位不吝赐教,感激不尽!

解决方案 »

  1.   

    这可是C++啊,C++编译后变量名信息都没有了,怎么能对a赋值?property1(路标) 是学java那种东西的吧?
      

  2.   

    to : binarier(沙子) 
         呵呵 我不懂 java 
         我是意思是 大家学过  编译原理 ,解决这样的问题应该不难。
      

  3.   

    语法分析很简单,没错,但就算分析出a和b两个变量,除非事先记录a和b,不然是没法知道它们的指针的。
        当然,如果楼主的情况是要做一个表达式计算之类的东西,而a和b又不是源程序里的变量,那实现起来没什么问。
      

  4.   

    凑热闹!
    我的一点想法:把每一个变量和一个字符串关联。
    Class V
    {
      void* p;
      char name[255];
      Class *next,*pre;
      V(void* , char*);
      ~V();
    ……
    //还有一些对链表进行操作的方法
    }
    将变量的地址放在p里,将变量名以字符串的形式保存在name里,然后将它连成一个双向链表。这样就可以通过这里链表将字符串的变量映射为变量的地址了,可能用数组更好一些。如果说错了各位见谅!
      

  5.   

    CString str = "a1=b1";
    int a1, b3;
    int a2, b3;
    int a3, b3;//以下是安全的做法
    BOOL Process(const CString&strExp)
    {
        // .. Get the left symbol strA
        CString strLeft = GetLeftSym(strExp);    // .. Get the right symbol strB
        CString strRight = GetRightSym(strExp);
         
        int *pLeft, *pRight;
        if( strLeft == 'a1' )
            pLeft = &a1;
        else if( strLeft == 'a2' )
            pLeft = &a2;
        else if( strLeft == 'a3' )
            pLeft = &a3;
        else
            pLeft = NULL;    if( strRight == 'b1' )
            pRight = &b1;
        else if( strRight == 'b2' )
            pRight = &b2;
        else if( strRight == 'b3' )
            pRight = &b3;
        else
            pRight = NULL;    if( pLeft && pRight )
        {
           *pLeft = *pRight;
           return TRUE;
        }
        return FALSE;
    }
    //以下是不安全的做法#define SYM(str1, str2) { str1 = str2; }void Process(const CString&strExp)
    {
        // .. Get the left symbol strA
        CString strLeft = GetLeftSym(strExp);    // .. Get the right symbol strB
        CString strRight = GetRightSym(strExp);
        
        SYM(strLeft, strRight);
    }