steamengines

Lab 5 gucci

Oct 7th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.73 KB | None | 0 0
  1. #include <iostream>
  2. #include "fraction.h"
  3.  
  4. int main()
  5. {
  6.     int num1 = 0, num2 = 0, den1 = 0, den2 = 0;
  7.  
  8.     std::cout << "Enter numerator and denominator of fraction 1 separated by whitespace:";
  9.     std::cin >> num1 >> den1;
  10.     std::cout << "Enter numerator and denominator of fraction 2 separated by whitespace:";
  11.     std::cin >> num2 >> den2;
  12.  
  13.     fraction a(num1, den1);
  14.     fraction b(num2, den2);
  15.  
  16.     std::cout << " Reduced Fraction 1= ";
  17.     a.display();
  18.     std::cout << "\tReduced Fraction 2= ";
  19.     b.display();
  20.     cout << endl;
  21.  
  22.     fraction result;
  23.  
  24.     result = a.add(b);
  25.     std::cout << "\n Sum of Fraction 1 and Fraction 2 is:";
  26.     result.display();
  27.  
  28.     result = a.subtract(b);
  29.     std::cout << "\n Difference of Fraction 1 and Fraction 2 is:";
  30.     result.display();
  31.  
  32.     result = a.multiply(b);
  33.     std::cout << "\n Product of Fraction 1 and Fraction 2 is:";
  34.     result.display();
  35.  
  36.     result = a.divide(b);
  37.     std::cout << "\n Quotient of Fraction 1 and Fraction 2 is:";
  38.     result.display();
  39.  
  40.     if (a.isEqual(b))
  41.         std::cout << "\n Fraction 1 is equal to Fraction 2";
  42.  
  43.     if (a.isNotEqual(b))
  44.         std::cout << "\n Fraction 1 is not equal to Fraction 2";
  45.  
  46.     if (a.greaterThan(b))
  47.         std::cout << "\n Fraction 1 is greater than Fraction 2";
  48.  
  49.     if (a.lessThan(b))
  50.         std::cout << "\n Fraction 1 is less than Fraction 2";
  51.  
  52.     std::cout << std::endl;
  53.  
  54.     system("pause");
  55.     return 0;
  56. }
  57.  
  58. //***********************************************SOURCE**************************************^^^^^^^^
  59.  
  60. #ifndef FRACTION_H
  61. #define FRACTION_H
  62. #include <iostream>
  63. #include <string>
  64. #include <sstream>
  65. using namespace std;
  66.  
  67. // Regular prototypes
  68. //int stringToNumber(const string &Text);
  69. int GCD(int, int);
  70. int LCM(int, int);
  71.  
  72. class fraction{
  73.  
  74. public: // Access Specifier
  75.     int numerator;
  76.     int denominator; // Can never be 0
  77.  
  78.  
  79.  
  80.     // Function Prototypes
  81.     fraction(int num = 0, int denom = 1);
  82.     void setNumDen(int num, int denom);
  83.     void reduce();
  84.     void add(const fraction &f);
  85.     void subtract(const fraction &f);
  86.     void multiply(const fraction &f);
  87.     void divide(const fraction &f);
  88.     void isEqual();
  89.     void isNotEqual();
  90.     void greaterThan();
  91.     void lessThan();
  92.     void display();
  93.  
  94.  
  95. };
  96.  
  97. // Member functions definitions
  98. fraction::fraction(int num, int denom)
  99. {
  100.     setNumDen(num, denom);
  101. }
  102.  
  103. void fraction::setNumDen(int num, int denom)
  104. {
  105.     numerator = num;
  106.     denominator = denom;
  107.  
  108.     if (numerator > 0 && denominator < 0)
  109.     {
  110.         numerator = -numerator;
  111.         denominator = -denominator;
  112.     }
  113.  
  114.     if (numerator < 0 && denominator < 0)
  115.     {
  116.         numerator = -numerator;
  117.         denominator = -denominator;
  118.     }
  119.     reduce();
  120. }
  121.  
  122. void fraction::reduce()
  123. {
  124.     int myGCD = 0;
  125.  
  126.     myGCD = GCD(abs(numerator), abs(denominator));
  127.  
  128.     numerator /= myGCD;
  129.     denominator /= myGCD;
  130. }
  131.  
  132. void fraction::add(const fraction & fun)
  133. {
  134.     int leastComDen;
  135.  
  136.    
  137.  
  138. }
  139.  
  140. void fraction::subtract(const fraction & fun)
  141. {
  142.  
  143. }
  144.  
  145. void fraction::multiply(const fraction & fun)
  146. {
  147.  
  148. }
  149.  
  150. void fraction::divide(const fraction & fun)
  151. {
  152.  
  153. }
  154.  
  155. void fraction::isEqual()
  156. {
  157.  
  158. }
  159.  
  160. void fraction::isNotEqual()
  161. {
  162.  
  163. }
  164.  
  165. void fraction::greaterThan()
  166. {
  167.  
  168. }
  169.  
  170. void fraction::lessThan()
  171. {
  172.  
  173. }
  174.  
  175. void fraction::display()
  176. {
  177.     if (denominator == 0)
  178.         cout << numerator;
  179.     else
  180.         cout << numerator << "/" << denominator;
  181. }
  182.  
  183. //int main()
  184. //{
  185. //
  186. //  fraction fractionObject, fractionObject2;
  187. //
  188. //  fractionObject.setNumDen();
  189. //  fractionObject2.setNumDen();
  190. //  // Reducing and displaying the reduced fractions
  191. //  fractionObject.reduce();
  192. //  fractionObject2.reduce();
  193. //  cout << "Reduced Fraction 1 = " << fractionObject.numerator << "/" << fractionObject.denominator << "\t" << "Reduced Fraction 2 = " << fractionObject2.numerator << "/" << fractionObject2.denominator << endl;
  194. //  // Adding and displaying the fractions
  195. //
  196. //
  197. //
  198. //  system("pause");
  199. //  return 0;
  200. //}
  201.  
  202. // Function to convert string to number
  203. //int stringToNumber(const string &Text)//Text not by const reference so that the function can be used with a
  204. //{                               //character array as argument
  205. //  stringstream ss(Text);
  206. //  int result;
  207. //  return ss >> result ? result : 0;
  208. //}
  209.  
  210. // result=GCD(a,b)
  211. int GCD(int a, int b) {
  212.     int temp = 0;
  213.  
  214.     while (b != 0) {
  215.         temp = b;
  216.         b = a%b;
  217.         a = temp;
  218.     }
  219.     return a;
  220. }
  221.  
  222. // result=LCM(a,b);
  223. int LCM(int a, int b) {
  224.     int result = 0;
  225.  
  226.     result = a * (b / LCM(a, b));
  227.     return result;
  228. }
  229. #endif
  230. ////*****************************************************************************************************************************************************************************************************************************************
  231.  
  232. //#include <iostream>
  233. //#include <string>
  234. //#include <sstream>
  235. //using namespace std;
  236. //
  237. //// Regular prototypes
  238. //int stringToNumber(const string &Text);
  239. //int GCD(int, int);
  240. //int LCM(int, int);
  241. //
  242. //class fraction{
  243. //
  244. //public: // Access Specifier
  245. //  int numerator;
  246. //  int denominator; // Can never be 0
  247. //  int numerator2;
  248. //  int denominator2; // Also can never be 0
  249. //
  250. //
  251. //
  252. //  // Function Prototypes
  253. //  fraction();
  254. //  void setNumDen();
  255. //  void reduce();
  256. //  void add(const fraction &f);
  257. //
  258. //
  259. //};
  260. //
  261. //// Member functions definitions
  262. //fraction::fraction()
  263. //{
  264. //  numerator = 0;
  265. //  denominator = 0;
  266. //  numerator2 = 0;
  267. //  denominator2 = 0;
  268. //}
  269. //
  270. //void fraction::setNumDen()
  271. //{
  272. //  string numString;
  273. //  string denString;
  274. //  string numString2;
  275. //  string denString2;
  276. //
  277. //  do{
  278. //      cout << "Enter a numerator and denominator of fraction 1 separated by whitespace: ";
  279. //      getline(cin, numString, ' ');
  280. //      getline(cin, denString);
  281. //      if (denString == "0")
  282. //          cout << endl << "Please enter a number that isn't zero." << endl;
  283. //  } while (denString == "0"); // Making sure denominator is not zero
  284. //
  285. //  do{
  286. //      cout << "Enter a numerator and denominator of fraction 2 separated by whitespace: ";
  287. //      getline(cin, numString2, ' ');
  288. //      getline(cin, denString2);
  289. //      if (denString2 == "0")
  290. //          cout << endl << "Please enter a number that isn't zero." << endl;
  291. //  } while (denString2 == "0"); // Making sure denominator is not zero
  292. //
  293. //  numerator = stringToNumber(numString);
  294. //  denominator = stringToNumber(denString);
  295. //  numerator2 = stringToNumber(numString2);
  296. //  denominator2 = stringToNumber(denString2);
  297. //
  298. //}
  299. //
  300. //void fraction::reduce()
  301. //{
  302. //  int leastCommonMultiple = 0;
  303. //  int leastCommonMultiple2 = 0;
  304. //
  305. //  leastCommonMultiple = LCM(numerator, denominator);
  306. //  leastCommonMultiple2 = LCM(numerator2, denominator2);
  307. //
  308. //  numerator /= leastCommonMultiple;
  309. //  denominator /= leastCommonMultiple;
  310. //  numerator2 /= leastCommonMultiple2;
  311. //  denominator2 /= leastCommonMultiple2;
  312. //}
  313. //
  314. //void fraction::add(const fraction & fun)
  315. //{
  316. //  int leastComDen;
  317. //
  318. //
  319. //
  320. //}
  321. //
  322. //int main()
  323. //{
  324. //
  325. //  fraction fractionObject;
  326. //
  327. //  fractionObject.setNumDen();
  328. //  // Reducing and displaying the reduced fractions
  329. //  fractionObject.reduce();
  330. //  cout << "Reduced Fraction 1 = " << fractionObject.numerator << "/" << fractionObject.denominator << "\t" << "Reduced Fraction 2 = " << fractionObject.numerator2 << "/" << fractionObject.denominator2 << endl;
  331. //  // Adding and displaying the fractions
  332. //
  333. //
  334. //
  335. //  system("pause");
  336. //  return 0;
  337. //}
  338. //
  339. //// Function to convert string to number
  340. //int stringToNumber(const string &Text)//Text not by const reference so that the function can be used with a
  341. //{                               //character array as argument
  342. //  stringstream ss(Text);
  343. //  int result;
  344. //  return ss >> result ? result : 0;
  345. //}
  346. //
  347. //// result=GCD(a,b)
  348. //int GCD(int a, int b) {
  349. //  int temp = 0;
  350. //
  351. //  while (b != 0) {
  352. //      temp = b;
  353. //      b = a%b;
  354. //      a = temp;
  355. //  }
  356. //  return a;
  357. //}
  358. //
  359. //// result=LCM(a,b);
  360. //int LCM(int a, int b) {
  361. //  int result = 0;
  362. //
  363. //  result = a * (b / LCM(a, b));
  364. //  return result;
  365. //}
Advertisement
Add Comment
Please, Sign In to add comment