C++编程:算术运算符、变量声明与操作
1. 使用算术运算符
在编程中,无论是统计击杀敌人的数量,还是降低玩家的生命值,都需要程序进行数学运算。C++和其他语言一样,拥有内置的算术运算符。
1.1 昂贵计算器程序
下面是“昂贵计算器”程序,它可以将高性能游戏电脑变成一个简单的计算器,同时展示了内置的算术运算符。你可以从 Course Technology网站 下载该程序的代码,程序位于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 = "