编序读取附加材料中的歌曲列表文件,在一个文本文件中输出统计数据——总的播放时间

求各位达人们啊尽管是输到txt文档但是运行出的结果确实不对的
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
ifstream in("song.txt");
ofstream out("songout.txt");
if( !in )
{
cout<<"Error: 打开输入文件失败!"<<endl;
return 1;
}
if( !out )
{
cout<<"Error: 打开输出文件失败!"<<endl;
return 1;
}
map<string, int> singerlist;
int ptime = 0;
string line;

while( getline(in, line) )
{
string name;
int min = 0;
int sec = 0;

//歌手
int pos1 = line.find_first_of('.');
int pos2 = line.find(" - ");
if( pos1!=string::npos && pos2!=string::npos )
{
pos1 += 2;
name = line.substr(pos1, pos2-pos1);
singerlist[name]++;
//cout<<name<<" ";
}

//时间
pos1 = line.find_last_of('(');
pos2 = line.find_last_of(':');
if( pos1!=string::npos && pos2!=string::npos )
{
pos1 += 1;
min = atoi(line.substr(pos1, pos2-pos1).c_str());

pos1 = pos2 + 1;
pos2 = line.find_last_of(')');
if( pos2!=string::npos )
{
sec = atoi(line.substr(pos1, pos2-pos1).c_str());
ptime += (min*60+sec);
//cout<<min<<":"<<sec<<" ";
}
}
}
out<<"总的播放时间:"<<(ptime/60)<<"分"<<(ptime%60)<<"秒"<<endl;

in.close();
out.close();

return 0;

第1个回答  2011-11-26
这个 很容易啊
相似回答
大家正在搜