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

📄 doubledata.cpp

📁 嵌入式Qt下的一个计算器源码
💻 CPP
字号:
/******************************************************************************** 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 <qstring.h>#include "doubledata.h"#include "engine.h"// Data typeDoubleData::DoubleData(): Data() {};void DoubleData::set(double d) {    dbl = d;    edited = false;    formattedOutput = formattedOutput.sprintf("%13.13f", dbl);        int point = formattedOutput.indexOf('.');    if (point > 11)    {           QRegExp reg = QRegExp("[1..9]+");        if (point > 12)            systemEngine->setError(eSurpassLimits);        else if ( point == 12 && formattedOutput.mid(13).contains(reg) ) {            systemEngine->setError(eSurpassLimits);        }    }    formattedOutput.truncate(12);    //remove trailing zeros if decimal point is present    if (formattedOutput.indexOf('.') > -1){        int max = formattedOutput.length();        int i = max - 1;        while (formattedOutput.at(i) == '0')            i--;        formattedOutput.remove(++i, max-i);                if (formattedOutput.at( i-1 ) == '.')            formattedOutput.remove( i-1 , 1 );    }        if (!strcmp(formattedOutput.toLatin1(),"nan")) { // No tr	systemEngine->setError(eNotANumber);    } else if (!strcmp(formattedOutput.toLatin1(),"inf")) { // No tr	systemEngine->setError(eInf);    } else if (!strcmp(formattedOutput.toLatin1(),"-inf")) { // No tr	systemEngine->setError(eNegInf);    }}double DoubleData::get() { return dbl; }bool DoubleData::push(char c, bool commit) {    if (edited && formattedOutput.length() >= 12)	return false;    // Allow zero to be input as a value, but only once    if (formattedOutput == "0" && c == '0')	return !edited;    //when +/- is pressed while no number has been entered    //return to !edited mode    if (formattedOutput == "0" && edited)         edited = !edited;    QString tmpString = formattedOutput;    if (!edited) {	if (c == '.') 	    tmpString = QString("0");	else	    tmpString.truncate(0);	// Dont change the value of edited on the test run	if (commit)	    edited = true;    }    tmpString.append(QChar(c));    bool ok;    double tmp = tmpString.toDouble(&ok);    if (ok) {	if (commit) {	    formattedOutput = tmpString;	    dbl = tmp;	}    } else	qDebug("Wrong character pushed");    return ok;}bool DoubleData::del() {    if (!edited)	return true;    if (formattedOutput.length() == 1) {	formattedOutput.truncate(0);	formattedOutput.append("0");	edited = false;	dbl = 0;        return true;    } else {	QString tmpString = formattedOutput;	tmpString.truncate(formattedOutput.length()-1);	bool ok;	double tmp = tmpString.toDouble(&ok);	if (ok) {	    formattedOutput = tmpString;	    dbl = tmp;	} else            return true;    }    return false;}void DoubleData::clear() {    dbl = 0;    formattedOutput.truncate(0);    formattedOutput.append("0");    edited = false;}

⌨️ 快捷键说明

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