概述
习题2-1
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 |
#include
<iostream> #include <string> using namespace std; void main( void ) { string name( "Damon" ); //第一种初始化方式 int age = 98 ; //第二种初始化方式 cout<< "name is " <<name<<endl; cout<<name<< "'s age is" <<age<<endl; system( "pause" ); } |
2-2
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 |
#include
<iostream> #include <string> using namespace std; const double PI = 3 . 1415926 ; //常量代替宏 void main( void ) { int r; std::cout<< "Please input the R " <<endl; std::cin>>r; cout<< "the area of circle is " <<r*r*PI<<endl; system( "pause" ); } |
2-3
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include
<iostream> #include <string> #include <fstream> using namespace std; void main( void ) { string str; static int num = 0 ; ifstream in( "file.txt" ); //打开文件可读 getline(in,str); //把文件读到string对象中,有利于对字符串的操作 for ( int i = 0 ;i<str.size();i++) //用string类的函数 { if (str[i]== ' ' ) { ++num; //空个数 ,单词数比空格多一个 } } cout<<num+ 1 <<endl; system( "pause" ); } |
2-5
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include
<iostream> #include <string> #include <fstream> using namespace std; void main( void ) { string str; string dstr( "you" ); int num = 0 ; ifstream in( "file.txt" ); //打开文件可读 getline(in,str); //把文件读到string对象中,有利于对字符串的操作 for ( int i = 0 ;i<str.size();i++) //用string类的函数 { cout<<str[str.size()- 1 -i]<<endl; } system( "pause" ); } |
2-5
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include
<iostream> #include <string> #include <fstream> #include <vector> using namespace std; void main( void ) { vector<string> words; string word; ifstream in( "file.txt" ); while (in>>word) //>> 是定向输入, >>前面是源,后面是目标in>>word就是将in的内容读取到word内 { words.push_back(word); //存入到vector中 } for ( int i = 0 ;i<words.size();i++) { cout<<words[words.size()- 1 -i]; //逆向输出 } system( "pause" ); } |
2-6
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include
<iostream> #include <string> #include <fstream> #include <vector> using namespace std; void main( void ) { vector<string> words; string word; string line; ifstream in( "file.txt" ); while (in>>word) { words.push_back(word); } for ( int i = 0 ;i<words.size();i++) { line += words[i]; } cout<<line; system( "pause" ); } |
2-8
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include
<iostream> #include <string> #include <fstream> #include <vector> using namespace std; const int _INPUT_NUM = 10 ; void main( void ) { vector< float > myArray; //定义浮点型容器 float j; for ( int i = 0 ; i < _INPUT_NUM; i++) { int k = i + 1 ; float num; cout << "please input the " << k << "number:" << endl; cin >> num; myArray.push_back(num); //将元素放入容器 } for ( int j = 0 ; j < myArray.size(); j++) { cout << myArray[j] << " " ; } system( "pause" ); } |
2-9
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include
<algorithm>
#include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; const int _INPUT_NUM = 3 ; void main( void ) { vector< float > myArray1; //定义浮点型容器 vector< float > myArray2; //定义浮点型容器 vector< float > myArray3; //定义浮点型容器 for ( int i= 0 ;i<_INPUT_NUM;i++) { int k = i+ 1 ; float num; cout<< "please input myArray1 the " <<k<< "number:" <<endl; cin>>num; myArray1.push_back(num); //将元素放入容器 } for ( int i= 0 ;i<_INPUT_NUM;i++) { int k = i+ 1 ; float num; cout<< "please input the myArray1 " <<k<< "number:" <<endl; cin>>num; myArray2.push_back(num); //将元素放入容器 } int myArray2_size = myArray2.size(); //主要算法实现把两个容器归到myArray1中 for ( int i = 0 ;i<myArray2_size;i++) { myArray1.push_back(myArray2[i]); } myArray3 = myArray1; for ( int i = 0 ;i<myArray3.size();i++) { cout<<myArray3[i]<<endl; } system( "pause" ); } |
2-10
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//#include<stdafx.h> #include <algorithm> #include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; const int _INPUT_NUM = 3 ; void main( void ) { vector< float > myArray1; //定义浮点型容器 for ( int i= 0 ;i<_INPUT_NUM;i++) { int k = i+ 1 ; float num; cout<< "please input myArray1 the " <<k<< "number:" <<endl; cin>>num; myArray1.push_back(num*num); //将元素放入容器 } for ( int i = 0 ;i<myArray1.size();i++) { cout<<myArray1[i]<<endl; } system( "pause" ); } |
最后
以上就是慈祥百褶裙为你收集整理的C++编程思想 第二章习题2_2的全部内容,希望文章能够帮你解决C++编程思想 第二章习题2_2所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复