Thread: math problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Question math problem

    hi there im trying to make a math program
    the input is

    a/b # c/d

    # = / or + or - or *


    example

    input
    1/4 / 1/4
    output
    1
    input
    2/7 - 4/14
    output
    0
    input
    4/5 + 4/7
    output
    1 13/35
    input
    2/3 * 4/5
    output
    8/15


    this is my code for only + sign math program
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void){
        int a, b, c, d, e, f, g;
        
        scanf("%d/%d%d/%d", &a, &b, &c, &d);
        b > 0;
        d > 0;
        
        e = (a * d) + (b * c);
        f = b * d;
        
        if(e < f)
        printf("%d/%d + %d/%d = %d/%d", a, b, c, d, e, f);
        if(e > f)
        printf("%d/%d + %d/%d = %d %d/%d", a, b, c, d, e / f, e % f, f);
        if(e == f)
        printf("%d/%d + %d/%d = 1", a, b, c, d);
        
        return 1;
        }
    my question is how to replace # in input with / or * or + or -

    sorry for my bad english tq

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Just read the # as a character in between your %d/%d in scanf. The flag for character is %c. Then, depending on what character you have read, you will know what calculation you need to perform.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    hi there im trying to make a math program
    the input is

    a/b # c/d

    # = / or + or - or *

    example

    input
    1/4 / 1/4 = 0 / 0 = ?
    output
    1

    input
    2/7 - 4/14 = 0 - 0 = 0
    output
    0

    input
    4/5 + 4/7 = 0 + 0 = 0
    output
    1 13/35

    input
    2/3 * 4/5 = 0 * 0 = 0
    output
    8/15

    you have declared a,b,c,d,e as integer variables which gives output as 0 for these situations.

    had it been (5/4)/(8/3) = (1.25) / (2.666) = (1) / (2) = 0

    corrected code is

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a, b, c, d, e;
    	char hash;
        
     	printf("\n\nEnter a,b,c,d : ");   
    	scanf("%d%d%d%d", &a, &b, &c, &d);
        
    	printf("Enter # : ");	
    	
    	fflush(stdin);
    	scanf("%c",&hash);
    	
    	switch(hash)
    	{
    		case '/' :
    				e = (a/b)/(c/d);
    				break;	
    		case '*' :
    				e = (a/b)*(c/d);				
    				break;
    		case '+' :
    				e = (a/b)+(c/d);				
    				break;	
    		case '-' :
    				e = (a/b)-(c/d);				
    				break;
    		default :
    				printf("\n\nInvalid operator");
    	
    	}
    	
    	printf("\n\n   (%d/%d)%c(%d/%d) = %d \n\n",a,b,hash,c,d,e);
    
        return 0; 
    }
    thank you

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    thx man!
    now i understand

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Gurav:

    Don't ever fflush(stdin).

    Don't provide complete solutions to people who ask questions. It defeats the purpose of learning.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Don't ever fflush(stdin)

    why ?

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Because it triggers undefined behavior. Read here:

    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Help!!c problem in math
    By feelsunny in forum Tech Board
    Replies: 2
    Last Post: 10-06-2007, 03:35 AM
  3. Math Problem....
    By NANO in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-11-2002, 04:37 AM
  4. math problem
    By unixOZ in forum Linux Programming
    Replies: 4
    Last Post: 10-19-2002, 12:17 AM
  5. Little math problem
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-27-2001, 07:44 PM