Skip to content
Snippets Groups Projects
Commit 03e9f377 authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Chapter 1 examples

parent 8224ffb9
Branches
No related tags found
No related merge requests found
Showing
with 397 additions and 0 deletions
cmake_minimum_required(VERSION 3.28)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(cxx_course_examples CXX)
FILE (GLOB sources ./ *.cc)
foreach(source ${sources})
get_filename_component(withoutext "${source}" NAME_WE)
add_executable("${withoutext}" "${source}")
endforeach()
#include "binform.hh"
#include <iostream>
template <class T>
void handle()
{
std::cout << "Enter value: ";
T v;
std::cin >> v;
cxx_course::showbits(v);
}
auto main() -> int
{
bool keeplooping{true};
while (keeplooping) {
int ch{};
std::cout << "Select type (1: int, 2: unsigned int, 3: long, 4: unsigned long, 5: float, 6: double): ";
std::cin >> ch;
switch (ch) {
case 1:
handle<int>(); break;
case 2:
handle<unsigned int>(); break;
case 3:
handle<long>(); break;
case 4:
handle<unsigned long>(); break;
case 5:
handle<float>(); break;
case 6:
handle<double>(); break;
case 0: keeplooping = false; break;
default:;
};
}
}
#include <iostream>
auto is_prime(unsigned int n) -> bool
{
// recipe to tell if n is a prime or not
return false;
}
auto main() -> int
{
std::cout << "Number to check: ";
unsigned int inp{};
std::cin >> inp;
if (is_prime(n))
std::cout << "Yes, that's a prime.\n";
else
std::cout << "Not a prime.\n";
}
#include <iostream>
#include <string>
auto main(int argc, char* argv[]) -> int
{
unsigned long* p = nullptr;
if (argc > 1) {
auto i = std::stoul(argv[1]);
p = &i;
// Since p outlives i, storing address
// of i in p is a bad idea
std::cout << "p is pointing at " << p << " storing a value " << *p << "\n";
} else {
std::cout << "Needs one integer command line argument!\n";
}
// At this point, if argc > 1, p is pointing to
// a location where there was a variable called i
// which has run out of scope. Accessing p here is
// undefined behaviour.
if (p)
std::cout << "p is pointing at " << p << " storing a value " << *p << "\n";
}
#include <vector>
#include <iostream>
auto main() -> int
{
std::vector v{1, 2, 3};
const auto& vstart = v.front();
v.push_back(4);
v.push_back(5);
v.push_back(6);
v.push_back(7);
v.push_back(8);
std::cout << "Start element of v = " << vstart << "\n";
}
#include <iostream>
#include <string>
auto f(double x) -> double&
{
auto y = x * x;
return y;
}
auto main() -> int
{
auto a = 4.0;
auto&& z = f(a);
std::cout << z << "\n";
}
#include <iostream>
#include <stdexcept>
double f(double x)
{
double answer=1;
if (x>=0 and x<10) {
while (x>0) {
answer*=x;
x-=1;
}
} else {
throw(std::invalid_argument("Bad parameter value " + std::to_string(x)));
}
return answer;
}
int main()
{
double x=9.0;
try {
std::cout<<"Enter start point : ";
std::cin >> x;
auto res=f(x);
std::cout <<"The result is "<<res<<'\n';
} catch (std::exception &ex) {
std::cerr<<"Cought exception "<<ex.what()<<'\n';
}
}
#include <iostream>
#include <limits>
#include <format>
auto main() -> int
{
using namespace std;
size_t umax{ numeric_limits<size_t>::max() };
unsigned nfib = 0;
size_t f0{ 0 }, f1{ 1 };
// Insert your code here
cout << format(R"(The largest number in the Fibonacci sequence under {0:} is {1:},
and there are {2:} entries under {0:} in that sequence.
)" , umax, f1, nfib);
}
#include <iostream>
auto euclid_gcd(unsigned long smaller, unsigned long larger) -> unsigned long
{
if (smaller > larger)
std::swap(smaller, larger);
// Fill in your code to implement Euclid's algorithm here.
return ? ? ? ;
}
auto main(int argc, char* argv[]) -> int
{
if (argc != 3) {
std::cout << "Usage:\n"
<< argv[0] << " number1 number2\n";
return 1;
}
unsigned long n1 = std::stoul(argv[1]), n2 = std::stoul(argv[2]);
std::cout << euclid_gcd(n1, n2) << "\n";
}
#include <iostream>
#include <string>
auto the_other(int i, int j) -> int { return 3 - i - j; }
void transfer(unsigned long n, int from, int to)
{
int oth = the_other(from, to);
if (n > 1)
transfer(n - 1, from, oth);
std::cout << n << ": " << from << "->" << to << "\n";
if (n > 1)
transfer(n - 1, oth, to);
}
auto main() -> int
{
size_t N = 6;
transfer(N, 0, 1);
}
// examples/hello_qa.cc
#include <string>
#include <iostream>
auto main() -> int
{
std::string name;
std::cout << "What's your name ? ";
std::cin >> name;
std::cout << "Hello, " << name << "\n";
}
// examples/hello_xyz.cc
#include <iostream>
auto main(int argc, char* argv[]) -> int
{
std::cout << "Hello, ";
if (argc > 1)
std::cout << argv[1] << "!\n";
else
std::cout << "world!\n";
}
// examples/midpt.cc
#include <iostream>
auto mid(int a, int b) -> int
{
// recipe needed!
return a;
}
auto main() -> int
{
int i = 0, j = 0;
std::cout << "Enter i, j: ";
std::cin >> i >> j;
std::cout << "A number half way between " << i << " and " << j
<< " is " << mid(i, j) << "\n";
}
// examples/min_of_three.cc
#include <iostream>
auto min_of_three(int a, int b, int c) -> int
{
// recipe needed!
return a;
}
auto main() -> int
{
int i = 0, j = 0, k = 0;
std::cout << "Enter i, j and k: ";
std::cin >> i >> j >> k;
std::cout << "The smallest of the three is " << min_of_three(i, j, k) << "\n";
}
#include <iostream>
using namespace std;
namespace UnitedKingdom {
string London = "Big city";
}
namespace UnitedStates {
string London = "Small town in Kentucky";
}
auto main() -> int
{
using namespace UnitedKingdom;
cout << London << "\n";
cout << UnitedStates::London << "\n";
}
#include <iostream>
namespace UnitedKingdom {
std::string London = "Big city";
}
namespace UnitedStates {
namespace KY {
std::string London = "Small town in Kentucky";
}
namespace OH {
std::string London = "Small town in Ohio";
}
}
// The following definition is ok since C++17
//namespace mylibrary::onefeature {
// double solve(int i);
//}
auto main() -> int
{
namespace USOH = UnitedStates::OH;
std::cout << USOH::London << "\n";
}
#include <iostream>
#include <string>
#include <fstream>
#include <filesystem>
#include <vector>
#include <ranges>
#include <algorithm>
namespace fs = std::filesystem;
auto as_lines(fs::path file) -> std::vector<std::string>
{
std::ifstream fin{ file };
std::string line;
std::vector<std::string> lines;
while (getline(fin, line)) lines.push_back(line);
return lines;
}
auto main(int argc, char* argv[]) -> int
{
if (argc != 2) {
std::cerr << "Usage:\n"
<< argv[0] << " filename\n";
return 1;
}
auto content = as_lines(argv[1]);
std::ranges::sort(content,
[](auto l1, auto l2) {
return std::stod(l1) < std::stod(l2);
}
);
for (std::string_view line : content) {
std::cout << line << "\n";
}
}
// examples/onespace.cc
#include <iostream>
auto main(int argc, char* argv[]) -> int
{
std::string line;
while (getline(std::cin, line)) {
if (line.empty()) {
continue;
}
bool sp{true};
for (auto c : line) {
if (isspace(c)) {
if (not sp) std::cout << '\t';
sp = true;
} else {
sp = false;
std::cout << c;
}
}
std::cout << "\n";
}
}
import sys
if __name__ == "__main__":
if len(sys.argv) > 1:
N = int(sys.argv[1])
else:
N = 5
def fact(n):
if n > 1:
return n * fact(n-1)
return 1
while N > 0:
print(fact(N))
if N % 4 == 0:
fact = N * (N - 1) / 2
N = N -1
#include <iostream>
#include <string>
int main()
{
std::cout << "Use a single backslash character at the end of the line to continue input to the next line, as shown ...\n"
<< "OBJ=a.o \\\n\tb.o\\\n\tc.o\n";
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment