1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
int gcd(int x,int y);
main(){
ifstream file_in;
string strns , str, s1 , mv,
strs[100], c;
double x , y , z, a ,
num, b , d ;
int e , v , w ,f , g;
file_in.open("probf.in");
z = 0;
while( !file_in.eof() ){
getline( file_in , s1);
if(z==0){
a = atoi(s1.c_str());
}
else {
num = atof(s1.c_str());
c = s1.substr(0 , s1.find('.'));
b = atoi(c.c_str()); //whole number
d = num - b; // decimal part
x = d * 10;
y = 10;
v = int(x);
w = int(y);
e = gcd(v,w);
f = v / e;
g = w / e;
cout << b <<" "<< f <<"/ "<< g << endl;
}
z++;
}
cout << endl;
system("PAUSE");
}
int gcd(int x,int y){
int temp;
if ( y == 0 )
return x;
else if ( x >= y && y > 0 )
return gcd(y, x%y);
else {
temp = x;
x = y;
y = temp;
return gcd(y, x%y);
}
}
|