Problem Solving with C++ 10th Edition Global - Sách của Walter Savitch

Hướng dẫn toàn diện về kỹ thuật giải quyết vấn đề trong lập trình C, tập trung vào phương pháp nén dữ liệu và tối ưu hóa hiệu suất code. Các ví dụ thực tế và

Chuyên ngành

Khoa học máy tính

Người đăng

Ẩn danh

Thể loại

Giáo trình

2018

1.1K
0
0

Phí lưu trữ

0 Point

Tóm tắt

I. Tổng quan về problem solving with C

Problem solving with C++ là phương pháp giải quyết vấn đề bằng ngôn ngữ lập trình C++. Cuốn sách của Walter Savitch trình bày cách tiếp cận này một cách hệ thống. Người học bắt đầu từ khái niệm máy tính cơ bản. Họ tiến dần đến cú pháp và tư duy thuật toán. C++ là ngôn ngữ mạnh và linh hoạt. Nó hỗ trợ cả lập trình thủ tục và hướng đối tượng. Mục tiêu cốt lõi là biến vấn đề thực tế thành chương trình chạy được. Quá trình này đòi hỏi tư duy rõ ràng. Lập trình viên phải hiểu yêu cầu trước khi viết mã. Họ chia bài toán lớn thành phần nhỏ. Mỗi phần được giải quyết riêng. Sau đó các phần ghép lại thành giải pháp hoàn chỉnh. C++ chạy trên PC, workstation và mainframe. Cấu hình cơ bản giống nhau cho mọi loại máy. Điều này giúp người học áp dụng kiến thức rộng rãi. Problem solving with C++ không chỉ dạy cú pháp. Nó rèn luyện cách suy nghĩ có cấu trúc và logic.

1.1. Vai trò của C trong giải quyết vấn đề

C++ cung cấp công cụ mạnh để mô hình hóa bài toán. Ngôn ngữ này cho phép điều khiển bộ nhớ chi tiết. Nó cũng hỗ trợ trừu tượng hóa cấp cao. Nhờ vậy, lập trình viên giải quyết được cả vấn đề nhỏ và hệ thống lớn. C++ chạy nhanh và hiệu quả. Nó được dùng rộng rãi trong kinh doanh, công nghiệp và khoa học. Problem solving with C++ tận dụng sức mạnh này. Người học áp dụng C++ để biến ý tưởng thành chương trình. Tư duy giải quyết vấn đề trở thành kỹ năng nền tảng. Kỹ năng này theo người học suốt sự nghiệp lập trình.

1.2. Cấu trúc cuốn sách Savitch

Cuốn Problem Solving with C++ ấn bản thứ mười được Walter Savitch viết. Kenrick Mock đóng góp nội dung bổ sung. Sách bắt đầu bằng giới thiệu máy tính và lập trình C++. Nó trình bày năm thành phần phần cứng chính. Đó là thiết bị nhập, xuất, bộ xử lý, bộ nhớ chính và bộ nhớ phụ. Sách đi kèm tài nguyên số như VideoNotes và mã nguồn. Người học có quyền truy cập 12 tháng. Cách trình bày theo trình tự từ dễ đến khó. Mỗi chương xây dựng trên chương trước. Cấu trúc này giúp người mới tiếp cận problem solving with C++ dễ dàng và vững chắc.

II. Phân tích vấn đề trong problem solving with C

Phân tích vấn đề là bước đầu tiên và quan trọng nhất. Nhiều lập trình viên mới vội viết mã ngay. Cách làm này thường dẫn đến lỗi. Problem solving with C++ nhấn mạnh việc hiểu bài toán trước. Người lập trình phải xác định đầu vào và đầu ra. Họ cần biết dữ liệu nào được nhập vào. Họ cũng phải biết kết quả mong muốn là gì. Ví dụ chương trình tính số hạt đậu cần ba biến số. Đó là số vỏ đậu, số hạt mỗi vỏ và tổng số hạt. Mỗi biến có vai trò rõ ràng. Vấn đề thường phức tạp hơn vẻ ngoài. Lập trình viên phải tách nó thành các bước nhỏ. Mỗi bước phải đơn giản và rõ ràng. Sai sót ở khâu phân tích lan ra toàn chương trình. Vì vậy giai đoạn này đòi hỏi sự cẩn thận. Người lập trình và người dùng thường là hai người khác nhau. Hiểu nhu cầu người dùng giúp phân tích chính xác. Phân tích tốt tạo nền cho giải pháp đúng.

