2014月金山WPS实习生C++笔试
哈哈,早上花了半个小时把最后一道大题做出来了
哈哈哈,真希望能拿到面试的机会啊。
readFile.cpp
/*
一个源txt
一个字典txt hello,世界 前一个为字典,后一个为要替换的词
读取源txt,例如,读到的内容为”hello,world”输出为”世界,world”
写到另一个文件中
*/
#include
#include
#include
#include
using namespace std;
const int LIMIT=30;
string find(string ar[][2],int size,const string findstr);
void readFile()
{
ofstream fout;//操作target.txt,写入
ifstream finL;//操作lib.txt
ifstream finS;//操作source.txt
int sizeLib=0;
//vector<vector
string lib[LIMIT][2];//发现vector不能满足,用string数组吧
finL.open(“lib.txt”);
finS.open(“source.txt”);
fout.open(“target.txt”);
if(finL.fail())
cout<<”Lib.txt open failure!”<<endl;
else
cout<<”Lib.txt open success”<<endl;
if(finS.fail())
cout<<”source.txt open failure!”<<endl;
else
cout<<”source.txt open success”<<endl;
if(fout.fail())
cout<<”target.txt open failure!”<<endl;
else
cout<<”target.txt open success”<<endl;
//string libS;//source
//string libT;//target
int i=0;
while(finL.good()&&i<LIMIT)//流正常时
{
char libS[100]={0};//不能写成char *buff形式,否则会出现无法正常初始化错误
char libT[100]={0};//要每次都初始化,否则char数组里面会留有不确定的值
finL.getline(libS,100,’,’);//getline会自动丢弃换行符,长度要设好
lib[i][0]=libS;
//cout<<”libS is”<<libS<<” libS=”<<lib[i][0]<<endl;
finL.getline(libT,100,’\n’);//这里的终止应为’\n’
lib[i][1]=libT;
//cout<<”libT is”<<libT<<” libT=”<<lib[i][1]<<endl;
i++;
sizeLib++;
}
if(finL.eof())
cout<<”Lib.txt read success!”<<endl;
else if(finL.fail())
cout<<”Lib.txt read failure!”<<endl;
finL.close();
/*
cout<<sizeof(lib[0][0])<<endl;
cout<<sizeof(lib[0][1])<<endl;
for(int j=0;j<sizeLib;j++)
cout<<lib[j][0]<<” “<<lib[j][1]<<endl;
*/
//cout<<find(lib,LIMIT,”my”)<<endl; //find测试成功
while(!finS.fail()&&fout.good())
{
string tstr;//
char fstr[100]={0};//从source中读到的字符串
finS.getline(fstr,100,’,’);//源文件的英文要注意”,”
tstr=find(lib,LIMIT,fstr);
cout<<tstr<<endl;
fout<<tstr<<endl;//写入
}
if(finS.eof())
cout<<”source.txt read success!”<<endl;
else if(finS.fail())
cout<<”source.txt read failure!”<<endl;
finL.close();
if(fout.fail())
cout<<”target.txt failure!”<<endl;
fout.close();
cin.get();
}
//找匹配的字符串
//成功则返回替换字符串,否则返回原字符串
string find(string ar[][2],int size,const string findstr)
{
int i=size;
while(i–)
{
if(ar[i][0]==findstr)
return ar[i][1];
}
return findstr;
}
source.txt
hello,哈哈,wawa,ohmygodess,真的
lib.txt
hello,你好1346578
wawa,哦哦哦
world,世界
you,你
my,我的C++棒棒棒
真的,really?
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/2014月金山wps实习生c笔试/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!