字符串四则运算(计算器)

来源:互联网 发布:java版微信格式 编辑:程序博客网 时间:2024/05/14 08:55

今天就弄了这个,郁闷····

bool CCalculate::isOperChar ( char ch ) /* 判断是否是加减乘除操作符 */{if ( ch == '+' || ch == '-' || ch == '*' || ch == '/' )return true;return false;}/////////////////////////////////////////////float CCalculate::operCal(CString str){int i, k;CCalculate obj;/************************************************************//* 通过遍历字符串,在所有表示负号(非减号)的数字前加'0' */int len = str.GetLength ();for ( i = 0; i < len; i++ )  {if ( obj.isOperChar(str[i]) ){if ( i == 0 || ( ! isdigit(str[i-1]) && str[i-1] != ')' ) ){str.Insert( i, "0" );len++; i++;}}}/************************************************************//* 将处理好的字符串表达式变为后缀表达式,用队列存储操作数以及操作符(即后缀表达式),   由于操作数可能是浮点数,需要用字符串来表示。*/node temp;               /* 节点包含一个字符串 */temp.item [0] = '#';     /* 可假设'#'优先级最低 */queue<node> operNum;     /* 储存后缀表达式的队列*/stack<node> operCh;      /* 储存操作符的栈 */operCh.push(temp);       /* 将'#'放入栈底,作为标准*//* 若第一个字符是数字,则一直搜索直至遇到操作符或者括号, 此段为一个,完整操作数 */for ( i = 0; i < len; i++ )  {if ( isdigit ( str[i] ) )       {                                for ( k = 0; i < len; i++ ){    if ( isdigit(str[i]) || str[i] == '.' )        temp.item[k++] = str[i];    else break;}    temp.item[k] = '\0';    operNum.push ( temp );}if ( i >= len ) break;   /* 若已经遍历整个字符串,则跳出循环 */    if ( str[i] == '+' || str[i] == '-' )  /* 不低于'+','-'的操作符全部入队 */{temp = operCh.top();while ( temp.item [0] != '#' && temp.item[0] != '(' ){operNum.push(temp);operCh.pop();temp = operCh.top();}temp.item[0] = str[i];temp.item[1] = '\0';operCh.push(temp);}else if ( str[i] == '*' || str[i] == '/' )  /* 不低于'*','/'优先级的操作符全部入队 */{temp = operCh.top();while ( temp.item[0] != '#' && temp.item[0] != '+' && temp.item[0] != '-' && temp.item[0] != '(' ){operNum.push(temp);operCh.pop();temp = operCh.top();}temp.item[0] = str[i];temp.item[1] = '\0';operCh.push(temp);}else if ( str[i] == '(' )   /* '('的优先级最高,直接入队,因为是先计算括号内,再括号外 */{temp.item [0] = str[i];temp.item [1] = '\0';operCh.push(temp);}else if ( str[i] == ')' )  /* 碰到'('则要一直寻找到第一个与其匹配的')',中间的操作符全部入队 */{temp = operCh.top();while ( temp.item [0] != '(' && temp.item [0] != '#' ){operNum.push ( temp );operCh.pop();temp = operCh.top();}operCh.pop();}}while ( operCh.top().item[0] != '#' ) /* 剩下的操作符全部加入队尾 */{operNum.push( operCh.top() );operCh.pop();}/************************************************************//* 对后缀表达式进行计算 */stack<float> cal;   float num1, num2, sum;while ( ! operNum.empty() ){temp = operNum.front();operNum.pop();if ( ! obj.isOperChar(temp.item[0]) ) /* 为操作数,则将字符串转换为浮点数,并入栈 */{cal.push( atof(temp.item) );continue;}if ( cal.size() <= 1 ) break;   /* 当栈里只剩下一个数字的时候,该数字即是结果 */num2 = cal.top(); cal.pop();  /* 取出两个操作数进行计算 */num1 = cal.top(); cal.pop();if ( temp.item[0] == '+' ) sum = num1 + num2;else if ( temp.item[0] == '-' ) sum = num1 - num2;else if ( temp.item[0] == '*' ) sum = num1 * num2;else if ( temp.item[0] == '/' ) sum = num1 / num2;cal.push ( sum );    /* 将计算结果放回栈中 */} return cal.top();}


原创粉丝点击