Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1042901
  • 博文数量: 254
  • 博客积分: 10185
  • 博客等级: 上将
  • 技术积分: 2722
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-25 15:04
文章存档

2011年(8)

2009年(1)

2008年(31)

2007年(214)

分类: C/C++

2007-09-17 00:23:54

3.1在3.0基础上,修正了负数输入的错误,并且增加了更多的智能表达式判断功能。

3.0在2.0基础上,完全改为面向对象(C++)编程,并且增加了一些判断。

同时,增加了实用功能:计算log以2为底的对数值,希望大家喜欢!

转载请您保留版权:尘封の觉醒 技术博客:http://cstech.cublog.cn

为了方便您研究学习,特打包提供下载(VS2005):

文件: StackCalculator.rar
大小: 319KB
下载: 下载


StackCalculator.cpp

// StackCalculator.cpp : 定义控制台应用程序的入口点。

//


#include "stdafx.h"

#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <float.h>
using namespace std;

#include "CsStack.h"
#include "CalculatorOpt.h"

enum Calculate_State{OK,FALSE,QUIT};

void quitCalculate()
{
    printf("\n\t →成功退出←\n\n CS堆栈科学计算器期待您的再次使用!\n\n");
    getch();
}

Calculate_State doCalculate()
{
    CSStack<double> stackSZ;
    CSStack<char> stackZF;
    CalculatorOpt<double> calculatorOpt;

    char temp_opt;
//Temporary Operation Saver.

    double temp_number;
//Temporary Number Saver.

    double temp_prefix,temp_postfix;
//Temporary Opt Prefix(Number),Postfix(Number) Saver.

    double temp_final;
//Temporary Midway Final Saver.

    double final;
//The Final Calculate Result.

    char temp_c;
//Temporary Get One Char.

    char convert_str2double[100];
//Use For Convert String To Double.

    char restore_restchar[100];
//Use For Restore The Rest Chars.

    int judge=0;
//The Currently Digit Saver.


    stackZF.push('=');
    printf("\n请您输入算术表达式:");
    temp_c=getchar();
//Get The First Char.

    if((temp_c=='+' || temp_c=='-' || temp_c=='*' || temp_c=='/' || temp_c=='=' || temp_c=='^' || temp_c==')' || temp_c==10 || temp_c==13) && judge==0)
    {
        if(temp_c==13 ||temp_c==10)
        {
            judge=0;
            return FALSE;
        }
        else
        {
            printf("\n\t →出错啦←\n");
            printf("| 原因:首位不能是运算符。 |\n");
            printf("| 详细:您首位出现了运算符“%c”|\n",temp_c);
            judge=0;
            gets(restore_restchar);
//Restore The Rest Chars.

            return FALSE;
        }
    }
    judge++;
    stackZF.getTop(temp_opt);
// Get the Top Element Of the StackZF.

    while(temp_c != '=' || temp_opt != '=')
    {
        if(calculatorOpt.isOpt(temp_c))
// Judge "temp_c" is Opt or not.

        {
            switch(calculatorOpt.compareOpt(temp_opt,temp_c))
// Return The PRI State: ">" , "<" or "=".

            {
            case '<':
//Top Element PRI < temp_c.

                stackZF.push(temp_c);
                temp_c=getchar();
                stackZF.getTop(temp_opt);
                break;
            case '=':
//Shuck Off The "( )" And Calculate "temp_final".

                stackZF.pop(temp_opt);
                stackZF.getTop(temp_opt);
                if(temp_opt=='s'|| temp_opt=='l'|| temp_opt=='c'|| temp_opt=='g'|| temp_opt=='t'|| temp_opt=='o'|| temp_opt=='f' || temp_opt=='e')
//Extend Calculator

                {
                    stackSZ.pop(temp_final);
                    stackZF.pop(temp_opt);
                    stackSZ.push(calculatorOpt.extendOpt(temp_opt,temp_final));
                    stackZF.getTop(temp_opt);
                    temp_c=getchar();
                    break;
                }
                else
                {
                    temp_c=getchar();
                    break;
                }
            case '>':
//Pop From the stackZF And stackSZ and Do Calculate.

                if(temp_opt=='+'||temp_opt=='-'||temp_opt=='*'||temp_opt=='/'||temp_opt=='^')
                {
                    stackZF.pop(temp_opt);
                    stackSZ.pop(temp_postfix);
                    stackSZ.pop(temp_prefix);
                    temp_final=calculatorOpt.standardOpt(temp_prefix,temp_opt,temp_postfix);
//Calculate Out The temp_final.

                    stackSZ.push(temp_final);
                    stackZF.getTop(temp_opt);
                    break;
                }
                else
                {
                    break;
                }
            }
        }
        else if((temp_c>='0' && temp_c<='9') || temp_c=='.')
//Combination The Number(Double)

        {
            int i=0;
            do
            {
                convert_str2double[i]=temp_c;
                i++;
                temp_c=getchar();
            }while((temp_c<='9' && temp_c>='0') || temp_c=='.');
            convert_str2double[i]=0;
            temp_number=atof(convert_str2double);
//Use The Function atof() to Convert A String Var To A Double One.

            stackSZ.push(temp_number);
        }
        else if(temp_c=='q' ||temp_c=='Q')
//If Input "q" Or "Q" Then QUIT.

        {
            return QUIT;
        }
        else
        {
            printf("\n\t →出错啦←\n");
            printf("| 原因:表达式输入格式错误。 |\n");
            if((temp_c<'0' || temp_c>'9') && temp_c!=10)
            {
                printf("| 详细:包含非法字符“%c…”。 |\n",temp_c);
            }
            else
            {
                printf("| 详细:表达式结束缺少“=”。 |\n");
            }
            if(temp_c==13 ||temp_c==10)
            {
                judge=0;
                return FALSE;
            }
            else
            {
                judge=0;
                gets(restore_restchar);
//Restore The Rest Chars.

                return FALSE;
            }
        }
    }
    stackSZ.getTop(final);
    if(_isnan(final))
    {
        printf("\n您输入的表达式不正确!",final);
    }
    else
    {
        printf("\n计算结果(保留15位小数):%15.15f",final);
    }
    stackZF.clearStack();
    stackSZ.clearStack();
    return OK;
}

