文章目录
前言
看书容易睡着,敲一遍代码先,按照书中的程序标号。
2.1 myfirst.cpp
代码
#include <iostream>
using namespace std;
int main()
{
cout << "Come up and C++ me some time.";
cout << endl;
cout << "You won't regret it!" << endl;
cin.get();
return 0;
}
结果
知识点
1.C++程序的组成
2.2 carrot.cpp
代码
#include<iostream>
using namespace std;
int main()
{
int carrots;
carrots = 25;
cout << "I have ";
cout << carrots;
cout << " carrots.";
cout << endl;
carrots = carrots - 1;
cout << "Now I have " << carrots << " carrots." << endl;
return 0;
}
结果
知识点
1.变量的声明、赋值
2.cout的分开使用和连续使用
2.3 getinfo.cpp
代码
#include <iostream>
using namespace std;
int main()
{
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots;
cout << "Here are two more. ";
carrots = carrots + 2;
cout << "Now you have " << carrots << " carrots." << endl;
return 0;
}
结果
其中10是自己输入的数字。
知识点
- cin的使用
2.4 sqrt.cpp
代码
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
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;
}
结果
知识点
1.sqrt()函数用法
2.5 ourfun.cpp
代码
#include <iostream>
using namespace std;
//函数声明
void simon(int);
int main()
{
//函数使用
simon(3);
cout << "Pick an integer: ";
int count;
cin >> count;
//函数使用
simon(count);
cout << "Done!" << endl;
return 0;
}
//函数定义
void simon(int n)
{
cout << "Simon says touch your toes " << n << " times." << endl;
}
结果
知识点
1.函数声明
2.函数定义
3.函数使用
2.6 convert.cpp
代码
#include <iostream>
using namespace std;
int stonetolb(int);
int main()
{
int stone;
cout << "Enter the weight in stone: ";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << " stone = ";
cout << pounds << " pounds." << endl;
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
结果
知识点
- 有返回值的函数声明和定义
总结
加油!!!