您的位置:首页 > 编程语言

华为软件编程题:简单的四则运算

2013-08-30 22:12 447 查看
/* calculate.cpp
* 问题描述:
* 输入一个只包含个位数字的简单四则运算表达式字符串,
* 计算该表达式的值
* 注:
* 1、表达式只含 +, -, *, / 四则运算符,不含括号
* 2、表达式数值只包含个位整数(0-9),且不会出现0作为除数的情况
* 3、要考虑加减乘除按通常四则运算规定的计算优先级
* 4、除法用整数除法,即仅保留除法运算结果的整数部分。
* 比如8/3=2。输入表达式保证无0作为除数情况发生
* 5、输入字符串一定是符合题意合法的表达式,
* 其中只包括数字字符和四则运算符字符,
* 除此之外不含其它任何字符,不会出现计算溢出情况
* 要求实现函数:int calculate(int len,char *expStr)
* 【输入】
* int len: 字符串长度;
* char *expStr: 表达式字符串;
* 【输出】无
* 【返回】
* 计算结果
* 示例
* 1)输入:char *expStr = “1+4*5-8/3”
* 函数返回:19
* 2)输入:char *expStr = “8/3*3”
* 函数返回:6
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stack>

//判断运算符的优先级函数
int Precede(char a, char b)
{
switch (a)
{
case '*':
case '/':
if ((b == '+') || (b == '-'))
{
return 1;    //a的优先级高于b
}
else
{
return 0;    //a的优先级等于b
}
case '+':
case '-':
if ((b == '*') || (b == '/'))
{
return -1;    //a的优先级低于b
}
else
{
return 0;
}
default:
printf("运算符错误!\n");
break;
}
}

int Operate(int j, int k, char operater)
{
//int temp = 0;
switch (operater)
{
case '+':
return k + j;
//break;
case '-':
return k - j;
//break;
case '*':
return k * j;
//break;
case '/':
//temp = k / j;
return k / j;
//break;
default:
printf("运算错误!\n");
break;
}
}

int calculate(int len, char *expStr)
{
//表达式异常处理,注意len <= 0要加括号
if ((len <= 0 ) || (expStr == NULL))
{
printf("表达式为空!\n");
}

int i = 0, j = 0, k = 0;
std::stack<int> date;
std::stack<char> operate;

while (i < len)
{
if ((expStr[i] >= '0') && (expStr[i] <= '9'))
{
date.push(expStr[i] - '0');
i++;
}
else if ((expStr[i] == '+') || (expStr[i] == '-')
|| expStr[i] == '*' || expStr[i] == '/')
{
if (operate.empty())
{
operate.push(expStr[i]);
i++;
}
else
{
//与栈顶运算符判断优先级
switch (Precede(expStr[i], operate.top()))
{
case 0:
case -1:    //栈顶运算符优先级高
j = date.top();
date.pop();
k = date.top();
date.pop();
//date.push(Operate(j, k , expStr[i]));
date.push(Operate(j, k , operate.top()));
operate.pop();
//i++;
break;
//case 0:
case 1:        //栈运算符顶优先级低
operate.push(expStr[i]);
i++;
break;
default:
printf("优先级判断错误!\n");
break;
}
}
}
else
{
printf("表达式无法识别!\n");
break;
}
}

while (!operate.empty())
{
j = date.top();
date.pop();
k = date.top();
date.pop();
date.push(Operate(j, k , operate.top()));
operate.pop();
}

return date.top();
}

int main()
{
int len = 0;
int i= 0, input_flag = 0;

//为了提高程序的鲁棒性,写了下面一段循环
//此处关键是flushall()的使用,如果不使用
//由于键盘输入缓存的存在使得程序陷入死循环
//关于flush()参见我的下一篇博客
while (!input_flag)
{
printf("请输入表达式的长度:");
input_flag = scanf("%d", &len);
if (!input_flag)
{
printf("输入有误,仅可输入数字!\n");
//flushall();
}
flushall();
}

char *expStr = (char *)malloc(len  * sizeof(char));
input_flag = 0;
while (!input_flag)
{
printf("请输入表达式:");
for (i = 0; i < len; i++)
{
input_flag = scanf("%c", &expStr[i]);
//scanf("%c", &expStr[i]);
/*if ((i < len) && (expStr[i] == '\n'))
{
printf("长度不够,请重新输入:");
i = 0;
}*/
if (!input_flag)
{
printf("表达式输入有误!\n");
flushall();
break;
}
}
}

