how I can simplify fraction after do some process on it
(add, sub...)

this part of program (add fraction)

int i,j,k,m,a,b,c,d;
     char x;
     printf("Please Enter fraction number 1\n");
     scanf( "%d%c%d",&a,&x,&b);
     printf("please Enter fraction number 2\n");
     scanf("%d%c%d",&c,&x,&d);
      if (b == 0 || d == 0){
          printf("undife number\n");
          system ("pause");
          }
     else if(a == 0){
          printf("number1-number2=%d/%d\n",c,d);
          system ("pause");
          }
     else if (c == 0) {
          printf("number1-number2=%d/%d\n",a,b);
          system ("pause");
          }     
     i=a*d;
     j=b*c;
     k=i+j;
     m=b*d;
     printf("its = %d/%d\n",k,m);

Recommended Answers

All 10 Replies

any one can help ??

What are you trying to do?
Each fraction number has 3 components? (int) (char) (int)

You print a # / # are you trying to divide? but you don't divide?

Why are three of your last equations products, and one a summation?

And what's with the comparisons? In a fraction you need to avoid a divide by zero, and you're working in integer, which will result in an integer result!

this is the question what am trying to solve :
Write a c program to do some tasks related to fraction operations and others related to file processing:
The main menu will be as shown below:
1- Fraction addition
2- Fraction subtraction
3- Fraction multiplication
4- Fraction division
5- File processing
6- Exit
The result of fraction operations will be a fraction in its lowest form.
You need to use LCM (Least common multiple) and GCD (Greatest common divisor) for fraction addition, subtraction, and reduction to lowest form.
If the user chooses file processing, the program will show another menu as shown below.

1- count words in the file
2- find and display characters with their frequencies
3- search for a word in the file
4- go to main menu
5- Exit

The Exit option in both menus will terminate the program. The 3 first choices of the file processing menu will ask for the file name which will be read from the user. Option 2 will look for all characters present in the file (excluding spaces and new line characters) and put them in a string with their count. After that the program will display them with their frequency i.e. how many times they are present in the file. For option 3, the file name and the word to search for will be read from the user.

Your posted code and the problems don't match!

You need a menu driven system loop. Accept 1...6, otherwise try again.
Fraction's with integer results will require numerator/denominator data entry for the two individual values. Since it's a menu no need for an operator (char) 2nd argument, unless the menu is just a recommendation and not a requirement.

To find the denominator quickly you can merely multiply the denominators, else, write a function passing two denominators and then calculate the LCM.

File processing? Are mathematical expressions suppose to be read in from a file and not data entry?

Simultaneous posts.

Sounds like you have two tasks. Focus on one first, then the other! You can load the entire text file into memory, and write a parser to extract a word at a time, and then call a skipspace function that skips over whitespace. You can use that in one loop run to count words, or other processing.

this is what i did ?
do u mean that or what?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100

void add(); //fuanction for add two fraction
void subt();//fuanction for subtract two fraction
void multip(); //fuanction for multiplacation two fraction
void divi(); //fuanction for divition two fraction
void count_words(char words[]);
void find_char();
void search();

int main () {
    int n,d;
    char s[SIZE];
    printf("\nPleas put the number of what do u want to do from below minu:\n");
     printf("\n1-Fraction addition\n");
    printf("2-Fraction subtraction\n");
    printf("3-Fraction multiplication\n");
    printf("4-Fraction division\n");
    printf("5-File processing\n");
    printf("6-Exit\n");
    printf("Ex: if i want write 1/2 ... print 1 after that / finally 2\n ");
    printf("-----WeLcOmE ---- To ---- PrOgRaM --- 092 ---\n");
    scanf("%d",&n);
switch (n) {
       case (1):
            add();
                 
            break;
       case (2):
            subt();     
            break;
       case (3):
            multip();
            break;
       case (4) :
            divi();
            break;
       case (5):
            printf("\n\n\nPleas Enter number from the below menu\n");
            printf("1-count words in the file\n2-find and display characters with their frequencies\n3-search for a word in the file\n4-go to main menu\n5-Exit\n\n");
            scanf("%d",&d);
       
switch (d) {
       case (1):
            //count_words(s){};
            break;
       case (2):
            
            break;
       case (3):
            
            break;
       case (4):
            return main();
            break;
       case (5):
            exit (1);
            break; 
            default:
        printf("sorry wrong answer .... please write a right number\n");
        system ("pause");
         }     
                                       
            break;     
       case (6) :
            exit (1);
            break;  
            default:
                 
        printf("sorry wrong answer .... please write a right number\n");
        system ("pause");
                 }                
 

system ("puase");
return 0;
}

