Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- #include <thread>
- #include <mutex>
- #include <vector>
- #include <stdlib.h>
- void calculate(long long first_number, long long second_number, char &operation)
- {
- if (operation == '+')
- {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- std::cout << first_number + second_number << std::endl;
- }
- if (operation == '-')
- {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- std::cout << first_number - second_number << std::endl;
- }
- if (operation == '*')
- {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- std::cout << first_number * second_number << std::endl;
- }
- if (operation == '/')
- {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- std::cout << first_number / second_number << std::endl;
- }
- }
- void print_menu()
- {
- std::cout << "Input your expression (For example, 1+2)" << std::endl;
- std::cout << "Possible opeartions are: + - * /" << std::endl;
- }
- int main(){
- std::vector<std::thread> thread_vector;
- std::string user_input;
- std::vector<std::string> command;
- print_menu();
- while (1){
- std::getline(std::cin, user_input);
- if (user_input == "exit"){
- break;
- }
- char *operation = &user_input[1];
- std::string::size_type t;
- long long first_number = std::atoi(&user_input[0]);
- long long second_number = std::atoi(&user_input[2]);
- std::thread count(calculate, first_number, second_number, *operation);
- thread_vector.push_back(std::move(count));
- }
- for (int i = 0; i < thread_vector.size(); i++){
- thread_vector[i].join();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement