导航:网站首页 >
算术表达式的求解 给定一个算术表达式,通过程序求... matlab怎样算出一个表达式的最后结果
算术表达式的求解 给定一个算术表达式,通过程序求... matlab怎样算出一个表达式的最后结果
相关问题:
匿名网友:
ComlexCalcOperator.h
#ifndef __COMPLEXCALCOPERATOR_H_
#define __COMPLEXCALCOPERATOR_H_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class ComplexCalcOperator
{
public:
ComplexCalcOperator();
~ComplexCalcOperator();
int Calc(
char *lpszStr,
const int iStrLen,
float* fCalcResult );
private:
inline bool IsOperator( const char chCur ){
switch( chCur )
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '=':
return true;
default:
return false;
}
}
inline bool IsNumberStart( const char* chCur ){
int length = strlen( chCur );
if (( *chCur >= '0') && ( *chCur <= '9' )){
if ( length > 1 ){
switch( *( chCur + 1 ))
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '=':
case '#':
return true;
case '.':
if ( length > 2 ){
if (('0' <= *( chCur + 2 )) && ( '9' >= *( chCur + 2 )))
return true;
}
break;
default:
if ( '0' != *chCur ){
if (('0' <= *( chCur + 1 )) && ( '9' >= *( chCur + 1 )))
return true;
}
else
if ( IsOperator( *( chCur + 1)) || ( '#' == *( chCur + 1 )))
return true;
}
}
}
return false;
}
bool Calc( const float fLeft,
const float fRight,
const char chOp,
float& fResult );
};
#endif
ComplexCalcOperator.cpp
#include "stdafx.h"
#include
#include
#include "ComplexCalcOperator.h"
#define FLOAT_EQ( x, v ) (((( v ) - FLT_EPSILON ) < (x)) && ((x)
ComplexCalcOperator::ComplexCalcOperator()
{
}
ComplexCalcOperator::~ComplexCalcOperator()
{
}
/* 横向表示栈内操作符,纵向表示当前操作符
+ - * / ( ) #
+ 1 1 -1 -1 -1 1 1
- 1 1 -1 -1 -1 1 1
* 1 1 1 1 -1 1 1
/ 1 1 1 1 -1 1 1
( -1 -1 -1 -1 -1 0 -2
) 1 1 1 1 -2 1 1
# -1 -1 -1 -1 -1 -2 0
*/
char opc[7] = {'+', '-', '*', '/', '(', ')', '#'};
int p[7][7] = {
{ 1, 1, -1, -1, -1, 1, 1},
{ 1, 1, -1, -1, -1, 1, 1},
{ 1, 1, 1, 1, -1, 1, 1},
{ 1, 1, 1, 1, -1, 1, 1},
{-1, -1, -1, -1, -1, 0, -2},
{ 1, 1, 1, 1, -2, 1, 1},
{-1, -1, -1, -1, -1, -2, 0},
};
int PRI( const char chTop, const char chCur ){
int i = -1, j = -1;
for ( int index = 0; index < 7; ++index ){
if ( opc[index] == chTop ){
i = index;
if ( -1 != j )
break;
}
if ( opc[index] == chCur ){
j = index;
if ( -1 != i )
break;
}
}
return p[i][j];
}
bool ComplexCalcOperator::Calc( const float fLeft,
const float fRight,
const char chOp,
float& fResult ){
switch( chOp )
{
case '+':
fResult = fLeft + fRight;
break;
case '-':
fResult = fLeft - fRight;
break;
case '*':
fResult = fLeft * fRight;
break;
case '/':
if ( FLOAT_EQ( fRight, 0.0f ))
return false;
fResult = fLeft / fRight;
default:
break;
}
return true;
}
int ComplexCalcOperator::Calc( char *lpszStr,
const int iStrLen,
float* fCalcResult ){
if (( !fCalcResult ) || ( iStrLen <= 0 ) || !lpszStr )
return 0;
std::stack stackData;
std::stack stackOperator;
stackOperator.push( '#' );
char* pbackup = lpszStr;
while ( '\0' != *pbackup ){
if ( IsOperator( *pbackup ) || ( '#' == *pbackup )){
bool bflag = true;
while ( bflag && !stackOperator.empty())
{
switch( PRI( stackOperator.top(), *pbackup )){
case -1:
stackOperator.push( *pbackup );
bflag = false;
break;
case 0:
stackOperator.pop();
bflag = false;
break;
case 1: {
char chTop = stackOperator.top();
stackOperator.pop();
float fRight = stackData.top();
stackData.pop();
float fLeft = stackData.top();
stackData.pop();
float fResult = 0.0f;
if ( !Calc( fLeft, fRight, chTop, fResult ))
return 0;
stackData.push( fResult );
bflag = true;
}
break;
default:
bflag = false;
break;
}
}
++pbackup;
}
else if ( IsNumberStart( pbackup )) {
int temp1 = 0;
float temp2 = 1.0f;
float temp3 = 0.0f;
bool point = false;
do {
if ( point ){
temp2 /= 10;
temp3 += temp2 * ( *pbackup - '0' );
}
else{
temp1 *= 10;
temp1 += ( *pbackup - '0');
}
++pbackup;
if ( *pbackup == '.'){
if ( point )
return -2;
point = true;
++pbackup;
}
} while(( *pbackup >= '0') && ( *pbackup <= '9' ));
stackData.push( temp1 + temp3 );
}
else
{
return 0;
}
}
*fCalcResult = stackData.top();
return 1;
}
Test.cpp
#include
#include
#include
#include
#include
#include "ComplexCalcOperator.h"
int main(int argc, char* argv[])
{
ComplexCalcOperator clacOperator;
char aa[100];
memset( aa, 0, 100 );
scanf("%s",aa);
int length = strlen( aa );
float result = 0.0f;
aa[length] = '#';
aa[length+1] = '\0';
int ret = clacOperator.Calc( aa, length + 1, &result );
if ( !ret ) {
printf("=? (表达式含有错误指令)\n");
return 0;
}
printf("=%.2f\n", result );
return 1;
}
匿名网友:
1.算术表达式的求解 给定一个算术表达式,通过程序求...
问:基本要求: (1)从键盘输入要求解的算术表达式; (2)采用栈结构进行算术...
2.matlab怎样算出一个表达式的最后结果
答:1、如果对某个变量进行赋值计算,没有在后面加分号结束该计算,运算结果会自动以ans显示该值,如 y = 9或y = 9 x = 10; 运算结果中会自动出现ans = 9,即使并没有要求将其输出,如果是迭代运算,则ans会反复出现。 2、如果文件名由纯数字组成,...
3.Java设计题 求1+2+…+100之和,并将求和表达式与所...
答:public class Sum{ public static void main(String args[]){ int sum = 0; for(int i = 1 ;i
4.i=1,j=4,k=5 i=j=k---2,求出表达式的值和变...
答:k=5; k---2= (k--)-2 =4; 这考核了 -- 与 - 的运算符顺序。 k--的--是运算后生效,也就是k-- 最后 = 4,但当前表达式中依然是5; 所以 i=j=k(5旧值)-2=3; SO k=4,j=3,i=3;
5.c语言中宏替换时先求出实参表达式的值,然后再带入...
问:求高手解答啊!!!
6.C语言 求这2个表达式的结果和详细解释
问:要求:求出表达式的值和变量i k j 的值(初值分别为i 1 j 4 k 5) 1:i=j...
7.excel中 第一个单元格为计算公式表达式,后面一个...
答:如图操作 选择B1单元格,插入---名称---字义 定义名称: ABC 引用位置: =evaluate(a1) 确定 在B1单元格中输入 =ABC
8.高等数学,求高阶导数,已经把y的表达式算出来了,...
答:这个我会 考研常考 我记得给你回答过吧 稍等
9.已知二叉树的前缀表达式为ABCDE,中缀表达式为BDCE...
问:已知二叉树的前缀表达式为ABCDE,中缀表达式为BDCEA,后缀表达式怎么求...
10.c语言实现:问题描述:给定一个算术表达式,通过程...
答:typedef struct{ char *text; char key; void (*func)(); }ItemType; void menu(ItemType *,int); void menu(ItemType *item,int n) { int a,b; char ch; for(a=0;a20||b
问题推荐