C语言的科学与技术(英文版)

所需积分/C币:10 2015-04-18 23:31:30 10.05MB PDF
收藏 收藏
举报

THE ART AND SCIENCE OF C(A LIBRARY-BASED INTRODUCTION TO COMPUTER SCIENCE)
Computers are a profoundly empowering technology. The advances we have seen up to now are small compared to what we will see in the next century. Computers will play a major role in shaping that century, just as they have the last 50 years. Those of you who arc students today will soon inherit the responsibility of guiding that progress. As you do sO, knowing how to use computers can only help Like most skills that are worth knowing, learning how computers work and how to control their enormous power takes time. You will not understand it all at once. but you must start somewhere Twenty-five centuries ago the Chinese philosopher Lao-tzu observed that the longest journey begins with a single step. This book can be your beginning For many of you, however, the first step can be the hardest to take Many students find computers overwhelming and imagine that computcr scicncc is beyond thcir rcach Learning the basics of programming, however, does not require advanced mathematics or a detailed understanding of electronics. What matters in programming is whether you can progress from the statement of a problem to its solution. To do So, you must be able to think logically. You must have the necessary discipline to express your logic in a form that the computcr can undcrstand. Perhaps most importantly, you must be able to scc the task through to its completion without getting discouraged by difficulties and setbacks. If you stick with the process, you will discover that reaching the solution is so exhilarating that it more than makes up for any frustrations you encounter along the way This book is designed to teach you the fundamentals of programming and the basics of C, which is thc dominant programming language in thc computing industry today. It treats the whys of programming as well as the hows, to give you a feel for the programming process as a whole. It also includes several features that will help you focus on the essential points and avoid errors that slow you down. The next few pages summarize these features and explain how to use this book effectively as you begin your journey into the exciting world of computer science Using the Art and Science of c To make the best possible use of this textbook for learning the C language, be sure to take advantage of the tools it provides For Chapter review Each chaptcr includes casily accessible matcrial to guidc your study and facilita The list of objectives previews the key topics covered by the chapter Because each objective identifies a concrete skill, the chapter objectives help you to assess your mastery of the essential material The Summary dcscribcs, in morc dctail, what you should havc Icarncd in connection with the objectives Learning to program Programming is both a science and an art. Learning to program well requires much more than memorizing a set of rules. You must learn through experience and by reading other programs. This text includes several features to aid in this process The text includes a large number of Program examples that illustrate how individual C complete are used to create complete programs. These examples also serve as models for your own programs; in many cases, you can solve a new programming problem by making simple modifications to a program from the text Syntax boxes summarize kcy rulcs of C syntax, for an at-a-glance revicw of kcy rogramming concepts pr To Avoid errors All programmers, even the best ones, make mistakes. Finding these mistakes, or bugs in your programs is a critically important skill. Thc following fcaturcs will help you to build this skill To help you learn to recognize and correct bugs, this text includes several buggy programs that illustrate typical errors. To make sure you do not use thes 3 programs as models, such incorrect programs are marked with a superimposed image of a bug Common Pitfalls providc handy reminders about mistakes all beginning programmers are likely to make, and how to avoid them. Faulty lines of code are highlighted with a bug image and annotated in color After the Chapter Lcarning to program rcquires considcrablc practicc. To makc surc that you gct the necessary experience, each chapter includes an extensive set of exercises and questions that test your mastery of the material Each chapter concludes with a wealth of Review Questions, which require brief answers to chapter content questions, code adoptions, or debugging exercises Programming Exercises call for you to try your hand at morc cxtcnsivc programming projects o the instructor In 1991-92, Stanford decided to restructure its introductory computer science curriculum to use ANSI C instead of Pascal. We chose to adopt ANSI C in our introductory courses for the following reasons a Students demanded a more practical language. Future employers want students to have more direct experience with the tools industry uses, which today are principally C and C++. With fcw employment opportunities listed for Pascal programmers in the newspaper employment section, our students began to question the relevance of their education Our Pascal-based curriculum taught students to program in a language that they would ncvcr again usc. We discovered that many of thosc students, when they abandoned Pascal for more modern languages, often forgot everything they had learned about programming style and the discipline of software engineering. Having now taught these skills in the context of a language that the students continue to use we have found that they end up writhing much belter programs Many of our advanced computer science courses, particularly in the systems area, require students to program in C. Working with C from the beginning gives students much more experience by the time they reach the advanced courses a Lcarning C carly paves the way to learning C++ and the objcct-oricntcd paradigm Because our students have a strong background in C programming afler their first year of study, we have been able to offer our course on object-oriented system design much earlier in the curriculum a C makes it possible to cover several important topics, such as modular development and abstract data typcs, that arc hard to teach in a Pascal cnvironment o In the last five years. Chas make significant headway toward replacing Fortran as the Lingua Franca of programming for the engineering sciences Given our experience over the last three years, I am convinced that the choice was a good one and that our program is stronger because of the change At the samc timc, it is important to recognize that tcaching C in the first programming course is not always easy. C was designed for programmers, not introductory students Many of its features make sense only when understood in terms of a larger conceptual framework that new students do not recognize. In many respects, C is a complex language To teach it at the introductory level, we must find a way to control its complexity The library-based approach One of the central goals of this text is to enable teachers to manage Cs inherent complexity. Managing complexity, however, is precisely what we do as programmers When we are faced with a problem that is too complex for immediate solution, we divide it into smaller pieces and consider each one independently. Moreover, when the omplexity of one of those pieces crosses a certain threshold, it makes sense to isolate that complexity by defining a separate abstraction that has a simple interface. The interface protects clients from the underlying details of the abstraction, thereby simplifying the conceptual structure The same approach works for teaching programming. To make the material easier for students to learn, this text adopts a library-based approach that emphasizes the principle of abstraction. The essential character of that approach is reflected in the following two features that set this book apart from other introductory texts 1. Libraries and modular development essential concepts in modern programming-are covered in considerable detail early in the presentation. Part II focuses entirely on the topics of libraries, interfaces, abstractions, and modular development. Students learn how to use these techniques effectively before they learn about arrays 2. The text demonstrates thc powcr of libraries by using them. It is onc thing to tcll students that libraries make it possible to hide complexity. It is quite another to demonstrate that concept. This text introduces several new libraries that hide details from the students until they are ready to assimilate them. The libraries give students the power to write useful programs that they could not develop on their own. Later chapters reveal the implementation of those libraries, thereby dents to ap te the power of abstract In 1992, i attempted to teach the introductory course using only the ansi libraries The results wcrc not encouraging. Each ncw topic rcquircd that thc student understand so much about the rest of C that there was no effective way to present the material. For example, students had to understand the mechanics of arrays, pointers, and allocation before they could use string data in and\y interesting way, even though string manipulation is simpler conceptually. My best students managed to understand what was gong on by the cnd of thc quarter. Most, howcvcr, did not. Sincc wc introduccd the library-bascd approach in early 1993, students have assimilated the material more easily and learned much more about computer science The library interfaces and associated implementations used in this text are reprinted in Appendix b, which also gives instructions for obtaining the source code electronically through anonymous FTP(Filc Transfcr Protocol) The order of presentation This book presents topics in the same order as Stanfords introductory course, except for the material in Chapter 17, which we cover in the second course. Depending on your audience and the goals of your course, you may want to vary the order of presentation. the following notes provide an overview of the chapters and indicate some of the more important dependencies Chapter 1 traces the history of computing and describes the programming process The chapter requires no programming per se but provides the contextual background for the rest of the text I have designed Chapter 2 and 3 for students with little or no background in programming. These chapters are conceptual in their approach and focus on problem solving rather than on the details of the C language When new students are faced with detailed rules of syntax and structure, they concentrate on learning the rules instead of the underlying concepts, which are vastly more important at this stage. If your students already know some programming, you could move much more quickly through this material Chapters 2 and 3 are informal in their approach and concentrate on developing the student's problem-solving skills. Along the way, they introduce several basic statement forms and control structures, but only as idioms to accomplish a specific task. Chapter 4 adds formal structure to this topic by describing each statement form in turn, detailing its syntax and semantics. the chapter also includes an extensive discussion of boolean data Chapter 5 introduces functions and procedures. It begins with simple examples of functions and then continues with increasingly sophisticatcd cxamplcs. Thc mechanics of parameter passing are discussed in a separate section that includes many diagrams to help students follow the flow of data from one function to another. The chapter ends with a significant programming example that illustrates the concept of stepwise refinement The algorithmic concepts presented in Chapter 6 are fundamental to computer science but may not be required for all students. If your audience consists of engineers or potential computer science majors the material will prove extremely valuable. For less technical audiences, however, you can eliminate much of this material without disturbing the flow of the prcscntation I have found that integrating graphics in the introductory course is a great way to increase student interest in the material. Chapter 7 exists for that reason. At this stage students have learned the mechanics of functions but have no burning need to write them Letting students draw complex pictures on the screen gives them that incentive. The graphics library is implcmcntcd for scvcral of thc major programming environments and an therefore be used in most institut Chapter& has two themes, which are in some sense separable. The first part of the chapter discusses design criteria for interfaces and is essential for anyone who needs to understand how modern programming works. Then second part of the chapter applies thosc principlcs to build a random number library. Thc random. h intcrfacc itsclf is lcss important than the general principles, although use of the random number library is required for a few of the exercises later in the text Chapter 9 introduces strings as an abstract type and represents, to a certain extent, the cornerstone of the library-based approach. By using a dynamic string library, students can easily write programs that perform sophisticated string manipulation, even though they do not yet understand the underlying representation, which is covered in Chapter 14 Introducing strings at this point in the presentation enables students to write much more exciting programs than they could otherwise On a first reading, it is easy to miss the purpose of Chapter 10, which appears to be an xtension of the discussion of strings begun in Chapter 9. The fundamental value of Chapter 10 does not lie in the Pig Latin program, which is more fun than it is practical The entire reason for the example is that it provides the motivation to build the scanner interface used to separate words on the input line. The scanner module proves its usefulness over and over again, not only in the first course but in subsequent courses as well. It is the most practical tool students create in the course and therefore serves as a compelling example of the value for modularity Chapter 11 through 16 introduce the fundamental compound structures-arrays, pointers, files, and recordsin an order that has worked well in practice. Because the base language is C, it is important to resent pointers as soon as possible after introducing arrays so that you can emphasize the connections between them. Moreover, having established these concepts, it is then possible in Chapter 14 to consider string data more closely thereby revealing the underlying representation that was concealed by the abstract definition. Chapter 16 integrates the fundamental data structures with the construction of a data-driven teaching machine, which is the most sophisticated example of programming resented in the test 17 includes thrcc cs that often appear in the first pro course: recursion, abstract data types, and analysis of algorithms. At Stanford, which is on the quarter system we teach all these topics in the second souse. If you decide to teach recursion in the first course, I strongly recommend that you do so early enough to allow students time to assimilate the material. One possibility is to discuss recursive functions immediately after Chapter 5 and recursive algorithms after Chapter6. Another approach is to cove recursion and analysis of algorithms together at the end of chapter 12 Instructor 's manual The Instructors manual Contains supplemental materials including a course syllabus suggestions for lecture design, sample assignments and examination, and solutions to all programming cxcrciscs. In addition to the printed manual, instructors who adopt this text can retrieve electronic copies of solution sets and related materials. For details on obtaining solutions, please contract your local Addison-Wesley representative. All other supplemental material is available on-line. For explicit instructions see Appendix B Acknowledgments The text has come a long way from its initial stages as class notes, in large measure bccausc of suggcstions from pcoplc who have worked with the text in onc capacity of another. I am particularly grateful to the Stanford lecturers Jon Becker, Katie Capps, Stephen Clausing, Todd Feldman, Allison Hansen, Margaret Johnson, Jim Kent, Andrew Kosoresow. Mehran Sahami, and Julie Zelenski who have taught this material in 14 different sections of the introductory course over the past three years. I am also indebted to all the scction leaders and tcaching assistants as wcll as the coordinators of the student-teaching program-Felix Baker. John Lilly, Sandy nguyen, Bryan Rollins, and Scott Wiltamuth-who provided much needed logistical support Many of the best ideas for the text came from a seminar on teaching introductory computer science that I conducted beginning in 1992-93. It provided a forum for thrashing out the hard issues and made a significant difference in the text i want to thank everyone who participated Perry Arnold, Jon Becker, Tom Bogart, Karl Brown, Bryan Busse, Katie Capps, Peter Chang, Scott Cohen, Stacey Doerr, Jeff Forbes, Stephen Freund, Jon Goldberg, Tague Griffith, Matt Harad, Lindsay Lack, Christine Lee, Tricia Lee, John Lilly, Albert Lin, Mara Mather, Hugh McGuire, Jeffrey Oldham, David O'Keefe, Bryan rollins Samir Saxena, Nikhyl Singhal, Eric Tucker, Garner, Garner Weng, Howard Wong-Toi, and John youn I also want to thank all the students who have used and critiqued the draft version of the text: Adjo amekudzi, Eric Anderson, Andrew Arnold, Kevin Berk, Kevin Carbajal Ajit Chaudhari, Alida Cheung, Hye-won Choi, Elisabeth Christensen, Ralph Davis, Joel Firehammer, Peter Frelinghuysen, Trevor Gattis, Teddy Harris, Heather Heal, Enoch luang, Ann Lee, Ted Lee, Daphne Lichtensztajn, Lisa Maddock, Allan Marcus, Brad Me Goran Forrest melton adrienne osborn Masatoshi saito. Anne stern Ricardo urena and nichole wilson In 1993-94, several faculty members at other universities tested this material in draft form and made several valuable suggestions. i especially want to thank margo seltzer at Harvard University, rob Langsner at the University of Nevada(Reno), Richard Chang at the University of Maryland (Baltimore County ) Jane Turk at La Salle University, and Kim Kihlstrom at Westmont College for helping to refine the text from its early stages I am also indebted to my reviewers Stephen allan Utah State University James schmelze Tufts Universit Don goelman Villanova university Michael skolnick Renseelaer Polytechnic Stan kolasa Rutgers Universit Jeffery a slomka Southwest Texas State University Harry R. Lcwis Harvard Univcrsity Kevin smith Emory university Bill muellner Elmhurst College Phil tromovitch SUNY-Stony Brook Rayon niemi Rochester Institute of Technology John a. trono St. Michaels’ College Robert g. plantz Sonoma State University Robert walker Rensselaer polytechnic David rosenthal Seton hall Richard Weinand Wayne state University In addition, I have received useful advice along the way from several friends and colleagues, including Josh Barnes, Pavel Curtis, Kathleen Kells, James Finn, and Steve Lewin-Berlin The final version of this text owes much to my editors at Addison-Wesley, who have been helpful throughout the process. In particular, I thank lynne doran Cote, Sue Gleason, Peter Gordon, Laura Michaels, Jim Rigney, Karen Stone, and Amy Willcutt for all their work. And I am extremely fortunate to have lauren rusk as both my developmental editor and my partner; without her, nothing would ever come out as well as it should Chapter l Introduction TThe Analytical Engine offers a new, a vast, and a powerful language .. for the purposes of mankind Augusta Ada byron, Lady Lovelace, The Sketch of the Analytical Eng ine Invented by Charles babbage, 1843 OBJECTIVES To understand the distinction between hardware and software To recognize that problem solving is an essential component of computer cience To understand the meaning of the term algorithm To appreciate the role of the compiler as a translator between a higher-level programming language and thc lowcr-lcvcl machinc language To recognize the principal types of programming errors. To appreciate the importance of software maintenance and the use of good software engineering practice s we approach the end of the twentieth century, it is hard to believe that computers did not even exist as recently as 1940. Computers are everywhere today and it is the popular wisdom, at least among headline writers, to say that we live in the computer age 1-1 A brief history of computing In a certain sense. however, computing has been around since ancient times Much of early mathematics was devoted to solving computational problems of practical importance, such as monitoring the number of animals in a herd, calculating the arca of a plot of land, or rccording a commcrcial transaction. Thesc activitics required people to develop new computational techniques and in some cases, to invent calculating machines to help in the process. For example, the abacus, a a simple counting device consisting of beads that slide along rods, has been used in Asia for thousands of years, possibly since 2000 B C Throughout most of its history, computing has progressed rclativcly slowly. In 1623. a german scientist named Wilhelm Schickard invented the first known mechanical calculator, capable of performing simple arithmetical computations automatically. Although Schickard's device was lost to history through the ravages of the Thirty Years'War(1618-1648), the French philosopher Blaise Pascal used similar techniques to construct a mcchanical adding machinc in thc 1640s, a copy of which