void _tmain(int argc, _TCHAR* argv[])
{
    printf(" 〓〓 欢迎使用CS堆栈计算器 Ver 3.1 beta (C++) 〓〓〓〓〓〓〓〓〓〓〓〓〓 \n");
    printf(" 〓〓 程序设计:北京信息工程学院 信息与计算科学专业 05141班 曹硕 〓〓 \n");
    printf(" 〓★目前支持★ 加、减、乘、除、指数、正余弦、正余切及对数、复合运算 〓 \n");
    printf(" 〓★独特功能★ 人性化交互界面、可多次进行运算、自动检错报错机制 〓 \n");
    printf(" 〓★三角函数★ 正弦函数:s(),余弦函数:c(),正切函数:t(),余切函数:o() 〓 \n");
    printf(" 〓★对数函数★ 以e为底自然对数:e(),以10为底对数:g(),以2为底对数:l() 〓 \n");
    printf(" 〓★输入格式★ 输入“计算表达式=”按回车后立即出结果(精确15位小数) 〓 \n");
    printf(" 〓★重要说明★ 负数请表示为例:f(1)为-1,不可与三角、对数函数嵌套使用〓 \n");
    printf(" 〓★操作说明★ 程序自带多次使用功能,若您想中途退出,请输入Q回车 〓 \n");
    printf(" 〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓 Revision At (2007-10-09) 〓〓〓 \n");
    Calculate_State cstate = OK;

    char ctrl_redo;
//Control To REDO The Calculate.

    char restore_restchar[100];
//Use For Restore The Rest Chars.

    while(1)
    {
        cstate = doCalculate();
        switch(cstate)
        {
        case OK:
            printf("\n\n您是否还要继续计算其他表达式?(按Y键继续)\n");
            ctrl_redo=getch();
            if(ctrl_redo == 89 || ctrl_redo == 121)
//If Input "y" Or "Y" Then Continue.

            {
                gets(restore_restchar);
                continue;
            }
            else
            {
                quitCalculate();
                return;
            }
            break;
        case FALSE:
            continue;
        case QUIT:
            quitCalculate();
            return;
        }
    }
}

CalculatorOpt.h

由于篇幅有限,请您下载附件进行学习研究。

CsStack.h
 
由于篇幅有限,请您下载附件进行学习研究。
阅读(2867) | 评论(6) | 转发(0) |
0

上一篇:I/O流的常用控制符

下一篇:什么是DirectX?

给主人留下些什么吧!~~

savior19872008-07-06 00:58:01

请您用VisualStudio打开

chinaunix网友2008-06-23 08:52:07

这算什么?

chinaunix网友2008-06-23 08:51:36

回复 | 举报