第一章到第三章

程序设计是这样一门艺术,它将问题求解方案描述成计算机可以执行的形式。程序设计中很多工作都花费在求解方案以及对其求精上。通常,只须在真正编写程序求解一个问题的过程中才会对问题本身理解透彻。
为什么学习C++这门程序设计语言呢?学习程序设计不可能不借助一门程序设计语言,而C++直接支持现实世界中的软件所使用的那些关键概念和技术。C++是使用最为广泛的程序设计语言之一,其应用领域几乎没有局限。从大洋深处到火星表面,到处都能发现C++程序的身影。C++是由一个开放的国际标准组织全面考量、精心设计的。在任何一种计算机平台上都能找到高质量和免费的C++实现。而且,你用C++所学到的程序设计思想,大多数都可直接用于其他程序设计语言,如C、C#、Fortran以及Java。最后一个原因,我喜欢C++适合编写优美、高效的代码这一特点。

下面是经典的第一个程序的一个版本。它在你的屏幕上输出"Hello,World!":

#include<iostream>

using namespace std;

int main(void)  //输出 hello world 
{
	cout << "hello world \n";
	system("pause");   //窗口暂停
}

note: return 0 可以省略。这是C++语言的特性。

#include<iostream>
#include<string>

using namespace std;

int main(void)  //输出 first name  
{
	cout << "please enter your first name(knock enter to game over) \n";
	string first_name;
	cin >> first_name;
	cout << "hello:" << first_name << "!\n"
	<< endl;
	system("pause");
}

note: string 是c++独有的特性 。输入字符串,按enter(\n)键结束输入。
cin >> first_name>>last_name;
note:多个输入变量,按enter键进行分割。

#include<iostream>
#include<string>

using namespace std;

int main(void)
{
	string first_name, last_name;
	first_name = "jinhui";
	last_name = "liu";
	cout << last_name + first_name;
	system("pause");
}

note: 标准c++中不存在#include<string.h>.

//计算字符串中单词的个数
/*一,问题描述:

给定一个字符串,统计该字符串中有多少个单词。单词以空格、回车、TAB键 分隔。

比如: "    I come   from \n china"   有4个单词。

注意,字符串不一定以字母开头,也可以从空格开头。

 

二,实现思路:

使用一个 bool isWhiteSpace用来标记当前字符所处的模式:比如,当前字符不是26个字母表中的字母时,说明 处在 ”空格模式“,即它是一个分隔符,而不是单词。

只有当从空格模式转化成 ”单词模式 “时,计数增1。*/

#include<iostream>
#include<string>

using namespace std;

int main(void)
{
	cout << "输入字符串:i am a student" << endl;
	bool iswhitespace = true;
	string s1 = "i am a student";
	int countofword = 0;
	for (int i = 0; i < s1.length(); i++)
	{
		char c = s1.at(i);//返回下标为n的元素引用
		if(c == ' ' || c == '\t' || c == '\n')
		{
			iswhitespace = 1;
		}
		else if(iswhitespace)
		{
			countofword += 1;
			iswhitespace = 0;
		}
	}
	cout << "字符串中单词个数:"<<countofword<<endl;
	system("pause");
}


note:字符串流以空格作为结束标志。
窄化:int a = 1000;char b = a,结果b = -24.
double a = 1000;
int c(a);//c = 1000,类型int
int c{a};//窄带转换报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值