major noob question

This is a discussion on major noob question within the C Programming forums, part of the General Programming Boards category; Hi, I'm very new to C programming but I need some help. How would I go about programming something so ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    major noob question

    Hi, I'm very new to C programming but I need some help. How would I go about programming something so that it takes 3 float numbers and 2 operators and adds them up according to PEMDAS, or basically order of operations.


    ex: 3,4,5 are my numbers and I have % and + as my operators. How would I make it display (3%4)+5 = total ?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What have you tried so far?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    I don't even understand C enough to come up with anything to try. I'm thinking it probably requires the use of a function or two but I'm not sure.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You cannot use the modulus operator with floats or doubles. Here is how you might dispaly an operation such as you have described, using integer values:
    Code:
     printf("\nThe number is: %d\n", 3 % 3 + 5);
      return 0;
    Note that I did not use brackets around the 3 % 3 - the modulus operator takes higher precedence than the binary + operator. Of course you can use brackets if it makes it clearer to you. The above code won't do much for you, so you will have to figure out what to add to it to make your program work.

    ~/
    [edit]
    Ugh - I should stay off these boards - I totally misread what it was you wanted. Its been a long day...
    [/edit]
    Last edited by kermit; 03-22-2004 at 03:20 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    C has a full range of arithmetic operators: /, *, %, +, -. You can use character input to read the operators, floating-point input to read the numbers, and then use comparisons to determine the operations. Here is a severely crippled example of one way to go about it:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      double a, b, c;
      char o1, o2;
    
      printf ( "Enter an infix expression: a op b op c: " );
      if ( scanf ( "%lf %c %lf %c %lf", &a, &o1, &b, &o2, &c ) == 5 ) {
        double result = 0.0;
        /* First part, assume only high precedence operations */
        switch ( o1 ) {
        case '/': result = a / b; break;
        case '*': result = a * b; break;
        }
        /* Second part, any operation valid */
        switch ( o2 ) {
        case '/': result /= c; break;
        case '*': result *= c; break;
        case '+': result += c; break;
        case '-': result -= c; break;
        }
        printf ( "(%.0f%c%.0f)%c%.0f = %f\n", a, o1, b, o2, c, result );
      }
    
      return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21