目录
一、程序清单
程序清单 2.1 m yfirst.cpp
程序清单2.1中的示例非常简单,只包含一个名为main()的函数。myfirst.cpp示例包含下述元素。
• 注释,由前缀//标识。
• 预处理器编译指令#include。
• 函数头:int main()。
• 编译指令using namespace。
• 函数体,用{和}括起。
• 使用C++的cout工具显示消息的语句。
• 结束main()函数的return语句。
// myfirst.cpp--displays a message
#include <iostream> // a PREPROCESSOR directive
int main() // function header
{ // start of function body
using namespace std; // make definitions visible
cout << "Come up and C++ me some time."; // message
cout << endl; // start a new line
cout << "You won't regret it!" << endl; // more output
// If the output window closes before you can read it,
// add the following code:
// cout << "Press any key to continue." <<endl;
// cin.get();
return 0; // terminate main()
} // end of function body
执行结果:
Come up and C++ me some time.
You won't regret it!
程序清单2 .2 carrots.cpp
C++程序是一组函数,而每个函数又是一组语句。C++有好几种语句,下面介绍其中的一些。程序清单2.2提供了两种新的语句。声明语句创建变量,赋值语句给该变量提供一个值。另外,该程序还演示了cout的新功能。
note:
int carrots;
这条语句提供了两项信息:需要的内存以及该内存单元的名称。具体地说,这条语句指出程序需要足够的存储空间来存储一个整数,第二条陚值语句表明,可以对变最的值进行修改 carrots = carrots - 1; / / modify the variable
// carrots.cpp -- food processing program
// uses and displays a variable
#include <iostream>
int main()
{
using namespace std;
int carrots; // declare an integer variable
carrots = 25; // assign a value to the variable
cout << "I have ";
cout << carrots; // display the value of the variable
cout << " carrots.";
cout << endl;
carrots = carrots - 1; // modify the variable
cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
// cin.get();
return 0;
}
执行结果:
I have 25 carrots.
Crunch, crunch. Now I have 24 carrots.
程序清单 2.3 getinfo.cpp
程序清单2.3中的程序对前一个程序进行了扩展,要求在程序运行时输入一个值。为实现这项任务,它使用了 cin,这是与cout对应的用于输入的对象。另外,该程序还演示了cout对象的多功能性。
// getinfo.cpp -- input and output
#include <iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots; // C++ input
cout << "Here are two more. ";
carrots = carrots + 2;
// the next line concatenates output
cout << "Now you have " << carrots << " carrots." << endl;
// cin.get();
// cin.get();
return 0;
}
执行结果:
How many carrots do you have?
12
Here are two more. Now you have 14 carrots.
程序清单2.4 sqrt.cpp
程序清单2.4 演示了库函数sqrt()的用法,它通过包含cmath文件来提供该函数的原型:
// sqrt.cpp -- using the sqrt() function
#include <iostream>
#include <cmath> // or math.h
int main()
{
using namespace std;
double area;
cout << "Enter the floor area, in square feet, of your home: ";
cin >> area;
double side;
side = sqrt(area);
cout << "That's the equivalent of a square " << side
<< " feet to the side." << endl;
cout << "How fascinating!" << endl;
// cin.get();
// cin.get();
return 0;
}
执行结果:
Enter the floor area, in square feet, of your home: 1536
That's the equivalent of a square 39.1918 feet to the side.
How fascinating!
使用库函数
C++库函数存储在库文件中。编译器编译程序时,它必须在库文件搜索您使用的函数。至于自动搜索 哪些库文件,将因编译器而异。如果运行程序清单2.4时,将得到一条消息,指出_sqrt是一个没有定义的外部函数(似乎应当避免),则很可能是由于编译器不能自动搜索数学库(编译器倾向于给函数名添加下划 线前缀——提示它们对程序具有最后的发言权)。如果在UNIX实现中遇到这样的消息,可能需要在命令行结尾使用-lm选项:
CC sqrt.C -lm
在Linux系统中,有些版本的Gnu编译器与此类似:g++ sqrt.C -lm
只包含cmath头文件可以提供原型,但不一定会导致编译器搜索正确的库文件。
程序清单 2.5 ourfunc.cpp 用户定义的函数
对于库函数,在使用之前必须提供其原型,通常把原型放到main()定义之前。但现在您必须提供新函数的源代码。最简单的方法是,将代码放在main()的后面。 程序清单2.5演示了 这些元素。
// ourfunc.cpp -- defining your own function
#include <iostream>
void simon(int); // function prototype for simon()
int main()
{
using namespace std;
simon(3); // call the simon() function
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count); // call it again
cout << "Done!" << endl;
// cin.get();
// cin.get();
return 0;
}
void simon(int n) // define the simon() function
{
using namespace std;
cout << "Simon says touch your toes " << n << " times." << endl;
} // void functions don't need return statements
执行结果:
Simon says touch your toes 3 times.
Pick an integer: 512
Simon says touch your toes 512 times.
Done!
程序清单 2.6 convert.cpp 用户定义的有返回值的函数
// convert.cpp -- converts stone to pounds
#include <iostream>
int stonetolb(int); // function prototype
int main()
{
using namespace std;
int stone;
cout << "Enter the weight in stone: ";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << " stone = ";
cout << pounds << " pounds." << endl;
// cin.get();
// cin.get();
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
执行结果:
Enter the weight in stone: 15
15 stone = 210 pounds.
二、复习题
1.c++程序模块叫什么?
函数。
2.下面的预处理编译器指令是做什么用的?
#include<iostream>
在最终编译之前,将iostream里的文件内容替换该编译指令。
3.下面语句是做什么用的?
using namespace std;
使程序使用std名称空间的定义。
4.什么语句可以用来打印“Hello,world”,然后打印新的一行?
cout<<"Hello,world\n"; //或者cout<<"Hello,world"<<endl;
5.什么语句可以用来创建名为cheeses的整数变量?
int cheeses;
6.什么语句可以用来将32赋值给变量cheeses?
cheeses=32;
7.什么语句可以用来将键盘输入的值读入变量cheeses中?
cin>>cheeses;
8.什么语句可以用来打印“We have X varieties of cheeses”,其中X为变量cheese的当前值。
cout<<"We have "<<cheeses<<"varieties of cheeses"<<endl;
9.下面的函数原型指出关于函数的哪些信息?
int froop(double t);
void rattle(int n);
int prune(void);
int froop(double)
指出函数在调用时需要输入一个double类型的参数,函数返回一个int类型值。
void rattle(int n)
指出函数在调用的时候需要输入一个int类型参数,且该函数无返回值。
int prune(void)
指出函数不接收任何输入参数,函数返回一个int值。
10.定义函数时,什么情况下不必使用关键字return?
当函数的返回值类型为void的时,不用在函数中使用return。
11.假设你编写的main()函数包含如下代码:cout<<"Please enter your PIN:",而编译器指出cout是一个未知标识符。导致这种问题的原因很可能是什么?指出3种修复这种问题的方法。
可能原因:
没有
#include<iostream>
,或未使用using namespace std
。解决方法:
1.添加#include<iostream>
,在main()函数外使用using namespace std
;
2.添加include<iostream>
,使用using std::cout<<"Please enter your PIN:"
;
3.添加include<iostream>
,使用std::cout << "Please enter your PIN:"
;
三、编程练习
1.编写一个c++程序,它显示您的姓名和地址。
#include<iostream>
using namespace std;
int main()
{
cout<<"My name is wendenglizi,my address is BLABLA.."<<endl;
return 0;
}
2.编写一个c++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long等于200码)。
#include<iostream>
using namespace std;
int main()
{
int longNum=0;
cout<<"Please enter distance(long): ";
cin>>longNum;
cout<<"You input "<<longNum<<" long"<<endl;
cout<<longNum<<"long = "<<longNum*220<<" 码"<<endl;
return 0;
}
3.编写一个c++程序,他使用3个用户定义的函数(包括main(),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行;另外一个函数也调用两次,并生成其余的输出。
#include<iostream>
using namespace std;
void A_function()
{ cout<<"Three blind mice"<<endl; }
void B_function()
{ cout<<"See how they run"<<endl; }
int main()
{
A_function();
A_function();
B_function();
B_function();
return 0;
}
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下图所示:
Enter your age:29
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Enter your age:";
cin>>age;
cout<<"your age include "<<age*12<<" months."<<endl;
return 0;
}
5.编写一个程序,其中main()调用一个用户定义的的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value:20
20 degrees Celsius is 69 degrees Fahrenheit.
下面是转换公式:
华氏温度=1.8*摄氏温度+32.0
#include<iostream>
using namespace std;
double CtoF(double C)
{
double F=1.8*C+32.0;
return F;
}
int main()
{
double celsiusValue,fahrenheitValue;
cout<<"Please enter a Celsius value: ";
cin>>celsiusValue;
fahrenheitValue=CtoF(celsiusValue);
cout<<celsiusValue<<" degrees Celsius is "<<fahrenheitValue<<" degress Fahrenheit"<<endl;
}
6.编写一个程序,其main()调用一个用户的函数(以光年为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years: 4.2
4.2 light years =265608 astronomical units.
天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型,转换公式为:1光年=63240天文单位。
#include<iostream>
using namespace std;
double LYtoAU(double lightYears)
{
double astroUnits=lightYears*63240;
return astroUnits;
}
int main()
{
double lightYears;
cout<<"Enter the number of light years: ";
cin>> lightYears;
double astroYears=LYtoAU(lightYears);
cout<<lightYears<<" light Years ="<<astroYears<<" astronomical units"<<endl;
return 0;
}
7.编写一个程序,要求用户输入小时数和分钟数,在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
#include<iostream>
using namespace std;
void Time(int hours,int minutes)
{
cout<<"Time: "<<hours<<":"<<minutes<<endl;
}
int main()
{
int hours,minutes;
cout<<"Enter the number of hours: ";
cin>>hours;
cout<<"Enter the number of minutes: ";
cin>>minutes;
Time(hours,minutes);
return 0;
}
四、总结
C++程序由一个或多个被称为函数的模块组成。程序从main()函数(全部小写)开始执行,因此该函数必不可少。函数由函数头和函数体组成。 函数头指出函数的返回值(如果有的话)的类型和函数期望通过参数传递给它的信息的类型。函数体由一系列位于花括号({})中的C++语句组成。
有多种类型的C++语句,包括下述6种。
声明语句: 定义函数中使用的变量的名称和类型。
赋值语句: 使用赋值运算符(=)给变量赋值。
消息语句: 将消息发送给对象,激发某种行动。
函数调用: 执行函数。被调用的函数执行完毕后,程序返回到函数调用语句后面的语句。
函数原型: 声明函数的返回类型、函数接受的参数数量和类型。
返回语句: 将一个值从被调用的函数那里返回到调用函数中。
类是用户定义的数据类型规范,它详细描述了如何表示信息以及可对数据执行的操作。对象是根据类 规范创建的实体,就像简单变量是根据数据类型描述创建的实体一样。
C++提供了两个用于处理输入和输出的预定义对象(cin和cout),它们是istream和ostream类的实例, 这两个类是在iostream文件中定义的。为ostream类定义的插入运算符(《)使得将数据插入到输出流成 为可能;为istream类定义的抽取运算符(〉〉)能够从输入流中抽取信息。cin和cout都是智能对象,能够 根据程序上下文自动将信息从一种形式转换为另一种形式。
C++可以使用大量的C库函数。要使用库函数,应当包含提供该函数原型的头文件。