1.读取键盘输入
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
char ch=0;
cout<<"Please enter:";
while((ch=cin.get())!='@')
{
if(isdigit(ch))
{
continue;
}
else if (islower(ch))
{
cout<<(char)toupper(ch);
}
else if (isupper(ch))
{
cout << (char)tolower(ch)<<endl;;
}
}
cout << "Ending" << endl;
return 0;
}
2.存十个数值
#include "stdafx.h"
#include<iostream>
#include<string>
#include<array>
using namespace std;
const unsigned int SIZE=10;
int main()
{
array<double,SIZE> donation;
int enter=0;
double total_value=0.0;
double avg=0.0;
int over_avg=0;
cout<<"Please enter ten doulbe value(Non-numeric to exit):";
while(enter<10&&(cin>>donation[enter]))
{
total_value+=donation[enter];
enter++;
}
avg=total_value/enter;
for(int i=0;i<enter;i++)
{
if(donation[i]>avg)
{
over_avg++;
}
}
cout<<"The average value is "<<avg<<",and there are "<<over_avg
<<" double value more than average !"<<endl;
return 0;
}
3.菜单驱动程序
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
char ch=0;
cout<<"Please enter one of the following choices:\n"
"c) carnivore p)pianist \n"
"t) tree g)game\n";
while(cin>>ch)
{
switch(ch)
{
case 'c':
cout<<"carnivore"<<endl;
break;
case 'p':
cout << "pianist" <<

本文提供了C++ Primer Plus第六版中第六章的编程练习解答,包括键盘输入处理、存储数值、菜单驱动程序实现、运算符重载、税务计算、捐助计算、单词读取、文件字符读取及文件信息读取等实战练习。
最低0.47元/天 解锁文章
506

被折叠的 条评论
为什么被折叠?



