![]() |
#2
jmchang2010-07-17 16:35
|

#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(void)
{
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;
}
试下优化玩玩... #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(void)
{
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;
}