...展开详情
试读 127P C语言的科学与技术(英文版)
立即下载 低至0.43元/次 身份认证VIP会员低至7折
抢沙发
一个资源只可评论一次,评论内容不能少于5个字
上传资源赚积分or赚钱
最新推荐
C语言的科学与技术(英文版) 10积分/C币 立即下载
1/127
C语言的科学与技术(英文版)第1页
C语言的科学与技术(英文版)第2页
C语言的科学与技术(英文版)第3页
C语言的科学与技术(英文版)第4页
C语言的科学与技术(英文版)第5页
C语言的科学与技术(英文版)第6页
C语言的科学与技术(英文版)第7页
C语言的科学与技术(英文版)第8页
C语言的科学与技术(英文版)第9页
C语言的科学与技术(英文版)第10页
C语言的科学与技术(英文版)第11页
C语言的科学与技术(英文版)第12页
C语言的科学与技术(英文版)第13页
C语言的科学与技术(英文版)第14页
C语言的科学与技术(英文版)第15页
C语言的科学与技术(英文版)第16页
C语言的科学与技术(英文版)第17页
C语言的科学与技术(英文版)第18页
C语言的科学与技术(英文版)第19页
C语言的科学与技术(英文版)第20页

试读结束, 可继续阅读

10积分/C币 立即下载 >