1.最简单的C++程序框架
#include <iostream>
int main(void) //函数头,main()=送的礼物 (void)=纸条,送礼物可以有纸条也可以没纸条(为了说明)
{
using namespace std;
return 0; //花括号及里面内容里面是函数体,return0这一句话是C++语句,必须以分号结尾
}
//return0是返回语句标志函数运行结束
//int表示函数返回值类型,integer,返回的0,正好与int对应上
//是单行注释
/* 是多行注释 */
2.使用cout打印消息以及换行符的使用
#include <iostream>
int main(void)
{
using namespace std;
cout << "Come up and C++ me some time.\n";
cout << endl;
cout << "You won't regret it!" << endl;
return 0;
}
//cout << endl 和 \n(换行符) 都可以用来换行
//控制符endl:重起一行,和cout一样,在头文件iostream中定义,位于命名空间std中
3.声明变量并赋值
#include <iostream>
int main(void)
{
using namespace std;
int carrots; //变量声明(通常指出要存储的数据类型以及程序对存储在这里的数据使用的名称)
carrots = 25; //赋值运算符
cout << "I have ";
cout << carrots;
cout << " carrots";
cout << endl;
carrots = carrots - 1;
cout << "crunch, crunch, crunch, Now I have "<< carrots <<" carrots";
cout << endl;
return 0;
}
4.cin的使用
#include <iostream>
int main(void)
{
using namespace std;
int carrots;
cout << "How many carrots do you hanve?" << endl;
cin >> carrots;
cout << "Here are two more. ";
carrots = carrots + 2;
cout << "Now you have " << carrots << " carrots." << endl; //使用cout进行多语句拼接
return 0;
}
//cout是ostream类对象,定义了cout数据以及操作部分
//cin是istream类对象
//类描述了一种数据类型的全部属性(包括可使用它执行的操作),对象是根据这些描述创建的实体
5.函数的使用
#include <iostream>
#include <cmath> //函数原型的声明
int main(void)
{
using namespace std;
double area;
cout << "Enter the floor area, in square feets, of your home: ";
cin >> area;
double side;
side = sqrt(area);
cout << "That's the equivalent of a square " << side << " feet to the side" << endl;
return 0;
}
//C++的函数分为两种,一种是有返回值的一种是没有返回值的,我们也可以自己创建这两种类型的函数
//参数是发送给函数的信息,返回值是从函数中发送回去的值
//sqrt()的函数原型为 double sqrt(double);
//函数原型和函数定义不一样,函数原型只描述函数接口,它描述的是发送给函数的信息(参数)以及返回的信息(返回值)
6.自定义函数
6.1 自定义无返回值含参数的函数
#include <iostream>
void simon(int n); //函数的声明
//using namespace std; 全局有效
int main(void)
{
using namespace std;
cout << "Please input touch toes times: ";
int count;
cin >> count;
simon(count); //函数的使用
cout << "Done!" << endl;
return 0;
}
void simon(int n) //函数的定义
{
using namespace std;
cout << "Simon says touch your toes " << n << " times" << endl;
}
//由于simon()无返回值,因此不能这样使用:simple = simon(3);
6.2 自定义有返回值含参数的函数
#include <iostream>
int stonetolb(int);
int main(void)
{
//using namespace std;
std::cout << "Enter the weight in stone: ";
int stone;
std::cin >> stone;
int pounds = stonetolb(stone); //有返回值哦
std::cout << stone << " stone = " << pounds << " pounds" << std::endl;
return 0;
}
int stonetolb(int sts)
{
//int pounds = 14 * sts;
//return pounds;
return 14 * sts; //sts为形参
}
7.复习题 (第六版书籍2.6复习题)
1. 函数
2. 在即将编译时,将iostream中的内容与源代码合并载编译
3. 命名空间std,可以使用命名空间中定义的函数,如次cin、cout
4. cout << endl;
5. int cheeses;
6. int cheeses = 32;
7. int cheeses; cin >> cheeses;
8. cout << "We have " << cheeses << " varieties of cheese"
9. 返回值类型以及有无参数输入
10. 无返回值的函数
11. 没有预先说明其命名空间 ,全局/局部 using namespace std; std ::cout
8.编程练习题(第六版书籍2.7编程练习)
//第一题
#include <iostream>
int main(void)
{
using namespace std;
cout << "Your name is Chenyang" << endl;
cout << "Your age is 18 years old" << endl;
cout << "Your adress is Chengdu" << endl;
cout << "Your name is Chenyang."
<< "Your age is 18 years old."
<< "Your adress is Chengdu." << endl;
return 0;
}
//第二题
#include <iostream>
int conv(int para);
int main(void)
{
using namespace std;
int changdu;
cout << "Please input long: ";
cin >> changdu;
int code = conv(changdu);
cout << changdu << " changdu= " << code << " code" << endl;
return 0;
}
int conv(int changdu)
{
return changdu * 220; //changdu使用para也是可以的
}
//第三题
#include <iostream>
void output1(void);
void output2(void); //别忘记了声明
using namespace std;
int main(void)
{
output1(); //加括号才是函数哦,不加什么也不是
output1();
output2();
output2();
return 0;
}
//自定义函数1
void output1(void)
{
cout << "Three blind mice." << endl;
}
//自定义函数2
void output2(void)
{
cout << "See how they run." << endl;
}
//第四题
#include <iostream>
int conv(int);
int main(void)
{
using namespace std;
int age;
cout << "Please input your age: ";
cin >> age;
int month = conv(age);
cout << age << " age= " << month << " month" << endl;
return 0;
}
int conv(int para)
{
return para * 12;
}
//第五题
#include <iostream>
double conv(double);
int main(void) //这里没得分号
{
using namespace std;
double celsies;
cout << "Please enter a Celsius value: ";
cin >> celsies;
double fahrenheit = conv(celsies);
cout << celsies << " degrees Celeies is " << fahrenheit << " degree Fahrenheit" << endl;
return 0; //模板时别忘了
}
double conv(double para)
{
return para * 1.8 + 32.0; //别忘了语句加.
}
//第六题
#include <iostream>
double conv(double);
int main(void)
{
using namespace std;
double lightyears;
cout << "Enter the number of light years: ";
cin >> lightyears;
double tianwenunits = conv(lightyears);
cout << lightyears << " light years = " << tianwenunits << " astronomical units." << endl;
return 0;
}
double conv(double para)
{
return 63240 * para;
}
//第七题
#include <iostream>
using namespace std;
void display_time(int h, int m);
int main(void)
{
int hours, minutes;
cout << "Enter the number of hours: ";
cin >> hours;
cout << "Enter the number of minutes: ";
cin >> minutes;
display_time(hours, minutes);
return 0;
}
void display_time(int h, int m)
{
cout << "Time: " << h << ":" << m << endl; //函数体别打成圆括号哈
}