1.

字符函数库cctype isalnum( )  如果参数是字母数字,即字母或数字,该函数返回true isalpha( ) 如果参数是字母,该函数返回true iscntrl( ) 如果参数是控制字符,该函数返回true isdigit( ) 如果参数时数字(0~9),该函数返回true isgraph( ) 如果参数时除空格以外的打印字符,该函数返回true islower( ) 如果参数是小写字母,该函数返回true isprint( ) 如果参数是打印字符(包括空格),该函数返回true ispunct( ) 如果参数标点符号,该函数返回true isspace( ) 如果参数是标准的空白字符(空格、进制、换行符、回车、制表符),该函数返回true isupper( ) 如果参数是大写字母,该函数返回true isxdigit( ) 如果参数是十六进制数字,0~9,a~f,A~F,,该函数返回true tolower( ) 如果参数是大写字母,则返回其小写,否则返回该参数 toupper( ) 如果参数是小写字母,则返回其大写,否则返回该参数

例子

#include<iostream>
#include<cctype>
using namespace std;
int main()
{cout<<"Enter text for analysis, and type @"" to terminate input.\n";char ch;int whitespace = 0;int digits = 0;int chars = 0;int punct = 0;int others = 0;cin.get(ch);while(ch != '@'){if(isalpha(ch))chars++;else if(isspace(ch))whitespace++;else if(isdigit(ch))digits++;else if(ispunct(ch))punct++;elseothers++;cin.get(ch);}cout<< chars <<" letters, "<<whitespace<<" whitespace, "<<digits<<" digits, "<<punct<<" punctuations, "<<others<<" others."<<endl;return 0;
}

2.
enum { a,b,c,d,e,f,g};
a~g代表0~6

3.
clear( )用来重置错误输入的标记,同时也重置文件尾

4.

#include<iostream>
using namespace std;
const int Max = 5;
int main()
{int golf[Max];cout<<"Please enter your golf scores."<<endl;cout<<"You must enter "<<Max<<" rounds"<<endl;int i;for(i=0;i<Max;i++){cout<<"round #"<<i+1<<": ";while(!(cin>>golf[i])){cin.clear();while(cin.get()!='\n')continue;cout<<"Please enter a number: ";}}double total = 0.0;for(i = 0;i<Max;i++)total+=golf[i];cout<<total/Max<<" = average score "<<Max<<" rounds"<<endl;return 0;
}

clear( )方法重置输入,如果省略这条语句,程序将拒绝继续读入输入,接下来,程序在while循环中使用cin.get( )来读取行尾之前的所有输入,从而删除这一行中的错误输入。最后程序告诉用户,应输入一个数字

3.
写入到文本文件中

#include<iostream>
#include<fstream>
using namespace std;
int main()
{char automobile[50];int year;double a_price;double b_price;ofstream outFile;outFile.open("carinfo.txt");cout<<"Enter the make and model of automaic: ";cin.getline(automobile,50);cout<<"Enter the model year: ";cin>>year;cout<<"Enter the original asking price: ";cin>>a_price;b_price = 0.913 * a_price;cout<<fixed;cout.precision(2);cout.setf(ios_base::showpoint);cout<<"Make and model: "<<automobile<<endl;cout<<"Year: "<<year<<endl;cout<<"Was asking $ "<<a_price<<endl;cout<<"Now asking & "<<b_price<<endl;outFile<<fixed;outFile.precision(2);outFile<<"Make and model: "<<automobile<<endl;outFile<<"Year: "<<year<<endl;outFile<<"Was asking $ "<<a_price<<endl;outFile<<"Now asking & "<<b_price<<endl;outFile.close();return 0;
}

*必须包含头文件fstream
*头文件fstream定义了一个用于处理输出的ofstream类
*需要声明一个或多个ofstream变量(对象),遵守命名规则
*必须指明空间std;
*需要将ofstream对象和文件联系起来,通过open( )
*使用完文件后,应使用方法close( )将其关闭

ofstream ontFile;
ofstteam fout;
outFile.open("bowling.txt");
char filename[50];
cin>>filename;
fout.open(filename);
double wt;
outFile << wt; //write a number to bowling.txt
char line[81]="sdasdwadw";
fout<<line<<endl;//write a line of text

打开文件用于接受输入可能失败,例如文件已经存在,但禁止对其进行访问

4.

ifstream inFile;
ifstteam fin;
inFile.open("bowling.txt");
char filename[50];
cin>>filename;
fin.open(filename);
double wt;
inFile >> wt; //read a number from bowling.txt
char line[81];
fin.getline(line,81);//read a line of text

如果试图打开一个不存在的文件用于输入,情况会怎样?这种错误会导致后面使用ifstream对象进行输入时失败。检查文件是否被成功打开首先是使用is_open( )

inFile.open("bowlinf.txt");
if(!inFile.is_open())
{exit(EXIT_FAILURE);
}

如果文件被成功打开,方法is_open( )将返回true。exit( )的原型在cstdlib中,用于终结程序

例子:

#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE = 60;
using namespace std;
int main()
{char filename[SIZE];ifstream inFile;cout<<"Enter name of data file: ";cin.getline(filename,SIZE);inFile.open(filename);if(!inFile.is_open()){cout<<"Cloud not open the file "<<filename<<endl;cout<<"Programme terminated."<<endl;exit(EXIT_FAILURE);}double value;double sum = 0.0;int count = 0;inFile >> value;while(inFile.good()){++count;sum+=value;inFile>>value;}if(inFile.eof())cout<<"End of file reached."<<endl;else if(inFile.fail())cout<<"Input terminated by data mismatch."<<endl;else cout<<"Input terminatedn for unknown reason."<<endl;if(count==0)cout<<"No data processed"<<endl;else{cout<<"Items read: "<<count<<endl;cout<<"Sum: "<<sum<<endl;cout<<"Average: "<<sum/count<<endl;}inFile.close();return 0;
}

5.
假设输入如下
Hi!
send 10or 10 or 20 now!
将输出什么?注意输入会被缓冲

#include<iostream>
using namespace std;
int main()
{char ch;int ct1,ct2;ct1=ct2=0;while((ch=cin.get())!='$'){cout<<ch;ct1++;if(ch='$')ct2++;cout<<ch;}cout<<"ct1 = "<<ct1<<", ct2 = "<<ct2<<endl;return 0;
} 

在第二次打印前,每个字符都被转换为字符。另外,表达式ch= ′  字符。另外,表达式ch='’的值是$的字符编码,因此是非0值,所以ct2将被加1

6.
!!x与x不一定相等
自己代入数值

7.

int line = 0;
char ch;
while(cin.get(ch))
{if(ch == 'Q')break;if(ch != '\n')continue;line++;
}
改写成
int line = 0;
char ch;
while(cin.get(ch)&&ch!='Q')
{if(ch == '\n')line++;
}

8.
字母小写变大写,大写变小写

#include <iostream>
#include <cctype>int main()
{using namespace std;char ch;cin.get(ch);while(ch!='@'){if(isdigit(ch))cin.get(ch);else{if(islower(ch))ch=toupper(ch);elsech=tolower(ch);cout<<ch;cin.get(ch);}}return 0;
}   

9.

#include <iostream>
#include<cctype>
int main()
{using namespace std;double sum=0,average=0;double num[10];int i=0,total=0;double temp;while(cin>>temp&&i<10&&!isdigit(temp)){num[i]=temp;sum+=num[i];++i;}if(i!=0)average=sum/i;for(int j=0;j<i;++j)if(num[j]>average)++total;cout<<"这些数字的平均值为"<<average<<endl;cout<<"并且共有"<<total<<"个数字大于平均值。\n";return 0;
} 

10.

#include <iostream>int main()
{using namespace std;cout<<"Please enter one of the following choices:\n"<<"c)carnivore          p)pianist\n"<<"t)tree               g)game\nf\n";    //书上的这个f个人认为是打印错误cout<<"Please enter a c, p, t, or g: ";char ch;cin>>ch;while(ch!='c'&&ch!='p'&&ch!='t'&&ch!='g'){cout<<"Please enter a c, p, t, or g: ";cin>>ch;}switch(ch){case 'c':cout<<"A maple is a carnivore.\n";break;case 'p':cout<<"A maple is a pianist.\n";break;case 't':cout<<"A maple is a tree.\n";break;case 'g':cout<<"A maple is a game.\n";}return 0;
}

11.

#include <iostream>int main()
{using namespace std;cout<<"Please enter one of the following choices:\n"<<"c)carnivore          p)pianist\n"<<"t)tree               g)game\nf\n";    //书上的这个f个人认为是打印错误cout<<"Please enter a c, p, t, or g: ";char ch;cin>>ch;while(ch!='c'&&ch!='p'&&ch!='t'&&ch!='g'){cout<<"Please enter a c, p, t, or g: ";cin>>ch;}switch(ch){case 'c':cout<<"A maple is a carnivore.\n";break;case 'p':cout<<"A maple is a pianist.\n";break;case 't':cout<<"A maple is a tree.\n";break;case 'g':cout<<"A maple is a game.\n";}return 0;
}//ex.6.4
#include <iostream>
const int strsize=20;struct bop{char fullname[strsize];char title[strsize];char bopname[strsize];int preference;
};
int main()
{using namespace std;cout<<"Benevolent Order of Programmers Report\n"<<"a. display by name        b. display by title\n"<<"c. display by bopname     d. diplay by preference\n"<<"q. quit\n";char ch;bop member[5]={{"Wimp Macho","English Teacher","DEMON",0},{"Raki Rhodes","Junior Programmer","BOOM",1},{"Celia Laiter","Super Star","MIPS",2},{"Hoppy Hipman","Analyst Trainee","WATEE",1},{"Pat Hand","Police","LOOPY",2}};cout<<"Enter your choice:";while(cin>>ch&&ch!='q'){switch(ch){case 'a':for(int i=0;i<5;i++)cout<<member[i].fullname<<endl;break;case 'b':for(int i=0;i<5;i++)cout<<member[i].title<<endl;break;case 'c':for(int i=0;i<5;i++)cout<<member[i].bopname<<endl;break;case 'd':for(int i=0;i<5;i++){if(member[i].preference==0)cout<<member[i].fullname<<endl;else if(member[i].preference==1)cout<<member[i].title<<endl;else if(member[i].preference==2)cout<<member[i].bopname<<endl;}break;}cout<<"Next choice: ";}cout<<"Bye!\n";return 0;
}

12.

#include <iostream>
int main()
{using namespace std;double income,revenue;cout<<"请输入你的收入:";while(cin>>income&&income>=0){if(income<=5000)revenue=0.0;else if(income<=15000)revenue=0.1*(income-5000);else if(income<=35000)revenue=0.1*(15000-5000)+0.15*(income-15000);elserevenue=0.1*(15000-5000)+0.15*(35000-15000)+0.2*(income-35000);cout<<"你的所得税为"<<revenue<<endl;cout<<"请输入你的收入:";}return 0;
}

13.

#include <iostream>
#include <string>
using namespace std;
struct patron{string name;double money;
};int main()
{int num,temp=0;cout<<"请输入捐款的人数:";cin>>num;cin.get();patron *ps=new patron[num];for(int i=0;i<num;++i){cout<<"请输入第"<<i+1<<"位捐款人的名字:";getline(cin,ps[i].name);cout<<"请输入第"<<i+1<<"位捐款人捐款的数目:";cin>>ps[i].money;cin.get();}cout<<"Grand Patrons:\n";for(int i=0;i<num;++i)if(ps[i].money>10000){cout<<ps[i].name<<"\n"<<ps[i].money<<endl;++temp;}if(temp==0)cout<<"none\n";cout<<"Patrons:\n";for(int i=0;i<num;++i)if(ps[i].money<=10000){cout<<ps[i].name<<"\n"<<ps[i].money<<endl;++temp;}if(temp==0)cout<<"none\n";delete [] ps;        return 0;
} 

14.

#include <iostream>
#include <cctype>int main()
{using namespace std;int vowel=0,consonant=0,other=0;char word[15];cout<<"Enter words (q to quit):\n";while(cin>>word){if(isalpha(word[0])){if(word[0]=='q'&&strlen(word)==1)break;else if(word[0]=='a'||word[0]=='i'||word[0]=='u'||word[0]=='e'||word[0]=='o')++vowel;else++consonant;}else++other;}cout<<vowel<<" words beginning with vowels\n";cout<<consonant<<" words beginning with consonants\n";cout<<other<<" others\n";return 0;
}

15.
计算文件字符

#include <iostream>
#include <fstream>
#include <cstdlib>int main()
{using namespace std;char ch;int sum=0;ifstream inFile;inFile.open("abc.txt");if(!inFile.is_open()){cout<<"Could not open the file \n";cout<<"Program terminating.\n";exit(EXIT_FAILURE);}inFile>>ch;while(inFile.good()){++sum;inFile>>ch;}if(inFile.eof())cout<<"End of file reached.\n";else if(inFile.fail())cout<<"Input terminated by data mismatch.\n";elsecout<<"Input terminated for unkonwn reason.\n";cout<<"总共有"<<sum<<"个字符在这个文件中。"<<endl;return 0;
}

16.

#include <iostream>
#include <fstream>
#include <cstdlib>struct member
{char name[20];double donation;
};int main()
{using namespace std;int num,count1=0,count2=0;ifstream fin;char file[20];cout<<"Enter name of data file: ";cin.getline(file,20);fin.open(file);if(!fin.is_open()){cout<<"Could not open the file-"<<file<<endl;cout<<"Program terminating.\n";exit(EXIT_FAILURE);}fin>>num;fin.get();member *pd=new member[num];for(int i=0;i<num;i++){fin.getline(pd[i].name,20);fin>>pd[i].donation;fin.get();}cout<<"Grand Patrons:\n";for(int i=0;i<num;i++)if(pd[i].donation>=10000){cout<<pd[i].name<<"\n"<<pd[i].donation<<endl;count1++;}if(count1==0)cout<<"none\n";cout<<"Patrons:\n";for(int i=0;i<num;i++)if(pd[i].donation<10000){cout<<pd[i].name<<"\n"<<pd[i].donation<<endl;count2++;}if(count2==0)cout<<"none\n";delete [] pd;return 0;
}

C++ Primer Plus(第六版)--学习杂记(第六章)相关推荐

  1. PMBOK(第六版) 学习笔记 ——《第一章 引论》

    系列文章目录 PMBOK(第六版) 学习笔记 --<第一章 引论> PMBOK(第六版) 学习笔记 --<第二章 项目运行环境> PMBOK(第六版) 学习笔记 --<第 ...

  2. C++ Primer Plus(第六版)--学习杂记(第四章)

    1. 如果将sizeof运算符用于数组名,得到的将是整个数组中的字节数.但如果将sizeof用于数组元素,则得到的将是元素的程度(单位是字节).表明yams是一个数组,而yams[1]只是一个int变 ...

  3. 人大版统计学教材第六版学习笔记--第3章 数据的图表展示

    合理使用图表描述统计结果是应用统计的基本技能之一. 文章目录 数据的预处理 数据审核 数据筛选 数据排序 数据透视表 定性数据的整理与展示 分类数据的整理与图示 数据的整理与计算 分类数据的图示 顺序 ...

  4. PMBOK(第六版) 学习笔记 ——《第七章 项目成本管理》

    系列文章目录 PMBOK(第六版) 学习笔记 --<第一章 引论> PMBOK(第六版) 学习笔记 --<第二章 项目运行环境> PMBOK(第六版) 学习笔记 --<第 ...

  5. PMBOK(第六版) 学习笔记 ——《考试、成绩、PDU》

    系列文章目录 PMBOK(第六版) 学习笔记 --<第一章 引论> PMBOK(第六版) 学习笔记 --<第二章 项目运行环境> PMBOK(第六版) 学习笔记 --<第 ...

  6. PMBOK(第六版) 学习笔记 ——《第六章 项目进度管理》

    系列文章目录 PMBOK(第六版) 学习笔记 --<第一章 引论> PMBOK(第六版) 学习笔记 --<第二章 项目运行环境> PMBOK(第六版) 学习笔记 --<第 ...

  7. PMBOK(第六版) 学习笔记 ——《第十章 项目沟通管理》

    系列文章目录 PMBOK(第六版) 学习笔记 --<第一章 引论> PMBOK(第六版) 学习笔记 --<第二章 项目运行环境> PMBOK(第六版) 学习笔记 --<第 ...

  8. C++ primer第五版学习记录:第二章变量和基本类型

    C++ primer第五版学习记录:第二章变量和基本类型 断断续续把第二章看完用了大概两周时间,发扬一下之前默写的传统,顺便让自己梳理一下该章节的具体框架及结构.可能会有理解错误的地方,欢迎大家批评指 ...

  9. C++ Primer Plus(第六版)第十六章课后习题

    C++ Primer Plus(第六版)第十六章课后习题 16.10.1 #include <iostream> #include <string> using namespa ...

最新文章

  1. Web开发人员必读的12个网站
  2. Maven教程(3)--Maven导入工程常见问题(编码、MavenArchiver、Lifecycle Mapping、maven install 没有反应)...
  3. C++基础算法学习——完美立方
  4. 转-iOS开发系列--地图与定位
  5. mysql 自动归档,如何将数据库从非归档模式转为自动归档模式:
  6. php网站需要装zend吗,php-zend网站也需要别名
  7. js-比较两个日期的大小
  8. binder,hwbinder,vndbinder之间的关系
  9. shell 多个引号冲突_Html多个引号重叠使用冲突解决办法
  10. Docker 存储 网络
  11. 微信昵称上标电话号码,实用的新玩法
  12. Linux下make -j加快编译速度
  13. 【京东】scrapy爬虫抓取京东图书详情、评论
  14. 一文带你由浅入深Netty异步非阻塞世界
  15. 使用FFmpeg和Intel显卡视频转码——10张DVD光盘压缩成8小时4G的MP4
  16. smtp协议支持身份认证与不认证两种状态。
  17. 解决酷比魔方iwork手写板系列TF卡以及USB3.0不识别的问题
  18. HC32L130基于Xmodem协议实现IAP串口在线升级
  19. 位置在此计算机上运行程序灰色,Win10电脑中定位服务按钮灰色无法开启的2种解决方法...
  20. pacemaker和keepalived的区别

热门文章

  1. spring开发_邮箱注册_激活_获取验证码
  2. HDU 2448 Mining Station on the Sea 最短路+KM
  3. CentOS7 从零安装NVIDA、CUDA、cuDNN
  4. 个人博客站点添加谷歌联盟Google Adsense
  5. 二叉树的叶子结点按从左到右的顺序连成一个单链表
  6. 极光魔链(JMLink)使用教程
  7. Flash安全沙箱调研
  8. .com 域名三十年回顾:从 1 到 1 亿个,一部互联网的变迁史
  9. python读取oracle数据转换成json文件_python 读取网页json数据库中
  10. 3D游戏建模制作流程介绍,这么复杂繁琐,小白劝退警告