C - 如何制作一个简单的计时器,以及如何使用基于语句的语句使用
C++ - How to make a simple timer and how would I go about using if statements based on it?
im制作一种虚拟宠物。在某种程度上,我要复制tamagochi。
// VirtualPetProjClassesandObjectsPRCT.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include "Vpet.h"
int main()
{
// Set Name & General Info
std::string pop;
std::cout << "What would you like to call your pet?" << std::endl;
std::cin >> pop;
std::cout << "List of commands you can do with " << pop << std::endl << "'state'" << std::endl << "'quit'" << std::endl << "'feed'." << std::endl;
std::cout << "If you feed bob less than quarter his weight he will gain weight but still be hungry!" << std::endl;
std::cout << "Bob weighs 50 kgs!" << std::endl;
VPet bob(50, false);
bob.feedPet(5);
do
{
//input
std::string input;
std::cin >> input;
if (input == "Quit" || input == "quit")
{
return 0;
}
if (input == "Feed" || input == "feed")
{
std::cout << "Enter the amount you would like to feed " << pop << " - ";
int x;
std::cin >> x;
if (x > (bob.getWeight() * 0.5))
{
std::cout << "Bob can't eat that much!" << std::endl;
}
else
{
bob.feedPet(x);
std::cout << "You have fed " << pop << " " << x << " kgs of food!" << std::endl;
}
}
// State
if (input == "State" || input == "state")
{
std::cout << pop << " weighs: " << bob.getWeight() << std::endl;
if (bob.getHungry())
{
std::cout << pop << " is hungry." << std::endl;
}
else
{
std::cout << pop << " is not hungry." << std::endl;
}
}
} while (0 != 1);
}
ive创建了一个班级和一些功能来喂养宠物,检查其体重,如果他饿了,但我也想制作一个时钟一定的数量已经过去了,我会打印出使用前面定义的bool变量的虚拟宠物饿了。谢谢。
您可以使用库Chrono测量时间板,然后采取相应的行动。
#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
int main () {
auto start = std::chrono::system_clock::now();
while (1) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// In your real code you will perhaps not need the sleep
const auto now = std::chrono::system_clock::now();
const auto duration = now - start;
if ( duration.count() > 2e9) {
break;
}
std::cout << "waiting ..." << std::endl;
}
std::cout <<"done" << std::endl;
}
time ./chrono_example
此输出:
等待...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...等待 ...完成
真实0M2.013S
用户0m0.002s
sys 0m0.004s
在main()
中,创建了一个计时器,该计时器允许PET未燃料5秒,并检查是否每秒未露面。如果在最后一顿饭之前超过5秒钟,则喂宠物并记录喂养时间。计时器运行10秒,然后作为演示停止。
在不同的持续时间内,请查看std::chrono::duration
的助手类型。
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
class FeedTimer {
public:
using Clock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<Clock>;
using Duration = std::chrono::seconds;
FeedTimer(Duration limit, Duration tick) : alive(false),
lastFed(Clock::now()),
limit(limit),
tickRate(tick) { }
~FeedTimer() {
// If running, the thread is stopped when it is destroyed.
if (alive) {
stop();
}
}
// Starts the timer.
void start() {
alive = true;
thread = std::thread(&FeedTimer::update, this);
std::cout << "Timer started.n";
}
// Stops the timer.
void stop() {
alive = false;
thread.join();
std::cout << "Timer stopped.n";
}
void update() {
while (alive) {
TimePoint now = Clock::now();
// Determines if more time has passed than the allowed limit.
if (now - lastFed > limit) {
lastFed = now;
std::cout << "Just fed the pet.n";
} else {
std::cout << "Pet is full.n";
}
std::this_thread::sleep_for(tickRate);
}
}
private:
std::atomic<bool> alive; // Thread stops when this is false.
std::thread thread;
TimePoint lastFed; // The last time the pet was fed.
Duration limit; // Maximum amount of time the pet can go unfed.
Duration tickRate; // Rate at which the timer runs.
};
int main() {
FeedTimer timer = FeedTimer(std::chrono::seconds(5),
std::chrono::seconds(1));
timer.start();
std::this_thread::sleep_for(std::chrono::seconds(10));
timer.stop();
}
您需要修改它,以便VPet
可以与之交互。一种方法是在PET饥饿并向用户打印消息后,将计时器 stop()
等待,等待用户喂食宠物,将lastFed
设置为std::chrono::steady_clock::now()
,然后再次将计时器设置为start()
。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 如何使更安全的C++变体访问者,类似于切换语句?
- C - 如何制作一个简单的计时器,以及如何使用基于语句的语句使用
- 如何制作增强多指数复合范围语句,等效于x和y
- 为什么在语句"std::cout << std::endl;"中使用时需要命名空间限定,给定依赖于参数的查找?
- 多线程程序只适用于打印语句
- 条件预处理器相对于条件语句的优势
- 将using语句应用于函数的返回类型,而不应用于整个命名空间
- Switch语句只适用于某些情况
- 变量是否可以仅在 if 语句的作用域中定义,类似于通常为 for 循环所做的那样?
- C++:不应用于简单嵌套if-then-else语句的条件