寻求C++程序设计教程(第二版:钱能著)第三章第四题答案

来源:百度知道 编辑:UC知道 时间:2024/06/07 06:36:11
题目:有一些日期,在文件abc.txt中,后面加*号的表示要加班的日期,试汇总所有每个月25号的天数,如果是加班日,则该天乘2。

abc.txt(如下):

Oct. 25 2003
Oct. 26 2003
Sep. 12 2003*
Juy. 25 2002*
App. 25 2004
是统计含25号的天数,而且有*号的25号应该加一。

那我理解得没错了,程序如下
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
ifstream ifs("abc.txt");
if (!ifs)
{
cerr<<"can not open abc.txt";
exit(-1);
}
string str;
int num=0;
while (getline(ifs, str, '\n'))
{
if (str.find("25")!=string::npos)
{
if (str.find("*")!=string::npos)
{
++ ++num;
}
else
{
++num;
}
}
}
cout<<num;
return 0;
}