Thread: I need only one function in that programm,can anyone write? (scanFraction)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    I need only one function in that programm,can anyone write? (scanFraction)

    Code:
    #include<stdio.h>
    #include<stdlib.h>   /*provides function abs*/
    
    /*Function prototypes*/
    
    
    void scanFraction(int *nump, int *denomp);
    
    
    char getOperator(void);
    
    
    void addFractions (int n1, int d1,int n2,int d2,int *nAnsp, int *dAnsp);
    
    
    
    void multiplyFractions(int n1, int d1,int n2,int d2,int *nAnsp, int *dAnsp);
    
    
    int findGcd (int n1,int n2);
    
    
    void reduceFraction (int *nump, int *denomp);
    
    
    void printFraction (int num, int denom);
    
    
    
    int
    main(void)
    {
    	int n1, d1;								/*Numerator,deminator of first fraction		   */							
    	int n2, d2;								/*Numerator,deminator of second fraction	   */	
    	char op;								/*Arithmatic operator +-* or /                 */
    	char again;								/*y or n depending on user's desire to continue*/
    	int nAns, dAns;							/*Numerator ,deminarator of answer			   */
    
    
    
    //While.......
    
    	do {
    		//Gets a fraction problem
    		scanFraction(&n1, &d1);
    		op=getOperator();
    		scanFraction(&n2, &d2);
    
    
    		//Computes the result 
    
    		switch (op)  {
    
    		case '+':
    			addFractions ( n1,  d1, n2, d2, &nAns,  &dAns);
    			break;
    		
    		
    		case '-':
    			addFractions ( n1,  d1, -n2, d2, &nAns,  &dAns);
    			break;
    
    		case '*':
    			multiplyFractions( n1,  d1, n2, d2, &nAns,  &dAns);
    			break;
    
    		case '/':
    
    
    			multiplyFractions( n1,  d1, n2, d2,&nAns,  &dAns);
    		}
    	
    	    reduceFraction ( &nAns,  &dAns);
    
    
    		//Display 
    		printf("\n");
    		printFraction(n1,d1);
    		printf("%c",op);
    		printFraction(n2,d2);
    		printf("=");
    		printFraction(nAns,dAns);
    
    		//Ask user about ....
    
    
    		printf("\nDo another problem?(y/n)>");
    		scanf("%c",&again);
    	
    		} while (again=='y'	||	again=='Y');
    		return(0);
    
    
    }
    //*****************************************
    
    
    
    
    //void 
    //scanFraction(int *nump, int *denomp)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //*******************************************
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    char 
    getOperator(void)
    
    {
    	char op;
    
    	printf("Enter an aritmetic operator (+,-,*,or /)\n>");
    
    	for (scanf("%c", &op);
    		op !='+'	&&	op !='-'	&&
    		op !='*'	&&  op !='/';
    
    		scanf("%c",&op))  {
    	if (op!= '\n')
    			printf("%c invalid,reenter operator (+,-,*,/)\n>",op);
    
    		}
    
    		return(op);
    }
    
    
    
    
    
    
    		
    void 
    addFractions (int n1, int d1,int n2,int d2,int *nAnsp, int *dAnsp)
    
    {
    	int denom,numer,
    		signFactor;
    
    	//Finds a common denominator
    	denom=d1 * d2;
    
    	//Computes numerator
    	numer= n1 * d2 + n2 * d1;
    
    	if (numer * denom >=0)
    			signFactor=1;
    	else
    			signFactor=-1;
    
    
    	numer=signFactor * abs (numer);
    	denom=abs (denom);
    
    
    	*nAnsp=numer;
    	*dAnsp=denom;
    }
    
    
    
    
    void 
    multiplyFractions(int n1, int d1,int n2,int d2,int *nAnsp, int *dAnsp)
    
    {
    	//Displays....
    
    	printf("\nEntering multiplyFractions with\n");
    	printf("n1=%d, d1=%d, n2=%d, d2=%d\n",n1,d1,n2,d2);
    
    	*nAnsp=1;
    	*dAnsp=1;
    
    
    
    }
    int 
    findGcd (int n1,int n2)
    
    {
    	int gcd;
    
    	printf("\n Entering findGcd with n1=%d, n2=%d\n",n1,n2);
    
    
    	printf("gcd of %d and %d?>", n1,n2);
    	scanf("%d",&gcd);
    
    
    
    	printf("findGcd returning %d\n",gcd);
    	scanf("%d", &gcd);
    
    	return(gcd);
    
    }
    
    
    
    void 
    reduceFraction (int *nump, int *denomp)
    
    {
    	int gcd;
    
    	gcd=findGcd (*nump, *denomp);
    
    
    	*nump= *nump /gcd;
    	*denomp=*denomp / gcd;
    
    }
    
    
    
    
    
    void 
    printFraction (int num, int denom)
    
    {
    	printf("%d/ %d",num,denom);
    }

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Might want to check out the homework policy. No one will write it for you but we can help you write it yourself.
    Don't quote me on that... ...seriously

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int 
    findGcd (int n1,int n2)
    
    {
    	int gcd;
    
    	printf("\n Entering findGcd with n1=&#37;d, n2=%d\n",n1,n2);
    
    
    	printf("gcd of %d and %d?>", n1,n2);
    	scanf("%d",&gcd);
    
    
    
    	printf("findGcd returning %d\n",gcd);
    	scanf("%d", &gcd);
    
    	return(gcd);
    
    }
    You overwrite the value of the variable gcd with another scanf() call. I think that perhaps you need to return two values; in that case, you'll need to make the parameters pointers and do it that way.

    Also, if that function returns zero, you'll probably get a division by zero error in reduceFraction().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM