有两个C++程序,需要拜托人帮忙写一下,谢谢了!
#include <iostream>
#include <map>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include<algorithm>
#include<functional>
using namespace std;
int cmp(const pair<string,int> & x, const pair<string,int>& y)
{
return x.second > y.second;
}
int main()
{
map<string,int>::iterator it;
map<string,int> ma;
ifstream in("in.txt");// 读入的文件。
ofstream out("result.txt");
string str;
string st;
while( in>>st )
{
it=ma.find(st);
if( it!=ma.end() )
++(*it).second;
else
ma[st]=1;
}
vector< pair<string,int> > vec;
for( it=ma.begin(); it!=ma.end(); ++it )
{
vec.push_back(make_pair(it->first,it->second));
cout<<it->first<<it->second<<endl;
}
sort(vec.begin(),vec.end(),cmp); //要用vector< pair<string,int> > 转一次
cout<<"xiaojiba"<<endl;、
vector<pair<string,int> > ::iterator ivec=vec.begin();
while(ivec!=vec.end())
{
cout<< ivec->first<<" "<<ivec->second<<endl;
out<<ivec->first<<" "<<ivec->second<<endl;
ivec++;
}
//cout<<(*it).first<<" "<<(*it).second<<endl;
return 0;
}
第二个很麻烦的在于怎么浏览目录下的文件呢?主要是第二个有些问题,再次感谢!
第二个。俺们调用是用的linux 的shell借口。
你可以吧du-k 改成tree命令。
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
for (int i = 0; i<argc; i++)
cout<<argv[i]<<endl;
cout << "mysh# ";
cout<<"input filename"<<endl;
string filename;
cin>>filename;
string cmd="du -k "+filename+">xiaojiba.txt";
system(const_cast<char*>(cmd.c_str()));
int count=0;
string temp;
string filelocate=filename+"/xiaojiba.txt";
cout<<filelocate<<endl;
ifstream infile(filelocate.c_str());
while(infile >> temp)
{
count++;
cout<<temp<<endl;
}
cout<<"the number of file is: "<<(count-1)/2<<endl;
cout << "\n";
return 0;
}