Пример #1
0
/**
 * 习题3.21,将中序表达式写成逆波兰式
 */
void RPN(SqStack &S1, SqStack &S2)
{
	char c;
	int temp;
	InitStack(S1);		//存储临时运算符
	InitStack(S2);		//存储逆波兰式
	Push(S1, '#');
	c = getchar();
	while (c != '#' || !StackEmpty(S1)) {
		if (!In(c)) {		//不是运算符,是操作数
			Push(S2, c);
			c = getchar();	//读入下一个字符
		} else {		//是运算符
			if ('(' == c) {
				Push(S1, c);
				c = getchar();	//读入下一个字符
			} else if (')' == c) {
				while (GetTop(S1) != '(') {
					Pop(S1, temp);
					Push(S2, temp);
				}
				if (GetTop(S1) == '(')
					Pop(S1, temp);
				c = getchar();
			} else {
				switch (Precede(GetTop(S1), c)) {
				case '<':
					Push(S1, c);
					c = getchar();
					break;
				case '>':
					while ('>' == Precede(GetTop(S1), c)) {
						Pop(S1, temp);
						Push(S2, temp);
					}
					Push(S1, c);
					c = getchar();
					break;
				}
			}
		}
		if ('#' == c) {
			while ('#' != GetTop(S1)) {
				Pop(S1, temp);
				Push(S2, temp);
			}
			Pop(S1, temp);
		}
	
	}
}
Пример #2
0
ElemType OperaType(void){
	//全数据为整型操作
	SqStack OPTR,OPND;//OPTR运算符栈,OPND运算栈
	InitStack(OPTR);
	Push(OPTR,'#');
	InitStack(OPND);
	int ch,x,a,b,theta;
	ch=getche();
	while(ch!='#' || GetTop(OPTR)!='#'){
		if(!In(ch)){
			Push(OPND,ch-48);
			ch=getche();
		}else{
			switch(Precede(GetTop(OPTR),ch)){
			case '<':
				Push(OPTR,ch);
				ch=getche();
				break;
			case '=':
				Pop(OPTR,x);
				ch=getche();
				break;
			case '>':
				Pop(OPTR,theta);
				Pop(OPND,b);
				Pop(OPND,a);
				Push(OPND,Operate(a,theta,b));
				break;
			}//switch
		}//if else
	}//while
	return GetTop(OPND);
}// OperaTyp
Пример #3
0
int EvaluateExpression()
{
   char ch; type e,theta,a,b;
   SqStack OPTR,OPND;    //OPTR为运算符栈,OPND为运算数栈
   StackInit(&OPTR); StackInit(&OPND);
   Push(&OPTR,'#');
   ch=getchar();
   while(ch!='#' || GetTop(&OPTR)!='#')
   {
      if(!In(ch))
      {
         Push(&OPND,ch);
         ch=getchar();
      }
      else
      {
         switch(Precede(GetTop(&OPTR),ch))
         {
            case '<':Push(&OPTR,ch); ch=getchar(); break;
            case '=':Pop(&OPTR,&e); ch=getchar(); break;
            case '>':Pop(&OPTR,&theta);Pop(&OPND,&b);Pop(&OPND,&a);
                     Push(&OPND,Operate(a,theta,b)); break;
         }
      }
   }
   return GetTop(&OPND);
}
Пример #4
0
GainType LKH::LKHAlg::Minimum1TreeCost(int Sparse)
{
    Node *N, *N1 = 0;
    GainType Sum = 0;
    int Max = INT_MIN;

    MinimumSpanningTree(Sparse);
    N = FirstNode;
    do {
        N->V = -2;
        Sum += N->Pi;
    }
    while ((N = N->Suc) != FirstNode);
    Sum *= -2;
    while ((N = N->Suc) != FirstNode) {
        N->V++;
        N->Dad->V++;
        Sum += N->Cost;
        N->Next = 0;
    }
    FirstNode->Dad = FirstNode->Suc;
    FirstNode->Cost = FirstNode->Suc->Cost;
    do {
        if (N->V == -1) {
            Connect(N, Max, Sparse);
            if (N->NextCost > Max) {
                N1 = N;
                Max = N->NextCost;
            }
        }
    }
    while ((N = N->Suc) != FirstNode);
    N1->Next->V++;
    N1->V++;
    Sum += N1->NextCost;
    Norm = 0;
    do
        Norm += N->V * N->V;
    while ((N = N->Suc) != FirstNode);
    if (N1 == FirstNode)
        N1->Suc->Dad = 0;
    else {
        FirstNode->Dad = 0;
        Precede(N1, FirstNode);
        FirstNode = N1;
    }
    if (Norm == 0) {
        for (N = FirstNode->Dad; N; N1 = N, N = N->Dad)
            Follow(N, N1);
        for (N = FirstNode->Suc; N != FirstNode; N = N->Suc)
            N->Dad = N->Pred;
        FirstNode->Suc->Dad = 0;
    }
    return Sum;
}
Пример #5
0
void makeStack(OPNDType *top1, OPTRType *top2)
{
	OPNDType	*Np, *Nq;
	OPTRType	*Tp, *Tq;
	char		ch, operator;
	char		save[20] = {0};
	int		i, f, k = 0, k1;
	double		a, b;

	PushStack2(top2, '#');		//先给运算栈低存入 # 起始标识符
	ch = getchar();
	while( 1 ){

		if( !decide(ch) )	//若 非运算符运算数 则转换成 # 结束标识符
			ch = '#';

		i = 0, f = 0, k1 = 0;	//i和f辅助存入 n 位数字 k1 辅助存入负数

		if( (ch >= '0' && ch <= '9') || ch == '.')	f = 1;
		if( (k == '#' || k == '(') && !f && ch == '-'){		//判断减号是否为负号

			ch = getchar(), k1 = 1;
			if( ch >= '0' && ch <= '9' )	f = 1;
		}
		while( (ch >= '0' && ch <= '9') || ch == '.')
			save[i++] = ch, ch = getchar();		//将字符型数字存到数组中

		if( f ){

			save[i] = '\0';
			if( k1 )
				PushStack1(top1, atof(save)*-1);	//将字符串转换成双精度
			else
				PushStack1(top1, atof(save));
			continue ;
		}
		k = ch;			//辅助存入负数
		switch(Precede(GetTop(top2), ch)){

			case 1 : return ;	//运算完毕
			case '<' :
				PushStack2(top2, ch);	ch = getchar();	break;	//进栈继续接收
			case '=' :
				PopStack2(top2, &operator);	ch = getchar();	break;	//出栈继续接收
			case '>' :			//运算 不接收
				PopStack2(top2, &operator);
				PopStack1(top1, &a), PopStack1(top1, &b);
				PushStack1(top1, Operate(b, operator, a));
				break ;
		}
		if( ch == '#' && !top2->next)
			return ;
	}
}
Пример #6
0
int EvaluateExpression(){

    int n;
    int flag;
    int c;
    char x,theta;
    int a,b;

    char OP[]="+-*/()#";
    SqStack  OPTR;
    SqStack  OPND;

    init_stack(&OPTR);      
    push(&OPTR,'#');
    init_stack(&OPND);
    flag=getNext(&c);

    get_top(OPTR, &x);
    while(c!='#' || x != '#')
    {
        if(flag == 0)
	     {
                  push(&OPND,c);
                  flag = getNext(&c);
             }        else
	{
            get_top(OPTR, &x);
            switch(Precede(x, c))
	    {
                case '<'://栈顶元素优先级低                    
                    push(&OPTR,c);
                    flag = getNext(&c);
                    break;
                case '='://脱括号并接受下一字符 
                    pop(&OPTR,&x);
                    flag = getNext(&c);
                    break;
                case '>':// 退栈并将运算结果入栈                                       
                    pop(&OPTR, &theta);
                    pop(&OPND,&b);
                    pop(&OPND,&a);
                    push(&OPND, Operate(a, theta, b));
                    break;
            }
        }
        get_top(OPTR, &x);
    }
    get_top(OPND, &c);
    return c;
}
Пример #7
0
int EvaluateExpression(){
	// P53 算法3.4
	// 算术表达式求值的算符优先算法,设OPTR和OPND分别为运算符栈和运算数栈,OP为运算符集合
	SqStack OPTR, OPND;
	InitStack(OPTR);
	bool judgePre=0;	// 判断输入的前一个字符是否为数字,用于多位数的运算
	Push(OPTR, '#');
	InitStack(OPND);
	int c, x, medi;
	OperandType theta;
	int a, b;
	c = getchar();
	while (c!='#' || GetTop(OPTR)!='#'){
		if (!In(c)){	//不是运算符则进栈
			c = c - '0';
			if (!In(GetTop(OPND)) && judgePre){
				Pop(OPND, medi);
				c += medi * 10;
			}
			Push(OPND, c);
			judgePre = 1;
			c = getchar();
		} else {
			switch (Precede(GetTop(OPTR),c)){
				case '<':
					Push(OPTR, c); 
					c = getchar();
					break;
				case '=':
					Pop(OPTR, x);
					c = getchar();
					break;
				case '>':
					Pop(OPTR, theta);
					Pop(OPND, b);
					Pop(OPND, a);
					Push(OPND, Operate(a, theta, b));
					break;
				default:
					break;
			}
			judgePre = 0;
		}
		
	}
	return GetTop(OPND);
}// EvaluateExpression
Пример #8
0
// zaradi pozadavek na casovou osu seznamu SQS
// before = true ... pred vsechny pozadavky se stejnym evTime
// before = false ... pred pozadavky s vestim evTime
void CEventNotice::Rank(bool before)
{
    CEventNotice* p;

    p = (CEventNotice*) CProcess::SQS->First();

    while ((p != NULL) && (p->evTime < evTime))
        p = (CEventNotice*) p->Suc();
    
    if (!before)
        while ((p != NULL) && (p->evTime == evTime))
            p = (CEventNotice*) p->Suc();
    
    SetName();
    if (p != NULL) Precede(p);
    else Into(CProcess::SQS);
}
Пример #9
0
SElemType EvaluateExpression() /* 算法3.4 */
{   /* 算术表达式求值的算符优先算法。设OPTR和OPND分别为运算符栈和运算数栈 */
    SqStack OPTR,OPND;
    SElemType a,b,c,x,theta;
    InitStack(&OPTR);
    Push(&OPTR,'#');
    InitStack(&OPND);
    c=getchar();
    GetTop(OPTR,&x);
    while(c!='#'||x!='#')
    {
        if(In(c)) /* 是7种运算符之一 */
            switch(Precede(x,c))
            {
            case'<':
                Push(&OPTR,c); /* 栈顶元素优先权低 */
                c=getchar();
                break;
            case'=':
                Pop(&OPTR,&x); /* 脱括号并接收下一字符 */
                c=getchar();
                break;
            case'>':
                Pop(&OPTR,&theta); /* 退栈并将运算结果入栈 */
                Pop(&OPND,&b);
                Pop(&OPND,&a);
                Push(&OPND,Operate(a,theta,b));
                break;
            }
        else if(c>='0'&&c<='9') /* c是操作数 */
        {
            Push(&OPND,c);
            c=getchar();
        }
        else /* c是非法字符 */
        {
            printf("ERROR4\n");
            exit(ERROR);
        }
        GetTop(OPTR,&x);
    }
    GetTop(OPND,&x);
    return x;
}
Пример #10
0
	//如果##配对,表达式求值完成
	while (alpa != "#" || character_1.top() != "#")
	{
		if (Punction(alpa) == 0)
		{
			stringstream ss;
			ss << alpa;          /*操作数入栈*/
			ss >> tmp;         /*将String转化为int*/
			num_1.push(tmp);
			alpa = key.front();
			key.pop();
		}
		else
		{
			/*比较栈顶操作符和新取得的操作符的优先关系*/
			int pa = Precede(character_1.top(), alpa);
			if (pa == 1) /*栈顶优先权低*/
			{
				character_1.push(alpa);
				alpa = key.front();
				key.pop();
			}
			else if (pa == 0)/*括号配对,栈顶括号弹出*/
			{
				character_1.pop();
				alpa = key.front();
				key.pop();
			}
			else if (pa == 2)/*栈顶优先权高,先弹出,计算,结果操作数入栈*/
			{
				string QAT = character_1.top();
Пример #11
0
BOOL CMatrixDoc::GetResult(const CString &data,CArrayMatrix & matrix)
{
//在这可以通过修改data字符串和m_VariablePtrList变量链表来实现
//具体方法是首先假如data是包含多个运算符的
	CString sDataString=data;
	sDataString.TrimLeft("\n");
	sDataString.TrimRight("\n");
	CString *pVar;
	CArrayMatrix result;
	bool ok;
	int VarNum=GetVariableNum(sDataString,pVar);
	if(VarNum==0) return FALSE;
	CTypedPtrList<CObList,CArrayMatrix *> tpVarList;
	if(!StringsToMatrixs(pVar,tpVarList,VarNum)) 
	{
		if(pVar!=NULL) delete []pVar;
		return FALSE;
	}
	TurnString(sDataString);
	//表达式求值
	{
		sDataString=sDataString+"#";
		CStack<TCHAR> OPTR;
		CStack<CArrayMatrix *> OPND;
		OPTR.Push('#');
		int index=0;
		TCHAR c=sDataString[index];
		int nVarIndex=0;
		while(c!=TCHAR('#')||OPTR.GetTop()!=TCHAR('#'))
		{
			if(!IsOperator(c)) 
			{
				POSITION pos=tpVarList.GetHeadPosition();
				CArrayMatrix * pt=NULL;
				for(int i=0;i<=nVarIndex&&i<tpVarList.GetCount();i++)
				{
					pt=tpVarList.GetAt(pos);
					tpVarList.GetNext(pos);
				}
				if(pt==NULL) return FALSE;
				OPND.Push(pt);
				nVarIndex++;
				index++;
				c=sDataString[index];
			}
			else
			{
				switch(Precede(OPTR.GetTop(),c))
				{
				case -1:
					{
						OPTR.Push(c);
						index++;
						c=sDataString[index];
						break;
					}
				case 0:
					{
						TCHAR x;
						OPTR.Pop(x);
						index++;
						c=sDataString[index];
						break;
					}
				case 1:
					{
						TCHAR theta;
						OPTR.Pop(theta);
						CArrayMatrix * b=NULL;
						CArrayMatrix * a=NULL;
						OPND.Pop(b);
						OPND.Pop(a);
						OPND.Push(Operate(a,theta,b,ok));
						break;
					}
				}
			}
		}
		result=*(OPND.GetTop());
	}
	

	
	//销毁tpvarlist变量里面的东西
	{
		POSITION pos=tpVarList.GetHeadPosition();
		int len=tpVarList.GetCount();
		for(int i=0;i<len;i++)
		{
			CArrayMatrix * tp=tpVarList.GetAt(pos);
			delete tp;
			tpVarList.GetNext(pos);
		}
		tpVarList.RemoveAll();
	}
	if(pVar!=NULL) delete []pVar;
	if(!ok) return FALSE;
	matrix=result;
	return TRUE;
}
Пример #12
0
void main( )
{  
char express[30], num[10], theta, tp , d;//express[30]用来读入一个表达式
    double a ,b , result, tps;          //num[10]用来存放表达式中连接着的数字字符
int t, i, j;
int position = 0;//表达式读到的当前字符
Initstack(OPND); 
Initstack(OPTR); Push(OPTR , '=');
printf("input 'b' to run the calc:\n");
scanf("%c" , &d);
getchar();
while(d == 'b')
{
printf( "请输入表达式( 可使用+、-、*、/、(、)、= ): " ) ;
gets(express);
while(express[position] != '='||Gettop(OPTR , tp) != '=' )
{
if(!Is_op(express[position])) 
{ //用两个计数器t和j实现浮点数的读取
t = position;j=0;
while(!Is_op(express[position]))
{
position++;
}
for(i = t; i<position ; i++ )
{//把表达式中第t到position-1个字符赋给num[10]
num[j] = express[i];
j++;
}
Push(OPND , atof(num));
    memset(num,0,sizeof(num));//将num[10]清空
}
else
{
switch(Precede(isp(Gettop(OPTR , tp)),icp(express[position])))
{
case '<':
Push(OPTR,express[position]);position++;break;
    case '=':
    Pop(OPTR , tp) ;position++;break; 
    case '>':
{
Pop(OPTR , theta) ;
        Pop(OPND , b) ; 
        Pop(OPND , a) ; 
        result = Operate(a, theta ,b);
        Push(OPND , result);break;  
}//end sase
}//end switch
}//end else
}//end while

printf("%s%8.4f\n",express,Pop(OPND,tps));

    memset(express , 0 , sizeof(express));
    position = 0;
    printf("input 'b' to run the calc again:\n");
    scanf("%c" , &d);
    getchar();
}//end while
}