以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆。仅供参考,DEV C++已通过编译运行
。
练习1
#include<iostream>
#include<cctype>
int main()
{
using namespace std;
char ch;
cout << "Type some sentences(@ to quit):" << endl;
cin.get(ch);
while (ch != '@')
{
if (isdigit(ch));//除外数字,倘若保留只需给下方套一个else
else if (isalpha(ch))//处理非数字非字母字符
{
if (isupper(ch))
cout << char(tolower(ch));//必须要用char()强制转换,否则会输出对应的整型
else
cout << char(toupper(ch));
}
else
cout << ch;
cin.get(ch);
}
return 0;
}
练习2
#include<iostream>
#include<array>
const int ArSize = 10;
int main()
{
using namespace std;
array<double, ArSize>donation;
int count = 0;//实际数组数量计数器
int num = 0;//大于均值计数器
cout << "Enter double_type number into an array: " << endl;
cout << "donation[1] : ";
while (count < ArSize && cin >> donation[count])//cin>>将正确读入目标类型值,因此在输入非数字的情况下会返回false
{
++count;
if( count < ArSize)
cout << "donation[" << count + 1 << "] : ";
}
double sum, average;
sum = 0;
for (int cnt = 0; cnt < count; ++cnt)//计算平均值,count实际值可能会小于ArSize。
{
sum += donation[cnt];
}
average = sum / count;
for (int cnt = 0; cnt < count; ++cnt)//找出大于均值的数
{
if (donation[cnt] > average)
++num;
}
cout << "There are " << count << " numbers in donation_array" << endl;
cout << "The average of donation_array is " << average << endl;
cout << "There are " << num << " number over average." << endl;
return 0;
}
练习3
#include<iostream>
int main()
{
using namespace std;
cout << "Please enter one of thr following choices: " << endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" << endl;
char ch;
do
{
cin >> ch;
switch (ch)
{
case 'c':cout << "carnivore";
break;
case 'p':cout << "pianist";
break;
case 't':cout << "A map is a tree";
break;
case 'g':cout << "game";
break;
default: cout << "Please enter a c,p,t or g:";
}
} while ((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g'));
return 0;
}
练习4
#include<iostream>
const int strsize = 30;
const int member = 5;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main()
{
using namespace std;
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a.display by name b.display by title" << endl;
cout << "c.display by bopname d.display by preference" << endl;
cout << "q.quit" << endl;
bop Bop[member] =
{
{"Wimp Macho","Wm","WMWM",0},
{"Raki Rhodes","Junior Programmer","RRRR",1},
{"Celia Laiter","Senior Programmer","MIPS",2},
{"Hoppy Hipman","Analyst Trainee","HHHH",1},
{"Pat hand","Student","LOOPY",2}
};
char ch;
int cnt;
cout << "Enter your choice: ";
do
{
cin >> ch;
switch (ch)
{
case 'a':
for (cnt = 0; cnt < member; ++cnt)
cout << Bop[cnt].fullname << endl;
break;
case 'b':
for (cnt = 0; cnt < member; ++cnt)
cout << Bop[cnt].title << endl;
break;
case 'c':
for (cnt = 0; cnt < member; ++cnt)
cout << Bop[cnt].bopname << endl;
break;
case 'd':
for (cnt = 0; cnt < member; ++cnt)
{
if (Bop[cnt].preference == 0)
cout << Bop[cnt].fullname << endl;
else if (Bop[cnt].preference == 1)
cout << Bop[cnt].title << endl;
else
cout << Bop[cnt].bopname << endl;
}
break;
}
if (ch != 'q')
cout << "Next choice: ";
else
cout << "Bye!" << endl;
} while (ch != 'q');
return 0;
}
练习5
#include<iostream>
const double tax1 = 0.1;
const double tax2 = 0.15;
const double tax3 = 0.2;
int main()
{
using namespace std;
double income, tax;
cout << "Please input your income: ";
while (cin >> income && income >= 0)
{
if (income > 35000)
tax = (income - 35000) * tax3 + 20000 * tax2 + 10000 * tax1;
else if (income > 15000)
tax = (income - 15000) * tax2 + 10000 * tax1;
else if (income > 5000)
tax = (income - 5000) * tax1;
else
tax = 0;
cout << "Your personal tax is : " << tax << endl;
cout << "Please input your income: ";
}
return 0;
}
练习6
#include<iostream>
#include<string>
using namespace std;
const double max = 10000;
struct Donation
{
string name;
double money;
};
int main()
{
int num, gpnum, pnum;
gpnum = pnum = 0;
cout << "Enter the number of donation: ";
cin >> num;
cin.get();
Donation* donation = new Donation[num];
for (int cnt = 0; cnt < num; ++cnt)
{
cout << "Enter the name: ";
getline(cin, donation[cnt].name);
cout << "Enter the money: ";
cin >> donation[cnt].money;
cin.get();
}
cout << "Input complete!" << endl;
cout << endl;
cout << "Grand Patrons:" << endl;
for (int cnt = 0; cnt < num; ++cnt)
{
if (donation[cnt].money >= max)
{
cout << donation[cnt].name << " ";
cout << donation[cnt].money << endl;
++gpnum;
}
}
if (gpnum == 0)
cout << "none." << endl;
cout << "Patrons: " << endl;;
for (int cnt = 0; cnt < num; ++cnt)
{
if (donation[cnt].money < max)
{
cout << donation[cnt].name << " ";
cout << donation[cnt].money << endl;
++pnum;
}
}
if (pnum == 0)
cout << "none." << endl;
delete[]donation;
return 0;
}
练习7
#include<iostream>
#include<cctype>
#include<cstring>//string比较好一些
const int ArSize = 30;
int main()
{
using namespace std;
cout << "Enter words(q to quit):" << endl;
char word[ArSize];
int vowels, consonants, others;
vowels = consonants = others = 0;
cin >> word;
while (strcmp(word,"q"))//C语言形式比较,两个都为字符串,string对象则只需要逻辑运算
{
if (isalpha(word[0]))
{
switch (word[0])
{
case 'a':
case 'A': ++vowels;
break;
case 'e':
case 'E':++vowels;
break;
case 'i':
case 'I':++vowels;
break;
case 'o':
case 'O':++vowels;
break;
case 'u':
case 'U':++vowels;
break;
default:++consonants;
break;
}
}
else
++others;
cin >> word;//切勿忘记在没有接收到q指令循环还在继续,需要一个继续键入的过程
}
cout << vowels << " words beginning with vowels" << endl;
cout << consonants << " words beginning with consonants" << endl;
cout << others << " others" << endl;
return 0;
}
练习8
#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE = 30;
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;//TXT文件需与cpp文件在同一个根目录下
cout << "Enter name of file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Couldn't open the file " << filename << endl;
cout << "Program terminating." << endl;
exit(EXIT_FAILURE);
}
char ch;
int count = 0;
inFile >> ch;//除去空格
//inFile.get(ch);//包括空格
while (inFile.good())
{
++count;
cout << ch;
inFile >> ch;
//inFile.get(ch);
}
if (inFile.eof())
cout << "End of file reached." << endl;
else if (inFile.fail())
cout << "Input termainated by data mismatch." << endl;
else
cout << "Input termainated by unknowed error." << endl;
if (count == 0)
cout << "No data processed." << endl;
else
cout << count << " chars in this txt." << endl;
inFile.close();
return 0;
}
练习9
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
const int SIZE = 30;
const double max = 10000;
struct Donation
{
string name;
double money;
};
int main()
{
char filename[SIZE];
ifstream inFile;//TXT文件需与cpp文件在同一个根目录下
cout << "Enter name of file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Couldn't open the file " << filename << endl;
cout << "Program terminating." << endl;
exit(EXIT_FAILURE);
}
if (inFile.eof())
cout << "End of file reached." << endl;
else if (inFile.fail())
cout << "Input termainated by data mismatch." << endl;
else
cout << "Input termainated by unknowed error." << endl;
int num,gpnum, pnum;
gpnum = pnum = 0;
inFile >> num;
Donation* donation = new Donation[num];
for (int cnt = 0; cnt < num; ++cnt)
{
//inFile.ignore();
inFile.get();
getline(inFile, donation[cnt].name);
inFile >> donation[cnt].money;
}
cout << "Input complete!" << endl;
cout << endl;
cout << "Grand Patrons:" << endl;
for (int cnt = 0; cnt < num; ++cnt)
{
if (donation[cnt].money >= max)
{
cout << donation[cnt].name << " ";
cout << donation[cnt].money << endl;
++gpnum;
}
}
if (gpnum == 0)
cout << "none." << endl;
cout << "Patrons: " << endl;;
for (int cnt = 0; cnt < num; ++cnt)
{
if (donation[cnt].money < max)
{
cout << donation[cnt].name << " ";
cout << donation[cnt].money << endl;
++pnum;
}
}
if (pnum == 0)
cout << "none." << endl;
delete[]donation;
inFile.close();
return 0;
}