Huawei Machine Test-arithmetic (array resolution)

Source: Internet
Author: User
Tags integer division

Title: Arithmetic

Problem Description:
Enter a simple arithmetic expression string that contains only single digits to evaluate the value of the expression
Note:
3.1. expression contains only +,-, *,/arithmetic, without brackets
3.2, the expression value contains only single digits (0-9), and does not appear as a divisor of 0 cases
3.3, to consider subtraction according to the usual arithmetic the calculation priority
3.4, Division uses integer division, that is, only the integer portion of the result of the division operation is preserved. Like 8/3=2. The input expression guarantees that no 0 occurs as a divisor condition
3.5, the input string must be in accordance with the test instructions legal expression, which includes only numeric characters and arithmetic character, in addition to any other characters, there will be no calculation overflow situation


Required implementation functions:
int calculate (int len,char *expstr)
"Input" int len: string length; char *expstr: expression string;
"Output" none
"Return" Calculation results


Example:
1) Input: char *expstr = "1+4*5-8/3"
function return: 19
2) Input: char *expstr = "8/3*3"
function return: 6


Problem Analysis:

The topic requirement is simple, just to calculate single-digit arithmetic, and without parentheses. The main study of the use of the stack, the implementation of the use of two auxiliary stacks, one to hold the operand, one to store the operator. Calculated based on the precedence of the operator. This topic in the Min "data structure" this book will be a detailed introduction to the implementation process.


Basic ideas:

Two arrays store operands and operators, scan the input string, and when encountered * and/, the side calculates the side of the storage, the result of the calculation into the array of operands, when encountering the + and-number is only stored not calculated. When all the data is stored, because the operation is left with the-+ operation, because +-is the exchange principle, so in a stack-like way, starting from the end of the array, and each time the result of the operation to store the operand array, the result of the last arithmetic is the operand of the remaining array.


Specific implementation:

#include <iostream>using namespace std;int calculate (int len,char *expstr) {int result = 0;   int* datas = new Int[len];  Simulate the operand stack char* opers = new Char[len] with an array;    Operation with array simulation Fu Yi int datatop =-1;    int opertop =-1;    memset (datas,0,len*sizeof (int));    memset (opers,0,len*sizeof (char)); for (int i=0;i<len;++i) {if (Expstr[i] >= ' 0 ' && expstr[i] <= ' 9 ') datas[++datatop] = expstr[i]-' 0 '; if ( Expstr[i] = = ' + ' | | Expstr[i] = = '-' | | Expstr[i] = = ' * ' | | Expstr[i] = = '/') {char oper = expstr[i];if (oper = = ' * ' | | oper = = '/') {int num1 = Datas[datatop--];int num2 = expstr[++i]-' 0 ' if (oper = = ' * ') num1*= num2;else if (oper = = '/') num1/=num2;datas[++datatop] = NUM1;} Elseopers[++opertop] = Expstr[i];}} while (opertop!=-1) {char oper = opers[opertop--];int NUM1 = Datas[datatop--];int num2 = datas[datatop--];if (oper = = ' + ') nu M2+=num1;else if (oper = = '-') num2-=num1;datas[++datatop]=num2;} result = Datas[datatop];return result;} int main () {char a[100];while (cin>>a) {int len = StrlEn (a); Cout<<calculate (Len,a) <<endl;} Cout<<endl;return 0;}


Test results, may not be thoughtful, welcome to check the leak:



Huawei Machine Test-arithmetic (array resolution)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.