printf("表达式的计算结果为:%d\n", calculate(len, expStr));
return 0;
}

//#include <iostream>
//
//using namespace std;
//
//int calculate(int len, char *expStr)
//{
//    //定义操作符栈
//    struct
//    {
//        char opdate[200];
//        int top;
//    }opstack;
//
//    //定义栈操作符
//    opstack.top = -1;
//    int i = 0;    //遍历字符串的下标
//    int t = 0;    //当前后缀表达式的长度
//    char ch = expStr[i];
//    while (ch != '\0')
//    {
//        switch (ch)
//        {
//            case '+':
//            case '-':
//                while (opstack.top != -1)
//                {
//                    expStr[t] = opstack.opdate[opstack.top];
//                    opstack.top--;
//                    t++;
//                }
//                opstack.top++;
//                opstack.opdate[opstack.top] = ch;
//                break;
//            case '*':
//            case '/':
//                while (opstack.top != -1 &&
//                    (opstack.opdate[opstack.top] == '*' ||
//                    opstack.opdate[opstack.top] == '/'))
//                {
//                    expStr[t] = opstack.opdate[opstack.top];
//                    opstack.top--;
//                    t++;
//                }
//                opstack.top++;
//                opstack.opdate[opstack.top] = ch;
//                break;
//            default:
//                expStr[t] = ch;
//                t++;
//                break;
//        }
//        i++;
//        ch = expStr[i];
//    }
//
//    while (opstack.top != -1)    //将栈中所有剩余的运算符出栈
//    {
//        expStr[t] = opstack.opdate[opstack.top];
//        opstack.top--;
//        t++;
//    }
//    expStr[t] = '\0';
//
//    struct
//    {
//        int numeric[200];
//        int top;
//    }date;
//    date.top = -1;
//    i = 0;
//    ch = expStr[i];
//    while (ch != '\0')
//    {
//        if (ch >= '0' && ch <= '9')
//        {
//            date.top++;
//            date.numeric[date.top] = ch - '0';
//        }
//        else if ('+' == ch)
//        {
//            int tmp = date.numeric[date.top - 1] +
//                date.numeric[date.top];
//            date.top--;
//            date.numeric[date.top] = tmp;
//        }
//        else if ('-' == ch)
//        {
//            int tmp = date.numeric[date.top - 1] -
//                date.numeric[date.top];
//            date.top--;
//            date.numeric[date.top] = tmp;
//        }
//        else if ('*' == ch)
//        {
//            int tmp = date.numeric[date.top - 1] *
//                date.numeric[date.top];
//            date.top--;
//            date.numeric[date.top] = tmp;
//        }
//        else if ('/' == ch)
//        {
//            if (date.numeric[date.top] == 0)
//            {
//                printf("cannot be zero of the divide\n");
//                exit(1);
//            }
//            int tmp = date.numeric[date.top - 1] /
//                date.numeric[date.top];
//            date.top--;
//            date.numeric[date.top] = tmp;
//        }
//        i++;
//        ch = expStr[i];
//    }
//    return date.numeric[date.top];
//}
//
//void main()
//{
//    char expStr[] = "9/3*5";
//    printf("%s\n", expStr);
//    int result = calculate(strlen(expStr), expStr);
//    printf("%d", result);
//}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: