Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,287 people online right now. Registration is fast and FREE... Join Now!




I need HELP~ C calculator

 
Reply to this topicStart new topic

I need HELP~ C calculator

minamjun11
2 Jun, 2008 - 12:23 AM
Post #1

New D.I.C Head
*

Joined: 28 May, 2008
Posts: 3

i Used Passing

Both function main () I would like to put in


#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h>

int token; void error();
void getToken();
void match(char);
int expr();
int term();
int factor();
int power();
int number();
int digit();



int main()
{
int result;

getToken();
result = expr();
if(token == '\n')
printf("The result is: %d\n", result);
else
error();
}

void error()
{
printf("parse error\n");
exit(1);
}

void getToken()
{
while((token = getchar()) == ' ');
}

void match(char c)
{
if(token == c)
getToken();
else
error();
}

int expr() //expr -> term { '+' | '-' term }
{
int result = term();
while(token == '+' || token == '-')
{
switch(token)
{
case '+' :
match('+');
result += term();
break;

case '-' :
match('-');
result -= term();
break;
}
}
return result;
}

int term() //term -> factor { '*' | '/' | '%' factor }
{
int result = factor();
while(token == '*' || token == '/' || token == '%')
{
switch(token)
{
case '*' :
match('*');
result *= term();
break;
case '/' :
match('/');
result /= term();
break;
case '%' :
match('%');
result %= term();
break;
}
}
return result;
}

int factor() //factor -> power { '^' factor }
{
int result = power();
while(token == '^')
{
match('^');
result = (int)pow((double)result, (double)factor());
}
return result;
}

int power() //power -> '(' expr ')' | number
{
int result;
if (token == '(')
{
match('(');
result = expr();
match(')');
}
else
result = number();
return result;
}

int number() //number -> digit { digit }
{
int result = digit();
while(isdigit(token))
result = 10 * result + digit();
return result;
}

int digit() //digit -> '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
{
int result;
if(isdigit(token))
{
result = token - '0'; match(token);
}
else
error();

return result;
}

User is offlineProfile CardPM
+Quote Post

joske
RE: I Need HELP~ C Calculator
2 Jun, 2008 - 12:29 AM
Post #2

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
what is your question/problem?

(and: can you please put your code inside code tags?)
User is offlineProfile CardPM
+Quote Post

minamjun11
RE: I Need HELP~ C Calculator
2 Jun, 2008 - 01:49 AM
Post #3

New D.I.C Head
*

Joined: 28 May, 2008
Posts: 3

look forward to;long for

i wish.........
add main function and all function


and so
I Am Asian
My English leaves much to be desired
User is offlineProfile CardPM
+Quote Post

joske
RE: I Need HELP~ C Calculator
2 Jun, 2008 - 04:41 AM
Post #4

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
You already have a main function: int main(){...}. Do you mean how to add functions like sqrt(), sin(), cos(), etc?

Maybe the following (c++) example code for an expression parser can help you?
http://speqmath.com/tutorials/expression_p..._cpp/index.html

(and there are lots of examples findable on the internet)
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: I Need HELP~ C Calculator
2 Jun, 2008 - 05:10 AM
Post #5

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
I'm not too sure what your problem is...

Your program compiles and runs *as far as I can see* absolutely fine. 0 errors and 0 warnings.

Can you expand on your problem a little?
User is offlineProfile CardPM
+Quote Post

minamjun11
RE: I Need HELP~ C Calculator
2 Jun, 2008 - 07:44 AM
Post #6

New D.I.C Head
*

Joined: 28 May, 2008
Posts: 3

I Have 9 function.

void function
error
get token
macht

int function
expr
term
factor
power
number
digit


i want one function(main) program.

All function..... put together
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: I Need HELP~ C Calculator
2 Jun, 2008 - 08:42 AM
Post #7

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Simple: just copy the contents of the functions into the main where it calls them.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 01:01PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month