快速预览:
int用来保存整数,当使用整型的时候,一定要注意溢出和除法的问题。当不考虑整数的size的时候,使用int就够了。当整型的精度很重要的时候(或者由于范围或者内存的限制),建议使用定宽整型。
float用来保存实数,也就是带有小数的数字。当使用浮点型的时候,要注意精度问题,舍入误差和比较问题。
Boolean类型的值只有真或者假,它的使用没有什么大问题。
Char类型是可以被翻译成ASCII码的整型,使用的时候不要混淆ASCII的码值和数字,同样注意除法的问题。
使用const来替代宏整数,更加安全。
1.为什么符号常量要比文字常量好?为什么const修饰符要比#define修饰符好?
用文字常量让你的code非常难以理解和维护。符号常量首先通过符号就基本可以判断常数的意思,其次只需要改动定义的地方,全文就都可以改。#define的定义会有可能造成命名冲突,而且载debugger中也不会出现真实数字。
2.选择一种数据类型来表示下面的例子,需要尽可能详细,比如int long,以及是否需要const修饰。
- 使用者的年龄(年) int
- 使用者是否喜欢这种颜色 boolean
- Pi(3.1415.......) double
- 教科书的页数(size非常重要) int16_t
- 你的身高(英寸,2位数) float
- 你出生后眨了多少次眼睛(可能是millions)long
- 一个用户从菜单里选择一个选项 char
- 某个人出生的年数(size很重要) int16_t
3.使用者输入两个浮点数字,然后输入一个符号:+ - * /,计算输出两个数字的计算结果。如果输入无效的话,请输出空。
例如:
Enter a Number :
Enter a Number:
Enter a synbol: + - * /
6.2*5 = 31
#include <iostream>
double getDouble()
{
std::cout << "Enter a double value: ";
double x;
std::cin >> x;
return x;
}
char getOperator()
{
std::cout << "Enter one of the following: +, -, *, or / ";
char op;
std::cin >> op;
return op;
}
void printResult(double x, char op, double y)
{
if (op == '+')
std::cout << x << " + " << y << " is " << x + y << '\n';
else if (op == '-')
std::cout << x << " - " << y << " is " << x - y << '\n';
else if (op == '*')
std::cout << x << " * " << y << " is " << x * y << '\n';
else if (op == '/')
std::cout << x << " / " << y << " is " << x / y << '\n';
}
int main()
{
double x = getDouble();
double y = getDouble();
char op = getOperator();
printResult(x, op, y);
return 0;
}
4.这个问题有点挑战,写一个简短的程序模拟一个球从塔顶上掉下来。首先用户应该询问塔的高度,假设重力加速度(9.8 m/),球没有初始速度。输出球在1s,2s,3s,4s,5s的高度。
程序中需要一个头文件,constance.h,里面包含一个namespace,MyConstance,包含了重力加速度9.8,用下面的公式计算球下落x s后的距离:distance fallen = gravity_constance * x_ /2
constance.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
namespace myConstants
{
const double gravity(9.8); // in meters/second squared
}
#endif
主函数:
#include <iostream>
#include "constants.h"
// gets height from user and returns it
double getTowerHeight()
{
std::cout << "Enter the height of the tower in meters: ";
double towerHeight;
std::cin >> towerHeight;
return towerHeight;
}
// Returns height from ground after "seconds" seconds
double calculateHeight(double towerHeight, int seconds)
{
// Using formula: [ s = u * t + (a * t^2) / 2 ], here u(initial velocity) = 0
double distanceFallen = (myConstants::gravity * (seconds * seconds)) / 2;
double currentHeight = towerHeight - distanceFallen;
return currentHeight;
}
// Prints height every second till ball has reached the ground
void printHeight(double height, int seconds)
{
if (height > 0.0)
std::cout << "At " << seconds << " seconds, the ball is at height: " << height << " meters\n";
else
std::cout << "At " << seconds << " seconds, the ball is on the ground.\n";
}
void calculateAndPrintHeight(double towerHeight, int seconds)
{
double height = calculateHeight(towerHeight, seconds);
printHeight(height, seconds);
}
int main()
{
const double towerHeight = getTowerHeight();
calculateAndPrintHeight(towerHeight, 0);
calculateAndPrintHeight(towerHeight, 1);
calculateAndPrintHeight(towerHeight, 2);
calculateAndPrintHeight(towerHeight, 3);
calculateAndPrintHeight(towerHeight, 4);
calculateAndPrintHeight(towerHeight, 5);
return 0;
}