教材《C++ Primer Plus (第六版) 中文版》 ,这里有教材中的示例源文件以及部分练习题代码。
目录
3.6 morechar.cpp 成员函数 cout.put()
一些技巧
1.重命名变量:选中变量,按住Ctrl的同时按两次R,即可重命名。
2.一个项目多个包含main的源文件:在不需要运行的源文件处,鼠标右击>>属性>>从生成中排除右边选择是。
3.按Tab即可选中输入框中的词。
22.8.10
第二章
2.1
#include<iostream> //编译指令 使用cin和cout进行输入和输出的程序必须包含头文件iostream
int main() /*function heading 函数头,作为接口:
返回一个值{int(整数值) 函数返回类型};
括号部分为形参列表:从调用函数传递给被调用函数的信息;*/
{
using namespace std; //using编译指令, 将类、函数和变量放置在名称空间std(使std::cout可简化成cout等,避免名称混淆)
cout << "come up and C++ me some time."; //cout输出流;<<将右侧信息插入到流中
cout << endl; // endl;换行 也可以写成"\n"
cout << "You won't regret it!" << endl;
return 0; //main()函数中可不写这句
}
2022.8.19
2.2 carrot.cpp
//carrots.cpp--food processing program
//use and dispaly a variable
#include <iostream>
int main()
{
using namespace std;
int carrots;
carrots = 25;
cout << "I have ";
cout << carrots << " carrots." << endl;
cout << "Crunch,crunch.Now I have " << carrots - 1 << " carrots."<< endl;
return 0;
}
2.3 getinfo.cpp 输入和输出
//input and output
#include <iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots; //the next line concatenates output(连续输出,若输入c则赋值carrots为0
cout << "Here are two more.";
carrots = carrots + 2; //cout语句的输出紧跟在前一条cout语句的输出后面
cout << "Now you have " << carrots << " carrots." << endl;
/*cout << "Now you have "
<< carrots
<< " carrots."
<< endl;
*/
return 0;
}
2.4 sqrt.cpp
#include<iostream>
#include<cmath> //提供sqrt()的函数原型,原型只描述函数接口(发送给函数的信息和返回的信息)
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;
return 0;
}
2.5 ourfunc.cpp 自定义函数
#include<iostream>
void simon(int);
//可以将编译指令using namespace std;放在函数前面,使函数都能访问名称空间std。
int main()
{
using namespace std;
simon(3);
cout << "Pick an integer:";//integer(整数)
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;
}
2.6 convert.cpp
#include<iostream>
int stonetrans(int);
int main()
{
using namespace std;
int stone; //选中变量,按住Ctrl+R 再按一次R;即可重命名该变量
cout << "Enter the wight in stone: ";
cin >> stone;
int pounds = stonetrans(stone); //直接定义并赋值
cout << stone << " stone = " << pounds << " pounds." << endl;
}
int stonetrans(int n)
{
return 14*n; //return 后面可以跟表达式
}
2.7 编程练习
1、2
#include<iostream>
using namespace std;
int main()
{
//cout << "NAME PLACE"<<endl; //第1题
/*cout << "请输入一个以long为单位的距离"; //第2题
int lon;
cin >> lon;
cout << lon << " long = " << lon * 220 << " 码 .";
*/
}
3
void fir(int);
void sec(int);
using namespace std;
int main()
{
fir(1);
fir(1);
sec(2);
sec(2);
}
void fir(int n)
{
cout << "Three blind mice\n";
}
void sec(int n)
{
cout << "See how they run"<<endl;
}
5.
#include<iostream>
double c2f(int);
using namespace std;
int main()
{
cout << "Please enter a Celsius value: ";
int C;
cin >> C;
cout << C << " degrees Celsius is " << c2f(C) << " degrees Fahrenheit."<<endl;
}
double c2f(int n)
{
return n * 1.8 + 32;
}
7.
#include<iostream>
void Time(int,int); //函数可以有多个接受的参数
using namespace std;
int mai