• 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.
    "
    		   "Input sDigits to time the seconds you want.
    "
    		   "Input mDigits to time the minutes you want.
    "
    		   "For example input s9 to time 9 seconds and
    "
    		   "input m1.5 to time 1 minutes and 30 seconds.
    "
    		   "input q to quit.
    ");
    
    	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()) != '
    ' && 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 == '	')
    			{
    				continue;
    			} else {
    				printf("Lexical Error!!
    ");
    				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!!!
    " << endl;
    #ifdef WIN32
    		system("wscript " TEMPNAME);
    #else
    		system("gdialog --msgbox "Time Is Up!"");
    #endif
    	}
    EndOfFile:
    #ifdef WIN32
    	system("del /s " TEMPNAME);
    #endif
    
    	return 0;
    }
    




  • 相关阅读:
    以太坊DApp开发(2):以太坊智能合约开发环境搭建以及第一个Dapp
    以太坊DApp开发(1):入门-开发环境搭建
    全文搜索引擎 Elasticsearch (四)MySQL如何实时同步数据到ES
    nginx 添加https 配置
    spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
    linux中没有dos2UNIX或者UNIX2dos命令解决办法
    linux 安装redis4.0
    maven 不同环境打包命令
    PowerShell 中使用 mvn 编译报错 Unknown lifecycle phase ".test.skip=true". 解决办法
    通过更改scrapy源码进行spider分发实现一个综合爬虫
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6740220.html
Copyright © 2020-2023  润新知