Please support our C++ advertiser: Programming Forums
![]() |
•
•
Join Date: Mar 2004
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
the third program is one that adds and subtracts two fractions, not necessarily positive, and displays the exact sum or difference in the lowest terms.
Use the c++ construct "struct" to create a data type for the fractions.
This how I do this;
#include <iostream>
using namespace std;
struct frac{int numer, denom;};
frac f_add( frac, frac );
int main( ){
frac r, s, sum;
cout << "Enter the numerator and the denominator of the first fraction separated by a space=> " << endl;
cin >> r.numer >> r.denom;
while (r.numer,r.denom < 0)
{
cout <<"Yes you may enter a negative integer" <<endl;
}
cout << "Enter the numerator and the denominator of the second fraction separate by a space=> " << endl;
cin >> s.numer >> s.denom;
while (s.numer, s.denom < 0)
{
cout <<"Yes you also enter a negative integer" <<endl;
}
cout <<"You have entered "<< r.numer <<" / "<< r.denom << " and " << s.numer<<"/" <<s.denom<<"\n"<<endl;
int gcd = find gcd (r.denom, s.denom);
sum = f_add(r,s);
cout << "The sum of " << r.numer <<"/" << r.denom << " and "<< s.numer <<"/" << s.denom <<" in lowest term is " << sum.numer <<"/" << sum.denom <<".\n";
return 0;
}
frac f_add(frac r, frac s){
if (r=s)
return r;
else if (a=0)
return s;
else if (s=0)
return r;
else if (r>b);
return find gcd (s, r/s);
else
return findgcd (r, s/r);
}
}
I feel like i am doing something wrong but i cannot figure it out! Please help!
I am very confused with the construct "struct":
what do think i need and how to apply it and where?
thank you
Use the c++ construct "struct" to create a data type for the fractions.
This how I do this;
#include <iostream>
using namespace std;
struct frac{int numer, denom;};
frac f_add( frac, frac );
int main( ){
frac r, s, sum;
cout << "Enter the numerator and the denominator of the first fraction separated by a space=> " << endl;
cin >> r.numer >> r.denom;
while (r.numer,r.denom < 0)
{
cout <<"Yes you may enter a negative integer" <<endl;
}
cout << "Enter the numerator and the denominator of the second fraction separate by a space=> " << endl;
cin >> s.numer >> s.denom;
while (s.numer, s.denom < 0)
{
cout <<"Yes you also enter a negative integer" <<endl;
}
cout <<"You have entered "<< r.numer <<" / "<< r.denom << " and " << s.numer<<"/" <<s.denom<<"\n"<<endl;
int gcd = find gcd (r.denom, s.denom);
sum = f_add(r,s);
cout << "The sum of " << r.numer <<"/" << r.denom << " and "<< s.numer <<"/" << s.denom <<" in lowest term is " << sum.numer <<"/" << sum.denom <<".\n";
return 0;
}
frac f_add(frac r, frac s){
if (r=s)
return r;
else if (a=0)
return s;
else if (s=0)
return r;
else if (r>b);
return find gcd (s, r/s);
else
return findgcd (r, s/r);
}
}
I feel like i am doing something wrong but i cannot figure it out! Please help!
I am very confused with the construct "struct":
what do think i need and how to apply it and where?
thank you
![]() |
Similar Threads
Other Threads in the C++ Forum
- minimum weight perfect matching (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Visual C++ and Excel
- Next Thread: Preventing Hooks
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode