-->
当前位置:首页 > 题库 > 正文内容

函数题:Expression

Luz3年前 (2022-07-16)题库398
calc() is a function calculates an expression of natural numbers and zero. Only operators below are allowed in the expression:


+ - * /


*the result of / is a natural number without the decimal part.*

**Hint: **

1. atoi() is a function converts a string into an integer. For example, atoi("12") returns 12. To use this function, you should #include <stdlib.h>
2. pre-process commands like #include does not have to be at the beginning of a souce code file.

### Declaration of the function:
c
int calc(char *exp);


### Test program:
c
#include <stdio.h>

int calc(char *exp);

const int LEN = 80;

int main()
{
char line[LEN];
char fmt[LEN];
sprintf(fmt, "%%%ds", LEN-1);
scanf(fmt, line);
printf("%d\n", calc(line));
}

/* Put your function here. */


### Sample Input:

in
2+32/2-6


### Sample Output:

out
12


**Hint**

2+32/2-6 could be calculated as:

2+32/2 - 6

2 + 32/2 - 6

2 + 32 / 2 - 6





答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。