2.1. Xác định đầu vào và đầu ra

Mỗi bài toán có dữ liệu đầu vào và kết quả đầu ra. Problem solving with C++ yêu cầu xác định rõ hai yếu tố này. Lệnh cin đọc dữ liệu từ người dùng. Lệnh cout hiển thị kết quả ra màn hình. Trong ví dụ hạt đậu, chương trình đọc hai số. Đó là số vỏ và số hạt mỗi vỏ. Sau đó nó tính tổng và in ra. Việc tách biệt đầu vào và đầu ra giúp tư duy mạch lạc. Lập trình viên thấy rõ luồng dữ liệu. Nhờ vậy, họ thiết kế chương trình chính xác hơn và ít lỗi hơn.

2.2. Chia nhỏ bài toán phức tạp

Bài toán lớn khó giải quyết cùng lúc. Problem solving with C++ áp dụng chiến lược chia để trị. Lập trình viên tách vấn đề thành nhiều phần nhỏ. Mỗi phần giải quyết một nhiệm vụ riêng. Các phần nhỏ dễ kiểm tra và sửa lỗi. Sau khi xong, chúng ghép lại thành giải pháp hoàn chỉnh. Cách này giảm độ phức tạp đáng kể. Nó cũng giúp tái sử dụng mã. Một phần nhỏ có thể dùng cho nhiều bài toán. Tư duy phân rã là kỹ năng cốt lõi. Nó áp dụng cho mọi quy mô dự án lập trình.

III. Phương pháp giải quyết với problem solving C

Sau phân tích là giai đoạn thiết kế giải pháp. Problem solving with C++ hướng dẫn cách xây dựng thuật toán. Thuật toán là chuỗi bước rõ ràng để giải bài toán. Lập trình viên viết thuật toán bằng ngôn ngữ tự nhiên trước. Sau đó họ chuyển nó thành mã C++. Một chương trình C++ bắt đầu bằng dòng include iostream. Nó dùng namespace std để truy cập thư viện chuẩn. Hàm main là điểm khởi đầu của mọi chương trình. Bên trong main, các lệnh chạy theo thứ tự. Biến được khai báo trước khi sử dụng. Kiểu int dùng cho số nguyên. Phép gán lưu kết quả tính toán vào biến. Ví dụ totalPeas bằng numberOfPods nhân peasPerPod. Mỗi lệnh kết thúc bằng dấu chấm phẩy. Lệnh return 0 báo chương trình kết thúc thành công. Phương pháp này biến ý tưởng thành mã chạy được. Người học luyện tập nhiều lần để thành thạo. Thực hành đều đặn xây dựng kỹ năng vững chắc.

3.1. Thiết kế thuật toán trước khi viết mã

Thuật toán là kế hoạch giải bài toán. Problem solving with C++ khuyến khích thiết kế thuật toán đầu tiên. Lập trình viên mô tả các bước bằng lời. Họ kiểm tra logic trên giấy trước khi gõ mã. Cách này phát hiện lỗi sớm. Nó tiết kiệm thời gian sửa chữa về sau. Thuật toán tốt độc lập với ngôn ngữ. Cùng một thuật toán có thể viết bằng C++ hay ngôn ngữ khác. Khi logic đã rõ, việc chuyển sang mã trở nên đơn giản. Người học tập trung vào cú pháp mà không lo về luồng xử lý.

3.2. Cấu trúc một chương trình C cơ bản

Mọi chương trình C++ có cấu trúc chuẩn. Dòng đầu là chỉ thị tiền xử lý include iostream. Tiếp theo là using namespace std. Hàm int main mở đầu phần thân chương trình. Dấu ngoặc nhọn bao quanh khối lệnh. Biến được khai báo với kiểu dữ liệu cụ thể. Lệnh cout xuất dữ liệu ra màn hình. Lệnh cin nhận dữ liệu từ bàn phím. Cuối hàm là return 0. Hiểu cấu trúc này là bước nền tảng. Problem solving with C++ giúp người học nắm vững khung mẫu. Từ đó họ xây dựng chương trình phức tạp hơn.

