C++编程思想 第二章习题2_2

本文详细介绍了C++编程的基础知识,包括变量初始化、常量替代宏、文件读写操作、字符串处理、循环和条件语句等核心概念,并通过具体代码示例展示了如何在实际场景中应用这些知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

习题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");

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值