How can I call a specific object based off cin c++ -
so trying eliminate how reiterate same code in switch statement difference being object getting getanswer() method. appreciated
#include "main.hpp" #include "toolbox.hpp" #include <iostream> #include <chrono> problem1 problem1; problem2 problem2; problem3 problem3; problem4 problem4; problem5 problem5; problem6 problem6; problem7 problem7; problem8 problem8; problem9 problem9; problem10 problem10; problem11 problem11; problem12 problem12; problem13 problem13; problem14 problem14; problem15 problem15; problem16 problem16; void execute::print() { int choice; { std::cout << "please enter # of problem solve: "; std::cin >> choice; std::cout << std::endl; switch (choice) { case 1: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem1.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 2: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem2.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 3: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem3.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 4: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem4.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 5: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem5.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 6: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem6.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 7: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem7.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 8: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem8.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 9: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem9.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 10: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem10.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 11: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem11.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 12: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem12.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 13: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem13.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 14: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem14.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 15: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem15.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } case 16: { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << problem16.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; break; } default: std::cout << "problem not exist or not solved yet" << std::endl; } } while (choice != 17);
}
i new programming , looking solution allows me pick correct class object , print out without having large switch statement takes way space
use function template , move details function template.
template <typename problem> void getanswer(problem& prob) { auto timepoint1 = std::chrono::high_resolution_clock::now(); std::cout << prob.getanswer() << std::endl; auto timepoint2 = std::chrono::high_resolution_clock::now(); std::cout << std::endl; std::cout << "elapsed time: " << static_cast<std::chrono::duration<float>>(timepoint2 - timepoint1).count() << " seconds" << std::endl; } void execute::print() { int choice; { std::cout << "please enter # of problem solve: "; std::cin >> choice; std::cout << std::endl; switch (choice) { case 1: { getanswer(problem1); break; } case 2: { getanswer(problem2); break; } // etc. } } }
wiki
Comments
Post a Comment