Tải bản đầy đủ (.pdf) (21 trang)

Kỹ thuật lập trình C/C++ P8

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (170.38 KB, 21 trang )

Kỹ thuật lập trình
0101010101010101100001
0101010101010101100001
0101010101010101100001
0101010100101010100101
0101010100101010100101
0101010100101010100101
1010011000110010010010
1010011000110010010010
1010011000110010010010
1100101100100010000010
1100101100100010000010
1100101100100010000010
0101010101010101100001
0101010101010101100001
0101010101010101100001
0101010100101010100101
0101010100101010100101
0101010100101010100101
1010011000110010010010
1010011000110010010010
1010011000110010010010
1100101100100010000010
1100101100100010000010
1100101100100010000010
0101010101010101100001
0101010101010101100001
0101010101010101100001
0101010100101010100101
0101010100101010100101
0101010100101010100101


1010011000110010010010
1010011000110010010010
1010011000110010010010
1100101100100010000010
1100101100100010000010
1100101100100010000010
8/13/2007
y = A*x + B*u;
x = C*x + d*u;
StateController
start()
stop()
LQGController
start()
stop()
Chương 8: Tiếntớitư duy lậptrình
hướng ₫ốitượng
2
Chương 8: Tiếntớitư duy hướng đốitượng
Nộidung chương 8
8.1 Đặtvấn ₫ề
8.2 Giớithiệuvídụ chương trình mô phỏng
8.3 Tư duy "rất" cổ₫iển
8.4 Tư duy hướng hàm
8.5 Tư duy dựatrên₫ốitượng (object-based)
8.6 Tư duy thựcsự hướng ₫ốitượng
3
Chương 8: Tiếntớitư duy hướng đốitượng
8.1 Đặtvấn ₫ề
„Designing object-oriented software is hard, and designing reusable

object-oriented software is even harder...It takes a long time for
novices to learn what object-oriented design is all about. Exprienced
designers evidently know something inexperienced ones don't...
One thing expert designers know
not
to do is solve every problem from
first principles. Rather, they reuse solutions that have worked for
them in the past. When they find a good solution, they use it again
and again. Such experience is part of what makes them experts.
Consequently, you'll find recurring patterns of classes and
communicating objects in many object-oriented systems. These
patterns solve specific design problems and make object-oriented
design more flexible, elegant, and ultimately reusable...“
Erich Gamma et. al.:
Design Patterns: Elements of Reusable Object-
Oriented Software
, Addison-Wesley, 1995.
4
Chương 8: Tiếntớitư duy hướng đốitượng
8.2 Phầnmềmmôphỏng kiểuFBD
StaticGain Limiter
Integrator
Sum
Scope
1(t)
Nhiệmvụ:
Xây dựng phầnmềm ₫ể hỗ trợ mô phỏng thờigianthựcmột
cách linh hoạt, mềmdẻo, ₫áp ứng ₫ượccácyêucầucủatừng
bài toán cụ thể
Trướcmắtchưacầnhỗ trợ tạo ứng dụng kiểukéothả bằng

công cụ₫ồhọa
5
Chương 8: Tiếntớitư duy hướng đốitượng
8.3 Tư duy rấtcổ₫iển
// SimProg1.cpp
#include <iostream.h>
#include <conio.h>
#include <windows.h>
void main() {
double K =1,I=0, Ti = 5;
double Hi = 10, Lo = -10;
double Ts = 0.5;
double r =1, y=0, e, u, ub;
cout << "u\ty";
while (!kbhit()) {
e = r-y; // Sum block
u = K*e; // Static Gain
ub = max(min(u,Hi),Lo); // Limiter
I += ub*Ts/Ti; // Integrator state
y = I; // Integrator output
cout << '\n' << u << '\t' << y;
cout.flush();
Sleep(long(Ts*1000));
}
}
6
Chương 8: Tiếntớitư duy hướng đốitượng
Vấn ₫ề?
 Phầnmềmdướidạng chương trình, không có giá trị
sử dụng lại

 Rấtkhóthay₫ổihoặcmở rộng theo yêu cầucụ thể
củatừng bài toán
 Toàn bộ thuật toán ₫ược gói trong mộtchương trình
=> khótheodõi, dễ gây lỗi, không bảovệ₫ượcchất
xám
7
Chương 8: Tiếntớitư duy hướng đốitượng
// SimProg2.cpp
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include "SimFun.h"
void main() {
double K = 5.0, double Ti = 5.0;
double Hi = 10, Lo = -10;
double Ts = 0.5;
double r =1, y=0, e, u, ub;
cout << "u\ty";
while (!kbhit()) {
e = sum(r,-y); // Sum block
u = gain(K,e); // Static Gain
ub= limit(Hi,Lo,u); // Limiter
y = integrate(Ti,Ts,ub); // Integrator output
cout << '\n' << u << '\t' << y;
cout.flush();
Sleep(long(Ts*1000));
}
}
8.4 Tư duy hướng hàm
8

Chương 8: Tiếntớitư duy hướng đốitượng
// SimFun.h
inline double sum(double x1, double x2) { return x1 + x2; }
inline double gain(double K, double x) { return K * x; }
double limit(double Hi, double Lo, double x);
double integrate(double Ti, double Ts, double x);
// SimFun.cpp
double limit(double Hi, double Lo, double x) {
if (x > Hi) x = Hi;
if (x < Lo) x = Lo;
return x;
}
double integrate(double Ti, double Ts, double x) {
static double I = 0;
I += x*Ts/Ti;
return I;
}

×