先这里谈谈个人最近两个月的学程序感受把,到了中期对程序的热情多少有点降温,特别是后面内容的复杂和难度性加大,个人也有偷懒的时候,一个多礼拜都没有进步,挺让人懊恼的,不过在这里给那些和我一样程序员初学者说一句我目前的鸡血座右铭:努力没有尽头,成功不会太远。加油各位!
好下面就简单介绍一下我的小程序,就是一个对你的一段时间的阅读管理和检测程序把,挺简单的用到了vector和set的混用,实现起来就是一些循环和判断语句,下面废话不多说附上代码和运行结果:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//这里先创建一个容器,把想读的书名存进去
string bookname;
vector<string>ivec;
cout<<"这个月你想读的书有哪些:"<<endl;
while(cin>>bookname)
ivec.push_back(bookname);
size_t num=ivec.size();
//这里创建一个set,把已读书存进去
set<string>mark;
string choice,bname;
bool onemonth=false;
srand((unsigned)time(NULL));//根据系统时间获取一个随机数rand()
cin.clear();
while((!onemonth)&&(!ivec.empty()))
{
cin.clear();
cout<<"你现在想读书吗?Y/N"<<endl;
cin>>choice;
if(choice[0]=='y'||choice[0]=='Y')
{
int i=rand()%ivec.size();//这里就是把vector的下标设为随机数,让系统在vector里面随机推荐一本书给我们
bname=ivec[i];
cout<<"我们推荐这本书给你阅读:"<<endl;
cout<<bname<<endl;
mark.insert(bname);
ivec.erase(ivec.begin()+i);
cout<<"一个礼拜过后,你看完这本书了吗?Y/N"<<endl;
cin>>choice;
if(choice[0]=='n'||choice[0]=='N')
{
mark.erase(bookname);//这个时候把没看完的书从已读书中删除了
ivec.push_back(bookname);//存进未读书里面
}
}
cout<<"一个月到了吗?Y/N"<<endl;
cin>>choice;
if(choice[0]=='Y'||choice[0]=='y')
onemonth=true;
}
if(onemonth)
{
if(!mark.empty())
{
cout<<"这个月你读了这些书:"<<endl;
for(set<string>::iterator iter=mark.begin();iter!=mark.end();iter++)
{
cout<<*iter<<" ";
}
}
cout<<endl;
if(!ivec.empty())
{
cout<<"这个月你还有这些书未读:"<<endl;
for(vector<string>::iterator it=ivec.begin();it!=ivec.end();)
{
cout<<*it<<" ";
++it;
}
}
cout<<endl;
if(mark.size()==num)
cout<<"给力!全部书读完,真学霸!";
}
system("pause");
return 0;
}跟我一样广大的初学者,坚持下去!
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/jy_he/article/details/48054833