Пример #1
0
void CCalculator::Run(string SufExpression, CString& CResult)
{
	char ch;
	bool T_or_F;
	double newOperand;
	int i = 0;
	string Operand;
	ch = SufExpression[i];
	while (ch != '=')
	{
		switch (ch)
		{
		case '+':
		case '-':
			i++;
			if (isdigit(SufExpression[i]))
			{
				Operand = Operand + ch;		//将一个操作数存入string类型的Operand中
				ch = SufExpression[i];	//读取下一个字符
				if (ch == ',')		//读到","说明一个double型的操作数处理完,将其放入double型的栈中
				{
					newOperand = strtod(Operand.c_str(), NULL);
					AddOperand(newOperand);		//将操作数放入栈中
					Operand = "\0";
					ch = SufExpression[++i];		//读取下一个字符
				}
				break;
			}
			else
				i--;
		case '*':
		case '/':
		case '%':
			T_or_F = DoOperator(ch);
			if (T_or_F == false)
				CResult = "错误的表达式,请重新输入!";
			ch = SufExpression[++i];	//读取下一个字符
			if (ch == ',')
				ch = SufExpression[++i];	//忽略“,”读取下一个字符
			break;
		default:
			Operand = Operand + ch;		//将一个操作数存入string类型的Operand中
			ch = SufExpression[++i];	//读取下一个字符
			if (ch == ',')		//读到","说明一个double型的操作数处理完,将其放入double型的栈中
			{
				newOperand = strtod(Operand.c_str(), NULL);
				AddOperand(newOperand);		//将操作数放入栈中
				Operand = "\0";
				ch = SufExpression[++i];		//读取下一个字符
			}
			break;
		}
	}
	double result;	//最后的计算结果
	Num.Pop(result);
	CResult.Format(_T("%.9g"), result);
}
Пример #2
0
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void IndirectCallPromotion::MergeReturnedValues(BlockList& callBlocks, 
                                                Block* continuationBlock,
                                                CallInstr* instr) {
    // If the call returns 'void' or the result is not used
    // there are no values to merge.
    if(instr->IsVoid() || (instr->HasDestinationOp() == false)) {
        return;
    }

    auto unit = callBlocks[0]->ParentFunction()->ParentUnit();
    auto& references = unit->References();

    // Create the 'phi' that merges the returned values.
    auto phiResultOp = Temporary::GetTemporary(instr->ResultOp()->GetType());
    auto phiInstr = PhiInstr::GetPhi(phiResultOp, callBlocks.Count());
    continuationBlock->InsertInstructionFirst(phiInstr);

    // Now add the incoming operands.
    for(int i = 0; i < callBlocks.Count(); i++) {
        auto beforeGoto = callBlocks[i]->LastInstruction()->PreviousInstruction();
        auto callInstr = beforeGoto->As<CallInstr>();
        
        DebugValidator::IsNotNull(callInstr);
        DebugValidator::IsNotNull(callInstr->ResultOp());

        auto blockRef = references.GetBlockRef(callBlocks[i]);
        phiInstr->AddOperand(callInstr->ResultOp(), blockRef);
    }

    // The original returned value is replaced by the 'phi' result.
    instr->ResultOp()->ReplaceWith(phiResultOp);
}
Пример #3
0
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void RecursionElimination::CreateParameterPhis() {
    DebugValidator::IsNotNull(oldEntryBlock_);
    DebugValidator::IsNotNull(newEntryBlock_);

    // Create a 'phi' instruction for each argument of the function,
    // in the order in which they appear. These 'phi's are used
    // to "collect" the values that come from tail calls.
    int capacity = tailCalls_.Count() + 1;
    auto& refs = funct_->ParentUnit()->References();
    auto newEntryRef = refs.GetBlockRef(newEntryBlock_);

    for(int i = 0; i < funct_->ParameterCount(); i++) {
        auto resultOp = Temporary::GetTemporary(funct_->ParameterTypes()[i]);
        auto phiInstr = PhiInstr::GetPhi(resultOp, capacity);
        oldEntryBlock_->InsertInstructionFirst(phiInstr);
        parameterPhis_.Add(phiInstr);

        // Add as the first incoming operand the parameter
        // used on the first iteration.
        phiInstr->AddOperand(funct_->GetParameter(i), newEntryRef);
    }
}
Пример #4
0
bool CLogicLikeCal::Calculate(double &value){

	int num = m_strExp.length();
	char ch;
	for(int i = 0;i<num;i++){
		ch = m_strExp[i];
		if(isdigit(ch)){
			AddOperand(ch);
		}	
		else{
			SingleCalculate(ch);
		}
	}
	
	ch = m_sOperand.top();
	int index = ch - '0';
	value = m_vecvalue[index];
	//cout<<endl<<"结果为:"<<m_vecvalue[index];
	

	return true;
}