c++初学者求助!!!非求作业!!!
作用是反转列表。。。#include <iostream>
#include <list>
#include <algorithm>
#include <string.h>
#include <fstream>
using namespace std;
int main()
{
ifstream input("input.txt");
ofstream output("output.txt");
if(!input)
{
cout<<"input.txt open error"<<endl;
exit(0);
}
if(!output)
{
cout<<"output.txt open error"<<endl;
exit(0);
}
list<int> p;
list<int>::iterator q;
char one_line[1000];
char *number;
while(!input.eof())
{
input.getline(one_line,1000);
number=strtok(one_line,",");
while(number!=NULL)
{
p.push_back(atoi(number));
number=strtok(NULL,",");
}
reverse(p.begin(),p.end());
for(q=p.begin();q!=p.end();q++)
{
if(q!=p.begin())
output<<",";
output<<*q;
}
output<<endl;
}
input.close();
output.close();
return 0;
}
input.txt内容如下:
2,3,4,2,4,5,2,4,7
56,34,22,45,222
23,22,323,322
output.txt内容如下:
7,4,2,5,4,2,4,3,2
222,45,22,34,56,2,3,4,2,4,5,2,4,7
322,323,22,23,7,4,2,5,4,2,4,3,2,56,34,22,45,222
求高手教我!!!