IV. Ứng dụng và kết luận problem solving C

Problem solving with C++ mang lại giá trị thực tế rộng lớn. Kỹ năng này áp dụng trong nhiều lĩnh vực. C++ được dùng trong kinh doanh, công nghiệp và khoa học. Nó chạy trên PC, workstation và mainframe. Lập trình viên giải quyết bài toán ở mọi quy mô. Tư duy có cấu trúc là tài sản quý giá. Nó vượt ra ngoài việc viết mã đơn thuần. Người học rèn luyện cách phân tích và lập kế hoạch. Họ chia vấn đề lớn thành phần nhỏ. Họ thiết kế thuật toán trước khi viết mã. Những kỹ năng này theo họ suốt sự nghiệp. Cuốn sách Savitch cung cấp nền tảng vững chắc. Tài nguyên số hỗ trợ học tập hiệu quả. VideoNotes minh họa từng khái niệm trực quan. Mã nguồn giúp người học thực hành. Problem solving with C++ không chỉ là môn học. Nó là tư duy giải quyết vấn đề. Tư duy này có ích cho cả đời sống và công việc. Người kiên trì luyện tập sẽ thành thạo. Lập trình trở thành công cụ mạnh trong tay họ.

4.1. Ứng dụng thực tế của C

C++ hiện diện trong nhiều ngành nghề. Doanh nghiệp dùng nó để xây dựng phần mềm hiệu năng cao. Ngành khoa học dùng C++ cho tính toán phức tạp. Các hệ thống nhúng và trò chơi cũng dựa vào C++. Ngôn ngữ này chạy nhanh và quản lý bộ nhớ tốt. Problem solving with C++ trang bị kỹ năng nền tảng. Người học áp dụng nó vào dự án thật. Họ giải quyết bài toán cụ thể bằng mã hiệu quả. Khả năng này được nhà tuyển dụng đánh giá cao. C++ vẫn giữ vị trí quan trọng trong ngành công nghệ.

4.2. Lộ trình phát triển kỹ năng lập trình

Học problem solving with C++ là hành trình dài. Người mới bắt đầu từ khái niệm cơ bản. Họ học cú pháp và biến số. Sau đó họ nắm vòng lặp và hàm. Tiếp theo là cấu trúc dữ liệu và hướng đối tượng. Mỗi bước xây dựng trên bước trước. Thực hành đều đặn là chìa khóa thành công. Người học nên viết nhiều chương trình nhỏ. Họ học từ lỗi của chính mình. Tài nguyên như VideoNotes hỗ trợ quá trình này. Kiên trì và tư duy logic dẫn đến thành thạo. Kỹ năng lập trình mở ra nhiều cơ hội nghề nghiệp.

21/04/2026

Trích đoạn nội dung tài liệu

