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))

6
Contributors
7
Replies
8
Views
10 Years
Discussion Span
Last Post by ssharish2005

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?

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!

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.

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.

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));
}

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))

What statements ,what do you want to print i can only guess ?
Try to write some code .

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.