Advertisement
Guest User

Untitled

a guest
May 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include <mutex>
  6. #include <vector>
  7. #include <stdlib.h>
  8.  
  9.  
  10. void calculate(long long first_number, long long second_number, char &operation)
  11. {
  12. if (operation == '+')
  13. {
  14. std::this_thread::sleep_for(std::chrono::seconds(1));
  15. std::cout << first_number + second_number << std::endl;
  16. }
  17. if (operation == '-')
  18. {
  19. std::this_thread::sleep_for(std::chrono::seconds(1));
  20. std::cout << first_number - second_number << std::endl;
  21. }
  22. if (operation == '*')
  23. {
  24. std::this_thread::sleep_for(std::chrono::seconds(1));
  25. std::cout << first_number * second_number << std::endl;
  26. }
  27. if (operation == '/')
  28. {
  29. std::this_thread::sleep_for(std::chrono::seconds(1));
  30. std::cout << first_number / second_number << std::endl;
  31. }
  32. }
  33.  
  34. void print_menu()
  35. {
  36. std::cout << "Input your expression (For example, 1+2)" << std::endl;
  37. std::cout << "Possible opeartions are: + - * /" << std::endl;
  38. }
  39.  
  40.  
  41.  
  42. int main(){
  43. std::vector<std::thread> thread_vector;
  44. std::string user_input;
  45. std::vector<std::string> command;
  46. print_menu();
  47. while (1){
  48. std::getline(std::cin, user_input);
  49. if (user_input == "exit"){
  50. break;
  51. }
  52. char *operation = &user_input[1];
  53. std::string::size_type t;
  54. long long first_number = std::atoi(&user_input[0]);
  55. long long second_number = std::atoi(&user_input[2]);
  56. std::thread count(calculate, first_number, second_number, *operation);
  57. thread_vector.push_back(std::move(count));
  58. }
  59. for (int i = 0; i < thread_vector.size(); i++){
  60. thread_vector[i].join();
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement