Advertisement
Shailrshah

Rational Number Operations using Pointers

Apr 20th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct rational{
  4.         int numerator;
  5.         int denominator;
  6. }*n3 = NULL; //n3 is the final answer
  7. struct rational *insert(struct rational *n, int a, int b){
  8.         n = (struct rational*) malloc(sizeof(struct rational)); //memory is allocated for inserting values
  9.         n->numerator = a;
  10.         n->denominator = b;
  11.         return n;
  12. }
  13. void add(struct rational *n1, struct rational *n2){ //Formula for addition is given
  14.         int a = (n1->numerator)*(n2->denominator) + (n1->denominator)*(n2->numerator);
  15.         int b = n1->denominator * n2->denominator;
  16.         n3 = insert(n3, a, b); //resulting number will have a numerator and denominator
  17. }
  18. void multiply(struct rational *n1, struct rational *n2){ //formula for multiplication is given
  19.         int a = n1->numerator * n2->numerator;
  20.         int b = n1->denominator * n2->denominator;
  21.         n3 = insert(n3, a, b);  //resulting number will have a numerator and denominator
  22. }
  23. int main(){
  24.         int a, b, choice;
  25.         struct rational *n1 = NULL, *n2 = NULL; //good practict to initialize pointers to NULL
  26.         start: printf("Enter the numerator of first number: ");
  27.         scanf("%d", &a);
  28.         printf("Enter the denominator of first number: ");
  29.         scanf("%d", &b);
  30.         n1 = insert(n1, a, b); //n1 will contain a numerator and a denominator
  31.         printf("Enter the numerator of second number: ");
  32.         scanf("%d", &a);
  33.         printf("Enter the denominator of second number: ");
  34.         scanf("%d", &b);
  35.         n2 = insert(n2, a, b); //n2 will also contain numerator and denominator
  36.         while(1){
  37.                 printf("\nThe first number is %d/%d", n1->numerator, n1->denominator);
  38.                 printf("\nThe second number is %d/%d", n2->numerator, n2->denominator);
  39.                 printf("\n1.Addition 2.Multiplication 3.Different numbers 4.Exit: ");
  40.                 scanf("%d",&choice);
  41.                 switch(choice){
  42.                         case 1: add(n1, n2);
  43.                                     printf("Addition is %d/%d", n3->numerator, n3->denominator);
  44.                                     break;
  45.                         case 2: multiply(n1, n2);
  46.                                     printf("Multiplication is %d/%d", n3->numerator, n3->denominator);
  47.                                     break;
  48.                         case 3: goto start;
  49.                         case 4: return 0;
  50.                 }
  51.         }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement