Not Yet Answered # getting an statement

Aia 1,977 Discussion Starter sa-al dubeyprateek 26 invisal 381 invisal 381 kv79 -1 ssharish2005 62 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

I am not quite sure I am understanding what you want in its totality. However it should be in these lines:

Use fgets() to obtain pertinent information from user.

Do calculation with the information.

Store calculation in variable

display the value in variable.

What's sth? What do you intent to ask the user to input?

0

i mean getting a matemathical statement from user & calculate the result for example :

6=2*3+sin(1-cos(0))=6 .the program should result 6. i hope you will help me!

0

There is no way (at least in C and C++) you can input a mathematical expression in the form of mathematical expression. You can only input a string and convert it to mathematical expression and do the calculation.

0

There is no way (at least in C and C++) you can input a mathematical expression in the form of mathematical expression. You can only input a string and convert it to mathematical expression and

do the calculation.

No easy to do that.

0

To give you some idea, I've decided to write a simple code to calculate the simple mathematic expression. It supports only a very basic math operator such as +, -, *, and /. It doesn't support (...) yet. I will try add (...) feature into this tomorrow, if I have free time.

-------------------------------------**What it can do:**

20/10+30*15

50+40-20+22

What it can't do:

(20+1)/3

(-2)*50

--------------------------------------

```
double calc(const char* exp)
{
double num[20] = {0};
char op[20];
int i=0, j, k;
do {
// Check whether this char is a number
// 48-57 is the number character range.
if (*exp > 47 && *exp < 58)
num[i] = num[i] * 10 + (*exp - 48);
// Check wheter this char is a math operator
if (*exp == '-' || *exp == '+' ||
*exp == '*' || *exp == '/')
op[i++] = *exp;
exp++;
} while(*exp != '\0');
// Divide all the number first
for(j=0; j<i; j++) {
if (op[j] == '/') {
num[j] /= num[j+1];
for(k=j; k<i; k++) {
num[k+1] = num[k+2];
op[k] = op[k+1];
}
j--;
i--;
}
}
// Then Multiple all the number
for(j=0; j<i; j++) {
if (op[j] == '*') {
num[j] *= num[j+1];
for(k=j; k<i; k++) {
num[k+1] = num[k+2];
op[k] = op[k+1];
}
j--;
i--;
}
}
//for(j=0; j<=i; j++) printf(" %f ", num[j]);
// Then Minus all the number
for(j=0; j<i; j++) {
if (op[j] == '-') {
num[j] -= num[j+1];
for(k=j; k<i; k++) {
num[k+1] = num[k+2];
op[k] = op[k+1];
}
j--;
i--;
}
}
// Then sum all the number
for(j=0; j<i; j++) {
if (op[j] == '+') {
num[j] += num[j+1];
for(k=j; k<i; k++) {
num[k+1] = num[k+2];
op[k] = op[k+1];
}
j--;
i--;
}
}
return num[0];
}
int main()
{
char* a = "20/5*2+4+7";
printf("%f", calc(a));
}
```

0

hello i have a question .how can i write a program that gets a statement from user & prints the result sth like below:

2*3+sin(1-cos(0))

please help me

What statements ,what do you want to print i can only guess ?

Try to write some code .

0

OK, what you wanted to really do is to accept a infix express and then convert them into reverse polish express and then evaluate the express.

This is achieves using stack and Q's concept in data structures. Have you done data structure before. Get a data structure book and see the concept and of Q's and stacks. Its very easy to do with those two and perhaps can use trees as well.

And google infix and reverse polish expression if you havn't heard about them before.

ssharish

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...