1. ## Calc Program I made Tell me what u think

As I was reading a post it said somthing about a calc being easy to make so I sent out and made one in 30 mins tell me what you think about it if you want too. Yes I know it's basic but I had nothing better to do. I have to go let my dogs in now good bye.
And I know I shouldent of used system("cls"); but my compier would work with the other clear screen thing I forgot what that code was.

2. Its cool. It reminds me of the calc I made to when I was just starting out as a C programmer. (Oh my gosh it brings back memories of my baby, my girlfriend the apple of my eye... sob... )

Anyway, I'm tryin' out something new. Maybe you can help.

How 'bout makin' a calc that can do the math operations by just using a string input? Like this:

/*Output screen*/

Enter equation: 4 + 4 + 10 /2 * 5 - 10

It's kinda hard and I haven't even reached halfway.

3. I dont want to try that yet. But here's and update it has color now OOOOOHHHHHHh. These color functions might not work with your compiler i dont know.

4. Your going to be able to solve expressions with my calc now. Going to add it in nnnnnnnnnnnn..............ow.

5. I just added an expressions that you can do on the calc tell me what you think again. If you get an error compiling in might be this line of code

SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), 12);

The expression can only be #(y) + #(y) = x so is very basic.

6. Opps forgot the include the source sorry here it is.

7. Originally posted by billholm

Enter equation: 4 + 4 + 10 /2 * 5 - 10

It's kinda hard and I haven't even reached halfway.
i think this is what you need ;-)

3*(5+3)=24

------------------------------------------------------------------------------
------------------------------------------------------------------------------
#include <stdio.h>
#define PLUS 1
#define MUL 2
#define NUMBER 3
#define LEFT_PAR 4
#define RIGHT_PAR 5
#define INFO(x) ((x)->info)
#define LEFT(x) ((x)->left)
#define RIGHT(x) ((x)->right)
#define VALUE(x) ((x)->value)

struct t_node {int info; int value; struct t_node *left; struct t_node *right;};

/* function declarations */

int gettoken(void); /* type of the last token */
struct t_node *construct_structure(void); /* constructs the structure, binary tree */
int evaluate_structure(struct t_node *); /* evaluates the structure */
void treeprint(struct t_node *); /* prints the tree */

int tokentype, num;
char operand[50];

struct t_node *tree;

int gettoken(void) {
int c;
char *number = operand;

while ((c = getchar()) == ' ' || c == '\t' || c == '\n'); /* Skip blanks */
if (c == '+')
else if (c == '*')
else if (isdigit(c)) {
for (*number ++ = c; isdigit(c = getchar()); )
*number ++ = c;
*number = 0;
num = atoi(operand);
ungetc(c, stdin);
}
else if (c == '(')
else if (c == ')') {
return gettoken();
}
}

struct t_node *construct_structure() {
struct t_node *tp;

gettoken();
tp = (struct t_node *) malloc(sizeof(struct t_node));
if (tokentype == LEFT_PAR) {
LEFT(tp) = construct_structure();
INFO(tp) = gettoken();
RIGHT(tp) = construct_structure();
}
else if (tokentype == NUMBER) {
INFO(tp) = tokentype;
VALUE(tp) = num;
LEFT(tp) = RIGHT(tp) = NULL;
}
else {
printf("Stg wrong! \n");
exit(1);
}
return tp;
}

int evaluate_structure(struct t_node *tp) {
switch (INFO(tp)) {
case PLUS : return evaluate_structure(LEFT(tp)) + evaluate_structure(RIGHT(tp)); break;
case MUL : return evaluate_structure(LEFT(tp)) * evaluate_structure(RIGHT(tp)); break;
case NUMBER : return VALUE(tp); break;
default : printf("What ???\n"); exit(1);
}
}

void treeprint(struct t_node *p) {
if (p) {
treeprint(LEFT(p));
switch (INFO(p)) {
case PLUS : printf("+\n"); break;
case MUL : printf("*\n"); break;
case NUMBER : printf("%d\n", VALUE(p)); break;
}
treeprint(RIGHT(p));
}
}
int main() {
tree = construct_structure();
treeprint(tree);
printf("result = %d\n", evaluate_structure(tree));
return 0;
}

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

8. ## Code is not robust

hi there just saw ur calc.c
the one in which u added colour

u r using scanf for the menu options which makes it very easy for the program to crash

type abcd or any alphabet on the prompt
and see what ur code says

secondly in the division operation

u havent handled when someone tries to divide by zero

that will also crash ur code

whenever u write code expect the user of ur code to be naive

if he types alphabets instead of numbers ur code goes berzerk

use fgets instead to scan the input from the user and then use sscanf to get the integers

hope this helps

cheers

Hare Krsna

9. I thought fgets was for text???

10. CarpeDiem:
I want to test that future program with the worst possible senario and if it can't pass, then it's back to the drawing board.
It should be able to calculate MDAS method without any parentheses.

11. ## :)

how will ur program distingish teh difference between

2*3+5 and 2*(3+5)

the parantheses are a must case.
and the program i wrote here works perfectly if u enter rght data.
i dont care people who wnter wrong data

12. No no, parentheses are really important. What I meant was that if what I entered had no parentheses, I should immediately calculates in MDAS. But if there are parentheses, then the parentheses will have a higher precedence.

I'm currently working on it. When I upgrade it, I'll add a differential calculus capability.

Then I'll upgrade it into a Windows program. It's quite hard but it will be very useful.

13. >I should immediately calculates in MDAS.

It should immediately calculate in MDAS.

Stupid me I didn't even review my post.