KỸ THUẬT LẬP TRÌNH HỆ CƠ ĐIỆN TỬ
Programming for Mechatronic Systems
1
TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI
Giảng viên: TS. Nguyễn Thành Hùng
Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí
Nội, 2020
2
Chapter I. Basics and data management of C++
1. Creation of C++ programs
2. Basic data types, variables and constants
3. Basic operations and expressions
4. Control program structures
5. Exception handling
6. Pointers, references and dynamic memory
management
7. Arrays and strings
8. User-defined data types
3
1. Creation of C++ programs
Some important rules
The basic elements of the program: the characters of the 7 bit
ASCII code table
Character and text constants, as well as remarks may contain
characters of any coding
4
Some important rules
C++ compiler differentiates small and capital letters in the words
(names) used in the program.
Certain (English) words cannot be used as own names since
these are keywords of the compiler.
In case of creating own names please note that they have to start
with a letter (or underscore sign), and should contain letters,
numbers or underscore signs in their other positions.
1. Creation of C++ programs
5
The first C++ program in two versions
// Circle1.cpp
#include "cstdio"
#include "cmath"
using namespace std;
int main()
{
const double pi = 3.14159265359;
double radius, area, perimeter;
// Reading radius
printf("Radius = ");
scanf("%lf", &radius);
// Calculations
perimeter = 2 * radius*pi;
area = pow(radius, 2)*pi;
printf("Perimeter: %7.3f\n",
perimeter);
printf("Area: %7.3f\n", area);
// Waiting for pressing Enter
getchar();
getchar();
return 0;
}
// Circle2.cpp
#include "iostream"
#include "cmath"
using namespace std;
int main()
{
const double pi = 3.14159265359;
// Reading radius
double radius;
cout << "Radius = ";
cin >> radius;
// Calculations
double perimeter = 2 * radius*pi;
double area = pow(radius, 2)*pi;
cout << "Perimeter: " << perimeter <<
endl;
cout << "Area: " << area << endl;
// Waiting for pressing Enter
cin.get();
cin.get();
return 0;
}
C C++
1. Creation of C++ programs