1.修改Hello.cpp,使它能打印你的名字和年龄(或你的鞋码,爱犬的年龄等,只要你喜欢)。编译并运行修改后的程序
修改前的Hello.cpp
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, World! I am "<< 8 <<" Today!"<<endl;
}
输出结果为:
[root@localhost tttt]# g++ hello.cpp -o hello
[root@localhost tttt]# ./hello
Hello, World! I am 8 Today!
>
修改后的Hello.cpp如下:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello, World! I am "<< 8 <<" Today!"<<endl;
cout<<"My name is "<<"C++"<<" , and i am "<<21<<" years old!"<<endl;
}
输出结果如下:
[root@localhost tttt]# ./hello
Hello, World! I am 8 Today!
My name is C++ , and i am 21 years old!
>
原程序如下:
#include <iostream>
using namespace std;
int main()
{
int r;
cout<<"请输入半径的大小 r = ";
cin>>r;
double s = 3.14 *r * r;
cout<<"圆的面积为 S = "<<s<<endl;
}
执行结果为:
[root@localhost tttt]# ./hello
请输入半径的大小 r = 4
圆的面积为 S = 50.24
原程序如下:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("hello.cpp");
string word;
int n = 0;
while(in >> word){
cout << word << " " << endl;
n++;
}
cout << "以空格隔开的单词数目是: " << n << endl;
return 0;
}
运行结果如下:
源程序如下:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("hello.cpp");
string word = "<<";
string s;
int num = 0;
while(in >> s){
if(s == word){
num++;
}
}
cout << word << "出现的次数为:" << num << endl;
return 0;
}
运行结果如下:
[root@localhost tttt]# g++ num.cpp -o num
[root@localhost tttt]# ./num
<<出现的次数为:4
[root@localhost tttt]#
源代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
ifstream in("filevector.cpp");
string line;
while(getline(in, line)){
v.push_back(line);
}
for(int i = v.size() - 1; i >= 0; --i){
cout << i << ": " << v[i] << endl;
}
}
运行结果如下:
[root@localhost tttt]# g++ filevector.cpp -o filevector
[root@localhost tttt]# ./filevector
19: }
18: }
17: cout << i << “: ” << v[i] << endl;
16: for(int i = v.size() - 1; i >= 0; –i){
15: }
14: v.push_back(line);
13: while(getline(in, line)){
12:
11: string line;
10: ifstream in(“filevector.cpp”);
9: vector v;
8: {
7: int main()
6:
5: using namespace std;
4:
3: #include
2: #include
1: #include
0: #include
[root@localhost tttt]#
源代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
ifstream in("filevector.cpp");
string line;
while(getline(in, line)){
v.push_back(line);
}
for(int i = 0; i < v.size(); i++){
cout << v[i];
}
cout << endl;
}
运行结果如下:
[root@localhost tttt]# ./filevector
》#include #include#include #include using namespace std;int main(){ vector v; ifstream in(“filevector.cpp”); string line; while(getline(in, line)){ v.push_back(line); } for(int i = 0; i < v.size(); i++){ cout << v[i]; } cout << endl;}
[root@localhost tttt]#
源代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
ifstream in("filevector.cpp");
string line;
while(getline(in, line)){
cout << line;
cin.get();
}
return 0;
}
运行结果如下:
源代码如下:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<float> v;
for(int i = 0; i < 25; ++i){
v.push_back(i + i * 0.23);
}
for(int i = 0; i < v.size(); ++i){
cout << v[i] << ", ";
}
cout << endl;
}
运行结果如下:
[root@localhost tttt]# ./vectorfloat
0, 1.23, 2.46, 3.69, 4.92, 6.15, 7.38, 8.61, 9.84, 11.07, 12.3, 13.53, 14.76, 15.99, 17.22, 18.45, 19.68, 20.91, 22.14, 23.37, 24.6, 25.83, 27.06, 28.29, 29.52,
[root@localhost tttt]#
源代码如下:
#include <vector>
using namespace std;
int main()
{
vector<float> v1;
vector<float> v2;
vector<float> v3;
for(int i = 0; i < 25; ++i){
v1.push_back(i + i * 0.23);
}
for(int i = 0; i < 25; ++i){
v2.push_back(i + i * 0.2);
}
for(int i = 0; i < v1.size(); ++i){
v3.push_back(v1[i] + v2[i]);
}
for(int i = 0; i < v3.size(); ++i){
cout << v3[i] << " = " << v1[i] << " + " << v2[i] << endl;
}
}
运行结果如下:
[root@localhost tttt]# g++ vectorfloat.cpp -o vectorfloat
[root@localhost tttt]# ./vectorfloat
0 = 0 + 0
2.43 = 1.23 + 1.2
4.86 = 2.46 + 2.4
7.29 = 3.69 + 3.6
9.72 = 4.92 + 4.8
12.15 = 6.15 + 6
14.58 = 7.38 + 7.2
17.01 = 8.61 + 8.4
19.44 = 9.84 + 9.6
21.87 = 11.07 + 10.8
24.3 = 12.3 + 12
26.73 = 13.53 + 13.2
29.16 = 14.76 + 14.4
31.59 = 15.99 + 15.6
34.02 = 17.22 + 16.8
36.45 = 18.45 + 18
38.88 = 19.68 + 19.2
41.31 = 20.91 + 20.4
43.74 = 22.14 + 21.6
46.17 = 23.37 + 22.8
48.6 = 24.6 + 24
51.03 = 25.83 + 25.2
53.46 = 27.06 + 26.4
55.89 = 28.29 + 27.6
58.32 = 29.52 + 28.8
源代码如下:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<float> v;
for(int i = 0; i < 25; ++i){
v.push_back(i + i * 0.2);
}
cout << "before change!" << endl;
for(int i = 0; i < v.size(); ++i){
cout << v[i] << ", ";
}
cout << endl;
for(int i = 0; i < v.size(); ++i){
v[i] *= v[i];
}
cout << "after change!" << endl;
for(int i = 0; i < v.size(); ++i){
cout << v[i] << ", ";
}
cout << endl;
}
运行结果如下:
[root@localhost tttt]# g++ floatvector.cpp -o floatvector
[root@localhost tttt]# ./floatvector
before change!
0, 1.2, 2.4, 3.6, 4.8, 6, 7.2, 8.4, 9.6, 10.8, 12, 13.2, 14.4, 15.6, 16.8, 18, 19.2, 20.4, 21.6, 22.8, 24, 25.2, 26.4, 27.6, 28.8,
after change!
0, 1.44, 5.76, 12.96, 23.04, 36, 51.84, 70.56, 92.16, 116.64, 144, 174.24, 207.36, 243.36, 282.24, 324, 368.64, 416.16, 466.56, 519.84, 576, 635.04, 696.96, 761.76, 829.44,
[root@localhost tttt]#
这篇博客展示了如何修改C++程序Hello.cpp,使其能打印个人化信息,如名字和年龄。此外,还介绍了如何读取和显示文件内容,包括'filevector.cpp'的源代码行数和'vectorfloat.cpp'的数值运算过程。
33万+

被折叠的 条评论
为什么被折叠?



