ravesli/lesson_68/main.cpp

105 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* main.cpp
*
* Created on: 13 сент. 2021 г.
* Author: alexander
*/
#include <iostream>
#include <string>
int calculate(int a, int b, char c)
{
switch (c)
{
case ('+'):
return a + b;
case ('-'):
return a - b;
case ('*'):
return a * b;
case ('/'):
return a / b;
case ('%'):
return a % b;
default:
exit(0);
}
}
void exercise_1()
{
std::cout << "Введите первое число: ";
int a;
std::cin >> a;
std::cout << "Введите второе число: ";
int b;
std::cin >> b;
std::cout << "Введите символ операции: ";
char c;
std::cin >> c;
std::cout << a << ' ' << c << ' ' << b << " = " << calculate(a, b, c)
<< std::endl;
}
enum class Animal
{
PIG, CHICKEN, GOAT, CAT, DOG, OSTRICH
};
std::string getAnimalName(Animal &a)
{
switch (a)
{
case Animal::PIG:
return "pig";
case Animal::CHICKEN:
return "chicken";
case Animal::GOAT:
return "goat";
case Animal::CAT:
return "cat";
case Animal::DOG:
return "dog";
case Animal::OSTRICH:
return "ostrich";
default:
exit(0);
}
}
int printNumberOfLegs(Animal &a)
{
switch (a)
{
case Animal::PIG:
case Animal::CAT:
case Animal::DOG:
case Animal::GOAT:
return 4;
case Animal::CHICKEN:
case Animal::OSTRICH:
return 2;
default:
exit(0);
}
}
void exercise_2()
{
Animal cat(Animal::CAT);
Animal chicken(Animal::CHICKEN);
std::cout << "A " << getAnimalName(cat) << " has " << printNumberOfLegs(cat) << " legs." << std::endl;
std::cout << "A " << getAnimalName(chicken) << " has " << printNumberOfLegs(chicken) << " legs." << std::endl;
}
int main()
{
exercise_1();
exercise_2();
return 0;
}