10.1
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
cout<<"Please enter input file name: ";
string name;
cin>>name;
ifstream ist(name.c_str());
if(!ist) perror("can't open input file");
int temp,sum=0;
while(ist>>temp)
{
if(temp!=' ') sum=sum+temp;
}
cout<<"sum= "<<sum<<endl;
system("pause");
}
10.2
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"创建文件raw_temps.txt ..."<<endl;
ofstream ost("E:\\raw_temps.txt");
if(!ost) cout<<"无法创建文件..."<<endl;
int hour;
double temperature;
cout<<"输入时间 温度格式数据(输入0~24范围外的时间退出输入)"<<endl;
while(1)
{
cin>>hour;
if(hour<0||hour>24) break;
cin>>temperature;
ost<<hour<<" "<<temperature<<endl;
}
system("pause");
}
10.3
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
struct Reading
{
int hour;
double temperature;
Reading(int h,double t):hour(h),temperature(t){}
};
int main()
{
ifstream ist("E:\\raw_temps.txt");
if(!ist) cout<<"打开文件失败!"<<endl;
vector<Reading> temps;
int hour,i;
double temperature,sum;
sum=0.0;
while(ist>>hour>>temperature)
{
temps.push_back(Reading(hour,temperature));
}
for(i=0;i<temps.size();i++)
{
sum=sum+temps[i].temperature;
}
sum=sum/temps.size();
cout<<"平均值为: "<<sum<<endl;
system("pause");
}
10.11
/*************************
先找出文本中所有空格的的位置,
再逐个判断文本头与第一个空格之间、空格与空格之间、最后一个空格和文本末之间是否是整数。
*************************/
#include<iostream>
#include<fstream>
#include<vector>
#include<math.h>
using namespace std;
int main()
{
ifstream ist("E:\\text.txt");
if(!ist) cout<<"打开文件失败!"<<endl;
vector<char> ch;
vector<int> space;
char temp;
int i,n,flag=0,num=0,sum=0;
while((temp=ist.get())!=EOF) ch.push_back(temp);
space.push_back(0);
for(i=0;i<ch.size();i++) //找空格
{
if(ch[i]==' ') space.push_back(i);
}
space.push_back(ch.size());
for(n=0;n<space.size()-1;n++) //判断是否为整数
{
for(i=space[n]+1;i<space[n+1];i++)
{
num +=(ch[i]-48)*pow(10.0,(space[n+1]-i-1));
if(ch[i]<'0'||ch[i]>'9') flag=1;
if(flag==1)
{
flag=0;
num=0;
break;
}
}
sum +=num;
}
cout<<sum<<endl;
system("pause");
}
11.1
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream ist("E://C++_excise/11.1/input.txt");
if(!ist) cout<<"can't open file!"<<endl;
ofstream ost("E://C++_excise/11.1/output.txt");
if(!ost) cout<<"can't creat file!"<<endl;
char ch;
while(ist.get(ch))
{
if(isalpha(ch)) ch=tolower(ch);
ost<<ch;
}
ist.close();
ost.close();
}
11.10
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
vector<string> split(const string& s,const string& w)
{
vector<string> v;
string st;
istringstream is(s);
while(is>>st)
{
if(st!=w) v.push_back(st);
}
return v;
}
int main()
{
string s="she is a girl, but I'm not!",w="is";
vector<string> v;
v=split(s,w);
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<endl;
}
system("pause");
}
11.15
#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
int main()
{
ifstream ist("E://C++_excise/11.15/input.txt");
if(!ist) cout<<"can't open file!"<<endl;
int i,k,n=1;
vector<int> v;
while(ist>>i)
{
v.push_back(i);
}
sort(v.begin(),v.end());
for(i=0;i<v.size();i++)
{
for(k=i+1;k<v.size();k++)
{
if(v[i]==v[k]) n++;
else break;
}
cout<<v[i];
if(n==1) cout<<endl;
else cout<<'\t'<<n<<endl;
n=1;
i=k-1;
}
system("pause");
}