Hi guys,
Ok this is more to do with laziness than anything else but I was wondering how would you create a program that cancels down fractions into its simplest form. I've managed to do it for positive integers but I have a problem when dealing with negative integers.
Remember these rules:
Possible solutions include using the absolute function, or perhaps converting the integer to a string and then checking to see if the first element in that array is equal to '-' or '+' and then doing the necessary arithmetic manipulations.Code:+ / + = +
+ / - = -
- / + = -
- / - = +
so:
= -6
---
-10
= 3
---
5
If you can see a quick easy solution, I would be most grateful.
Here's my code so far, note it only handles positive integers!
Code:#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int main()
{
int top_part;
int bottom_part;
int smallest;
cout<<"Enter top part of fraction:";
cin>>top_part;
cout<<"Enter bottom part of fraction:";
cin>>bottom_part;
if (top_part>bottom_part)
{
smallest=bottom_part;
}
if (top_part<bottom_part)
{
smallest=top_part;
}
double it,its,newtop,newbot;
for (int a=1; a<=smallest; a++)
{
it=top_part%a;
its=bottom_part%a;
if (it==0)
{
if (its==0)
{
newtop=top_part/a;
newbot=bottom_part/a;
}
}
}
cout<<"When cancelled down..."<<endl;
cout<<newtop<<"/"<<newbot;
int stop;
cin>>stop;
return 0;
}