| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 556 人关注过本帖
标题:有个问题想问下...
只看楼主 加入收藏
ToBeOOP
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:106
专家分:181
注 册:2010-7-8
结帖率:100%
收藏
已结贴  问题点数:15 回复次数:4 
有个问题想问下...
程序代码:
#include <iostream>
#include <conio.h>
#include <cctype>

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

void eatspace(char* Str);  //用于取消在输入时输入的空格.
char FirstP(char* Str);  //处理一级运算.
char SecondP(char* Str,int& index);  //处理二级运算.
char number(char* Str,int& index); //将字符串中的数字组合起来.

int_main(void)
{
    const int MAX = 20;
    char Str[MAX] = {0};
    double reasult = 0;

    for(;;)
    {
        cin.getline(Str,MAX);  //输入字符串.

        eatspace(Str);  //调用eatspace函数.
   
        if( !Str[0] )  // 注意:( !Str )等价于 ( Str == 0 ).
            return 0;

        reasult = FirstP(Str);  //处理一级运算,在FirstP函数中包含了SecondP函数一级以及number函数.

        cout << "Reasult = " << reasult; //输出结果
    }
    _getch();
    return 0;
}
void eatspace(char* Str)  //创建eatspace函数.
{
    int a = 0;
    int b = 0;

    while( *(Str + a ) = *(Str + b++ ) != '\0' )
    {
        if( *( Str + a ) != '\0' )
            a++ ;
    }
    return;
}
char FiretP(char* Str)
{
    int index = 0;
    double value = 0.0;
   
    value = SecondP(Str,index);

    for( ; *(Str + index) != '\0'; )
    {
        switch( *(Str + index++) )
        {
            case '+' :
                value += SecondP(Str,index);
                break;
            case '-'  :
                value -= SecondP(Str,index);
                break;
            default :
                cout << endl
                        << "Warning!"
                        <<endl;
        }
    }
        return value;
}
char Second(char* Str,int& index)  //创建SecondP函数.
{
    double value = 0.0;

    value = number(Str,index);
   
    while( *(Str + index) == '*' || *(Str + index) == '/' )
    {
        if( *(Str + index) == '*' )
        {
            index++;
            value *= *(Str + index);
        }
        else
            if( *(Str +index) == '/' )
            {
                index++;
                value /= *(Str +index);
            }
    }
    return value;
}
char number(char* Str,int& index)  //创建number函数.
{
    double value = 0.0;

    while( isdigit( *( Str + index) ) )
        value += 10 * value + ( *(Str + index++) - '0' );
    if( *(Str + index) == '.' )
    {
        index++;
        double lessen = 0.1;

        while( isdigit( *(Str + index) ) )
        {
            value += lessen * ( *(Str + index++) - '0' );
            lessen *= lessen;
        }
    }
    return value;
}
1>c:\users\deathway\documents\visual studio 2008\projects\计算器(多则运算)\计算器(多则运算)\no.1.cpp(15) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
为什么会这样?该如何解决,谢谢!
2010-07-15 16:23
ToBeOOP
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:106
专家分:181
注 册:2010-7-8
收藏
得分:0 
自己顶一下,哪位好心帮帮忙...
2010-07-15 16:48
rainbow1
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:277
专家分:839
注 册:2010-5-19
收藏
得分:15 
试着把int_main中的下划线换成空格。
2010-07-15 18:40
ToBeOOP
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:106
专家分:181
注 册:2010-7-8
收藏
得分:0 
rian..我试了你的方法,可以.顺便再问个问题.我运行了修改后的代码

#include <iostream>
#include <conio.h>
#include <cctype>
#include <cstdlib>

using namespace std;

void eatspace(char* Str);  //用于取消在输入时输入的空格.
double FirstP(char* Str);  //处理一级运算.
double SecondP(char* Str,int& index);  //处理二级运算.
double number(char* Str,int& index); //将字符串中的数字组合起来.

