Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

7.4 处理负数

当测试完计算器程序后,你会发现它不能很好地处理负数。例如,输入

将返回一个错误信息,必须将其写为

但这并不符合人们的使用习惯。

在程序调试和测试后期发现这样的问题是很平常的事,只有这时我们才能有机会弄清楚程序到底实现了什么功能,并根据程序给出的反馈不断改进我们的设计。在程序的设计过程中,一种明智的做法是:在安排工作日程时就预留出时间,使我们能有机会体会开发过程中获得的经验教训,从中受益,回过头来改进程序。“1.0版”往往未经必要的精化就发布了,这通常是由于开发日程过紧,或者是为了防止在项目“后期”对详细设计进行修改而采取的呆板的项目管理策略造成的——“后期”添加“特性”将会是灾难性的。但实际上,当一个程序对于简单使用来说已经足够好,但还未到可以发布的程度时,开发进程还远未到“后期”。此时还是开发进程的“早期”,正是我们从程序中获取实实在在的经验教训、进行改进的好时机。在实际安排工作日程时,应该将这样的过程考虑其中。

对于本例,我们只需修改文法来处理负号。最简单的方式是修改Primary的定义,将

改为:

我们还增加了一元加运算符,C++语言也支持这个运算符。当有了一元减后,人们通常也会尝试一元加,因此实现它是有意义的。而且既然实现了一元减,实现一元加也很容易,没有必要纠缠于它到底有没有用。于是,实现Primary的函数变为 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252