首页 > 代码库 > Tcp/ip实验准备:一个简单的定时器——boost实现
Tcp/ip实验准备:一个简单的定时器——boost实现
tcp/ip实验须要在指定的时间查看结果,为了实验方便,做了一个定时器。用法是:
在命令行输入:timer
输入数字之后,计时对应秒数
输入m数字之后。计时对应分钟数(支持小数分钟数)
输入q退出。
时间到了之后会有3声蜂鸣,并显示Time is up!
OK,显示一个进度条会好用一些。
程序例如以下:
timer.cpp:
//g++ timer.cpp -o timer.exe -lboost_thread-mgw48-mt-1_56 -lboost_system-mgw48-1_56 -static #include <cstdio> #include <cstdlib> #include <cstring> #include <boost/thread.hpp> #include <boost/progress.hpp> #ifdef WIN32 #include <fstream> #include <unistd.h> #define TEMPNAME "WarningTemp~~~~~~.vbs" #endif using namespace std; #define MAXLEN 100 int main() { printf("This is a simple timer.\n" "Input sDigits to time the seconds you want.\n" "Input mDigits to time the minutes you want.\n" "For example input s9 to time 9 seconds and\n" "input m1.5 to time 1 minutes and 30 seconds.\n" "input q to quit.\n"); char op; int seconds; double minutes; char beep = 7; char line[MAXLEN]; int i; #ifdef WIN32 ofstream out; out.open(TEMPNAME); out << "MsgBox \"Time Is Up!\""; out.close(); #endif for (;;) { start: printf(">> "); for (i = 0; (op = getchar()) != ‘\n‘ && i < MAXLEN - 1; ++i) line[i] = op; line[i] = 0; seconds = 0; for (i = 0; line[i] != 0 && i < MAXLEN; ++i) { op = line[i]; if (op == ‘q‘) goto EndOfFile; if (op == ‘m‘) { sscanf(line + (++i), "%lf", &minutes); seconds += (int)(minutes * 60); while (isdigit(line[i]) || line[i] == ‘.‘ || isblank(line[i])) i++; --i; } else if (op == ‘s‘) { sscanf(line + (++i), "%lf", &minutes); seconds += minutes; while (isdigit(line[i]) || isblank(line[i])) i++; --i; } else if (op == ‘ ‘ || op == ‘\t‘) { continue; } else { printf("Lexical Error!!\n"); goto start; } } boost::progress_display prog(seconds); for (i = 0; i < seconds; ++i) { boost::this_thread::sleep (boost::posix_time::seconds(1)); ++prog; } for (i = 0; i < 3; ++i) cout << beep; cout << "Time is up!!!\n" << endl; #ifdef WIN32 system("wscript " TEMPNAME); #else system("gdialog --msgbox \"Time Is Up!\""); #endif } EndOfFile: #ifdef WIN32 system("del /s " TEMPNAME); #endif return 0; }
Tcp/ip实验准备:一个简单的定时器——boost实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。