6.1
#include<stdafx.h>
#include<iostream>
#include<cctype>
int main()
{
using namespace std;
char ch;
cout << "enter characters<end in @>:";
while ((ch=cin.get())!='@')//测试条件不能为(cin>>ch)!='@',cin返回值为cin对象、或者在需要bool类型的地方返回bool值;带参数的cin.get(ch)函数返回值也为流对象;只有不带参数的cin.get()函数返回值为int型
{
if (isdigit(ch))
continue;
else
{
cout << "the charicter is: " << ch<<endl;
if (islower(ch))
cout << "It's upper case is " << char(toupper(ch)) << endl;//将toupper返回值强制转换为char用于输出该字符
else if (isupper(ch))
cout << "It's lower case is: " << char(tolower(ch))<< endl;
else
continue;
}
}
return 0;
}
6.2
#include<stdafx.h>
#include<iostream>
const int SIZE = 10;
int main()
{
using namespace std;
double donation[SIZE];
double sum = 0;
double aver;
cout << "enter numbers<at most ten and end with a non-number>: " << endl;
int count = 0;
while (count<10&&(cin>>donation[count]))
{
sum += donation[count];
++count;
}
aver = sum / count;
cout << "you total enter " << count << "numbers.\n"
<< "the average is: " << aver;
return 0;
}
6.3
#include<stdafx.h>
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "Please enter one of the following choices:\n"
<< "c) carnivore P) pianist\n"
<< "t) tress g) game\n";
bool wrong = false;//用于控制循环,当输入不正确的重新输入并判断
while (cin>>ch&&!wrong)//当输入正确才会结束循环
{
switch (ch)
{
case 'c':
case'C ':
cout << "A maple is a carnivore.";
wrong = true;//输入正确控制while循环结束
break;//switch开关语句不加break会按顺序执行每条语句,因此在每种情况有多条语句时也不用加大括号
case 'p':
case'P':
cout << "A maple is a pianist.";
wrong = true;
break;
case't':
case'T':
cout << "A maple is a tress.";
wrong = true;
break;
case'g':
case'G':
cout << "A maple is a game.";
wrong = true;
break;
default:
cout << "Please enter a c, p, t, or g: ";
wrong = false;//输入错误再次执行while语句
break;
}
}
return 0;
}
6.4
#include<stdafx.h>
#include<iostream>
#include<iomanip>
const int strsize = 20;
const int number = 5;//存放人员数
using namespace std;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int perference;
};
int main()
{
bop list[number] =
{
{"Wimp Macho","Senior programmer","WM",0},
{"Raki Rhodes","Junior programmer","RR",1},
{"Celia Laiter","Analyst ","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","HHi",1},
{"Pat Hand","Juior programmer","LOOPY",2}
};
char ch;
cout << "Benevolent Order of Programmers Report\n";
cout.flags(ios::left);//设置输出左对齐(默认为右对齐)
cout << setw(25) << "a.display by name" << "b.display by title\n";//sew()用于设置其后的每个输出所占的字节宽度,仅对紧跟其后的一个输出有效,这里只对a选项有效占25字节
cout<<setw(25) << "c.display by bopname" << "d.display by preference\n";
cout << "q.quite\n";
cout << "enter your choice: ";
cin >> ch;
while (ch!='q')
{
if (ch == 'a')
for (int i = 0; i < number; i++)
{
cout << list[i].fullname << endl;
}
else if (ch == 'b')
for (int i = 0; i < number; i++)
{
cout << list[i].title << endl;
}
else if (ch == 'c')
for (int i = 0; i < number; i++)
{
cout << list[i].bopname;
}
else if (ch == 'd')
{
for (int i = 0; i < number; i++)
{
switch (list[i].perference)
{
case 0:cout << list[i].fullname << endl; break;
case 1:cout << list[i].title << endl; break;
case 2:cout << list[i].bopname << endl; break;
}
}
}
else
{
cout << "Please enter a,b,c,d or q: ";
cin >> ch;
break;
}
cout << "next choice: ";
cin >> ch;
}
cout << "Bye";
return 0;
}
6.5
#include<stdafx.h>
#include<iostream>
#include<iomanip>
const double tax_rate1 = 0.1;
const double tax_rate2 = 0.15;
const double tax_rate3 = 0.2;
using namespace std;
int main()
{
int income;
double tax ;
cout << "enter your income: ";
while (cin>>income&&income>0)//当输入补位数字时cin>>income返回false
{
if (income <= 5000)
tax = 0;
else if (income > 5000 && income <= 15000)
tax = (income - 5000)*tax_rate1;
else if (income > 1500 && income <= 3500)
tax = 1000 * tax_rate1 + (income - 1500)*tax_rate2;
else
tax = 1000 * tax_rate1 + 2000 * tax_rate2 + (income - 3500)*tax_rate3;
cout << "your income tax is: " << tax<<endl;
cout << "enter your income: ";
}
return 0;
}
6.6
#include<stdafx.h>
#include<iostream>
#include<string>
using namespace std;//定义名称空间要在结构体之前否则不能识别string,系统将不会将其视为结构体的成员之一
struct donar
{
string name;
double money;
};
int main()
{
int number;
int G_patr=0;
int patr=0;
cout << "Please enter the naumber of donars:";
cin >> number;
donar *P = new donar[number];
cout << "Please enter the message of each donar.\n";
for (int i = 0; i < number; i++)
{
cout << "name #"<<i+1<<": ";
cin.get();//前面输入过数字,换行符还存在缓冲区中,不将其吸收将影响下面输入字符
getline(cin,(P+i)->name);//cin不能输入多个单词
cout << "money #" << i + 1 << ": ";
cin >> (P + i)->money;
}
cout << "Grand Patrons:\n";
for (int i = 0; i < number; i++)
{
if ((P+i)->money>10000)
{
++G_patr;
cout << (P + i)->name << endl;
}
}
if (!G_patr)
cout << "none\n";
cout << "Patrions:\n";
for (int i = 0; i < number; i++)
{
if ((P + i)->money <10000)
{
++patr;
cout << (P + i)->name << endl;
}
}
if (!patr)
cout << "none\n";
return 0;
}
6.7
#include<stdafx.h>
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word;
int vowel = 0;
int cons = 0;
int others = 0;
cout << "enter words<end in q>: \n";
cin >> word;
while (word!="q")//不能直接用word!=‘q’为测试条件,word时string类类似于字符串,而‘q’是字符;
{
if (isalpha(word[0]))//cctype函数参数为字符而不是字符串不能用word
{
if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
++vowel;
else
++cons;
}
else
++others;
cin >> word;//不加此条语句word的值将一直是输入的第一个单词,其他的单词将留在缓冲队列里面,且输入换行符以后将不能再输入
}
cout << vowel << " words beginning with vowels\n"
<< cons << " words beginning with consonants\n"
<< others << " others";
return 0;
}
6.8
/*首先需要在可执行文件所在的文件夹中建立一个名为scores的文本文件,
并输入数据*/
#include<stdafx.h>
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
double number;
int count = 0;
ifstream inFile;
inFile.open("scores.txt");
if (!inFile.is_open())
{
cout << "can't opent the file!\n";
exit(EXIT_FAILURE);
}
inFile >> number;
while (inFile.good())
{
++count;
inFile >> number;
}
if (inFile.eof())
cout << "Reach the end of file.\n";
else if (inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated for unknown reason.\n";
cout << "Iteams read " << count;
return 0;
}
6.9
#include<stdafx.h>
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
struct donar
{
string name;
double money;
};
int main()
{
double number;
int G_patr= 0;
int patr = 0;
ifstream inFile;
inFile.open("mesage.txt");
if (!inFile.is_open())
{
cout << "can't opent the file!\n";
exit(EXIT_FAILURE);
}
inFile >> number;
donar *p = new donar[number];
for (int i = 0; i < number&&inFile.good(); i++)
{
inFile.get();//数字和字符的混合输入,文件中数字和字符中间有换行符,不将其吸收将不能正确输入名字
getline(inFile,(p+i)->name);//名字由多个单词组成,不能直接用inFile>>只能将一次单词读入,原理同cin
inFile >> (p+i)->money;
}
if (inFile.eof())//检查是否输入完成全部数据
cout << "Reach the end of file.\n";
else if (inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated for unknown reason.\n";
cout << "Grand Patrons:\n";
for (int i = 0; i < number; i++)
{
if ((p + i)->money > 10000)
{
++G_patr;
cout << (p + i)->name<<endl;
}
}
if (G_patr == 0)
cout << "none\n";
cout << "Patrons:\n";
for (int i = 0; i < number; i++)
{
if ((p + i)->money <=10000)
{
++patr;
cout << (p + i)->name << endl;
}
}
if (patr == 0)
cout << "none\n";
return 0;
}