- 0. 小结
- 3. Arithmetic Operations
- 6. Variable Assignment
- 11. PreFix and PostFix
- 13. Variable Assignment Operators
0. 小结
本节非常简单,C++的非常基础的知识。
3. Arithmetic Operations
#include <cmath>
std::pow(base,exponent)
answer = std::pow(3,5) //3**5
//pi is equal to M_PI
//M_PI = 3.14159265358979323846
area = M_PI * r * r
/*Goal: practice arithmetic operations in C++
**Write a program that calculates the volumes of:
**a cube, sphere, cone.
**Cube Volume = side^3
**Sphere Volume = (4/3) * pi * radius^3
**Cone Volume = pi * radius^2 * (height/3)
**Write the values to the console.
*/
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
//Dimension of the cube
float cubeSide = 5.4;
//Dimension of sphere
float sphereRadius = 2.33;
//Dimensions of cone
float coneRadius = 7.65;
float coneHeight = 14;
float volCube, volSphere, volCone = 0;
volCube = pow(cubeSide,3);
volSphere = (4.0/3.0) * M_PI * pow(sphereRadius,3);
volCone = M_PI * pow(coneRadius,2) * (coneHeight/3);
cout << volCube << endl;
cout << volSphere << endl;
cout << volCone << endl;
return 0;
}
输出:
157.464
52.9854
857.985
注意上面使用(4.0/3.0)
,如果是(4/3)
的话,结果为1.
6. Variable Assignment
- The type of every variable must be known at compile time.
- C++ allows for some implicit conversions 隐式转换.
/*Goal: understand C++ implicit variable assignments.
**Run the program to see implicit variable assignment at work.
*/
#include<iostream>
int main()
{
int a = 65;
char charA = 65;
char charB = 'B';
float answer = 0;
char charC = 67;
int integer = 80;
float floatNumber = 0.0;
std::cout<<"a = "<<a<<"\n";
std::cout<<"charA = "<<charA<<"\n";
std::cout<<"charB = "<<charB<<"\n";
//we can assign an integer to a float
floatNumber = integer;
std::cout<<"integer = "<<integer<<"\n\n";
std::cout<<"floatNumber = integer = "<<floatNumber<<"\n";
//we can assign a char to a float
floatNumber = charB;
std::cout<<"floatNumber = charB = "<<floatNumber<<"\n";
answer = floatNumber/4;
std::cout<<"answer = floatNumber/4 = "<<answer<<"\n";
//But assigning a float to a char doesn't quite work
charC = answer;
std::cout<<"charC = answer = "<<charC<<"\n";
//assigning a float to an interger, results in the float being truncated
integer = answer;
std::cout<<"integer = floatNumber = "<<integer<<"\n";
return 0;
}
a = 65
charA = A
charB = B
integer = 80
floatNumber = integer = 80
floatNumber = charB = 66
answer = floatNumber/4 = 16.5
charC = answer =
integer = floatNumber = 16
#include <iostream>
int main()
{
float width = 4.5;
float height = 5.5;
int area = width * height;
std::cout << "area = " << area;
return 0;
}
如果是float输出area = 24.75
,但是这里因为是int,则输出area = 24
。
注意下面的代码是错的:
#include <iostream>
int main(void)
{
int numerator = 4;
int denominator = 5;
float answer = numerator / denominator;
std::cout<<"answer = "<<answer;
return 0;
}
输出结果仍然是answer = 0
,这里涉及到低精度到高精度转换的问题,需要修改为如下,结果为answer = 0.8
:
#include <iostream>
int main(void)
{
int numerator = 4;
float denominator = 5;
float answer = 0;
answer = numerator / denominator;
std::cout<<"answer = "<<answer;
return 0;
}
11. PreFix and PostFix
Incrementing:
- prefix: ++a
- postfix: a++
Decrementing:
- prefix: –a
- postfix: a–
- Prefix operators increment the value of the variable, then return the reference to the variable.
- Postfix operators create a copy of the variable and increments the value of the variable. Then it returns a copy from BEFORE the increment.
以前知道这两者的运算结果不同,但是不理解原因,原来如此,prefix的时候是直接返回该变量的引用,所以得到的结果与加法后的一样。 但是postfix是先生成了一个copy再加法运算,再返回一个copy(未加法的)。
/*Goal: Practice PreFix and PostFix
**
*/
#include<iostream>
using namespace std;
int main()
{
int a, b = 0;
int post, pre = 0;
cout<<"Inital values: \t\t\tpost = "<<post<<" pre= "<<pre<<"\n";
post = a++; // post = 0, a = 1
pre = ++b; // pre = 1, b = 1
cout<<"After one postfix and prefix: \tpost = "<<post<<" pre= "<<pre<<"\n";
cout<<"After one postfix and prefix: \ta = "<<a <<" b= "<<b<<"\n";
post = a++; // post = 1, a = 2
pre = ++b; // pre = 2, b = 2
cout<<"After two postfix and prefix: \tpost = "<<post<<" pre= "<<pre<<"\n";
cout<<"After one postfix and prefix: \ta = "<<a <<" b= "<<b<<"\n";
return 0;
}
结果:
Inital values: post = 0 pre= 0
After one postfix and prefix: post = 0 pre= 1
After one postfix and prefix: a = 1 b= 1
After two postfix and prefix: post = 1 pre= 2
After one postfix and prefix: a = 2 b= 2
13. Variable Assignment Operators
/*Goal: understand variable assignment operators*/
#include<iostream>
int main()
{
int a = 0;
std::cout<<"Variable\t\tOperation\tResult\n";
std::cout<<"a = "<<a;
a += 2;
std::cout<<"\t\t\ta += 2 \t\t a = "<<a<<"\n"; // 2
std::cout<<"a = "<<a<<" : ";
a -= 4;
std::cout<<"\t\ta -= 4 \t\t a = "<<a<<"\n"; // -2
int b = 3;
std::cout<<"a = "<<a<<", b = "<<b<<" : "; // -2,3
a *= b;
std::cout<<"\ta *= b \t\t a = "<<a<<"\n"; // -6
std::cout<<"a = "<<a<<", b = "<<b<<" : "; // -6, 3
a /= b;
std::cout<<"\ta /= b \t\t a = "<<a<<"\n"; // -2
return 0;
}
输出:
Variable Operation Result
a = 0 a += 2 a = 2
a = 2 : a -= 4 a = -2
a = -2, b = 3 : a *= b a = -6
a = -6, b = 3 : a /= b a = -2