C++ Premier Plus 6th edition - Programming excercise - Chapter17 - 1

想到3种实现方法

// APPROACH 1:peek()
#include<iostream>
#include<sstream>

int main()
{
	using namespace std;
	istringstream s1("Hello world!$");
	string str = s1.str();
	cout << "Here is a string: " << str << "\n";
	char ch;
	int ct = 0;
	// whether extrac the charater is determined it's $ or not, telling by peek()
	cout << "\nFollowing characters are read: \n";
	while (s1.peek() != '$')// peek the character, if not $, extrac it
	{
		s1.get(ch);
		ct++;
		cout << ch << " ";
	}

	cout << "\n(Total characters read before delimiter '$': " << ct << " )";

	cout << "\n\nThe first character left in stream now is: ";
	cout.put(s1.peek());

	cin.get();
	return 0;
}

/*
// APPROACH 2:get() and unget()
#include<iostream>
#include<sstream>

int main()
{
	using namespace std;
	istringstream s1("Hello world!$");
	string str = s1.str();
	cout << "Here is a string: " << str << "\n";
	char ch;
	int ct = 0;
	// whether extrac the charater is determined it's $ or not, telling by peek()
	cout << "\nFollowing characters are read: \n";
	while (s1.get(ch) && ch != '$')
	{
		ct++;
		cout << ch << " ";
	}
	s1.unget();// make the most recently extracted character '$' available again

	cout << "\n(Total characters read before delimiter '$': " << ct << " )";
	cout << "\n\nThe first character left in stream now is: ";
	cout.put(s1.peek());

	cin.get();
	return 0;
}
*/

/*
// APPROACH 3: basic_istream& get( char_type* s, std::streamsize count, char_type delim );
#include<iostream>
#include<sstream>

int main()
{
	using namespace std;
	istringstream s1("Hello world!$");
	string str = s1.str();
	cout << "Here is a string: " << str << "\n";

	char* ps = new char[str.size()];// store characters will be extracted by get(). +1 is for ending null
	s1.get(ps, str.size(), '$');// get() will keep $ in input stream. But getline() will discard
	// or s1.get(ps,ct)

	ps[s1.gcount()] = '\n';// set ending null

	cout << "\nFollowing characters are read: \n";
	for (int i = 0; ps[i]!='\n'; i++)// or i<s1.gcount()
		cout << ps[i] << " ";

	cout << "\n(Total characters read before delimiter '$': " << s1.gcount() << " )";// or ct instead s1.gcount()
	cout << "\n\nThe first character left in stream now is: ";
	cout.put(s1.peek());

	delete ps;
	cin.get();
	return 0;
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This book is devoted to practical C programming. C is currently the premier language for software developers. That's because it's widely distributed and standard. Newer languages are available, such as C++, but these are still evolving. C is still the language of choice for robust, portable programming. This book emphasizes the skills you will need to do real-world programming. It teaches you not only the mechanics of the C language, but the entire life cycle of a C program as well (including the program's conception, design, code, methods, debugging, release, documentation, maintenance, and revision). Good style is emphasized. To create a good program you must do more than just type in code. It is an art in which writing and programming skills blend themselves together to form a masterpiece. True art can be created. A well-written program not only functions correctly, but is simple and easy to understand. Comments allow the programmer to include descriptive text inside the program. When clearly written, a commented program is highly prized. A program should be as simple as possible. A programmer should avoid clever tricks. This book stresses simple, practical rules. For example, there are 15 operator precedence rules in C. These can be simplified into two rules: 1. Multiply and divide come before add and subtract. 2. Put parentheses around everything else. Consider two programs. One was written by a clever programmer using all the tricks. The program contains no comments, but it works. The other program is well commented and nicely structured, but it doesn't work. Which program is more useful? In the long run, the broken one. It can be fixed. Although the clever program works now, sooner or later all programs have to be modified. The worst thing that you will ever have to do is to modify a cleverly written program. This handbook is written for people with no previous programming experience or programmers who already know C and want to improve their style and reliability. You should have access to a computer and TEAM FLY PRESENTS 9 know how to use the basic functions such as a text editor and the filesystem. Specific instructions are given for producing and running programs using the UNIX operating system with a generic cc compiler or the Free Software Foundation's gcc compiler. For MS-DOS/Windows users, instructions are included for Borland C++, Turbo C++, and Microsoft Visual C++. (These compilers compile both C and C++ code.) The book also gives examples of using the programming utility make for automated program production. How This Book is Organized You must crawl before you walk. In Part I we teach you how to crawl. These chapters enable you to write very simple programs. We start with the mechanics of programming and programming style. Next, you learn how to use variables and very simple decision and control statements. In Chapter 7, we take you on a complete tour of the software life cycle to show you how real programs are created. Part II describes all of the other simple statements and operators that are used in programming. You'll also learn how to organize these statements into simple functions. In Part III we take our basic declarations and statements and learn how they can be used in the construction of advanced types such as structures, unions, and classes. We'll also introduce the concept of pointers. Finally, a number of miscellaneous features are described Part IV. Chapter
### 回答1: 格外实用的c语言编程书籍——premier电子版。它是由Prata编写的,是一本<classic>级别的大部头教材,对c语言的各个方面进行了细致地讲解。该书在全球范围内广受好评,被誉为c语言编程经典入门读物。相较于传统的纸质版,电子版更具备便携性和交互性两大特点。在电子版上,你可以轻松地进行文字搜索、章节跳转、笔记记录等操作,在阅读体验上更为人性化,使你更好地掌握知识。 此书内容详尽、深入浅出,对想从事c语言编程的软件工程师具有广泛的适用性,从初学者到进阶者都能够从中获益良多。全书共计1100多页,涵盖了c语言的基础知识和高级应用,包括数据类型、流控制语句、结构体、函数、指针、内存管理等等。同时,该书中还有大量的示例代码和练习题,非常适合用来辅助学习和巩固所学知识。 总之,c语言是计算机编程语言中必不可少的一门,而premier电子版则是学习c语言的必备工具书。不管是从事软件开发的新手,还是追求技术深度的老手,都能够从这本书中汲取到宝贵的知识,帮助他们更好地编写优秀的程序。 ### 回答2: c premier是一种用于C语言编程的电子版本集成开发环境(IDE),它可以提供一个完整的开发环境,包括编辑器、编译器和调试器。使用c premier电子版可以方便地编写和管理C语言程序,加速开发流程,减少开发错误。c premier具有先进的功能,如语法突出显示、自动完成和脚本编写等,可用于各种类型的C语言程序开发包括嵌入式系统、桌面应用程序等。此外,c premier还具有强大的调试器,可以帮助开发人员查找和修复程序中的错误。c premier电子版的优势之一是它可以在不同平台上运行,包括Windows、Mac和Linux系统。总之,c premier电子版是一种非常实用的C语言开发环境,可以轻松编写高质量的C语言程序。 ### 回答3: Premier电子版是一种数字化的电子期刊,主要用于在线发布、订阅和阅读相关的经济、商业、金融和管理等领域的文章和研究。该电子版的优势在于其能够提供便利快捷的服务,用户无需购买实体版本,通过在线订阅即可随时随地浏览该刊物,方便用户学习和参考。此外,Premier电子版可以保证内容质量和权威性,其收录的文章和报告均来自于经过严格审稿的学术或专业期刊,保证了内容的可信度和专业性。另外,Premier电子版拥有较完善的检索和分类系统,用户可以按照自己的需求和兴趣对文章进行检索和筛选,从而节省时间和精力。总的来说,Premier电子版为学术研究者、商业从业者和爱好者提供了一个高质量、可信、便捷和多样的学习和借鉴资源,有助于推动相关领域的发展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值