Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 9:38pm On Jul 05, 2015 |
main.cpp /* * File: main.cpp * Author: pyjac * * Created on June 30, 2015, 1:51 AM */
#include <cstdlib> #include <iostream> #include "fraction.h" #include <cassert>
using namespace std;
/* * */ int main(int argc, char** argv) { Fraction a(3,1,2); Fraction b(2,3); Fraction c(4,5); Fraction d(4); Fraction e(6,2); Fraction f(9,6,; Fraction g = a + b - c * d / e -f; cout << "Solution: " << g <<endl; // -133/20 return 0; }
www.nairaland.com/attachments/2566275_imag20251_jpeg289e6d9aae576e422a9704ae0757fd0e REF : http://www.math.ucla.edu/~wittman/10a.1.10w/ccc/ch17/index.htmlCC pcguru1 1stdami BlueMagnificent seunthomas |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 9:36pm On Jul 05, 2015 |
fraction.cpp /* * File: fraction.cpp * Author: pyjac * * Created on July 5, 2015, 7:45 PM */
#include "fraction.h" #include <cassert> #include <stdexcept>
int Fraction::gcd(int n, int m) { // Euclids Greatest Common Divisor algorithm. assert((n > 0) && (m > 0)); while (n != m) { if (n < m) m = m - n; else n = n - m; } return n; }
Fraction::Fraction(int t, int b) : top(t), bottom(b) { normalize(); }
Fraction::Fraction(int wn,int t, int b): top( (wn * b) + t), bottom(b){ normalize(); }
Fraction::Fraction() : top(0), bottom(1) { }
Fraction::Fraction(int t) : top(t), bottom(1) { }
int Fraction::numerator() const { return top; }
int Fraction::denominator() const { return bottom; }
void Fraction::normalize() { // Normalize fraction by // (a) moving sign to numerator // (b) ensuring numerator and denominator have no common divisors int sign = 1; if (top < 0) { sign = -1; top = - top; } if (bottom < 0) { sign = - sign; bottom = - bottom; } assert(bottom != 0); int d = 1; if (top > 0) d = gcd(top, bottom); top = sign * (top / d); bottom = bottom / d; }
Fraction operator+(const Fraction& left, const Fraction& right) { Fraction result(left.numerator() * right.denominator() + right.numerator() * left.denominator(), left.denominator() * right.denominator()); return result; }
Fraction operator-(const Fraction& left, const Fraction& right) { Fraction result(left.numerator() * right.denominator() - right.numerator() * left.denominator(), left.denominator() * right.denominator()); return result; }
Fraction operator*(const Fraction& left, const Fraction& right) { Fraction result(left.numerator() * right.numerator(), left.denominator() * right.denominator()); return result; }
Fraction operator/(const Fraction& left, const Fraction& right) { Fraction result(left.numerator() * right.denominator(), left.denominator() * right.numerator()); return result; }
Fraction operator-(const Fraction& value) { Fraction result(-value.numerator(), value.denominator()); return result; }
int Fraction::compare(const Fraction& right) const { return numerator() * right.denominator() - denominator() * right.numerator(); // return the numerator of the difference }
bool operator<(const Fraction& left, const Fraction& right) { return left.compare(right) < 0; }
bool operator<=(const Fraction& left, const Fraction& right) { return left.compare(right) <= 0; }
bool operator==(const Fraction& left, const Fraction& right) { return left.compare(right) == 0; }
bool operator!=(const Fraction& left, const Fraction& right) { return left.compare(right) != 0; }
bool operator>=(const Fraction& left, const Fraction& right) { return left.compare(right) >= 0; }
bool operator>(const Fraction& left, const Fraction& right) { return left.compare(right) > 0; }
ostream& operator<<(ostream& out, const Fraction& value) { out << value.numerator() << "/" << value.denominator(); return out; }
istream& operator>>(istream& in, Fraction& r) { int t, b; // Read the top in >> t; // If there is a slash, read the next number char c; in >> c; if (c == '/') in >> b; else { in.putback(c); b = 1; } r = Fraction(t, b); return in; }
Fraction::operator double() const { // convert numerator to double, then divide return static_cast<double>(top) / bottom; }
Fraction& Fraction::operator++() { top += bottom; return *this; }
Fraction Fraction::operator++(int unused) { Fraction clone(top, bottom); top += bottom; return clone; }
Fraction& Fraction::operator+=(const Fraction& right) { top = top * right.denominator() + bottom * right.numerator(); bottom *= right.denominator(); normalize(); return *this; }
REF : http://www.math.ucla.edu/~wittman/10a.1.10w/ccc/ch17/index.html |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 9:33pm On Jul 05, 2015 |
fraction.h /* * File: fraction.h * Author: pyjac * * Created on July 5, 2015, 7:45 PM */
#ifndef FRACTION_H #define FRACTION_H
#include <iostream>
using namespace std;
class Fraction { public: /** Constructs a fraction with numerator 0 and denominator 1 */ Fraction();
/** Constructs a fraction with numerator t and denominator 1 @param t the numerator for the fraction */ Fraction(int t);
/** Constructs a fraction with given numerator and denominator @param t the initial numerator @param b the initial denominator */ Fraction(int t, int b); /** Constructs a fraction with given numerator and denominator @param wn the initial whole number @param t the initial numerator @param b the initial denominator */ Fraction(int wn,int t, int b);
/** Returns the numerator @return the numerator value */ int numerator() const;
/** Returns the denominator @return the denominator value */ int denominator() const;
/** updates a fraction by adding in another fraction value @param right the fraction to be added @ return the updated fraction value */ Fraction& operator+=(const Fraction& right);
/** increment fraction by 1 */ Fraction& operator++(); // Prefix form Fraction operator++(int unused); // Postfix form
/** converts a fraction into a floating-point value @return the converted value */ operator double() const;
/** compare one fraction value to another result is negative if less than right, zero if equal, and positive if greater than right @param right the fraction to be compared against */ int compare(const Fraction& right) const; private: /** place the fraction in least common denominator form */ void normalize(); /** compute the greatest common denominator of two integer values @param n the first integer @param m the second integer */ int gcd(int n, int m); int top; int bottom; };
Fraction operator+(const Fraction& left, const Fraction& right); Fraction operator-(const Fraction& left, const Fraction& right); Fraction operator*(const Fraction& left, const Fraction& right); Fraction operator/(const Fraction& left, const Fraction& right); Fraction operator-(const Fraction& value);
bool operator<(const Fraction& left, const Fraction& right); bool operator<=(const Fraction& left, const Fraction& right); bool operator==(const Fraction& left, const Fraction& right); bool operator!=(const Fraction& left, const Fraction& right); bool operator>=(const Fraction& left, const Fraction& right); bool operator>(const Fraction& left, const Fraction& right);
ostream& operator<<(ostream& out, const Fraction& value); istream& operator>>(istream& in, Fraction& r);
#endif
|
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 9:31pm On Jul 05, 2015 |
Guess I'd proffer my (temporal) solution.
CC pcguru1 1stdami BlueMagnificent seunthomas |
Nairaland / General / Re: A Thread For Night Crawlers. (late Sleepers) by jacob05(m): 5:06pm On Jul 04, 2015 |
|
Programming / Re: Php Programmer Help Me Out ! by jacob05(m): 3:57am On Jul 03, 2015 |
***Dream walks into the thread *** ***Steps Out*** |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 8:14pm On Jul 01, 2015 |
trigar12:
I wanna learn c++ but all the ebooks I ve been reading are always getting me confused ... Can you recommend a really good one for me please?
I really wanna learn this language
C++ problem analysis to program design...book is good |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 5:10pm On Jul 01, 2015 |
1stdami:
do you mean that u dont mind if i change this fractions to decimal also No oooo...decimal ke...That'd defect the main purpose of the question |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 12:29pm On Jul 01, 2015 |
pcguru1:
Hi jacob working on it, though might be late to submit cuz of work but out of curiosity (3 1/2) how would that be inputted am assuming the method will take 7/2 as 3 1/2 will might be interpreted as 31 then / 2 my intention is to parse the text. interesting task will get back on this, but might be slow but this is interesting regardless. Thanks boss... I care less how inputs are accepted...you can use the values in the pix... All I need is the implementation |
Programming / Re: Understanding Programming-(lesson 1- Abstraction Of Objects) by jacob05(m): 1:42pm On Jun 30, 2015 |
Hmmm.... ***sits |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 1:16am On Jun 30, 2015 |
Hmmm. Ok... I need an elegant way using oop concepts to solve the below problem.... The program can accept any number of mixed fraction, fraction or whole numbers and operations to perform on them... It should follow mathematical rules in solving them .... The program should , in the end, print the values accepted "elegantly" and results.. www.nairaland.com/attachments/2566275_imag20251_jpeg289e6d9aae576e422a9704ae0757fd0e CC pcguru1 1stdami BlueMagnificent seunthomas |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 3:54pm On Jun 28, 2015 |
1stdami: to compute wat the question na |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 3:52pm On Jun 28, 2015 |
BlueMagnificent:
Sorry, there is noting like C++ console. What I believe you mean is writing and reading from the console which every other programming language does. And C++ is as much object oriented as java Boss Abi ooo.... Just ain't ready to push the argument ni ooo... How's the game project going? |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 1:40pm On Jun 28, 2015 |
1stdami: i am talking abt c++ console here ...but java can perform the operation easier dont u tink so hmmmm..enough arguments... I need a C++ solution....... |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 1:28pm On Jun 28, 2015 |
C++ was introduced to bring OOP , Templating/Generics and others to the C family.....@1stdami..... |
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 1:26pm On Jun 28, 2015 |
|
Programming / Re: Check In: Submit Your C++ Questions Lets Solve by jacob05(m): 9:34am On Jun 28, 2015 |
Hmmm. Ok...
I need an elegant way using oop concepts to solve the below problem.... The program can accept any number of mixed fraction, fraction or whole numbers and operations to perform on them... It should follow mathematical rules in solving them .... The program should , in the end, print the values accepted "elegantly" and results..
CC pcguru1 1stdami |
Programming / Re: Check Out My Android App (exam Prep) by jacob05(m): 2:11pm On Jun 26, 2015 |
How do you handle questions with images, multi answers or boolean questions? |
Programming / Re: Need help learning Modern C++ by jacob05(m): 3:13pm On Jun 18, 2015 |
|
Programming / Re: Need help learning Modern C++ by jacob05(m): 4:45pm On Jun 17, 2015 |
|
Programming / Re: Unilorin C Programming Question. by jacob05(m): 10:11pm On May 29, 2015 |
after working on @mobolaji88 code
#include <stdio.h> const int HALF_LIFE = 5; const float RAD_SAFE_LEVEL = 0.855; const float SAFTY_FACTOR = 0.10; #define RAD_SAFTY_FACTOR_LEVEL SAFTY_FACTOR * RAD_SAFE_LEVEL int main(){ float initialRadLevel,tmpRadLevel; int days = 0;
printf("Enter initial Radiation Level: " scanf("%f", &initialRadLevel);
tmpRadLevel = initialRadLevel; printf("Initial Radiation Level is : %f\n", initialRadLevel); while(tmpRadLevel > RAD_SAFTY_FACTOR_LEVEL){ tmpRadLevel /= 2; days += HALF_LIFE;
if (tmpRadLevel <= RAD_SAFTY_FACTOR_LEVEL) { printf("The current safe Radiation Level of %f will be achieved in %d days time.\n", tmpRadLevel, days); break; }
printf("at day %d, Radiation Level is %f => unsafe !!\n", days, tmpRadLevel); } }
3 Likes |
Programming / Re: Python Code For Birthday Reminder by jacob05(m): 9:27am On May 27, 2015 |
hope it helps |
Programming / Re: Python Code For Birthday Reminder by jacob05(m): 6:59pm On May 26, 2015 |
from datetime import datetime, timedelta import time from calendar import Calendar
class Person: def __init__(self, name, birthdate): self.name = name self.birthdate = birthdate def setName(self, name): self.name = name def getName(self): return self.name def setBirthDate(self, birthdate): self.birthdate = birthdate def getBirthDate(self): return self.birthdate
class BirthdayBook: def __init__(self, personList = {}): self.personList = personList def addPerson(self, person): self.personList[person.getName()] = person def _getBirthdayByName(self, name): if self.personList.get(name): return self.personList.get(name).getBirthDate() return None def _getPersonByBirthDateMonth(self, month): marches = []; for p in self.personList.values(): if p.getBirthDate().month == month: marches.append(p) return marches def printBirthDateByName(self, name): d = self._getBirthdayByName(name) if d != None: print("Name:", name,"BirthDate:", d.day, "/" ,d.month ,"/" ,d.year) return print("Person Not Found" def printBirthdayByMonth(self, month): marches = self._getPersonByBirthDateMonth(month) if marches != []: print("Persons born in the month ",month) for p in marches: print(p.getName()) else: print("Persons born in the month ",month,"not in DB" def _getPersonsBirthdayWithinTheWeek(self, month, day): #This function is assuming #Monday as the first day of week and sunday as the last day .. marches = []; currentDatetime = datetime.now() userDateWeekday = datetime(currentDatetime.year,month,day).weekday() #If for the whole week #firstWeekdayTimestamp = datetime(currentDatetime.year,month,day) - timedelta(userDateWeekday)).timestamp() firstWeekdayTimestamp = datetime(currentDatetime.year,month,day).timestamp() lastWeekdayTimestamp = (datetime(currentDatetime.year,month,day) + timedelta(6 - userDateWeekday)).timestamp() for p in self.personList.values(): pBirthDate = p.getBirthDate() if pBirthDate.month == month: pBirthDate = datetime(currentDatetime.year,pBirthDate.month,pBirthDate.day) ptimestamp = pBirthDate.timestamp() if ptimestamp >= firstWeekdayTimestamp and ptimestamp <= lastWeekdayTimestamp: marches.append(p) return marches def printBirthdayWithinTheWeek(self, month, day): marches = self._getPersonsBirthdayWithinTheWeek(month,day) if marches != []: print("Persons born within the week of ",month,day) for p in marches: print(p.getName()) else: print("Persons born within the week of ",month,day,"not in DB" if __name__ == '__main__': person1 = Person("Jacob", datetime(1900,5,25)) person2 = Person("John", datetime(1900,5,18)) person3 = Person("Mayowa", datetime(1900,5,23)) person4 = Person("Xeun", datetime(1900,5,21)) person5 = Person("Phil", datetime(1900,2,21)) person6 = Person("Dayo", datetime(1900,5,5)) birthdayBook=BirthdayBook() birthdayBook.addPerson(person1) birthdayBook.addPerson(person2) birthdayBook.addPerson(person3) birthdayBook.addPerson(person4) birthdayBook.addPerson(person5) birthdayBook.addPerson(person6) birthdayBook.printBirthDateByName("Jacob" birthdayBook.printBirthdayByMonth(2) birthdayBook.printBirthdayWithinTheWeek(5,20)
|
Programming / Re: Python Code For Birthday Reminder by jacob05(m): 6:57pm On May 26, 2015 |
......sorry ....code pasted wrongly |
Programming / Re: Python Code For Birthday Reminder by jacob05(m): 4:10pm On May 22, 2015 |
Python, sweet python... would love to help you but it seems it's been a while since post this, hope you've solved it ? |
NYSC / Re: Kwara State Batch A, Let's Meet Here by jacob05(m): 12:47am On Apr 30, 2015 |
YIKPATA new intakes I hail ooo.. All the best...#TeamKwara14C |
Car Talk / Re: Photos Of The New LASTMA Cabs Set To Be Launched by jacob05(m): 7:41pm On Apr 23, 2015 |
Abeg, who get LASTMAS OGA number? I won discuss something with him...
***imagining carrying Caro on a tour in the ride...**** 1 Like 1 Share |
Webmasters / Re: Google Ranking And Engagement With Googleplus by jacob05(m): 11:39am On Apr 23, 2015 |
. |
Webmasters / Re: Google Ranking And Engagement With Googleplus by jacob05(m): 11:39am On Apr 23, 2015 |
Like if you don't understand This Thread.:/ 5 Likes |
Fashion / Re: Miss United Nations Nigeria Records Massive Turn Out At Enugu Zonal Audition by jacob05(m): 2:17pm On Apr 21, 2015 |
hmm.... Where are the Hot girls? 21 Likes 1 Share |
Computers / Re: Ubuntu Linux by jacob05(m): 6:00pm On Apr 20, 2015 |
pcguru1: Linux is also for me as a developer I prefer it however I prefer Xfce , Ubuntu is too heavy and cinnamon Linux Mint Xfce rocks !!! |
Programming / Re: Please Help Me Look At My Code by jacob05(m): 5:24pm On Apr 20, 2015 |
kudaisi: I take it you've gotten all the database connections part right so I'm going to leave that part as is. Rather than using an SLQ SUM in your query it makes more sense do an SQL SELECT of the required row (in this case total_ded) and return the values as an array. You can then Loop through the array and display the necessary table. Finally sum the array and equally display the sum. Here's what the code will look like<?php public funtion get_all_ded() $db=new Database(); $conn=$db->Connect_DB(); if($conn){ $query = "SELECT total_ded FROM february"; $result=mysqli_query($conn, $query); if($result){ $deds=mysqli_fetch_array($result); return $deds; }else{ return false; } }else{ return false; } }
// $all_deds = get_all_deds(); ?> <table> <tr><td>Deds</td></tr> <?php foreach($all_deds as $ded){ echo "<tr><td>".$ded."</td></tr>" } ?> </table> <div>Total Deds: <?php echo array_sum($all_deds) ?></div>
hmm..nice..but won't use a foreach on a large dataset (performance wise ) and would check falsity of $all_feds before displaying the table |