标题:c++初学者求助!!!非求作业!!!
取消只看楼主
heiphy
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-10-29
结帖率:100%
已结贴  问题点数:10 回复次数:2 
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


求高手教我!!!
搜索更多相关主题的帖子: 作业 
2010-11-22 22:42
heiphy
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-10-29
得分:0 
。。。
2010-11-23 20:27
heiphy
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-10-29
得分:0 
回复 4楼 玩出来的代码
,4楼给力。。。。感谢的必须。
for(q=p.begin();q!=p.end();q++)
     {
         if(q!=p.begin())
            output<<",";
         output<<*q;
     }
     output<<endl;
     p.clear();
我原先写成 clear();了。还是概念不太熟悉啊。
2010-11-23 22:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-326571-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.174176 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved