程序设计是这样一门艺术,它将问题求解方案描述成计算机可以执行的形式。程序设计中很多工作都花费在求解方案以及对其求精上。通常,只须在真正编写程序求解一个问题的过程中才会对问题本身理解透彻。
为什么学习C++这门程序设计语言呢?学习程序设计不可能不借助一门程序设计语言,而C++直接支持现实世界中的软件所使用的那些关键概念和技术。C++是使用最为广泛的程序设计语言之一,其应用领域几乎没有局限。从大洋深处到火星表面,到处都能发现C++程序的身影。C++是由一个开放的国际标准组织全面考量、精心设计的。在任何一种计算机平台上都能找到高质量和免费的C++实现。而且,你用C++所学到的程序设计思想,大多数都可直接用于其他程序设计语言,如C、C#、Fortran以及Java。最后一个原因,我喜欢C++适合编写优美、高效的代码这一特点。
下面是经典的第一个程序的一个版本。它在你的屏幕上输出"Hello,World!":
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello,World!\n"; //输出"Hello,World!"
return 0;
}有一个名为age的int类型的对象,其中保存的是整数42。通过使用一个字符变量,我们可以从输入中读取一个字符,然后将它打印出来,具体如下:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
cout << "Please enter your first name (followed by ‘enter‘):\n"
string first_name;
cin >> first_name;
cout << "Hello," << first_name << "!\n";
return 0;
}输入操作>>("get from")对是类型敏感的,它读取的值与变量的类型需要一致。例如:
//read name and age
int main(void)
{
cout << "Please enter your first name and age\n";
string first_name;
int age;
cin >> first_name;
cin >> age;
cout << "Hello," << first_name << "(age" << age << ")\n";
return 0;
}一个涉及浮点数的例子:
int main()
{
cout << "Please enter a floating-point value:";
double n;
cin >> n;
cout << "n == " << n
<< "\n n+1 == " << n+1
<< "\n three times n == " << 3*n
<<\ntwice n == " << n+n;
<< "\nsquared of n == " << n*n
<< "\nhalf of n == "<< n/2
<< "\nsquare root of n == " << sqrt(n)
<< endl;
}字符串+意味着连接,例如:
int main()
{
cout << "Please enter your first and second names\n";
string first;
string second;
cin >> first >> second;
string name = first +‘ ‘ +second;
cout >> "Hello, " << name << "\n";
}字符串的比较操作特别有用:
int main()
{
cout << "Please enter two name\n";
string first;
string second;
cin >> fisrt >> second;
if (first == second) cout << "that‘s the same name twice\n";
if (first < second)
cout << first << "is alphabetically before " << second << "\n";
if (fist > second)
cout << first << " is alphabetically after " << second << "\n";
}删除重复单词:
int main()
{
string previous = " ";
string current;
while (cin >> curren)
{
if (previous == curren)
cout << "repeated word:" << current << ‘\n‘;
previous = current;
}
}重复单词统计:
int main()
{
int number_of_words = 0; //我们将单词计数器置为0
string previous = "";
string current;
while (cin >> current)
{
++number_of_current; //每次输入一个单词,就将计数器递增
if (previous == current)
cout << "word number " << number_of_words << "repeated: " << current << "\n";
previous = current;
}
}if语句是最简单的选择语句,可以在两种可选分支中进行选择。例如,
int main()
{
int a = 0;
int b = 0;
cout << "Please enter two integers\n";
cin >> a >> b;
if (a < b)
cout << "max(" << a << "," << b << ") is " << b << "\n";
else
cout << "max(" << a << ", << b << ") is " << b << "\n";
}switch语句,基于数值与多个常量的比较的选择。
int main()
{
const double cm_per_inch = 2.54;
double length = 1;
char unit = ‘a‘;
cout << "Please enter a length followed by a unit(c or i):\n";
cin >> length >> unit;
switch(unit)
{
case "i":
cout << length << "in == " << cm_per_inch*length << "cm\n";
break;
case ‘c‘:
cout << length << "cm == " << length/cm_per_inch << "in\n";
break;
default:
cout << "Sorry, I don‘t know a unit called ‘" << unit << "‘\n";
break;
}
}与if语句相比,switch语句更加清晰易懂,特别是与多个常量进行比较时。关键字switch后括号中的值与一组常量进行比较,每个常量用一个case语句标记。如果该值与某一常量相等,将选择执行该case语句,每个case语句都以break结束。如果该值与任何一个case后的常量都不相等,则选择执行default语句。虽然default语句不是必须的,但我们建议你加上,除非你能够完全确定给出的分支已经覆盖了所有的情况。
while语句
int main()
{
int i = 0;
while (i < 100) {
cout << i << "\t" << square(i) << "\n";
++i;
}
}for语句
int main()
{
for (int i = 0; i < 100; ++i)
{
cout << i << "\t" << square(i) << ‘\n‘;
}
}
函数
int square(int x)
{
return x*x;
}
int main()
{
cout << square(2) << ‘\n‘;
cout << square(10) << ‘\n‘;
}显然,用C++语言描述比用自然语言(英语、汉语)描述更简洁。这一点在很多情况下都适用,比竟,程序语言的目的就是用一种更简洁、准确的方式来描述我们的思想。
函数定义(function definition)的法描述如下:
类型 函数名(参数表) 函数体
其中,类型是函数的返回值类型,函数名是函数的标记,括号内是参数表,函数体是实现函数功能的语句。参数表的每一个元素称为一个参数或形式参数,参数表可以为空。如果不需要函数返回任何结果,返回值类型可以设置为void。
使用函数的原因:当需要将一部分计算任务独立实现的时候,可以将其定义为一个函数,因为这样可以:
(1)实现计算逻辑的分离。
(2)使代码清晰(通过使用函数名)。
(3)利用函数,使得同样的代码在程序中可以多次使用。
(4)减少程序调试的工作量。
向量是一组可以通过索引来访问的顺序存储的数据元素。
vectof<int> v(6); vector<string> philosopher(4);
异常
int main()
{
try
{
vector<int> v;
int x;
while (cin >> x) v.push_back(x);
for(int i = 0; i <= v.size(); ++i)
cout << "v[" << i << "]==" << v[i] << endl;
}catch(out_of_range)
{
cerr << "Oops! Range error\n";
return 1;
}
catch(...)
{
cerr << "Exception: something went wrong\n";
return 2;
}
}原文地址:http://hthinker.blog.51cto.com/5611549/1575170