tailieunhanh - Lecture Programming principles and practice using C++: Chapter 15 - Bjarne Stroustrup
This chapter is a bit messy and full of details. The aim is to give some programming practice and examples after the conceptual high point in Chapter 14 and before the mindbending control inversion of Chapter 15. Basically, we go through a series of code examples to reinforce what has been shown before and to encourage some students that what they are learning is real. | Chapter 15 Functions and graphing Bjarne Stroustrup Abstract Here we present ways of graphing functions and data and some of the programming techniques needed to do so, notably scaling. Stroustrup/Programming Note This course is about programming The examples – such as graphics – are simply examples of Useful programming techniques Useful tools for constructing real programs Look for the way the examples are constructed How are “big problems” broken down into little ones and solved separately? How are classes defined and used? Do they have sensible data members? Do they have useful member functions? Use of variables Are there too few? Too many? How would you have named them better? Stroustrup/Programming Graphing functions Start with something really simple Always remember “Hello, World!” We graph functions of one argument yielding one value Plot (x, f(x)) for values of x in some range [r1,r2) Let’s graph three simple functions: double . | Chapter 15 Functions and graphing Bjarne Stroustrup Abstract Here we present ways of graphing functions and data and some of the programming techniques needed to do so, notably scaling. Stroustrup/Programming Note This course is about programming The examples – such as graphics – are simply examples of Useful programming techniques Useful tools for constructing real programs Look for the way the examples are constructed How are “big problems” broken down into little ones and solved separately? How are classes defined and used? Do they have sensible data members? Do they have useful member functions? Use of variables Are there too few? Too many? How would you have named them better? Stroustrup/Programming Graphing functions Start with something really simple Always remember “Hello, World!” We graph functions of one argument yielding one value Plot (x, f(x)) for values of x in some range [r1,r2) Let’s graph three simple functions: double one(double x) { return 1; } // y==1 double slope(double x) { return x/2; } // y==x/2 double square(double x) { return x*x; } // y==x*x Stroustrup/Programming Functions double one(double x) { return 1; } // y==1 double slope(double x) { return x/2; } // y==x/2 double square(double x) { return x*x; } // y==x*x Stroustrup/Programming How do we write code to do this? Simple_window win0(Point(100,100),xmax,ymax,"Function graphing"); Function s(one, -10,11, orig, n_points,x_scale,y_scale); Function s2(slope, -10,11, orig, n_points,x_scale,y_scale); Function s3(square, -10,11, orig, n_points,x_scale,y_scale); (s); (s2); (s3); ( ); Stroustrup/Programming Function to be graphed Range in which to graph [x0:xN) “stuff” to make the graph fit into the window First point We need some Constants const int xmax = (); // window size (600 by 400) const int ymax = (); const int x_orig = xmax/2; const int y_orig = ymax/2; const .
đang nạp các trang xem trước