Author Topic: Modified Calculator Program in Stroustrup Textbook C++PPP  (Read 761 times)

0 Members and 1 Guest are viewing this topic.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Modified Calculator Program in Stroustrup Textbook C++PPP
« on: December 17, 2018, 07:54:04 pm »
Bjarne Straustrup's Programming Principles and Practice Using C++

I worked through up to but not including chapter 8 so far.   By the end of chapter 7 I was very pleased with the simple command line calculator designed and implemented, and then, since I am currently working with permutations, and probability problems, I used this calculator as a template and transformed it, creating an entirely different version which uses the GMP library to handle very large integers (for factorials).  I also created a version which handles rational numbers with Big Integer numerator and denominator (also using GMP, that is #include <gmpxx.h>) for probability values between 0 and 1.

It's all very fascinating the way I had to define the grammar so as to teach the program the binding rules between terms.   From the command line, it understands things like (32C5)/(12C4).

I will attach a few files just for the hell of it.   I am engaging with the text in spurts as I do want to get through the math texts I am working through by early January or so.   Still, I am glad I gave the Stroustrup text some attention.  I was very pleased that he was teaching how to build a simple calculator as a thematic exercise throughout the book.  I love the details such a project is forcing my mind to think about.   Programming the grammar forces one to appreciate the binding rules we often take for granted in our arithmetic operations.



1. Stroustrup: Programming - Exercise Solutions

2. http://people.ds.cam.ac.uk/nmm1/C++/Exercises/

3. https://github.com/0p3r4t4/PPPUCPP2nd or https://gitlab.com/0p3r4t4/PPPUCPP2nd

4.  https://github.com/Chrinkus/stroustrup-ppp

5. https://github.com/Jtaim/Programming-Principles-and-Practice-Using-Cpp

6.  https://github.com/bewuethr/stroustrup-ppp

7.  https://bumpyroadtocode.com/tag/principles-and-practice-using-c/

8.  https://gist.github.com/cortical-iv/4c19719bbce455ec3f2d043fa011d638

9.  https://groups.google.com/forum/#!topic/ppp-public/BtlzdWGuQpQ


« Last Edit: December 22, 2018, 05:28:11 pm by Kaspar the Jaded »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Share on Facebook Share on Twitter


Ibra

  • Philosopher of the Void
  • Posts: 132
Re: Calculator Program in Stroustrup Textbook C++PPP
« Reply #1 on: December 18, 2018, 05:58:25 am »
That is cool. I tried once C++ but OOP languages are "not  my cup of tea".  Inheritance ,objects, classes are too taxing for my little neurons.  I programmed once in C language for micro controllers back in the day. i use functions and bunch of structs, that was enough for me. I might brush my knowledge of C language.

do you use gcc compiler or another one.

Suffering is the only fruit of human race

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: Calculator Program in Stroustrup Textbook C++PPP
« Reply #2 on: December 18, 2018, 11:03:30 pm »
Yes, I use g++ basically, whether in Linux or Windows.

Although, with the rational_calculator.cpp and integer_calculator.cpp, since they depend on the GMP library, when in Windows, I had to use Windows Linux Subsystem, so I don't have an actual Windows executable.  I don't care.   I grew tired of the all-too-enormous Visual Studio graphical interface a long time ago.  I do most of my compiling with the GNU GCC collection of compilers.


For the two programs mentioned, I had to build the GMP library locally then compile with something like:


g++ -lgmpxx lgmp -g rational_calculator.cpp -std=c++11 -o qc


I have a huge collection of my own command line math-related programs.  In fact, one of the motivations for revisiting "high school" mathematics was to inspire me to spontaneously create algebra-related programs along the way, doing fundamental things that I would use frequently for the remainder of my days in this Bizarroland World, things like factoring numbers into prime factors, finding gcd and lcm, and a host of other operations ... reducing a matrix to row-reduced echelon form  to solve systems of equations ... all kinds of useful programs inspired by the foundations and fundamentals.

By the way, Alex Stepanov (creator of Standard Template Library) said that object-oriented programming is a hoax.   C++ lends itself well to generic programming.

But, I am no expert … just an enthusiast and lifelong student.

OOP vs Generics : “Is” vs “Has” approach.

If you have intentions of compiling the code itself, you will also need the little header file for error handling (from Bjarne Stroustrup).  Place it in the same directory as the calculator.cpp files:  std_lib_facilities.h

The reason I altered it in the other versions was because I wanted to use it for probabilities, and I prefer rational numbers over decimal floating point (double).  I needed the GMP library to deal with big factorials.

rational_calculator.cpp can handle things like:  Combination of 4 elements taken 4 at a time times combination of 48 elements taken 9 at a time divided by combination of 52 eleements taken 13 at a time.   In plain English, that is the probability of being dealt 4 of a kind in a 13 card hand.

> {(4C4)*(48C9)}/(52C13)
= 11/4165 = 0.00264106

> 2^3000
= 1230231922161117176931558813276752514640713895736833715766118029160058800614672948775360067838593459582429649254051804908512884180898236823585082482065348331234959350355845017413023320111360666922624728239756880416434478315693675013413090757208690376793296658810662941824493488451726505303712916005346747908623702673480919353936813105736620402352744776903840477883651100322409301983488363802930540482487909763484098253940728685132044408863734754271212592471778643949486688511721051561970432780747454823776808464180697103083861812184348565522740195796682622205511845512080552010310050255801589349645928001133745474220715013683413907542779063759833876101354235184245096670042160720629411581502371248008430447184842098610320580417992206662247328722122088513643683907670360209162653670641130936997002170500675501374723998766005827579300723253474890612250135171889174899079911291512399773872178519018229989376 = inf

> 180!
= 200896062499134299656951336898466838917540340798867777940435335160044860953395980941180138112097309735631594101037399609671032132186331495273609598531966730972945653558819806475064353856858157445040809209560358463319644664891114256430017824141796753818192338642302693327818731986039603200000000000000000000000000000000000000000000 = inf


Note that in C++, the basic built-in type representation can only handle 20! but not 21!

Notice how large the integers are that can be handled using the GMP library.


Note that the math behind 52C13 is (52!)/(13! * 39!) = (52*51*50*49*48*47*46*45*44*43*42*41*40)/(13*12*11*10*9*8*7*6*5*4*3*2*1)

I'm sure you have been exposed to combinations and permutations.   I wanted to be able to type nCr into my command line calculator, and now I can.   It's very satisfying to do so when it is one's own agenda and not at the request of a professor or "manager/boss/supervisor".
« Last Edit: December 19, 2018, 02:04:56 pm by Kaspar the Jaded »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Modified [RATIONAL] Calculator Program for Big Integers
« Reply #3 on: December 22, 2018, 10:43:49 am »
I get a kick typing into my own creation: 

(6P5)+(4C1)*{5!/(2!*2!)}+{2*(7C3)*(5!/2!)}

It returns 5040

So what does "(6P5)+(4C1)*{5!/(2!*2!)}+{2*(7C3)*(5!/2!)}" actually ask in Plain English?

It solves the following problem:  Find the number of distinguishable 5-letter sequences that can be made from the letters in the word MANTISSA.

First, consider separately the cases where there are no duplications, both A and S are duplicated, or exactly one letter is duplicated.

(1)  no duplications:   6P5 = 6*5*4*3*2 = 720

(2) both A and S are duplicated:  after choosing A,A,S,S, there are 4 letters to select 1 letter from (M,N,T,I)

(4C1) * ( 5! / (2! * 2! ) ) = 4 * ( 5*4*3*2 / 2*2 ) = 120

(3)  A or S is duplicated:  2 * (7C3) * (5!/2!) = 2*(7*6*5 / 3*2*1)*(5*4*3) = 4200

Adding 720 + 120 + 4200 = 5040

So, you see, a considerable amount of thought must go in to the solution, but once I have figured out the logic of the solution, it is cool that I can check the "arithmetic" with my own homegrown calculator. I type qc at the command line:

-------------------------------------------------------------------------------------------------------------
Probability Calculator with Rational Numbers
(type ? for help)

> (6P5)+(4C1)*{5!/(2!*2!)}+{2*(7C3)*(5!/2!)}
= 5040 = 5040
>
--------------------------------------------------------------------------------------------------------------

The reason it lists the answer twice is because, when looking for probabilities, where results are between 0 and 1, I want to see the rational fraction as well as the decimal "floating point" approximation.
« Last Edit: December 22, 2018, 05:29:09 pm by Kaspar the Jaded »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: Course using Stroustrup (PPP) text (supportive)
« Reply #4 on: April 11, 2020, 07:50:23 pm »
some help with the first half of the text (one semester?) available here:

https://imada.sdu.dk/~marco/DM560/
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~