《C++ Primer Plus》第六章编程题自答答案VS运行通过.docx
- 文档编号:24903755
- 上传时间:2023-06-02
- 格式:DOCX
- 页数:22
- 大小:20.09KB
《C++ Primer Plus》第六章编程题自答答案VS运行通过.docx
《《C++ Primer Plus》第六章编程题自答答案VS运行通过.docx》由会员分享,可在线阅读,更多相关《《C++ Primer Plus》第六章编程题自答答案VS运行通过.docx(22页珍藏版)》请在冰豆网上搜索。
《C++PrimerPlus》第六章编程题自答答案VS运行通过
#include"stdafx.h"
#include
#include
usingnamespacestd;
intmain()
{
#pragmaregion第一题
/*第一题
编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),
同时将大写字符转换为小写,将小写转换为大写(别忘了cctype函数系列)。
*/
charch;
while((ch=cin.get())!
='@')
{
if(isdigit(ch))
{
}
elseif(isupper(ch))
{
ch=tolower(ch);
cout< } elseif(islower(ch)) { ch=toupper(ch); cout< } else { cout< } } #pragmaendregion cin.get(); cin.get(); return0; } #include"stdafx.h" #include #include #include #include #include usingnamespacestd; intmain() { #pragmaregion第二题 /*第二题 编写一个人程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类array)。 程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字不大于平均值。 */ array doublenum=0; intcount=0,i=0; for(;i<10;i++) { stringstr; doublech; cin>>str; if(atof(const_cast { ch=atof(const_cast donation[i]=ch; num+=ch; cin.get(); } else { break; } } num/=i; for(intj=0;j { if(donation[j] { count++; } } cout<<"平均值: "< "< "; #pragmaendregion cin.get(); cin.get(); return0; } #include"stdafx.h" #include usingnamespacestd; intmain() { #pragmaregion第三题 /*第三题 编写一个菜单驱动程序的雏形。 该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。 如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。 然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。 该程序的运行情况如下: Pleaseenteroneofthefollowingchoices: c)carnivorep)pianist t)treeg)game f Pleaseenterac,p,t,org: q Pleaseenterac,p,t,org: t Amapleisatree.*/ charch; printf("Pleaseenteroneofthefollowingchoices: \n" "c)carnivorep)pianist\n" "t)treeg)game\n"); cin>>ch; while(ch) { switch(ch) { case'c': printf("Amapleisacarnivore."); break; case'p': printf("Amapleisapianist."); break; case't': printf("Amapleisatree."); break; case'g': printf("Amapleisagame."); break; default: printf("Pleaseenterac,p,t,org: "); cin>>ch; } if(ch=='c'||ch=='p'||ch=='t'||ch=='g') { break; } } #pragmaendregion cin.get(); cin.get(); return0; } #include"stdafx.h" #include usingnamespacestd; #pragmaregion第四题 constintstrsize=20; voidName(); voidTitle(); voidBopname(); voidPreference(); //structbop; structbop{ charfullname[strsize];//realname chartitle[strsize];//jobtitle charbopname[strsize];//secretBOPname intpreference;//0=fullname,1=title,2=bopname }member[5]{ {"WimpMacho","WimpMacho1","SB1",0}, {"RakiRhodes","JuniorProgrammer","SB2",1}, {"CeliaLaiter","MIPS","SB3",2}, {"HoppyHipman","AnalystTrainee","SB4",0}, {"PatHand","LOOPY","SB5",1} }; #pragmaendregion intmain() { #pragmaregion第四题 /*第四题 加入BenevolentOrderofProgrammer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。 请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。 编写该程序时,请使用下面的结构: //BenevolentOrderofProgrammersnamestructure structbop{ charfullname[strsize];//realname chartitle[strsize];//jobtitle charbopname[strsize];//secretBOPname intpreference;//0=fullname,1=title,2=bopname }; 该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。 另外,该程序使用一个循环,让用户在下面的选项中进行选择: a.displaybynameb.displaybytitle c.displaybybopnamed.displaybypreference q.quit 注意,“displaybypreference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。 例如,如果偏好号为1,则选择d将显示程序员的头衔。 该程序的运行情况如下: BenevolentOrderofProgrammersReport a.displaybynameb.displaybytitle c.displaybybopnamed.displaybypreference q.quit Enteryourchoice: a WimpMacho RakiRhodes CeliaLaiter HoppyHipman PatHand Nextchoice: d WimpMacho JuniorProgrammer MIPS AnalystTrainee LOOPY Nextchoice: q Bye! */ charchoice; printf("BenevolentOrderofProgrammersReport\na.displaybynameb.displaybytitle\nc.displaybybopnamed.displaybypreference\nq.quit\nEnteryourchoice: "); while(cin>>choice) { switch(choice) { case'a': Name(); break; case'b': Title(); break; case'c': Bopname(); break; case'd': Preference(); break; case'q': choice='q'; break; default: if(choice! ='a'||choice! ='b'||choice! ='c'||choice! ='d'||choice! ='q') printf("Pleaseentera,b,c,dorq: "); } if(choice=='q') { printf("Bye! ~"); break; } if(choice=='a'||choice=='b'||choice=='c'||choice=='d') printf("Nextchoice: "); } #pragmaendregion cin.get(); cin.get(); return0; } #pragmaregion第四题 voidName() { for(inti=0;i<5;i++) { cout< } } voidTitle() { for(inti=0;i<5;i++) { cout< } } voidBopname() { for(inti=0;i<5;i++) { cout< } } voidPreference() { for(inti=0;i<5;i++) { switch(member[i].preference) { case0: cout< break; case1: cout< break; case2: cout< break; } } } #pragmaendregion #include"stdafx.h" #include usingnamespacestd; intmain() { #pragmaregion第五题 /*第五题 在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下: 5000tvarps: 不收税 5001~15000tvarps: 10% 15001~35000tvarps: 15% 35000tvarps以上: 20% 例如,收入为38000tvarps时,所得税为5000*0.00+10000*0.10+20000*0.15+3000*0.20,即4600tvarps。 请编写 一个程序,使用循环来要求用户输入收入,并报告所得税。 当用户输入负数或非数字时,循环将结束。 */ constdoubleCess_0=0.0; constdoubleCess_10=0.1; constdoubleCess_15=0.15; constdoubleCess_20=0.2; printf("请输入你的工资: "); intnumber; doublenum=0.0; while(cin>>number&&number>=0) { if(number<=5000) { printf("所得税为: "); cout< } elseif(number>=5001&&number<=15000) { num=((number-5000)*Cess_10); printf("所得税为: "); cout< } elseif(number>=15001&&number<=35000) { num=(10000*Cess_10+(number-15000)*Cess_15); printf("所得税为: "); cout< } else { num=(10000*Cess_10+20000*Cess_15+(number-35000)*Cess_20); printf("所得税为: "); cout< } printf("\n请输入你的工资: "); } #pragmaendregion cin.get(); cin.get(); return0; } #include"stdafx.h" #include #include usingnamespacestd; intmain() { #pragmaregion第六题 /*第六题 编写一个程序,记录捐助给“维护合法权利团体”的资金。 该程序要求用户输入捐献者数目,然后要求用户输入 每一个捐献者的姓名和款项。 这些信息被储存在一个动态分配的结构数组中。 每个结构有两个成员: 用来储存姓名的字符数组(或string对象)和用来储存款项的double成员。 读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。 该列表前应包含一个标题,指出下面的捐款者是重要捐款人(GrandPatrons)。 然后,程序将列出其他的捐款者,该列表要以Patrons开头。 如果某种类别没有捐款者,则程序将打印单词“none”。 该程序只显示这两种类别,而不进行排序。 */ structjuanxian{ stringName; doublemoney; }; printf("请输入捐献者数目: "); intnum; cin>>num; intcountMax=0; intcountMin=0; juanxian*juan=newjuanxian[num]; for(inti=0;i { printf("请输入第%d个捐献者姓名: ",i+1); cin>>juan[i].Name; printf("请输入第%d个捐献者捐献数目: ",i+1); cin>>juan[i].money; } intflagMax=0;//标题标志 for(inti=0;i { if(juan[i].money>10000) { if(flagMax==0) { printf("\nGrandPatrons\n"); flagMax=1; } ++countMax; cout< } } if(countMax==0){ printf("\nGrandPatrons\n\nnone\n"); } intflagMin=0;//标题标志 for(inti=0;i { if(juan[i].money<=10000) { if(flagMin==0) { printf("\nPatrons\n"); flagMin=1; } ++countMin; cout< } } if(countMin==0){ printf("\nPatrons\n\nnone\n"); } #pragmaendregion cin.get(); cin.get(); return0; } #include"stdafx.h" #include #include usingnamespacestd; intmain() { #pragmaregion第七题 /*第七题 编写一个程序,它每次读取一个单词,直到用户只输入q。 然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。 为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。 该程序的运行情况如下: Enterwords(qtoquit): The12awesomeoxenambled quietlyacross15metersoflawn.q 5wordsbeginningwithvowels 4wordsbeginningwithconsonants 2others*/ stringch; intnumAlpha1=0,numAlpha2=0,numNum=0; printf("Enterwords(qtoquit): "); while(cin>>ch) { if(isalpha(ch[0])) { if(ch[0]=='q'&&ch[1]==0) { break; } elseif(ch[0]=='a'||ch[0]=='A'||ch[0]=='i'||ch[0]=='I'||ch[0]=='e'||ch[0]=='E'||ch[0]=='o'||ch[0]=='O'||ch[0]=='u'||ch[0]=='U'){ ++numAlpha1; } else ++numAlpha2; } else{ ++numNum; } } printf("元音开头单词%d个\n辅音开头单词%d个\n其它字符%d个",numAlpha1,numAlpha2,numNum); #pragmaendregion cin.get(); cin.get(); return0; } #include"stdafx.h" #include #include usingnamespacestd; intmain() { #pragmaregion第八题 /*第八题 编写一个程序,它打开一个文件,逐个字符的读取该文件,直到到达文件尾,然后指出该文件中包含多少个字符。 */ charch; intcount=0; ifstreaminFile; inFile.open("123456.txt");//文件在工程目录下 while(inFile>>ch) { ++count; } cout< inFile.close(); #pragmaendregion cin.get(); cin.get(); return0; } #include"stdafx.h" #include #include #include usingnamespacestd; intmain() { #pragmaregion第九题 /*第九题 完成编程练习6,但从文件中读取所需的信息。 该文件的第一项应为捐款人数,余下的内容应为成对的行。 在每一对中,第一行为捐款人姓名,第二行为捐款数额。 即该文件类似于下面: 4 SamStone 2000
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ Primer Plus C+ Primer Plus第六章编程题自答答案VS运行通过 C+ Plus 第六 编程 答案 VS 运行 通过