int main()
{
    const int MAX = 20;
    char Str[MAX] = {0};
    double reasult = 0.0;

    for(;;)
    {
        cin.getline(Str,sizeof Str);  //输入字符串.

        eatspace(Str);  //调用eatspace函数.
   
        if( !Str[0] )  // 注意:( !Str )等价于 ( Str == 0 ).
            return 0;

        reasult = FirstP(Str);  //处理一级运算,在FirstP函数中包含了SecondP函数一级以及number函数.

        cout << "Reasult = " << reasult; //输出结果
    }
    _getch();
    return 0;
}
void eatspace(char* Str)  //创建eatspace函数.
{
    int a = 0;
    int b = 0;

    while( *(Str + a ) = *(Str + b++ ) != '\0' )
    {
        if( *( Str + a ) != '\0' )
            a++ ;
    }
    return;
}
double FirstP(char* Str)
{
    int index = 0;
    double value = 0.0;
   
    value = SecondP(Str,index);

    for( ; *(Str + index) != '\0'; )
    {
        switch( *(Str + index++) )
        {
            case '+' :
                value += SecondP(Str,index);
                break;
            case '-'  :
                value -= SecondP(Str,index);
                break;
            default :
                cout << endl
                        << "Warning!"
                        << endl;
        }
    }
        return value;
}
double SecondP(char* Str,int& index)  //创建SecondP函数.
{
    double value = 0.0;

    value = number(Str,index);
   
    while( *(Str + index) == '*' || *(Str + index) == '/' )
    {
        if( *(Str + index) == '*' )
        {
            index++;
            value *= *(Str + index);
        }
        else
            if( *(Str +index) == '/' )
            {
                index++;
                value /= *(Str +index);
            }
    }
    return value;
}
double number(char* Str,int& index)  //创建number函数.
{
    double value = 0.0;

    while( isdigit( *( Str + index) ) )
        value += 10 * value + ( *(Str + index++) - '0' );
    if( *(Str + index) == '.' )
    {
        index++;
        double lessen = 0.1;

        while( isdigit( *(Str + index) ) )
        {
            value += lessen * ( *(Str + index++) - '0' );
            lessen *= lessen;
        }
    }
    return value;
}
不过运行后一直显示Waring,,能帮找下原因吗?
2010-07-15 19:43
ToBeOOP
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:106
专家分:181
注 册:2010-7-8
收藏
得分:0 
程序代码:
#include <iostream>
#include <conio.h>
#include <cctype>

using namespace std;

void eatspace(char* Str);  //用于取消在输入时输入的空格.
double FirstP(char* Str);  //处理一级运算.
double SecondP(char* Str,int& index);  //处理二级运算.
double number(char* Str,int& index); //将字符串中的数字组合起来.

int main()
{
    const int MAX = 20;
    char Str[MAX] = {0};

    for(;;)
    {
        cin.getline(Str,MAX);  //输入字符串.

        eatspace(Str);  //调用eatspace函数.
   
        if( !Str[0] )  // 注意:( !Str )等价于 ( Str == 0 ).
            return 0;

        cout << "Reasult = " << FirstP(Str); //输出结果
    }
    _getch();
    return 0;
}
void eatspace(char* Str)  //创建eatspace函数.
{
    int a = 0;
    int b = 0;

    while( (*(Str + a ) = *(Str + b++ )) != '\0' )  // 注意:不能写成 *(Str + a ) = *(Str + b++ ) != '\0'
    {
        if( *( Str + a ) != ' ' )
            a++ ;
    }
    return;
}
double FirstP(char* Str)
{
    int index = 0;
    double value = 0.0;
   
    value = SecondP(Str,index);

    for( ; *(Str + index) != '\0'; )
    {
        switch( *(Str + index++) )
        {
            case '+' :
                value += SecondP(Str,index);
                break;
            case '-'  :
                value -= SecondP(Str,index);
                break;
            default :
                cout << endl
                        << "Warning!"
                        << endl;
        }
    }
        return value;
}
double SecondP(char* Str,int& index)  //创建SecondP函数.
{
    double value = 0.0;

    value = number(Str,index);
   
    while( (*(Str + index) == '*' ) || (*(Str + index) == '/') )
    {
        if( *(Str + index) == '*' )
        {
            index++;
            value *= number(Str,index);
        }
        else
            if( *(Str +index) == '/' )
            {
                index++;
                value /= number(Str,index);
            }
    }
    return value;
}
double number(char* Str,int& index)  //创建number函数.
{
    double value = 0.0;

    while( isdigit( *( Str + index) ) )
        value = 10 * value + ( *(Str + index++) - '0' );
    if( *(Str + index) == '.' )
    {
        index++;
        double lessen = 0.1;

        while( isdigit( *(Str + index) ) )
        {
            value = lessen * ( *(Str + index++) - '0' );
            lessen *= lessen;
        }
    }
    return value;
}
完成了!哈哈高兴,谢谢帮助!
2010-07-15 21:02
快速回复:有个问题想问下...
数据加载中...
 
   



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

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