⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 engine.cpp

📁 嵌入式Qt下的一个计算器源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 2000-2006 TROLLTECH ASA. All rights reserved.**** This file is part of the Phone Edition of the Qtopia Toolkit.**** Licensees holding a valid license agreement from Trolltech or any of its** authorized distributors may use this file in accordance with** the License Agreement provided with the Licensed Software.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for**   information about Trolltech's Commercial License Agreements.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.********** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <qtopia/qtopiaapplication.h>#include "engine.h"#include "doubleinstruction.h"#ifdef ENABLE_FRACTION#include "fractioninstruction.h"#endif#ifdef ENABLE_INTEGER#include "integerinstruction.h"#endif// Bracesclass iBraceOpen:public Instruction {public:    iBraceOpen():Instruction() {	name = "Open brace"; // No tr	precedence = 0;	displayString = "("; // No tr	argCount = 0;    };    ~iBraceOpen(){};    void eval(){ systemEngine->incBraceCount(); };};class iBraceOpenImpl:public Instruction {public:    iBraceOpenImpl():Instruction() {	name = "Open brace impl"; // No tr	precedence = 1;	displayString = "("; // No tr	argCount = 0;    };    ~iBraceOpenImpl(){};    void eval(){};};void Engine::openBrace () {    if (state == sError)	return;#ifdef QTOPIA_PHONE    if (!Qtopia::mousePreferred())        // prevents crash when x-y*(...) is entered => calc is not entirely correct        if (state == sAppend)	    return;#endif    pushInstruction("Open brace"); // No tr    if (state != sError)	changeState(sStart);}void Engine::closeBrace () {    if (braceCount) {	if (state == sStart) // this might be wrong here...	    executeInstructionOnStack("Factory"); // No tr	doEvalStack(0,true);	//braceCount--;    }}// Engine classEngine::Engine():QObject() {    // Variable initialisation    state = sAppend;    changeResetState(drNone);    mem = recoveredDStack = 0;    kDesc = 0;    lcd = 0;    memMark = kMark = 0;    braceCount = previousInstructionsPrecedence = 0;    currentType = wantedType = "NONE"; // No tr    // Register the common instructions    // System instructions - null, open/close braces    Instruction *da = new Instruction();    registerInstruction(da);    da = new iBraceOpen();    registerInstruction(da);    da = new iBraceOpenImpl();    registerInstruction(da);    if (Qtopia::mousePreferred()) {        // Factory        da = new iDoubleFactory();        registerInstruction(da);        // Normal instructions with full precedence        da = new iAddDoubleDouble();        registerInstruction(da);        da = new iMultiplyDoubleDouble();        registerInstruction(da);        da = new iSubtractDoubleDouble();        registerInstruction(da);        da = new iDivideDoubleDouble();        registerInstruction(da);        da = new iDoubleCopy();        registerInstruction(da);        da = new iDoubleNegate();        registerInstruction(da);#ifdef ENABLE_FRACTION        da = new iNegateFractionFraction();        registerInstruction(da);#endif#if !defined(QTOPIA_PHONE) && defined(ENABLE_SCIENCE)        da = new iDoublePow();        registerInstruction(da);        da = new iDoubleExp();        registerInstruction(da);        da = new iDoubleSinDeg();//Sin        registerInstruction(da);        da = new iDoubleSinRad();        registerInstruction(da);        da = new iDoubleSinGra();        registerInstruction(da);        da = new iDoubleCosDeg();//Cos        registerInstruction(da);        da = new iDoubleCosRad();        registerInstruction(da);        da = new iDoubleCosGra();         registerInstruction(da);        da = new iDoubleTanDeg();//Tan        registerInstruction(da);        da = new iDoubleTanRad();        registerInstruction(da);        da = new iDoubleTanGra();        registerInstruction(da);        da = new iDoubleASinDeg();//ASin        registerInstruction(da);        da = new iDoubleASinRad();        registerInstruction(da);        da = new iDoubleASinGra();        registerInstruction(da);        da = new iDoubleACosRad();//ACos        registerInstruction(da);        da = new iDoubleACosDeg();        registerInstruction(da);        da = new iDoubleACosGra();        registerInstruction(da);        da = new iDoubleATanDeg();//ATan        registerInstruction(da);        da = new iDoubleATanRad();        registerInstruction(da);        da = new iDoubleATanGra();        registerInstruction(da);        da = new iDoubleLog();        registerInstruction(da);        da = new iDoubleLn();        registerInstruction(da);        da = new iDoubleOneOverX();        registerInstruction(da);        da = new iDoubleFactorial();        registerInstruction(da);        da = new iDoubleSquareRoot();        registerInstruction(da);        da = new iDoubleCubeRoot();        registerInstruction(da);        da = new iDoubleXRootY();        registerInstruction(da);        da = new iDoubleSquare();        registerInstruction(da);#ifdef ENABLE_FRACTION        da = new iFractionCopy();        registerInstruction(da);        da = new iFractionFactory();        registerInstruction(da);        da = new iConvertDoubleFraction();        registerInstruction(da);        da = new iAddFractionFraction();        registerInstruction(da);        da = new iSubtractFractionFraction();        registerInstruction(da);        da = new iMultiplyFractionFraction();        registerInstruction(da);        da = new iDivideFractionFraction();        registerInstruction(da);        da = new iConvertFractionDouble();        registerInstruction(da);#endif#ifdef ENABLE_INTEGER        da = new iConvertIntDouble();        registerInstruction(da);#endif#endif    }}Engine::~Engine() {    while (!dStack.isEmpty())        delete dStack.pop();    dStack.clear();    while (!iStack.isEmpty())        delete iStack.pop();    iStack.clear();    delete mem;    while (!list.isEmpty())        delete list.takeLast();};void Engine::registerInstruction(Instruction *i) {     Instruction *it;    for (int c = 0; c < list.count(); c++) {	it = list.at(c);	if (it->name == i->name 		&& it->type == i->type		&& it->retType == i->retType)	    return;    }    list.append(i);}Instruction * Engine::resolve(QString name) {    // Create a shortlist of instructions with the same name    QList<Instruction*> shortList;    Instruction *it = 0;    int c = 0;    for (; c < list.count(); c++) {	it = list.at(c);	if (it->name == name) 	    shortList.append(list.at(c));    }    // No instructions by that name have been found    if (!shortList.count()) {	return resolve("NULL"); // No tr    }	// Should reuse "Factory" with a parameter?    if (name == "Convert") { // No tr	// Exact match	for (c = 0; c < shortList.count(); c++) {	    it = shortList.at(c);	    if (it->type == currentType && it->retType == wantedType)		return it;	}    } else {        if (wantedType != currentType)            currentType = wantedType;        	// Exact match	for (c = 0; c < shortList.count(); c++) {	    it = shortList.at(c);	    if (it->type == currentType && it->retType == currentType)		return it;	}	// Dont match return type to currentType	for (c = 0; c < shortList.count(); c++) {	    it = shortList.at(c);	    if (it->type == currentType )		return it;	}	// Dont match type to currentType	for (c = 0; c < shortList.count(); c++) {	    it = shortList.at(c);	    return it;	}    }    // Fail    return new Instruction();}void Engine::evaluate() {    if (!Qtopia::mousePreferred())        // this could go in doEvalStack but its more efficient here        if (!iStack.isEmpty() 		&& ((*iStack.top() == "EvaluateLine") || braceCount > 0)) // No tr	    return;    if (state == sStart)	executeInstructionOnStack("Factory"); // No tr    doEvalStack();    if (Qtopia::mousePreferred())        braceCount = 0;}void Engine::doEvalStack(int p,bool inbrace) {    if (state == sError || iStack.isEmpty())	return;    evalStack(p,inbrace);    if (!Qtopia::mousePreferred()) {        if (braceCount == 0)    	    iStack.push(new QString("EvaluateLine")); // No tr        if (braceCount > 0 && inbrace)	    --braceCount;    }    if (state != sError)	changeState(sAppend);    emit(stackChanged());}void Engine::evalStack(int p,bool inbrace) {    if (state != sError) {#ifdef QTOPIA_PHONE	QStack<QString*> tmpIStack;#endif	// could be more efficient and only resolve i once	Instruction *i;	while (!iStack.isEmpty ()		&& state != sError#ifdef QTOPIA_PHONE		&& *iStack.top() != "EvaluateLine" // No tr#endif		&& (p <= resolve(*iStack.top())->precedence)) {	    // Pop the next instruction	    QString *iString = iStack.pop();	    i = resolve(*iString);            if (Qtopia::mousePreferred())                 delete iString;#ifdef QTOPIA_PHONE            else	        tmpIStack.push(iString);#endif	    // Stop at the open brace	    if (i->name == "Open brace impl" && inbrace) { // No tr		i->eval();                if (Qtopia::mousePreferred())                     return;                else {#ifdef QTOPIA_PHONE		    //--braceCount;		    delete tmpIStack.pop(); // delete open brace instraction#endif		    return;                }	    }	    // Compare precedence with the next instruction on the stack	    if (!iStack.isEmpty ()) {		// Recurse into next instruction if necessary		int topPrec = resolve(*iStack.top())->precedence;		if ((p && p <= topPrec) || (!p && i->precedence <= topPrec)) {		    QStack<Data*> holdingStack;		    for (int c = 1;c < i->argCount;c++)			holdingStack.push(dStack.pop());		    evalStack(p,inbrace);		    for (int c = 1;c < i->argCount;c++)			dStack.push(holdingStack.pop());		}	    }	    // Evaluate 	    i->eval();#ifdef QTOPIA_PHONE            if (!Qtopia::mousePreferred()) {                if (!braceCount && !dStack.isEmpty()){                    Data *top = dStack.pop();                    for (int c = 0;c < i->argCount;c++)                        dStack.push(tmpDStack.pop());                    dStack.push(top);                } else {                    for (int c = 0;c < i->argCount;c++)                        delete tmpDStack.pop();                    delete tmpIStack.pop();                }            }#endif	}#ifdef QTOPIA_PHONE        if (!Qtopia::mousePreferred())    	    while (!tmpIStack.isEmpty())	        iStack.push(tmpIStack.pop());#endif    }}// Resetvoid Engine::dualReset() {    if (drs == drHard)	hardReset();    else	softReset();}void Engine::softReset() {    if (state == sStart && previousInstructionsPrecedence) {        if (!iStack.isEmpty())            delete iStack.pop();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -