注:本文练习题均出自《Essential C++》第一章

练习1,1 从一个简单程序开始

#include<iostream>
#include<string>
using namespace std; int main()
{
string user_name;
cout << "Please enter your first name :";
cin >> user_name;
cout << '\n'
<< "Hello,"
<< user_name
<< "... and goodbye!\n"; return ;
}

1,将string头文件注释掉,重新编译这个程序,会发生什么事?

目前还没有发现会发生什么事。

2,将using namespace std注释掉,重新编译,会发生什么事?

3,将函数名main()改为my_main(),然后重新编译,有什么结果?

练习1.2

将上述程序的内容进行扩充(1)要求用户同时输入名字(first name)和姓氏(last name);(2)修改输出结果,同时打印姓氏和名字。

1,定义两个string对象:string first_name,last_name;

2,定义一个vector,储存两个string对象:vector<string> usr_name(2);

#include<iostream>
#include<string>
using namespace std; int main()
{
string first_name,last_name;
cout << "Please enter your first name :";
cin >> first_name;
cout << "hi, " << first_name
<< "Please enter your last name: "; cin >> last_name;
cout << '\n';
cout << "Hello, "
<< first_name << ' ' << last_name
<< "... and goodbye!\n"; return ;
}

练习1.3

编写一个程序,能够询问用户的姓名,并读取用户所输入的内容。请确保用户输入的名称长度大于两个字符。如果用户的确输入了有效名称,就响应一些信息。

请以两种方式实现:第一种使用C-style字符串,第二种使用string对象。

1,C-style字符串

首先,我们必须决定user_name的长度;接下来,利用标准库的strlen()函数获得user_name的长度,cstring头文件中有strlen()的声明。

如果用户输入的字符串长度大于之前已经输入的字符,就没有足够的空间来存放终止字符(null字符)。为了防止这种事情的发生,我以iostream操纵符(manipulator)setw()保证不会读入超过127个字符。由于用到了setw()操纵符,因此必须包含iomanip头文件。

#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std; int main()
{
const int nm_size = ; //必须分配一个大小固定的空间
char user_name[nm_size];
cout << "Please enter your name: ";
cin >> setw(nm_size) >> user_name; switch(strlen(user_name))
{
case :
cout << "That is a very big name,indeed --"
<< "we may have needed to shorten it\n"
<< "In any case,\n"; default:
cout << "Hello, " << user_name
<< " -- happy to make your acquaintance!\n";
break;
} return ;
}

2,string对象(推荐)

#include<iostream>
#include<string>
using namespace std; int main()
{
string user_name;
cout << "Please enter your name: ";
cin >> user_name; switch(user_name.size()){
case :
cout << "Ah,the user with no name. ";
break; case :
cout << "A 1-character name? Hmm,have you read Kafka?: ";
break; default:
cout << "Hello, " << user_name
<< "-- happy to make your acquaintance!\n";
break;
}
return ;
}

练习1.4

编写一个程序,从标准输入设备读取一串整数,并将读入的整数依次放到array及vector,然后遍历这两种容器,求取数值综合。将总和及平均值输出至标准输出设备。

两者之间的区别

  • array的大小必须固定,vector可以动态地随着元素的插入而扩展储存空间。
  • array并不储存自身大小。
//使用vector 

#include<iostream>
#include<vector>
using namespace std; int main()
{
vector<int> ivec;
int ival,sum;
while(cin >> ival)
ivec.push_back(ival); for(int sum = ,ix = ;ix < ivec.size();++ix) //遍历vector元素,一一累加
sum += ivec[ix]; int average = sum / ivec.size(); cout << "Sum of " << ivec.size()
<< " elements: " << sum
<< ". Average: " << average << endl;
}
//使用array 

#include<iostream>
using namespace std; int main()
{
const int array_size = ;
int ia[array_size],sum;
int ival,icnt = ; while(cin >> ival && icnt < array_size)
ia[icnt++] = ival; for(int sum = ,ix = ;ix < icnt;++ix)
sum += ia[ix]; int average = sum / icnt; cout << "Sum of " << icnt
<< " elements: " << sum
<< ". Average: " << average << endl;
}

练习1.5

使用你最称手的编辑工具,输入两行(或更多)文字并存盘。然后编写一个程序,打开该文本文件,将其中每个字都读取到一个vector<string>对象中。遍历该vector,将内容显示到cout。然后利用泛型算法sort(),对所有文字排序:

#include<algorithm>
sort( container.begin(),container.end() );

再将排序后的结果输出到另一个文件。

#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std; int main()
{
string word;
ifstream in_file("D:\\Documents\\text.txt");
if(!in_file)
{
cerr << "oops! unable to open input file\n";
return -;
} ofstream out_file("D:\\Documents\\text.sort");
if(!out_file)
{
cerr << "oops! unable to open output file\n";
return -;
} string world;
vector < string > text;
while(in_file >> word)
text.push_back(word); int ix;
cout << "unsorted text: \n"; for(ix = ;ix < text.size();++ix)
cout << text[ix] << ' ';
cout << endl; sort(text.begin(),text.end()); out_file << "sorted text: \n";
for(ix = ;ix < text.size();++ix)
out_file << text[ix] << ' ';
out_file << endl; return ;
}

C++编程基础练习的更多相关文章

  1. 第二章 Matlab面向对象编程基础

    DeepLab是一款基于Matlab面向对象编程的深度学习工具箱,所以了解Matlab面向对象编程的特点是必要的.笔者在做Matlab面向对象编程的时候发现无论是互联网上还是书店里卖的各式Matlab ...

  2. [.net 面向对象编程基础] (1) 开篇

    [.net 面向对象编程基础] (1)开篇 使用.net进行面向对象编程也有好长一段时间了,整天都忙于赶项目,完成项目任务之中.最近偶有闲暇,看了项目组中的同学写的代码,感慨颇深.感觉除了定义个类,就 ...

  3. Android开发4: Notification编程基础、Broadcast的使用及其静态注册、动态注册方式

    前言 啦啦啦~(博主每次开篇都要卖个萌,大家是不是都厌倦了呢~) 本篇博文希望帮助大家掌握 Broadcast 编程基础,实现动态注册 Broadcast 和静态注册 Broadcast 的方式以及学 ...

  4. T-Sql编程基础

    T-sql编程 入门小游戏 T-sql编程基础,包括声明变量,if判断,while循环,以及使用一些基本函数. 记得在学校的时候,写过一个二人对打的文字输出游戏. 上代码 alter proc usp ...

  5. [Java入门笔记] 面向对象编程基础(二):方法详解

    什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...

  6. 如何夯实(Java)编程基础,并深入学习和提高

    如何夯实(Java)编程基础,并深入学习和提高? 240赞同反对,不会显示你的姓名 匿名用户 240 人赞同 多学习...网上自学的学习网站很多,见以下榜单~一.汇总榜单: 公开课_学习网站导航 收录 ...

  7. Web编程基础--HTML、CSS、JavaScript 学习之课程作业“仿360极速浏览器新标签页”

    Web编程基础--HTML.CSS.JavaScript 学习之课程作业"仿360极速浏览器新标签页" 背景: 作为一个中专网站建设出身,之前总是做静态的HTML+CSS+DIV没 ...

  8. LINQ to XML 编程基础

    1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: 隐藏行号 复制代码 ?创建 XML public static void CreateDocumen ...

  9. [.net 面向对象编程基础] (2) 关于面向对象编程

    [.net 面向对象编程基础]  (2)  关于面向对象编程 首先是,面向对象编程英文 Object-Oriented Programming 简称 OOP 通俗来说,就是 针对对象编程的意思 那么问 ...

  10. [.net 面向对象编程基础] (3) 基础中的基础——数据类型

    [.net 面向对象编程基础] (3) 基础中的基础——数据类型 关于数据类型,这是基础中的基础. 基础..基础..基础.基本功必须要扎实. 首先,从使用电脑开始,再到编程,电脑要存储数据,就要按类型 ...

随机推荐

  1. 【小白的CFD之旅】26 何为收敛

        小白最近对流体计算的收敛产生了困惑.以前在学习高等数学的时候,小白接触过了级数的收敛,由于当时贪玩,并未将其放在心上,因此大学结束了小白也只是记住有这么一个名词罢了.现如今在利用CFD的过程中 ...

  2. chorme插件 ,在浏览器上模拟手机,pad 查看网页|前端技术开发必备插件

    网址:http://lab.maltewassermann.com/viewport-resizer/使用方法:1在chrome浏览器上方右击,显示书签栏 2 打开插件网址,将<ignore_j ...

  3. 每日英语:As World's Kids Get Fatter, Doctors Turn To The Knife

    Daifailluh al-Bugami was just a year old when his parents noticed that his lips turned blue as he sl ...

  4. export default与export的区别

    1.export default 和export都可以用于导出常量,函数,文件,模块等: 2.可以在模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使 ...

  5. 一句命令激活windows/office

    激活windows 服务器地址:kms.03k.org (点击检测KMS服务器是否正常) 更新: 脚本维护更新:2016-11-02 服务端版本:2017-06-17 (1111) 有疑问可以戳QQ群 ...

  6. 单独的 python 脚本文件使用 django 自带的 model

    django1.9.5&python3.4.4 文件结构 在一个爬虫脚本中将爬取的数据通过django自带的model保存到数据库   修改的文件(其余pycharm新建Django项目生成, ...

  7. 【Miktex】使用教程以及数学符号整理总结

    LaTeX是当今世界上最流行和使用最为广泛的 TeX格式.它构筑在 Plain TeX的基础之上,并加进了很多的功能以使得使用者可以更为方便的利用 TeX的强大功能.使用 LaTeX基本上不需要使用者 ...

  8. JIRA python篇之展示多人未完成任务列表

    [本文出自天外归云的博客园] 通过python中的jira类我们可以方便的操作jira,获取一些我们想要再加工的信息. 这里举例,用html页面的形式展示分派给组内每个人的任务(未完成的.正在进行中的 ...

  9. CSS超过指定的宽度加省略号

    /*table-layout:fixed 会使表格均等分*/ #TreeView1 table { width:290px; table-layout: fixed; } #TreeView1 td: ...

  10. Caused by: java.io.IOException: Added a key not lexically larger than previous.

    为了重复这个实验,遇到不少坑 https://www.iteblog.com/archives/1889.html /** * Created by Administrator on 2017/8/1 ...