1.4 使用算术运算符
不管是清算杀死的敌人数目或是降低玩家的生命值,程序都需要做一些数学运算。C++(www.cppentry.com)和其他语言一样有内置算术运算符。
1.4.1 Expensive Calculator程序简介
大多数比较认真的计算机游戏程序员会在顶级、高性能的游戏平台上投入大量的精力。接下来,Expensive Calculator这个程序将计算机变成一个简单的计算器。该程序演示了内置算术运算符,程序结果如图1-4所示。
|
| 图1-4 C++(www.cppentry.com)可以进行加法、减法、乘法、除法甚至求余运算 |
可以从Course Technology网站(www.courseptr.com/downloads)或本书合作网站(http://www. tupwk.com.cn/downpage)上下载到该程序的代码。程序位于Chapter 1文件夹中,文件名为expensive_calculator.cpp。
- // Expensive Calculator
- // Demonstrates built-in arithmetic operators
-
- #include <iostream>
- using namespace std;
-
- int main()
- {
- cout << "7 + 3 = " << 7 + 3 << endl;
- cout << "7 - 3 = " << 7 - 3 << endl;
- cout << "7 * 3 = " << 7 * 3 << endl;
- cout << "7 / 3 = " << 7 / 3 << endl;
- cout << "7.0 / 3.0 = " << 7.0 / 3.0 << endl;
- cout << "7 % 3 = " << 7 % 3 << endl;
- cout << "7 + 3 * 5 = " << 7 + 3 * 5 << endl;
- cout << "(7 + 3) * 5 = " << (7 + 3) * 5 << endl;
- return 0;
- }