gpt4 book ai didi

c++ - 如何调用基于 cin c++ 的特定对象

转载 作者:搜寻专家 更新时间:2023-10-31 00:08:59 26 4
gpt4 key购买 nike

所以我试图消除我在 switch 语句中重复相同代码的方式,唯一的区别是什么对象正在获取 getAnswer() 方法。感谢任何帮助

#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;
do {
std::cout << "Please enter the # of the problem you would like to 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 does not exist or is not solved yet" << std::endl;
}

} while (choice != 17);

我是编程的新手,我只是在寻找一种解决方案,让我能够选择正确的类对象并将其打印出来,而无需占用太多​​空间的大型 switch 语句

最佳答案

使用功能模板,将细节移动到功能模板。

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;
do {
std::cout << "Please enter the # of the problem you would like to solve: ";
std::cin >> choice;
std::cout << std::endl;

switch (choice)
{
case 1:
{
getAnswer(problem1);
break;
}

case 2:
{
getAnswer(problem2);
break;
}

// etc.
}
}
}

关于c++ - 如何调用基于 cin c++ 的特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45808839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com