Concetti Chiave
- This C program performs arithmetic operations on fractions, including addition, subtraction, multiplication, and division.
- It calculates the least common multiple (LCM) of the denominators to ensure fractions are compatible for addition and subtraction.
- Switch-case logic is used to handle different operations based on user input, simplifying the result when possible.
- The program includes a loop that continues until the user decides to exit by entering '0'.
- Helper functions like 'mcm', 'scambio', and 'Operation4' are used to modularize the code and handle specific tasks.
int leggiFRAZ();
int scambio(int*D1,int*D2);
int mcm(int D1,int D2);
int Operation4(char SCELTA,int mcm1,int N1,int D1,int N2, int D2,int*NT,int*DT,int*ND,int*DD);
int main(){
int N1,N2,D1,D2,mcm1,SCELTA,EXIT;
do{
printf("inserisci le due frazioni numeratore e denominatore\n");
scanf("%d",&N1);
scanf("%d",&D1);
scanf("%d",&N2);
scanf("%d",&D2);
mcm1=mcm(D1,D2);
printf("mcm=%d\n",mcm1);
printf("inserisci l'opreazione con i corrispettivi simboli\n");
scanf("\n\n%c",&SCELTA);
switch(SCELTA){
case '+' :{
int somma1=0,NT,DT,ND,DD,DIV;;
somma1=Operation4(SCELTA,mcm1,N1,D1,N2,D2,&NT,&DT,&ND,&DD);
for(DIV=2;DIV if((somma1%DIV==0)&&(mcm1%DIV==0)){
somma1=somma1/DIV;
mcm1=mcm1/DIV;
}
}
if(mcm1==1){
printf("la somma semplificata(RIDOTTA) e' %d\n",somma1);
}
else{
printf("la somma semplificata(RIDOTTA) e' %d/%d\n",somma1,mcm1);
}
}
break;
case '-' :{
int sottra=0,NT,DT,ND,DD,DIV;
sottra=Operation4(SCELTA,mcm1,N1,D1,N2,D2,&NT,&DT,&ND,&DD);
if(mcm1==1){
printf("la somma semplificata(RIDOTTA) e' %d\n",sottra);
}
else{
printf("la somma semplificata(RIDOTTA) e' %d/%d\n",sottra,mcm1);
}
}
break;
case '*' :{
int NT=0,DT=0,ND,DD;
Operation4(SCELTA,mcm1,N1,D1,N2,D2,&NT,&DT,&ND,&DD);
if(DT==1){
printf("la somma semplificata(RIDOTTA) e' %d\n",NT);
}
else{
printf("la somma semplificata(RIDOTTA) e' %d/%d\n",NT,DT);
}
}
break;
case '/' :{
int ND=N2,DD=D2,NT,DT;
Operation4(SCELTA,mcm1,N1,D1,N2,D2,&NT,&DT,&ND,&DD);
if(DD==1){
printf("la somma semplificata(RIDOTTA) e' %d\n",ND);
}
else{
printf("la somma semplificata(RIDOTTA) e' %d/%d\n",ND,DD);
}
}
break;
default: printf("errore\n");
}
printf("per uscire premi 0 e qualsiasi numero per continuare\n");
scanf("%d",&EXIT);
}while(EXIT!=0);
}
int mcm(int D1,int D2){
int mcm=0,r;
r=D1*D2;
while(D1!=D2){
if(D2>D1){
scambio(&D1,&D2);
}
D1=D1-D2;
}
mcm=(r/D1);
return mcm;
}
int scambio(int*D1,int*D2){
int temp;
temp=*D1;
*D1=*D2;
*D2=temp;
}
int Operation4(char SCELTA,int mcm1,int N1,int D1,int N2, int D2,int*NT,int*DT,int*ND,int*DD){
if(SCELTA=='+'){
int somma=0;
somma=somma+(((mcm1/D1)*N1)+((mcm1/D2)*N2));
return somma;
}
else
if(SCELTA=='-'){
int sottra=0;
sottra=sottra+(((mcm1/D1)*N1)-((mcm1/D2)*N2));
return sottra;
}
else
if(SCELTA=='*'){
int DIV;
*NT=*NT+(N1*N2);
*DT=*DT+(D1*D2);
for(DIV=2;DIV
if((*NT%DIV==0)&&(*DT%DIV==0)){
*NT=*NT/DIV;
*DT=*DT/DIV;
}
}
}
else
if(SCELTA=='/'){
int RECIPROCO=0,DIV;
RECIPROCO=*ND;
*ND=*DD;
*DD=RECIPROCO;
*ND=*ND*N1;
*DD=*DD*D1;
for(DIV=2;DIV
if((*ND%DIV==0)&&(*DD%DIV==0)){
*ND=*ND/DIV;
*DD=*DD/DIV;
}
}
}
}