Thread: Calc Program I made Tell me what u think

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    41

    Talking 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.
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

  2. #2
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    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
    Answer: 23

    It's kinda hard and I haven't even reached halfway.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    41
    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.
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    41
    Your going to be able to solve expressions with my calc now. Going to add it in nnnnnnnnnnnn..............ow.
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    41
    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.
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    41
    Opps forgot the include the source sorry here it is.
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    2
    Originally posted by billholm


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

    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 == '+')
    return tokentype = PLUS;
    else if (c == '*')
    return tokentype = MUL;
    else if (isdigit(c)) {
    for (*number ++ = c; isdigit(c = getchar()); )
    *number ++ = c;
    *number = 0;
    num = atoi(operand);
    ungetc(c, stdin);
    return tokentype = NUMBER;
    }
    else if (c == '(')
    return tokentype = LEFT_PAR;
    else if (c == ')') {
    return gettoken();
    }
    else return tokentype = c;
    }

    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. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    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. #9
    Registered User
    Join Date
    May 2002
    Posts
    41
    I thought fgets was for text???
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

  10. #10
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    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.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    :)

    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. #12
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    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.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  13. #13
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225
    >I should immediately calculates in MDAS.

    It should immediately calculate in MDAS.

    Stupid me I didn't even review my post.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. little problem with a simple shell program i made.
    By keira in forum Linux Programming
    Replies: 2
    Last Post: 02-26-2008, 11:42 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM