注册 登录
编程论坛 C++教室

我弄了个计算机的程序,但是不知道哪出错了,希望能获得帮助,谢谢.

ToBeOOP 发布于 2010-07-11 11:04, 501 次点击
程序代码:
// 计算器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;
        }
}
运行出错
3 回复
#2
最近不在2010-07-11 14:14
程序代码:

#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想实现多则运算,我还以为*/的那个函数多余,给删除掉了,与+-的合并了
#3
ToBeOOP2010-07-11 16:26
多谢啦...哈哈...
#4
ToBeOOP2010-07-12 08:22
多则运算我就自己解决吧...
1