diff options
author | Oskar <[email protected]> | 2024-07-28 12:52:38 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-07-28 12:52:38 +0200 |
commit | 5aac3c3f8bcfef0b2aad28d91d87b9ebf1d5e7df (patch) | |
tree | 38736b9f0c57f69e272121e0fc1f3969bb7468e2 |
first commit
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | 1p19.cpp | 23 | ||||
-rw-r--r-- | 1p20.cpp | 19 | ||||
-rw-r--r-- | 1p21.cpp | 20 | ||||
-rw-r--r-- | 1p22-2.cpp | 22 | ||||
-rw-r--r-- | 1p22.cpp | 24 | ||||
-rw-r--r-- | Makefile | 29 | ||||
-rw-r--r-- | comments.cpp | 18 | ||||
-rw-r--r-- | exercise-template.cpp | 15 | ||||
-rw-r--r-- | for.cpp | 49 | ||||
-rw-r--r-- | helloworld.cpp | 9 | ||||
-rw-r--r-- | if.cpp | 21 | ||||
-rw-r--r-- | return.cpp | 3 | ||||
-rw-r--r-- | sales_item.hpp | 142 | ||||
-rw-r--r-- | sum.cpp | 25 | ||||
-rw-r--r-- | while.cpp | 40 |
16 files changed, 461 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57ce6fb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +redirection/ diff --git a/1p19.cpp b/1p19.cpp new file mode 100644 index 0000000..5d1d104 --- /dev/null +++ b/1p19.cpp @@ -0,0 +1,23 @@ +#include <iostream> + +/* + * + * Exercise 1.19 + * + * + */ + +int main () { + + int64_t beg; + int64_t end; + std::cout << "Enter beginning and end number: "; + std::cin >> beg >> end; + while(beg <= end) { + std::cout << beg << " "; + beg++; + } + + std::cout << std::endl; + return 0; +} diff --git a/1p20.cpp b/1p20.cpp new file mode 100644 index 0000000..7309b2c --- /dev/null +++ b/1p20.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * Exercise 1.20 + * + * + */ + +int main () { + + Sales_item item1; + while(std::cin >> item1) { + std::cout << item1 << std::endl; + } + + return 0; +} diff --git a/1p21.cpp b/1p21.cpp new file mode 100644 index 0000000..c13fb34 --- /dev/null +++ b/1p21.cpp @@ -0,0 +1,20 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * Exercise 1.21 + * + * + */ + +int main () { + + Sales_item item1; + Sales_item item2; + while(std::cin >> item1 >> item2) { + std::cout << item1 + item2 << std::endl; + } + + return 0; +} diff --git a/1p22-2.cpp b/1p22-2.cpp new file mode 100644 index 0000000..2196916 --- /dev/null +++ b/1p22-2.cpp @@ -0,0 +1,22 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * Exercise 1.22 (Not made by me) + * + * + */ + +int main() { + + Sales_item total, item; + if (std::cin >> total) { + while (std::cin >> item) + total += item; + + std::cout << total << std::endl; + } + + return 0; +} diff --git a/1p22.cpp b/1p22.cpp new file mode 100644 index 0000000..cd42a21 --- /dev/null +++ b/1p22.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * Exercise 1.22 + * + * + * + */ + +int main () { + + Sales_item item1; + Sales_item sum; + if(std::cin >> sum) { + while(std::cin >> item1) { + sum += item1; + } + + std::cout << sum << std::endl; + } + + return 0; +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..40ebce8 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +CC1=clang++ +CC2=g++ +CFLAGS_CLANG=-O0 -Wfatal-errors -Wall -Werror -Wextra -g3 -fsanitize=address -fsanitize=leak -Wpedantic -Wformat=2 -Wshadow -fno-common -std=c++11 +CFLAGS_GCC=-O0 -Wfatal-errors -Wall -Werror -Wextra -g3 -fsanitize=address -fsanitize=leak -Wpedantic -Wformat=2 -Wshadow -fno-common -std=c++11 -Wformat-truncation=2 -Wformat-overflow +BINDIR=bin +SRCS=$(wildcard *.cpp) +OBJS_CLANG=$(patsubst %.cpp, $(BINDIR)/clang/%, $(SRCS)) +OBJS_GCC=$(patsubst %.cpp, $(BINDIR)/gcc/%, $(SRCS)) +MAKEFLAGS += -j$(nproc) + +all: clang gcc + +clean: + rm -rf $(BINDIR)/gcc/* + rm -rf $(BINDIR)/clang/* + +clang: $(OBJS_CLANG) + +gcc: $(OBJS_GCC) + +$(BINDIR)/clang/%: %.cpp + @mkdir -p $(BINDIR)/clang + $(CC1) $(CFLAGS_CLANG) -o $@ $< + +$(BINDIR)/gcc/%: %.cpp + @mkdir -p $(BINDIR)/gcc + $(CC2) $(CFLAGS_GCC) -o $@ $< + +.PHONY: all clean clang gcc diff --git a/comments.cpp b/comments.cpp new file mode 100644 index 0000000..a379c1d --- /dev/null +++ b/comments.cpp @@ -0,0 +1,18 @@ +#include <iostream> +#include <stdio.h> + +/* + * Playing around with comments + * Nothing too important but learned something for sure. + * + */ + +int main (void) { + + std::cout << "/*"; + std::cout << "*/"; + std::cout << /* "*/"*/"; + std::cout << /* "*/" /* "/*" */; + + return 0; +} diff --git a/exercise-template.cpp b/exercise-template.cpp new file mode 100644 index 0000000..c60907f --- /dev/null +++ b/exercise-template.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include "sales_item.hpp" + +/* + * + * Description + * + * + */ + +int main () { + + + return 0; +} @@ -0,0 +1,49 @@ +#include <iostream> + +/* + * + * For looping + * + * + * + * + * + */ + +int main (void) { + + for(int val = 1 ; val <= 10 ; val++) { + std::cout << val << std::endl; + } + + for(int val2 = 10 ; val2 >= 1 ; val2--) { + std::cout << val2 << std::endl; + } + + std::cout << "Done." << std::endl; // We send text to 'cout' a so called 'ostream' + int64_t one = 0; + int64_t two = 0; + int64_t sum = 0; + std::cin >> one >> two; + int64_t original_one = one; + for( ; one <= two ; one++) { + sum += one; + } + + std::cout << "The sum of "<< original_one << " to " << two << " inclusive is: " << sum << std::endl; + int64_t sum2 = 0; + for(int64_t i = -100 ; i <= 100 ; ++i) { + sum2 += i; + } + + std::cout << sum2 << std::endl; + std::cout << "Insert as many numbers as you want" << std::endl; + std::cout << "Press CTRL + D when done." << std::endl; + int value = 0, sum3 = 0; + while(std::cin >> value) { + sum3 += value; + } + + std::cout << "sum of your numbers: " << sum3 << std::endl; + return 0; +} diff --git a/helloworld.cpp b/helloworld.cpp new file mode 100644 index 0000000..56575d4 --- /dev/null +++ b/helloworld.cpp @@ -0,0 +1,9 @@ +#include <iostream> +#include <stdio.h> + +int main (void) { + + std::cout << "Hello world!" << std::endl; + + return 0; +} @@ -0,0 +1,21 @@ +#include <iostream> + +int main () { + + int currVal = 0, val = 0, counter = 1; + if(std::cin >> currVal) { + while(std::cin >> val) { + if (currVal == val) { + ++counter; + } else { + std::cout << "You entered " << currVal << " " << counter << " times." << std::endl; + currVal = val; + counter = 1; + } + + } + std::cout << "You entered " << currVal << " " << counter << " times." << std::endl; + } + + return 0; +} diff --git a/return.cpp b/return.cpp new file mode 100644 index 0000000..c1ee4e0 --- /dev/null +++ b/return.cpp @@ -0,0 +1,3 @@ +int main () { + return 69; +} diff --git a/sales_item.hpp b/sales_item.hpp new file mode 100644 index 0000000..ff86669 --- /dev/null +++ b/sales_item.hpp @@ -0,0 +1,142 @@ +/* + * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. + * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the + * copyright and warranty notices given in that book: + * + * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." + * + * + * "The authors and publisher have taken care in the preparation of this book, + * but make no expressed or implied warranty of any kind and assume no + * responsibility for errors or omissions. No liability is assumed for + * incidental or consequential damages in connection with or arising out of the + * use of the information or programs contained herein." + * + * Permission is granted for this code to be used for educational purposes in + * association with the book, given proper citation if and when posted or + * reproduced.Any commercial use of this code requires the explicit written + * permission of the publisher, Addison-Wesley Professional, a division of + * Pearson Education, Inc. Send your request for permission, stating clearly + * what code you would like to use, and in what specific way, to the following + * address: + * + * Pearson Education, Inc. + * Rights and Permissions Department + * One Lake Street + * Upper Saddle River, NJ 07458 + * Fax: (201) 236-3290 +*/ + +/* This file defines the Sales_item class used in chapter 1. + * The code used in this file will be explained in + * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators) + * Readers shouldn't try to understand the code in this file + * until they have read those chapters. +*/ + +#ifndef SALESITEM_H +// we're here only if SALESITEM_H has not yet been defined +#define SALESITEM_H + +// Definition of Sales_item class and related functions goes here +#include <iostream> +#include <string> + +class Sales_item { +// these declarations are explained section 7.2.1, p. 270 +// and in chapter 14, pages 557, 558, 561 +friend std::istream& operator>>(std::istream&, Sales_item&); +friend std::ostream& operator<<(std::ostream&, const Sales_item&); +friend bool operator<(const Sales_item&, const Sales_item&); +friend bool +operator==(const Sales_item&, const Sales_item&); +public: + // constructors are explained in section 7.1.4, pages 262 - 265 + // default constructor needed to initialize members of built-in type + Sales_item() = default; + Sales_item(const std::string &book): bookNo(book) { } + Sales_item(std::istream &is) { is >> *this; } +public: + // operations on Sales_item objects + // member binary operator: left-hand operand bound to implicit this pointer + Sales_item& operator+=(const Sales_item&); + + // operations on Sales_item objects + std::string isbn() const { return bookNo; } + double avg_price() const; +// private members as before +private: + std::string bookNo; // implicitly initialized to the empty string + unsigned units_sold = 0; // explicitly initialized + double revenue = 0.0; +}; + +// used in chapter 10 +inline +bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) +{ return lhs.isbn() == rhs.isbn(); } + +// nonmember binary operator: must declare a parameter for each operand +Sales_item operator+(const Sales_item&, const Sales_item&); + +inline bool +operator==(const Sales_item &lhs, const Sales_item &rhs) +{ + // must be made a friend of Sales_item + return lhs.units_sold == rhs.units_sold && + lhs.revenue == rhs.revenue && + lhs.isbn() == rhs.isbn(); +} + +inline bool +operator!=(const Sales_item &lhs, const Sales_item &rhs) +{ + return !(lhs == rhs); // != defined in terms of operator== +} + +// assumes that both objects refer to the same ISBN +Sales_item& Sales_item::operator+=(const Sales_item& rhs) +{ + units_sold += rhs.units_sold; + revenue += rhs.revenue; + return *this; +} + +// assumes that both objects refer to the same ISBN +Sales_item +operator+(const Sales_item& lhs, const Sales_item& rhs) +{ + Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return + ret += rhs; // add in the contents of (|rhs|) + return ret; // return (|ret|) by value +} + +std::istream& +operator>>(std::istream& in, Sales_item& s) +{ + double price; + in >> s.bookNo >> s.units_sold >> price; + // check that the inputs succeeded + if (in) + s.revenue = s.units_sold * price; + else + s = Sales_item(); // input failed: reset object to default state + return in; +} + +std::ostream& +operator<<(std::ostream& out, const Sales_item& s) +{ + out << s.isbn() << " " << s.units_sold << " " + << s.revenue << " " << s.avg_price(); + return out; +} + +double Sales_item::avg_price() const +{ + if (units_sold) + return revenue/units_sold; + else + return 0; +} +#endif @@ -0,0 +1,25 @@ +#include <iostream> + +/* + * Sum program in C++ + * + * Mainly to learn about cin and cout + * + * Also learned a bit about << and >> + * + * + */ + +int main (void) { + + int num1 = 0; + int num2 = 0; + std::cout << "Enter two numbers please" << std::endl; // We send text to 'cout' a so called 'ostream' + std::cin >> num1 >> num2; // here we read test from 'cin' a so called 'istream'. We put the first input in num1 and 2nd input in num2 + std::cout << "num1: " << num1 // 'cout' same thing here, and these can be multiline which can be nice + << " num2: " << num2 + << "\nYour sum is: damn " << num1+num2 + << "\nYour product is: damn " << num1*num2 << std::endl; + + return 0; +} diff --git a/while.cpp b/while.cpp new file mode 100644 index 0000000..189d66a --- /dev/null +++ b/while.cpp @@ -0,0 +1,40 @@ +#include <iostream> + +/* + * + * A little bit of while looping + * + * + * + * + * + */ + +int main (void) { + + int val = 1; + int val2 = 10; + while(val <= 10) { + std::cout << val << std::endl; + val++; + } + + while(val2 >= 1) { + std::cout << val2 << std::endl; + val2--; + } + + std::cout << "Done." << std::endl; // We send text to 'cout' a so called 'ostream' + uint64_t sum = 0; + uint64_t one = 0; + uint64_t two = 0; + std::cin >> one >> two; + uint64_t original_one = one; + while(one <= two) { + sum += one; + one++; + } + + std::cout << "The sum of "<< original_one << " to " << two << " inclusive is: " << sum << std::endl; + return 0; +} |