//-------------------------------------------------------------------------------------------------------------
void add(){
     int i,j,k,m,a,b,c,d;
     char x;
     printf("Please Enter fraction number 1\n");
     scanf( "%d%c%d",&a,&x,&b);
     printf("please Enter fraction number 2\n");
     scanf("%d%c%d",&c,&x,&d);
      if (b == 0 || d == 0){
          printf("undife number\n");
          system ("pause");
          }
     else if(a == 0){
          printf("number1-number2=%d/%d\n",c,d);
          system ("pause");
          }
     else if (c == 0) {
          printf("number1-number2=%d/%d\n",a,b);
          system ("pause");
          }     
     i=a*d;
     j=b*c;
     k=i+j;
     m=b*d;
     printf("its = %d/%d\n",k,m);
   system ("pause");
     }
          
//-------------------------------------------------------------------------------------------------------------
void subt(){
     int i,j,k,m,a,b,c,d;
     char x;
     printf("Please Enter fraction number 1\n");
     scanf( "%d%c%d",&a,&x,&b);
     printf("please Enter fraction number 2\n");
     scanf("%d%c%d",&c,&x,&d);
     if (b == 0 || d == 0){
          printf("undife number\n");
          system ("pause");
          }
     else if(a == 0){
          printf("number1-number2=-%d/%d\n",c,d);
          system ("pause");
          }
     else if (c == 0) {
          printf("number1-number2=%d/%d\n",a,b);
          system ("pause");
          }     
     i=a*d;
     j=b*c;
     k=i-j;
     m=b*d;
     if (k == 0)
        printf("number1-number2 = 0");
     else   
        printf("number1-number2 = %d/%d\n",k,m);
  
   system ("pause");
     
     }
//-------------------------------------------------------------------------------------------------------------
void multip() {
     int i,j,a,b,c,d;
     char x;
     printf("Please Enter fraction number 1\n");
     scanf( "%d%c%d",&a,&x,&b);
     printf("please Enter fraction number 2\n");
     scanf("%d%c%d",&c,&x,&d);
     i=a*c;
     j=b*d;
     if (i ==0 && j != 0 )
           printf("number1*number2 = 0\n");
     else if ( j == 0) 
           printf("sorry undefine number\n");
     else 
           printf("number1*number2= %d/%d\n",i,j);
      
     system ("pause");           
          
     }
//-------------------------------------------------------------------------------------------------------------
void divi() {
     int i,j,a,b,c,d;
     char x;
     printf("Please Enter fraction number 1\n");
     scanf( "%d%c%d",&a,&x,&b);
     printf("please Enter fraction number 2\n");
     scanf("%d%c%d",&c,&x,&d);
     i=a*d;
     j=b*c;
     if (i ==0 && j != 0 )
           printf("number1*number2 = 0\n");
     else if ( j == 0) 
           printf("sorry undefine number\n");
     else 
           printf("number1*number2= %d/%d\n",i,j);
      
     system ("pause"); 
     
     }
//-------------------------------------------------------------------------------------------------------------
/*void count_words(char words[]){
               char word[SIZE],file[SIZE],ch;
               int x=0;
               FILE *infile;
               
               puts("Enter the file name:");
               scanf("%s",file);
               
               infile=fopen(file,"r");
               
               
                    while (ch=fgets(word,SIZE,infile)!=EOF)
                    x++;
                    printf("The number of names read is %d\n",x);

               fclose(infile);
               }*/
//--------------------------------------------------------------------------------------------------------------
void find_char(){
     
     }
//--------------------------------------------------------------------------------------------------------------
void search() {
/*     char str[SIZE],found,names[SIZE];
     int i,NUM;
      printf("\nEnter a name to search in the list: ");
    gets(str);
    i = 0;
    while (!found && i<NUM) {
        if(strcmp(names[i], str) == 0)
            found = 1;
        else
          i++;
    }*/   }
//--------------------------------------------------------------------------------------------------------------

Accept a character for a menu, compare to an ASCII digit and have a default case to loop back to a menu.
Don't concatenate your strings, use separate lines so they're more readable in source code.

I still don't understand why you're accepting three values for each fraction! Is the second field a divisor symbol (/) ?

You should work on your LCD function.
Add some comments to explain what the comparisons in each math function are for! You're checking for zero but I'm missing the reason why!

the char between the two int number is just the line between numerator and denominator

You need to inplement your LCD, not only for addition, subtraction, but also for reducing final fractions for all operator solutions!
Use a numerator, denominator label that is self explanatory. Not i,j.
Use Denom check first to save extra check for numerator!

if (0 == Den)
{
}
else if (0 == Num)
{
}
else
{
}

Also put in a (-) / (-) check. If negative over negative then both numbers are positive! Easiest method is to move (-) from denomiator to numerator where it should be anyway!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.