| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 399 人关注过本帖
标题:请教个问题,多谢
只看楼主 加入收藏
ToMoRRoWa
Rank: 1
来 自:保密
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-4-17
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:1 
请教个问题,多谢
// 计算器00.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
图片附件: 游客没有浏览图片的权限,请 登录注册

#include <cctype>
#include <cstdlib>

using std::cin;
using std::cout;
using std::endl;

void eatspace( char* StrE ) ;
double  CarryO( char* StrE );
double term( char* StrE,int& index );
double number( char* StrE,int& index );

int _tmain(int argc, _TCHAR* argv[])
{
    const int MAX = 20;
    char Str1[MAX] = {0};
    double reasult = 0;

    for(;;)
    {
        cin.getline(Str1,MAX);

        eatspace(Str1);
   
        if( !Str1[0] )
            return 0;

        reasult = CarryO(Str1);

        cout << "Reasult = " << reasult;
    }
    _getch();
    return 0;
}
void eatspace( char* StrE )
{
    const int MAX = 20;
    char Str2[MAX] = {0};
    int index = 0;
    int index2 = 0;

    for( ; StrE[index] != '\0'; index ++ )
    if( *(StrE + index) != ' ')
    {   
        *(Str2 + index2) = *(StrE + index);
        index2++;
    }
    return;
}
double CarryO(char* StrC)
{
    double value = 0;
    int index = 0;

    value = term( StrC,index );

    for(;;)
    {
        switch( *(StrC + index++) )
        {
            case '+' :
                value += term( StrC,index );
            
            case '-' :
                value -= term( StrC,index );
               
            case ' \0 ' :
                return value;

            default :
                cout << endl
                        << "Warning!"
                        << endl;
                exit(1);
        }
    }
}

double term(char* StrT,int& index)
{
    double value = 0;

    value = number(StrT,index);
    if( *(StrT + index)  != '\0')
    {if(*(StrT + index) == '*')
    {
            value *= number(StrT,index);
            return value;
    }
        if(*(StrT +index) == '/')
        {
            value /= number(StrT,index);
            return value;
        }
    }
        return value;
}

double number(char* StrT,int& index)
{
    double value = 0.0;
        

    while(isdigit(*(StrT + index) ) )
        {   
            value = 10 * value + ( *(StrT + index++)  - '0');
        }
    if(*(StrT + index) != '.')
        return value;
        double count = 1;

        while((isdigit(*(StrT + index))))
图片附件: 游客没有浏览图片的权限,请 登录注册

        {
            count *= 0.1;
            value = value + count * (*(StrT + index++) - '0');
            
            return value;
        }
   
}
搜索更多相关主题的帖子: 计算器 应用程序 include double number 
2010-06-16 01:44
chen3bing
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:507
专家分:104
注 册:2008-11-12
收藏
得分:10 
什么问题?
2010-06-17 15:53
快速回复:请教个问题,多谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015477 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved