用stl写了一个程序 (实现对文件中的数字出现次数进行统计,有bug求调试

楚江课的救赎 2013-01-25 11:51:23
#include <iostream>
#include<vector>
#include<list>
#include<iterator>
#include<string>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<fstream>
#include<algorithm>

using namespace std;
class point{

private:
int x;int y;
public:
point(int a,int b):x(a),y(b){}
void show(){cout<<x<<" "<<y<<endl;}
int get_x(){return x;}
int get_y(){return y;}

};
struct anal{

int data;
int count;};
void function(string &,string &);
void show_anal(anal xiaojiba){cout<<xiaojiba.data<<" "<<xiaojiba.count<<endl;}
int main()
{ srand(time(NULL));

vector<point>coll;
coll.reserve(1000);

for(int i=0;i<10;++i)
{ int a=rand()%10,b=rand()%10;
point temp=point(a,b);
coll.push_back(temp);
}
cout<<"please input the file name:";
string name;
cin>>name;
ofstream xiaojiba(name.c_str());
vector<point>::iterator piter=coll.begin();
while(piter!=coll.end())
{ static int count=0;
++count;
xiaojiba<<count<<':'<<(*piter).get_x()<<" ";
xiaojiba<<(*piter).get_y()<<endl;


++piter;
}
cout<<"please input the analysis file:";
xiaojiba.close();
string analy;
cin>>analy;
function(analy,name);

cout << "Hello world!" << endl;
return 0;
}
void function(string &ou,string &in)
{
list<anal>coll1;
ifstream infile(in.c_str());
ofstream outfile(ou.c_str());
int a;char ch;int b;int c;

while(!infile.eof() )
{
infile>>a>>ch>>b>>c;
cout<<a<<ch<<b<<c;
list<anal>::iterator anal_iter=coll1.begin();
anal temp;
while(anal_iter!=coll1.end())
{
if((*anal_iter).data!=b)
anal_iter++;
}
if(anal_iter==coll1.end())
{

temp.data=b;
temp.count=1;
coll1.push_back(temp);
}
else((*anal_iter).count++);


anal_iter=coll1.begin();
while(anal_iter!=coll1.end())
{
if((*anal_iter).data!=c)
anal_iter++;
}
if(anal_iter==coll1.end())
{
temp.data=c;
temp.count=1;
coll1.push_back(temp);
}
else((*anal_iter).count++);
for_each(coll1.begin(),coll1.end(),show_anal);
}

}
...全文
107 2 点赞 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lee_鹿游原 2013-01-27
加断点,自己步步跟踪。
  • 打赏
  • 举报
回复
leolee82 2013-01-27
变量名好怪...
  • 打赏
  • 举报
回复
相关推荐
发帖
C++ 语言
加入

6.0w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2013-01-25 11:51
社区公告
暂无公告