Skip to content

Commit 82f8f27

Browse files
author
Carlo Lanzi Luciani
committed
Chapter 27
1 parent f93b463 commit 82f8f27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+65
-27
lines changed

README.md

+15-2

learncpp.com-v09.2023/bookmark.txt

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
11
topic 1: learncpp.com (one day I will be able to write here 'FINISHED!')
2-
Bookmark: 27.1
3-
(at the end of ch26, return to check the lesson 24.9 on the navtab 'for advanced readers')
4-
5-
6-
7-
topic 2: learn smt about computer networks from the book 'a top down approach' + exercises
8-
Bookmark (ch.section(.subsection)): 2.3.1 (remember: do exercises at the end of section)
9-
10-
11-
12-
13-
topic 2b: practice of topic2 following the website of the book
14-
Bookmark: HTTP of the wireshark lab
15-
16-
topic 2c: interactive quiz from the website of Kurose, basic exercises that chatgpt suggests to learn networking with libraries cpp and c.
17-
18-
----------------------------- already done -------------------------------
19-
20-
21-
22-
23-
----------------------------- future topics --------------------------------
24-
julia basics (main motivation: mathematical and physical easy simulations and models. On the worst case ask chatgtp to create them for you, but then where's the fun for it? And also for complicated stuff it becomes inefficient to ask the AI, because you must thoroughly communicate your idea)
25-
following: ch1-4, appendix d-e of `Paul D. McNicholas, Peter A. Tait - Data Science with Julia-CRC Press (2019)`
26-
2+
Bookmark: 28.1
273

4+
already dealt with: appendix C. I will skip appendix B.

learncpp.com-v09.2023/ch27/x.1.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Write a Fraction class that has a constructor that takes a numerator and a denominator. If the user passes in a denominator of 0, throw an exception of type std::runtime_error (included in the stdexcept header). In your main program, ask the user to enter two integers. If the Fraction is valid, print the fraction. If the Fraction is invalid, catch a std::exception, and tell the user that they entered an invalid fraction.
2+
3+
Here’s what one run of the program should output:
4+
5+
Enter the numerator: 5
6+
Enter the denominator: 0
7+
Invalid denominator
8+
*/
9+
10+
#include <iostream>
11+
#include <stdexcept>
12+
#include <exception>
13+
14+
class Fraction
15+
{
16+
private:
17+
int m_numerator {};
18+
int m_denominator {};
19+
20+
public:
21+
Fraction(int numerator, int denominator)
22+
: m_numerator(numerator), m_denominator(denominator)
23+
{
24+
if(!denominator) { throw std::runtime_error{"Invalid denominator"}; }
25+
}
26+
};
27+
28+
29+
int main(void)
30+
{
31+
std::cout << "Enter the numerator of your fraction: ";
32+
int numerator {};
33+
std::cin >> numerator;
34+
35+
std::cout << "Enter the denominator of your fraction: ";
36+
int denominator {};
37+
std::cin >> denominator;
38+
39+
try { Fraction fraction{numerator, denominator}; }
40+
41+
catch(std::exception& exception)
42+
{
43+
std::cerr << exception.what() << '\n';
44+
return 0;
45+
}
46+
47+
return 0;
48+
}

0 commit comments

Comments
 (0)