| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 501 人关注过本帖
标题:我弄了个计算机的程序,但是不知道哪出错了,希望能获得帮助,谢谢.
只看楼主 加入收藏
ToBeOOP
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:106
专家分:181
注 册:2010-7-8
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
我弄了个计算机的程序,但是不知道哪出错了,希望能获得帮助,谢谢.
程序代码:
// 计算器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;
    int count = 0;

    for( ; count < MAX; count++ )
        Str2[count] = StrE[count];
    for( ; *(StrE + index)!= '\0'; index2 ++ )
    {
        if( *(Str2 + index2) != ' ')
        {   
            *(StrE + index) = *(Str2 + index2);
            index++;
        }

    }
    return;
}
double CarryO(char* StrE)
{
    double value = 0;
    int index = 0;

    value = term( StrE,index );

    for(;;)
    {
        switch( *(StrE + index++) )
        {
            case ' \0 ' :
                return value;
           
            case '+' :
                value += term( StrE,index );
           
            case '-' :
                value -= term( StrE,index );
           
            default :
                cout << endl
                        << "Warning!"
                        << endl;
                _getch();
                exit(1);
        }
    }
}

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

    value = number(StrE,index);
    while( (*(StrE +index) == '*') || (*(StrE + index) == '/'))
    {
        if(*(StrE + index) == '*')
        {
            index++;
            value *= number(StrE,index);
        }
        if(*(StrE +index) == '/')
        {
            index++;
            value /= number(StrE,index);
        }
        return value;
    }
}

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

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

        while((isdigit(*(StrE + index))))
        {
            count *= 0.1;
            value = value + count * (*(StrE + index++) - '0');
           
            return value;
        }
}
运行出错
搜索更多相关主题的帖子: 计算机 
2010-07-11 11:04
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
收藏
得分:10 
程序代码:
#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 main()
{
    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 << endl;
    }
    _getch();
    return 0;
}
void eatspace( char* StrE )
{
    const int MAX = 20;
    char Str2[MAX] = {0};
    int index = 0;
    int index2 = 0;
    int count = 0;

    for( ; count < MAX; count++ )
        Str2[count] = StrE[count];
       
    for( ; *(StrE + index)!= '\0'; index2 ++ )
    {
        if( *(Str2 + index2) != ' ')
        {  
            *(StrE + index) = *(Str2 + index2);
            index++;
        }

    }
    return;
}

double CarryO(char* StrE)
{
    double value = 0;
    int index = 0;
    int id = 0;

    value = number( StrE,index );
    index++; 

 
    for(int i = 0; i != strlen(StrE); ++i)   ////switch( *(StrE + index++) )第一步没找好运算符的话。。。直接就会退出
    {      
        switch( *(StrE + i) )
        {
            case '+' :
                value += number( StrE,index );
                id++;
                break;
          
            case '-' :
                value -= number( StrE,index );
                id++;
                break;
               
            case '*' :
                value *= number( StrE,index );
                id++;
                break;
               
            case '/' :
                value /= number( StrE,index );
                id++;
                break;
        }
       
        if(id == 1)
        {
            break;
        }
    }
   
    if(i == strlen(StrE))
    {
        cout<<"没有运算符"<<endl;
    }
   
    if(id == 1 && i == strlen(StrE)-1)
    {
        cout<<"没有第二个操作数"<<endl;
    }
   
    return value;
}

double number(char* StrE,int& index)
{
    double value = 0.0;
       
    while(isdigit(*(StrE + index) ) )   //这里用for比较好,要不第一步进不了循环。如123+ggg456是进不去的
    {  
        value = 10 * value + ( *(StrE + index++)  - '0');
    }
       
    if(*(StrE + index) != '.')   //还有return要保证函数各种情况都要有返回值。
      {
        double count = 1;   

        while((isdigit(*(StrE + index))))   //小数点的处理不知道怎么弄得,可能我搞错了。
        {
            count *= 0.1;
            value = value + count * (*(StrE + index++) - '0');
        }
      }
     
    return value;
}

改得差不多,才发现原来lz想实现多则运算,我还以为*/的那个函数多余,给删除掉了,与+-的合并了
2010-07-11 14:14
ToBeOOP
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:106
专家分:181
注 册:2010-7-8
收藏
得分:0 
多谢啦...哈哈...
2010-07-11 16:26
ToBeOOP
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:106
专家分:181
注 册:2010-7-8
收藏
得分:0 
多则运算我就自己解决吧...
2010-07-12 08:22
快速回复:我弄了个计算机的程序,但是不知道哪出错了,希望能获得帮助,谢谢.
数据加载中...
 
   



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

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