Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BP_Self_Programs.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- #include <string>
- #include <conio.h>
- #define s_c(wParam, lParam) ( static_cast<wParam> (lParam) )
- #define r_c(wParam, lParam) ( reinterpret_cast<wParam &> (lParam) )
- template <typename FROM, typename TO, typename STEP, typename SUM>
- auto sum_recursive(FROM from, TO to, STEP step, SUM &sum) -> decltype(&sum) {
- if (from < to) {
- sum += from;
- from += step;
- return sum_recursive(from, to, step, sum);
- }
- return 0;
- }
- int main() {
- size_t choice;
- do {
- system("cls");
- std::cout << "Menu: \n";
- std::cout << "[1] Recursive sum in range with step & variable type choose\n";
- std::cout << "[2] Exit\n";
- std::cout << "Your choice: ";
- std::cin >> choice;
- switch (choice) {
- case 1:
- std::string var_type("");
- auto from(.0), to(.0), step(.0), sum(.0);
- std::cout << "Enter number to sum from: ";
- std::cin >> to;
- std::cout << "Enter number to sum to: ";
- std::cin >> to;
- std::cout << "Enter incrementation: ";
- std::cin >> step;
- std::cout << "Write variable type: \n";
- std::cin >> var_type;
- if (var_type == "float")
- sum_recursive(s_c(float, from), s_c(float, to), s_c(float, step), r_c(float , sum));
- else if (var_type == "double")
- sum_recursive(s_c(double, from), s_c(double, to), s_c(double, step), r_c(double, sum));
- else if (var_type == "int")
- sum_recursive(s_c(int, from), s_c(int, to), s_c(int, step), r_c(int, sum));
- else if (var_type == "size_t")
- sum_recursive(s_c(size_t, from), s_c(size_t, to), s_c(size_t, step), r_c(size_t,sum));
- else {
- std::cout << "\n\nUnsupported type\n\n";
- break;
- }
- std::cout << "\n\nSum: " << sum << std::endl << std::endl;
- _getch();
- }
- } while (choice != 2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement