Sto avendo grosse difficoltà con quanto segue che ho bisogno di fare per un incarico:
a. Dichiarare una struttura dati che contiene un numero razionale.
b. Scrivi f'xns che farà +, -, *,/numeri razionali.
Tutti i f'xns devono passare 3 parametri, ciascuno dei quali punta a una struttura dati del tipo che ho dichiarato nella parte a; 2 dei parametri = operandi, 3 = risultato.
c. Scrivi un f'xn che accetta un puntatore alla struttura dati come parametro e restituisce il GCD del numero. & denom.
d. Usa il tuo f'xn dalla parte c per scrivere un f'xn che ridurrà una frazione (numero razionale) ai minimi termini. Passa in un puntatore alla frazione e ottieni la frazione modificata dal f'xn.
e. Scrive le funzioni di input e output in modo che un utente possa inserire una frazione nel formato 1/5, ad esempio.C Fraction Arithmetic
All'utente deve essere consentito immettere qualsiasi numero di problemi e il programma deve fornire la risposta nei termini minimi.
Sono sulla buona strada? Credo di avere a-c giù, ma non d e soprattutto e. Qualcuno può guidarmi o aiutarmi a correggere il mio copione?
int GCD (int numer, int denom)
{
int result;
while (denom > 0) {
result = numer % denom;
numer = denom;
denom = result;
}
return numer;
}
int getLCM (int numer, int denom)
{
int max;
max = (numer > denom) ? numer : denom;
while (1) {
if (max % numer == 0 && max % denom == 0)
break;
++max;
}
return max;
}
struct Fraction
{
int numer;
int denom;
};
typedef struct
{
int numer;
int denom;
};
Fraction
Fraction add_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.numer * b.denom) + (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
Fraction subtract_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.numer * b.denom) - (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
Fraction multiply_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.denom * b.denom);
sum.denom = (a.numer * b.numer);
return sum;
}
Fraction divide_fractions (Fraction a, Fraction b)
{
Fraction sum;
sum.numer = (a.denom * b.numer);
sum.denom = (a.numer * b.denom);
return sum;
}
int main()
{
char response;
printf ("FRACTION ARITHMETIC PROGRAM\n");
printf ("Enter your problem (example 2/3 + 1/5):\n");
scanf (, &problem);
if (denom == 0 || denom < 0) {
printf ("Illegal input!!\n");
printf ("Another problem (y/n)? ");
scanf ("%c%*c", &response);
} else {
printf ("The answer is ");
printf ("Another problem (y/n)? ");
scanf ("%c%*c", &response);
}
while ((response == 'y') || (response == 'Y')) {
printf ("\nWould you like to play again?\n");
scanf ("%c%*c", &response);
}
while ((response == 'n') || (response == 'N'))
printf ("Goodbye and thank you");
return 0;
}
Modifica dopo aver rimosso typedef grazie per commentare le risposte:
struct Fraction {
int numer;
int denom;
};
struct Fraction add_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.numer * b.denom) + (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
struct Fraction subtract_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.numer * b.denom) - (b.numer * a.denom);
sum.denom = a.denom * b.denom;
return sum;
}
struct Fraction multiply_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.denom * b.denom);
sum.denom = (a.numer * b.numer);
return sum;
}
struct Fraction divide_fractions (struct Fraction a, struct Fraction b)
{
struct Fraction sum;
sum.numer = (a.denom * b.numer);
sum.denom = (a.numer * b.denom);
return sum;
}
Per quanto mi riguarda, siete un po 'sulla buona strada; tuttavia, è possibile introdurre una funzione per ridurre le frazioni per impedire troppo rapidamente i valori del numeratore e del denominatore. – Codor
Il tipo 'struct Fraction' non è interamente correlato al tipo' Fraction', che è un tipo di struttura anonima (senza tag) con un nome typedef di 'Fraction'. Probabilmente dovresti combinare i due, sebbene sia possibile utilizzarne uno. Quello che è sbagliato è avere entrambi. –
Si può fare meglio nella funzione 'getLCM'. Sarebbe una buona idea normalizzare la frazione prima di tornare numerando e denomina con gcd. (Scrivi una funzione per normalizzare) È possibile rimuovere "typedef' o' struct Fraction' per evitare confusione. –