Commit 7525507b authored by Michael's avatar Michael

Merge branch 'master' of https://gitlab.com/bpcarm/trainings2017

parents 08325452 d3749656
#include <iostream>
int
main()
{
std::cout << "a) Using one statement with one stream insertion operator: " << "1 2 3 4 \n";
std::cout << "b) Using one statement with four stream insertion operators: " << "1 " << "2 " << "3 " << " 4 \n";
std::cout << "c) Using four statements: " << "1 ";
std::cout << "2 ";
std::cout << "3 ";
std::cout << "4 " << std::endl;
return 0;
}
prog:
g++ -E exercise_02_17.cpp -o exercise_02_17.ii
g++ -S exercise_02_17.ii -o exercise_02_17.s
g++ -c exercise_02_17.s -o exercise_02_17.o
g++ exercise_02_17.o -o exercise_02_17
echo exercise_02_17 > .gitignore
clean:
rm exercise_02_17.ii exercise_02_17.s exercise_02_17.o exercise_02_17 .gitignore
test:
./exercise_02_17
#include <iostream>
#include <string>
#include "Date.hpp"
Date::Date(int year, int month, int day)
{
setYear(year);
setMonth(month);
setDay(day);
}
void
Date::setYear(int year)
{
year_ = year;
}
int
Date::getYear()
{
return year_;
}
void
Date::setMonth(int month)
{
if (month < 1) {
month_ = 1;
std::cout << "We are setting month to 1, because it can't be less than 1. " << std::endl;
return;
}
if (month > 12) {
month_ = 1;
std::cout << "We are setting month to 1, because it can't be more than 12. " << std::endl;
return;
}
month_ = month;
}
int
Date::getMonth()
{
return month_;
}
void
Date::setDay(int day)
{
day_ = day;
}
int
Date::getDay()
{
return day_;
}
#include <string>
class Date
{
public:
Date(int year, int month, int day);
void setYear(int year);
void setMonth(int month);
void setDay(int day);
int getYear(void);
int getMonth(void);
int getDay(void);
private:
int year_;
int month_;
int day_;
};
#include <iostream>
#include "Date.hpp"
int
main()
{
Date date1(1999, 12, 11);
Date date2(1969, 777, 30);
std::cout << date1.getYear() << "/" << date1.getMonth() << "/" << date1.getDay() << std::endl;
std::cout << date2.getYear() << "/" << date2.getMonth() << "/" << date2.getDay() << std::endl;
return 0;
}
exercise_03_15: exercise_03_15.o Date.o | .gitignore
g++ $^ -o $@
Date.ii: Date.cpp
g++ -E $^ -o $@
Date.s: Date.ii
g++ -S $^ -o $@
Date.o: Date.s
g++ -c $^ -o $@
exercise_03_15.ii: exercise_03_15.cpp
g++ -E $^ -o $@
exercise_03_15.s: exercise_03_15.ii
g++ -S $^ -o $@
exercise_03_15.o: exercise_03_15.s
g++ -c $^ -o $@
.gitignore:
echo "exercise_03_15" > $@
clean:
rm -rf *.ii *.s *.o *.out exercise_03_15 .gitignore debug
test: exercise_03_15
./exercise_03_15 > test.out
diff test.out test.expected > /dev/null 2>&1 && echo PASSED || echo FAILED
.PRECIOUS: exercise_03_15.ii exercise_03_15.s Date.ii Date.s
.SECONDARY: exercise_03_15.ii exercise_03_15.s Date.ii Date.s
dbg: debug/exercise_03_15
debug/exercise_03_15: debug/exercise_03_15.o Date.o | .gitignore
g++ -Wall -Wextra -Werror -g3 $^ -o $@
debug/Date.o: Date.s
g++ -c -Wall -Wextra -Werror -g3 $^ -o $@
debug/Date.s: Date.ii
g++ -S -Wall -Wextra -Werror -g3 $^ -o $@
debug/Date.ii: Date.cpp
g++ -E -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_03_15.o: debug/exercise_03_15.s
g++ -c -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_03_15.s: debug/exercise_03_15.ii
g++ -S -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_03_15.ii: exercise_03_15.cpp | debug
g++ -E -Wall -Wextra -Werror -g3 $^ -o $@
debug:
mkdir -p debug
We are setting month to 1, because it can't be more than 12.
1999/12/11
1969/1/30
#include <iostream>
int
main()
{
double radius;
std::cout << "Enter the radius of circle: ";
std::cin >> radius;
if (radius < 1) {
std::cerr << "Error 1: Radius can't be Negative or zero." << std::endl;
return 1;
}
double perimeter = 2 * 3.14159 * radius;
double area = 3.14159 * radius * radius;
double diameter = 2 * radius;
std::cout << "The diameter of circle is " << diameter << std::endl;
std::cout << "The perimeter of circle is " << perimeter << std::endl;
std::cout << "The area of circle is " << area << std::endl;
return 0;
}
exercise_04_30: exercise_04_30.o | .gitignore
g++ $^ -o $@
exercise_04_30.ii: exercise_04_30.cpp
g++ -E $^ -o $@
exercise_04_30.s: exercise_04_30.ii
g++ -S $^ -o $@
exercise_04_30.o: exercise_04_30.s
g++ -c $^ -o $@
.gitignore:
echo "exercise_04_30" > $@
clean:
rm -rf *.ii *.s *.o *.out exercise_04_30 .gitignore debug
test: exercise_04_30
./exercise_04_30 < test.in > test.out
diff test.out test.expected > /dev/null 2>&1 && echo PASSED || echo FAILED
.PRECIOUS: exercise_04_30.ii exercise_04_30.s
.SECONDARY: exercise_04_30.ii exercise_04_30.s
dbg: debug/exercise_04_30
debug/exercise_04_30: debug/exercise_04_30.o | .gitignore
g++ -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_04_30.o: debug/exercise_04_30.s
g++ -c -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_04_30.s: debug/exercise_04_30.ii
g++ -S -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_04_30.ii: exercise_04_30.cpp | debug
g++ -E -Wall -Wextra -Werror -g3 $^ -o $@
debug:
mkdir -p debug
Enter the radius of circle: The diameter of circle is 11.4
The perimeter of circle is 35.8141
The area of circle is 102.07
#include <iostream>
#include <string>
int
main()
{
std::string mode;
std::cout << "Please choose mode (encode = encoding mode, decode = decoding mode): ";
std::cin >> mode;
if ("encode" == mode) {
int number;
std::cout << "Enter number for encoding: ";
std::cin >> number;
if (number < 1000) {
std::cerr << "Error 1: Please enter four digits number." << std::endl;
return 1;
}
if (number > 9999) {
std::cerr << "Error 1: Please enter four digits number." << std::endl;
return 1;
}
int digit1 = (number / 1000 + 7) % 10;
int digit2 = (number / 100 + 7) % 10;
int digit3 = (number / 10 + 7) % 10;
int digit4 = (number / 1 + 7) % 10;
std::cout << "Encoded number: " << digit3 << digit4 << digit1 << digit2 << std::endl;
return 0;
}
if ("decode" == mode) {
int number;
std::cout << "Enter number for encoding: ";
std::cin >> number;
if (number < 0) {
std::cerr << "Error 1: The number must be four digits." << std::endl;
return 1;
}
if (number > 9999) {
std::cerr << "Error 1: The number must be four digits." << std::endl;
return 1;
}
int digit1 = (number / 1000 + 3) % 10;
int digit2 = (number / 100 + 3) % 10;
int digit3 = (number / 10 + 3) % 10;
int digit4 = (number / 1 + 3) % 10;
std::cout << "Decoded number: " << digit3 << digit4 << digit1 << digit2 << std::endl;
return 0;
}
std::cerr << "Error 2: Please enter encode or decode." << std::endl;
return 2;
}
exercise_04_35: exercise_04_35.o | .gitignore
g++ $^ -o $@
exercise_04_35.ii: exercise_04_35.cpp
g++ -E $^ -o $@
exercise_04_35.s: exercise_04_35.ii
g++ -S $^ -o $@
exercise_04_35.o: exercise_04_35.s
g++ -c $^ -o $@
.gitignore:
echo "exercise_04_35" > $@
clean:
rm -rf *.ii *.s *.o *.out exercise_04_35 .gitignore debug
test2: exercise_04_35
./exercise_04_35 < test2.in > test2.out
diff test2.out test2.expected > /dev/null 2>&1 && echo PASSED || echo FAILED
test1: exercise_04_35
./exercise_04_35 < test1.in > test1.out
diff test1.out test1.expected > /dev/null 2>&1 && echo PASSED || echo FAILED
.PRECIOUS: exercise_04_35.ii exercise_04_35.s
.SECONDARY: exercise_04_35.ii exercise_04_35.s
dbg: debug/exercise_04_35
debug/exercise_04_35: debug/exercise_04_35.o | .gitignore
g++ -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_04_35.o: debug/exercise_04_35.s
g++ -c -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_04_35.s: debug/exercise_04_35.ii
g++ -S -Wall -Wextra -Werror -g3 $^ -o $@
debug/exercise_04_35.ii: exercise_04_35.cpp | debug
g++ -E -Wall -Wextra -Werror -g3 $^ -o $@
debug:
mkdir -p debug
Please choose mode (encode = encoding mode, decode = decoding mode): Enter number for encoding: Encoded number: 4582
Please choose mode (encode = encoding mode, decode = decoding mode): Enter number for encoding: Decoded number: 3393
#include <iostream>
int
main()
{
int number1;
int number2;
std::cout << "Enter first integer: ";
std::cin >> number1;
std::cout << "Enter second integer: ";
std::cin >> number2;
if (0 == number2) {
std::cout << "Error 1: Division by 0, because the denominator cannot be 0. " << std::endl;
return 1;
}
std::cout << number1 << " + " << number2 << " = " << (number1 + number2) << std::endl;
std::cout << number1 << " - " << number2 << " = " << (number1 - number2) << std::endl;
std::cout << number1 << " * " << number2 << " = " << (number1 * number2) << std::endl;
std::cout << number1 << " / " << number2 << " = " << (number1 / number2) << std::endl;
return 0;
}
Program:
g++ -E exercise_02_16.cpp -o exercise_02_16.ii
g++ -S exercise_02_16.ii -o exercise_02_16.s
g++ -c exercise_02_16.s -o exercise_02_16.o
g++ exercise_02_16.o -o exercise_02_16
echo exercise_02_16 > .gitignore
Cleanup:
rm exercise_02_16.ii exercise_02_16.s exercise_02_16.o exercise_02_16 .gitignore
Test:
./exercise_02_16
#include <iostream>
int
main()
{
std::cout << "••••••••• *** * *\n"
<< "* * * * *** * *\n"
<< "* * * * ***** * *\n"
<< "* * * * * * *\n"
<< "* * * * * * *\n"
<< "* * * * * * *\n"
<< "* * * * * * *\n"
<< "* * * * * * *\n"
<< "********* *** * *" << std::endl;
return 0;
}
Program:
g++ -E exercise_02_21.cpp -o exercise_02_21.ii
g++ -S exercise_02_21.ii -o exercise_02_21.s
g++ -c exercise_02_21.s -o exercise_02_21.o
g++ exercise_02_21.o -o exercise_02_21
echo exercise_02_21 > .gitignore
Clean:
rm exercise_02_21.ii exercise_02_21.s exercise_02_21.o exercise_02_21 .gitignore
Test:
./exercise_02_21
#include <iostream>
int
main()
{
int number1, number2, number3;
std::cout << "Enter three numbers " << std::endl;
std::cin >> number1 >> number2 >> number3;
std::cout << "Sum is " << number1 + number2 + number3 << std::endl;
std::cout << "Average is " << (number1 + number2 + number3) / 3 << std::endl;
std::cout << "Product is " << number1 * number2 * number3 << std::endl;
///small
int small = number1;
if (number2 < small) {
small = number2;
}
if (number3 < small) {
small = number3;
}
std::cout << small << std::endl;
///large
int large = number1;
if (number2 > large) {
large = number2;
}
if (number3 > large) {
large = number3;
}
std::cout << large << std::endl;
return 0;
}
Program:
g++ -E exercise_2_19.cpp -o exercise_2_19.ii
g++ -S exercise_2_19.ii -o exercise_2_19.s
g++ -c exercise_2_19.o -o exercise_2_19.o
g++ exercise_2_19.o -o exercise_2_19
echo exercise_2_19 > .gitignore
Cleanup:
rm exercise_2_19.ii exercise_2_19.s exercise_2_19.o exercise_2_19 .gitignore
Test:
./exercise_2_19
a. for (x = 100; x >= 1; --x) {
std::cout << x << std::endl;
}
b. switch (value % 2) {
case 0:
std::cout << "Even integer" << std::endl;
break;
case 1:
std::cout << "Odd integer" << std::endl;
break;
}
c. for (x = 19; x >= 1; x -= 2) {
std::cout << x << std::endl;
}
d. counter = 2;
do
{
std::cout << counter << std::endl;
counter += 2;
} while (counter <= 100);
#include <iostream>
int
main()
{
double total = 0;
while (true) {
int productNumber;
std::cout << "Enter product number (-1 to quit): ";
std::cin >> productNumber;
if (-1 == productNumber) {
break;
}
int quantity;
std::cout << "Enter quantity sold: ";
std::cin >> quantity;
if (quantity <= 0) {
std::cerr << "Error 1: Quantity can not be negative." << std::endl;
return 1;
}
switch (productNumber) {
case 1: total += quantity * 2.98; break;
case 2: total += quantity * 4.50; break;
case 3: total += quantity * 9.98; break;
case 4: total += quantity * 4.49; break;
case 5: total += quantity * 6.87; break;
default:
std::cerr << "Error 2: Your product number is invalid." << std::endl;
return 2;
}
}
std::cout << "Total is " << total << std::endl;
return 0;
}
exercise_05_14: exercise_05_14.o | .gitignore
g++ $^ -o $@
exercise_05_14.ii: exercise_05_14.cpp
g++ -E $^ -o $@
exercise_05_14.s: exercise_05_14.ii
g++ -S $^ -o $@
exercise_05_14.o: exercise_05_14.s
g++ -c $^ -o $@
.gitignore:
echo "exercise_05_14" > $@
clean:
rm -rf *.ii *.s *.o exercise_05_14 .gitignore *.out debug
test1: exercise_05_14
@./exercise_05_14 < test1.in > test1.out
@diff test1.out test1.expected > /dev/null 2>&1 && echo PASSED || echo FAILED
.PRECIOUS: exercise_05_14.ii exercise_05_14.s
.SECONDARY: exercise_05_14.ii exercise_05_14.s
dbg: debug/exercise_05_14
debug/exercise_05_14: debug/exercise_05_14.o
g++ -Wall -Wextra -Werror -g3 debug/exercise_05_14.o -o debug/DecisionMaking
debug/exercise_05_14.o: debug/exercise_05_14.s
g++ -c -Wall -Wextra -Werror -g3 debug/exercise_05_14.s -o debug/exercise_05_14.o
debug/exercise_05_14.s: debug/exercise_05_14.ii
g++ -S -Wall -Wextra -Werror -g3 debug/exercise_05_14.ii -o debug/exercise_05_14.s
debug/exercise_05_14.ii: exercise_05_14.cpp
g++ -E -Wall -Wextra -Werror -g3 exercise_05_14.cpp -o debug/exercise_05_14.ii
debug/exercise_05_14.ii: | debug
debug:
mkdir -p debug
Enter product number (-1 to quit): Enter quantity sold: Enter product number (-1 to quit): Enter quantity sold: Enter product number (-1 to quit): Enter quantity sold: Enter product number (-1 to quit): Enter quantity sold: Enter product number (-1 to quit): Enter quantity sold: Enter product number (-1 to quit): Total is 232.29
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment