2.2
//:S02:Area.cpp #include<iostream> using namespace std; int main() { const float pi=3.141592654; float radius; cout<<"Enter the radius: "; cin>>radius; cout<<"The area is "<<pi * radius * radius<<endl; } /*Sample Exclusion: Enter the radius: 10 The area is 314.159 */ ///:~
//:S02:WordCount.cpp #include<iostream> #include<string> #include<fstream> using namespace std; int main() { ifstream in("WordCount.cpp"); string s; int i=0; while (in>>s) { i++; } cout<<"Number of words = "<<i<<endl; } ///:~
//:S02:WordCount2.cpp #include<iostream> #include<string> #include<fstream> using namespace std; int main() { ifstream in("WordCount.cpp"); string s; string equal="word"; int i=0; while (in>>s) { if (s==equal) { i++; } } cout<<"the string \""+equal+"\" appeared "<<i<<" times!"<<endl; } ///:~
//:S02:FillVector.cpp #include<iostream> #include<string> #include<fstream> #include<vector> using namespace std; int main() { ifstream in("FillVector.cpp"); string line; vector<string>v; while (getline(in,line)) { v.push_back(line); } int nlines=v.size(); for (int i=0;i<nlines;i++) { cout<<nlines-i<<":"<<v[nlines-i-1]<<endl; } } ///:~
//:S02:FillVector.cpp #include<iostream> #include<string> #include<fstream> #include<vector> using namespace std; int main() { ifstream in("FillVector2.cpp"); string line,nlines; vector<string>v; while (getline(in,line)) { v.push_back(line); } for (int i=0;i<v.size();i++) { nlines += v[i]+"\n"; } cout<<nlines; } ///:~
//:S02:FileView.cpp #include<iostream> #include<string> #include<fstream> using namespace std; int main() { ifstream in("FileView.cpp"); string line; while (getline(in,line)) { cout<<line;//没有换行符 cin.get();//用户输入回车键 } } ///:~
//:S02:FloatVector.cpp #include<iostream> #include<vector> using namespace std; int main() { vector<float>v; for (int i=0;i<25;i++) { v.push_back(i+0.5); } for (i=0;i<v.size();i++) { cout<<v[i]<<" "; } cout<<endl; } ///:~
//:S02:FloatVector2.cpp #include<iostream> #include<vector> using namespace std; int main() { vector<float>v1,v2; for (int i=0;i<25;i++) { v1.push_back(i+0.5); v2.push_back(25-i-1); } vector<float>v3; for (i=0;i<v1.size();i++) { v3.push_back(v1[i]+v2[i]); } for (i=0;i<v1.size();i++) { cout<<v1[i]<<"+"<<v2[i]<<"="<<v3[i]<<endl; } } ///:~
//:S02:FloatVector3.cpp #include<iostream> #include<vector> using namespace std; int main() { vector<float>v; for (int i=0;i<25;i++) { v.push_back(i); } for (i=0;i<v.size();i++) { cout<<v[i]<<" "; } cout<<endl; for (i=0;i<v.size();i++) { v[i]=v[i]*v[i]; } for (i=0;i<v.size();i++) { cout<<v[i]<<" "; } cout<<endl; } ///:~