-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.cpp
82 lines (54 loc) · 2.71 KB
/
01.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
int main() {
// This is a comment
/*
This is a comment
on multiple lines
*/
// VARIABLES
//
// A variable is a symbol that represents a quantity that may vary.
//
// data_type identifier = value;
int age = 25; // The value 25 is assigned to variable age
// BASIC DATA TYPES
int sea_level = 25; // Integer
double temperature = -3.82; // Real number
std::string name = "Nacho López"; // String
bool has_car = true; // Boolean (only two values: true or false)
// ARITHMETIC OPERATIONS WITH NUMBERS
int x = 5;
int y = 2;
double z = x + y; // Addition. Result: 7.
z = x - y; // Subtraction. Result: 3.
z = x * y; // Multiplication. Result: 10.
z = x / y; // Division. Result: 2.5.
z = x % y; // Modulo (remainder of the integer division). Result: 1.
z = z + 1; // Increase the value of z by 1. Result: 2.
z = z - 1; // Decrease the value of z by 1. Result: 1.
z = 50 - x * 6 / -0.5; //
z = (50 - x) * 6 / -0.5; // The order of operations is as in mathematics
z = (50 - x * 6) / -0.5; //
z = 2 * z + 3; // Remember: the symbol = assigns a value to the variable
// BASIC OPERATIONS WITH STRINGS
std::string a = "GNU/";
std::string b = "Linux";
std::string c = a + b; // Concatenation Result: 'GNU/Linux'.
// c = std::string(a, 3; // Repetition Result: 'GNU/GNU/GNU/'.
// PRINT VARIABLES ON SCREEN
std::cout << "Hello, world!" << std::endl; // Prints on screen: Hello, world!
std::cout << x << std::endl; // Prints the variable x
// You can print on screen strings and variables
std::cout << "I have bought " << x << " oranges and " << y << " lemons.";
// DATA TYPE CONVERSION
std::string position = "5";
std::string calories = "95.4";
double altitude = -544.432;
int position_int = std::stoi(position); // Convert to integer.
double calories_dbl = std::stod(calories); // Convert to double.
std::string altitude_str = std::to_string(altitude); // Convert to string.
// String position_type = ((Object)position_int).getClass().getName(); // Type: integer.
// String calories_type = ((Object)calories_dbl).getClass().getName(); // Type: float.
// String altitude_type = ((Object)altitude_str).getClass().getName(); // Type: string.
}