凑热闹! 我的一点想法:把每一个变量和一个字符串关联。 Class V { void* p; char name[255]; Class *next,*pre; V(void* , char*); ~V(); …… //还有一些对链表进行操作的方法 } 将变量的地址放在p里,将变量名以字符串的形式保存在name里,然后将它连成一个双向链表。这样就可以通过这里链表将字符串的变量映射为变量的地址了,可能用数组更好一些。如果说错了各位见谅!
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);
呵呵 我不懂 java
我是意思是 大家学过 编译原理 ,解决这样的问题应该不难。
当然,如果楼主的情况是要做一个表达式计算之类的东西,而a和b又不是源程序里的变量,那实现起来没什么问。
我的一点想法:把每一个变量和一个字符串关联。
Class V
{
void* p;
char name[255];
Class *next,*pre;
V(void* , char*);
~V();
……
//还有一些对链表进行操作的方法
}
将变量的地址放在p里,将变量名以字符串的形式保存在name里,然后将它连成一个双向链表。这样就可以通过这里链表将字符串的变量映射为变量的地址了,可能用数组更好一些。如果说错了各位见谅!
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);
}