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;
}
浙公网安备 33010602011771号