您现在的位置是:首页 > 文章详情

C++实用编程第一期:通过文件操作和rand随机函数制作打卡则运势程序

日期:2018-11-26点击:438

我们在洛谷,云栖社区都可以看到打卡签到测运势,那么今天我们就来开发这个程序。


首先,我们来分析一下:

1.打卡一天只有一次,所以在我们测运势时不能测第二次。

2.所有的运势要存在一个文件中。

3.我们要用另一个文件来存储今日运势。


我们先来解决第一个问题。


在ctime库中,有一个函数和一个类:

一、time(time_t*)函数
函数定义如下:
time_t time (time_t* timer);
获取系统当前日历时间 UTC 1970-01-01 00:00:00开始的unix时间戳
参数:timer 存取结果的时间指针变量,类型为time_t,指针变量可以为null。如果timer指针非null,则time()函数返回值变量与timer指针一样,都指向同一个内存地址;否则如果timer指针为null,则time()函数返回一个time_t变量时间。
返回值,如果成功,获取当前系统日历时间,否则返回 -1。


二、结构体 struct tm

变量           类型                  说明                         范围
tm_sec         int              每分钟的秒数             [0 - 61]
tm_min        int        每小时后面的分钟数       [0 - 59]
tm_hour       int          凌晨开始的小时数         [0 - 23]
tm_mday     int      从每月份开始算的天数     [1 - 31]

tm_mon       int      从一月份开始的月份数     [0 - 11]
tm_year       int        从1900年开始的年数 
tm_wday     int      从每周天开始算的天数       [0 - 6]
tm_yday      int    一年的第几天,从零开始   [0 - 365]
tm_isdst      int                   夏令时
 
 

这里有几个地方要注意:
1. tm_sec 在C89的范围是[0-61],在C99更正为[0-60]。通常范围是[0-59],只是某些系统会出现60秒的跳跃。
2. tm_mon 是从零开始的,所以一月份为0,十二月份为11。


三、本地时间转换函数localtime(time_t*)


函数原型


struct tm * localtime (const time_t * timer);


将日历时间转换为本地时间,从1970年起始的时间戳转换为1900年起始的时间数据结构

来自大佬原文:https://blog.csdn.net/sweettool/article/details/76167654 


我们来看看code



#include <cstdio> #include <ctime> int main() { time_t rawtime; struct tm *ptminfo; time(&rawtime); ptminfo = localtime(&rawtime); printf("current: %02d-%02d-%02d %02d:%02d:%02d\n", ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec); return 0; }

我们就可以知道现在的时间。


在fstream库中,有ifstream和ofstream,就是文件输入输出流,用法如下:


ifstream fin("date.txt");

fin>>a;

ofstream fout("date.txt");

fout<<"a="<<a<<endl;


就和cin,cout差不多,所以还是很方便的。


技术上的问题都解决了,我们来设计cpp和文件date.txt、luck.txt、num.txt


cpp:



#include <cstdio> #include <ctime> #include <iostream> #include <fstream> #include <string> #include <cstdlib> #include <conio.h> using namespace std; ifstream fin("date.txt"); ofstream fout("date.txt"); ifstream finnum("num.txt"); ifstream finluck("luck.txt"); string lucky[21],unlucky[21],Yi[3],Ji[3]; int Ynum,Jnum; int n; int main() { srand(time(0)); time_t rawtime; struct tm *ptminfo; time(&rawtime); ptminfo = localtime(&rawtime); int year=ptminfo->tm_year + 1900,month=ptminfo->tm_mon + 1,date=ptminfo->tm_mday; int x,y,z; fin>>x>>y>>z; if(x==year&&y==year&&z==date) { cout<<"您今日已打卡\n今日运势:\n宜:\n"; finluck>>Ynum>>Jnum; for(int i=1;i<=Ynum;i++) { finluck>>Yi[i]; cout<<Yi[i]<<endl;; } cout<<"忌:\n"; for(int i=1;i<=Jnum;i++) { finluck>>Ji[i]; cout<<Ji[i]<<endl; } getch(); return 0; } fout<<year<<" "<<month<<" "<<date<<endl; cout<<"今日运势:\n"; Ynum=0+rand()%4; Jnum=0+rand()%4; for(int i=1;i<=10;i++) finluck>>lucky[i]; for(int i=1;i<=10;i++) finluck>>unlucky[i]; int num=0,v[21]; if(Ynum==0) { cout<<"诸事不宜\n"; getch(); return 0; } if(Jnum==0) { cout<<"诸事皆宜\n"; getch(); return 0; } cout<<"宜:\n"; while(num!=Ynum) { int out=1+rand()%10; for(int i=1;i<=num;i++) if(v[i]==out) continue; cout<<lucky[out]<<endl; num++; v[num]=out; } num=0; cout<<"忌:\n"; while(num!=Jnum) { int out=1+rand()%10; for(int i=1;i<=num;i++) if(v[i]==out) continue; cout<<unlucky[out]<<endl; num++; v[num]=out; } return 0; }



这也很明了了。


我们来看luck.txt


(仅供参考)



开电脑 刷题 好好工作 学习C++ 点赞短文 考试QAQ 考膜你赛 关注作者 上B站 上课睡觉 开电脑 刷题 好好工作 学习C++ 点赞短文 考试QAQ 考膜你赛 关注作者 上B站 上课睡觉

好了,今天的内容就到这里,大家可以用它来坑坑小伙伴,也可做运势的参考哦(尽管是随机的!)。


我们下周见!

原文链接:https://yq.aliyun.com/articles/673546
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章