GLOBAL EDITION Problem Solving with C++    TENTH EDITION    Walter Savitch Digital Resources for Students Your new textbook provides 12-month access to digital resources that may include VideoNotes (step-by-step video tutorials on programming concepts), source code, web chapters, quizzes, and more. Refer to the preface in the textbook for a detailed list of resources. Follow the instructions below to register for the Companion Website for Walter Savitch’s Problem Solving with C++, Tenth Edition, Global Edition. Go to www. Enter the title of your textbook or browse by author name. Click Companion Website. Click Register and follow the on-screen instructions to create a login name and password. Use a coin to scratch off the coating and reveal your access code. Do not use a sharp knife or other sharp object as it may damage the code. Use the login name and password you created during registration to start using the online resources that accompany your textbook. IMPORTANT: This prepaid subscription does not include access to MyProgrammingLab, which is available at www.com for purchase. This access code can only be used once. This subscription is valid for 12 months upon activation and is not transferable. If the access code has already been revealed it may no longer be valid. For technical support go to https://support.com/getsupport/ PROBLEM SOLVING with C++ Tenth Edition Global Edition PROBLEM SOLVING with C++ Walter J. Savitch UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR Kenrick Mock UNIVERSITY OF ALASKA, ANCHORAGE 330 Hudson Street, New York, NY 10013 Senior Vice President Courseware Portfolio Management: Marcia J. Horton Director, Portfolio Management: Engineering, Computer Science & Global Editions: Julian Partridge Portfolio Manager: Matt Goldstein Assistant Acquisitions Editor, Global Edition: Aditee Agarwal Portfolio Management Assistant: Kristy Alaura Field Marketing Manager: Demetrius Hall Product Marketing Manager: Yvonne Vannatta Managing Producer, ECS and Math: Scott Disanno Content Producer: Sandra L. Rodriguez Project Editor, Global Edition: K. Neelakantan Senior Manufacturing Controller, Global Edition: Angela Hawksbee Manager, Media Production, Global Edition: Vikram Kumar Cover Designer: Lumina Datamatics, Inc. Cover Photo: Iana Chyrva/Shutterstock The author and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of theories and programs to determine their effectiveness. The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation contained in this book. The author and publisher shall not be liable in any event for incidental or consequential damages with, or arising out of, the furnishing, performance, or use of these programs. Pearson Education Limited KAO Two KAO Park Harlow CM17 9NA United Kingdom and Associated Companies throughout the world Visit us on the World Wide Web at: www.com © Pearson Education Limited 2018 The rights of Walter Savitch to be identified as the author of this work has been asserted by him in accordance with the Copyright, Designs and Patents Act 1988. Authorized adaptation from the United States edition, entitled Problem Solving with C++, 10th Edition, ISBN 978-0-13-444828-2 by Walter Savitch published by Pearson Education © 2018. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without either the prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6–10 Kirby Street, London EC1N 8TS. All trademarks used herein are the property of their respective owners. The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners. British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library 10 9 8 7 6 5 4 3 2 1 ISBN 10:   1-292-22282-4 ISBN 13: 978-1-292-22282-0 Typeset by iEnergizer Aptara®, Ltd. Printed and bound in Malaysia Preface This book is meant to be used in a first course in programming and computer science using the C++ language. It assumes no previous programming experi- ence and no mathematics beyond high school algebra. If you have used the previous edition of this book, you should read the following section that explains the changes to this tenth edition and then you can skip the rest of this preface. If you are new to this book, the rest of this preface will give you an overview of the book. Changes to the Tenth Edition This tenth edition presents the same programming philosophy as the ninth edition. All of the material from the ninth edition remains, but with the fol- lowing enhancements: ■ Consistent use of camelCase notation rather than underscore_case through- out the text. ■ Discussion in Chapter 10 of shallow vs. ■ Additional material in Chapter 12 and 17 on compiling templates with header files. ■ Additional material in Chapter 18 on the std::array class, regular expres- sions, threads, and smart pointers in C++11. ■ Correction of errata and edits for clarity such as indicating preferred meth- ods for file I/O, naming of terminology, better definition of encapsulation, and removing material that is now standard in C++11 and higher. ■ Ten new Programming Projects. ■ Five new VideoNotes for a total of sixty nine VideoNotes. These VideoNotes walk students through the process of both problem solving and coding to help reinforce key programming concepts. An icon appears in the margin of the book when a VideoNote is available regarding the topic covered in the text. If you are an instructor already using the ninth edition, you can continue to teach your course almost without change. Flexibility in Topic Ordering This book was written to allow instructors wide latitude in reordering the material. To illustrate this flexibility, we suggest two alternative ways to order 5 6 PREFACE the topics. There is no loss of continuity when the book is read in either of these ways. To ensure this continuity when you rearrange material, you may need to move sections rather than entire chapters. However, only large sections in convenient locations are moved. To help customize a particular order for any class’s needs, the end of this preface contains a dependency chart, and each chapter has a “Prerequisites” section that explains what material needs to be covered before each section in that chapter. Reordering 1: Earlier Classes To effectively design classes, a student needs some basic tools such as control structures and function definitions. This basic material is covered in Chapters 1 through 6. After completing Chapter 6, students can begin to write their own classes. One possible reordering of chapters that allows for such early coverage of classes is the following: Basics: Chapters 1, 2, 3, 4, 5, and 6. This material covers all control struc- tures, function definitions, and basic file I/O. Chapter 3, which covers ad- ditional control structures, could be deferred if you wish to cover classes as early as possible. Classes and namespaces: Chapter 10, Sections 11.2 of Chapter 11, and Chapter 12. This material covers defining classes, friends, overloaded operators, and namespaces. Arrays, strings and vectors: Chapters 7 and 8 Pointers and dynamic arrays: Chapter 9 Arrays in classes: Sections 11.4 of Chapter 11 Inheritance: Chapter 15 Recursion: Chapter 14. (Alternately, recursion may be moved to later in the course.) Pointers and linked lists: Chapter 13 Any subset of the following chapters may also be used: Exception handling: Chapter 16 Templates: Chapter 17 Standard Template Library: Chapter 18 Reordering 2: Classes Slightly Later but Still Early This version covers all control structures and the basic material on arrays before doing classes, but classes are covered later than the previous ordering and slightly earlier than the default ordering. Basics: Chapters 1, 2, 3, 4, 5, and 6. This material covers all control structures, function definitions, and the basic file I/O. PREFACE 7 Arrays and strings: Chapter 7, Sections 8.2 of Chapter 8 Classes and namespaces: Chapter 10, Sections 11.2 of Chapter 11, and Chapter 12. This material covers defining classes, friends, overloaded operators, and namespaces. Pointers and dynamic arrays: Chapter 9 Arrays in classes: Sections 11.4 of Chapter 11 Inheritance: Chapter 15 Recursion: Chapter 14. (Alternately, recursion may be moved to later in the course.3 Pointers and linked lists: Chapter 13 Any subset of the following chapters may also be used: Exception handling: Chapter 16 Templates: Chapter 17 Standard Template Library: Chapter 18 Accessibility to Students It is not enough for a book to present the right topics in the right order. It is not even enough for it to be clear and correct when read by an instructor or other experienced programmer. The material needs to be presented in a way that is accessible to beginning students. In this introductory textbook, I have endeav- ored to write in a way that students find clear and friendly. Reports from the many students who have used the earlier editions of this book confirm that this style makes the material clear and often even enjoyable to students. ANSI/ISO C++ Standard This edition is fully compatible with compilers that meet the latest ANSI/ISO C++ standard. At the time of this writing the latest standard is C++14. Advanced Topics Many “advanced topics” are becoming part of a standard CS1 course. Even if they are not part of a course, it is good to have them available in the text as enrichment material. This book offers a number of advanced topics that can be integrated into a course or left as enrichment topics. It gives thorough coverage of C++ templates, inheritance (including virtual functions), exception han- dling, the STL (Standard Template Library), threads, regular expressions, and smart pointers. Although this book uses libraries and teaches students the importance of libraries, it does not require any nonstandard libraries. This book uses only libraries that are provided with essentially all C++ implementations. 8 PREFACE Dependency Chart The dependency chart on the next page shows possible orderings of chapters and subsections. A line joining two boxes means that the upper box must be covered before the lower box. Any ordering that is consistent with this partial ordering can be read without loss of continuity. If a box contains a section number or numbers, then the box refers only to those sections and not to the entire chapter. Summary Boxes Each major point is summarized in a boxed section. These boxed sections are spread throughout each chapter. Self-Test Exercises Each chapter contains numerous Self-Test Exercises at strategic points. Com- plete answers for all the Self-Test Exercises are given at the end of each chapter. VideoNotes VideoNote VideoNotes are designed for teaching students key programming concepts and techniques. These short step-by-step videos demonstrate how to solve prob- lems from design through coding. VideoNotes allow for self-paced instruction with easy navigation including the ability to select, play, rewind, fast-forward, and stop within each VideoNote exercise. Online Practice and Assessment with MyProgrammingLab MyProgrammingLab helps students fully grasp the logic, semantics, and syntax of programming. Through practice exercises and immediate, personalized feedback, MyProgrammingLab improves the programming competence of beginning students who often struggle with the basic concepts and paradigms of popular high-level programming languages. A self-study and homework tool, a MyProgrammingLab course consists of hundreds of small practice problems organized around the structure of this textbook. For students, the system automatically detects errors in the logic and syntax of their code submissions and offers targeted hints that enable students to figure out what went wrong—and why. For instructors, a comprehensive gradebook tracks correct and incorrect answers and stores the code inputted by students for review. MyProgrammingLab is offered to users of this book in partnership with Turing’s Craft, the makers of the CodeLab interactive programming exercise system. For a full demonstration, to see feedback from instructors and students, or to get started using MyProgrammingLab in your course, visit www.

Nội dung được bảo vệ bản quyền — Tải xuống đầy đủ