Thread: confused on problem

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Unhappy confused on problem

    I have to write a simple calculator that inputs an expression with two values separated by an operator computer will print out results. Calculator should include the ^ operator for exponentiation. All calculations should be done in an appropriate function (the functions will be small) and results printed in main().

    functions and Variables:

    main()
    op -- Operator
    x, y -- Values for Calculations
    add()
    subtract()
    multiply()
    divide()
    exponentiate()
    a, b --Local variables for each function

    Output:
    Enter expression (q to quit): 34.7 + 23.5
    58.2
    Enter expression (q to quit): 5.27 * 32.6
    171.802
    Enter expression (q to quit): 1.41414 ^ 2
    1.99979
    Enter expression (q to quit): 657.80 / 0
    Can't divide by 0
    Enter expression (q to quit): 534 & 26
    Invalid operator
    Enter expression (q to quit): q


    I am having trouble with the concept of functions and how to set this problem up. Please help. Below is what I have started.


    Code:
    #include <stdio.h>
    
    /*void add()
    void subtract()
    void multiply()
    void divide()
    void exponentiate()*/
    
    
    void main(void)
    
    {	char op;
    	float x, y;
    	
    	printf("Enter expression (q to quit): ");
    	scanf("%f %c %f", &x, &op, &y);
    
    	while(
    		
    
    
    
    }

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    First thing is if you're going to read in an equation, then try an array of chars to avoid the pitfalls of user's inputting wrong data types, scan that array to get the values and the operation

    Code:
    printf("Enter the operation: ");
    fgets(char_arr, sizeof(char_arr), stdin);
    You know a few things... the first is you expect a number, then an operation, and another number. You can use functions like strchr to split the array as you require, or sscanf to parse the array according to the values that you expect.

    functions are fairly easy.

    Code:
    int getProd(int, int); /*prototype */
    
    .....
    main
    
    printf("enter a number: ");
    scanf("%d", &num1);
    
    printf("enter another number: ");
    scanf("%d", &num2);
    
    printf("%d * %d = %d", num1, num2, getProd(num1, num2));
    
    
    end main
    ....
    
    int getProd(int a, int b) /* definition */
    {
        return (a * b);
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    I really don't understand this problem and the answer I received could somene else please help me. I'm just suppose to use the functions, variable that I stated. Could someone please help.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If you are intent on using scanf() so be it, but beware of the possible problems.

    This should help:
    Code:
    switch (op)
      {
      case '*':  result=multiply(x,y);break;
      case '/':  result=divide(x,y);break;
      case '+':  result=add(x,y);break;
      case '-':  result=subtract(x,y);break;
      case '^': result=exponentiate(x,y);break
      default:  printf("No valid operator.");
      }
    The function themselves should be a piece of cake as they are just doing basic math functions;

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    13
    thanks, I'll give it a shot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM