课后习题
1.编写以一个程序,读取键盘输入,直到遇到@符号位置,并显示输入(数字除外),同时将大写字母转换成小写字母,雄安写字母转换成大写字母
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
char ch;
char temp;
string str;
cout << "enter your string :";
cin.get(ch);
while (ch != '@')
{
//进一步判断是不是字母
if (isalpha(ch))
{
//进一步判断是大写还是小写,是大写转换成小写,小写转换成大写
if (isupper(ch)) //大写
{
temp = tolower(ch);
str += temp;
}
else
{
temp = toupper(ch);
str += temp;
}
}
cin.get(ch);
}
cout << str << endl;
return 0;
}
2.3 书上有类似例题,略
4.结构体数组指针,结构体指针作为函数参数
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
const int strsize = 30;
struct bop
{
char fulllname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void ShowChoice();
void Show_preference(struct bop* one_person);
void Show(struct bop** all_person, char ch);
void Print(struct bop** all_person);
int main()
{
//创建结构体数组
bop* person = new bop[5];
person[0] = { "li hua" ,"boss" ,"fox",1 };
person[1] = { "xiao wang" ,"worker" ,"bird",3 };
person[2] = { "lao wang" ,"manager" ,"cat",2 };
person[3] = { "chen " ,"CEO" ,"pig",2 };
person[4] = { "song" ,"white " ,"rabbit",1 };
char ch;
ch = ' ';
while (ch != 'e')
{
ShowChoice();
cout << "enter your choice:" ;
cin.get(ch);
Show(&person, ch);
cin.clear(); cin.ignore(INT_MAX, '\n'); //清空缓存区
}
return 0;
}
void Print(struct bop** all_person)
{
bop* pt = new bop[5];
pt = *all_person;
for (int i = 0; i < 5; i++)
{
cout << pt[i].fulllname << " " << pt[i].title << " "<< pt[i].bopname << " " << pt[i].preference<<endl;
}
}
void ShowChoice()
{
cout << "a. display by name " << "b. display by title" << endl;
cout << "c. display by bopname " << "d. display by preference" << endl;
cout << "e. quit" << endl;
}
void Show_preference(struct bop *one_person)
{
//传入结构体数组里面的一个结构体的结构体指针,根据里面的preference来决定显示什么
if (one_person->preference == 1)
{
cout << one_person->fulllname << endl;
}
if (one_person->preference == 2)
{
cout << one_person->title << endl;
}
if (one_person->preference == 3)
{
cout << one_person->bopname << endl;
}
}
void Show(struct bop **all_person ,char ch)
{
bop* pt = new bop[5];
pt = *all_person;
//传入整个结构体数组,指定要显示的东西
cout << "********************" << endl;
if (ch == 'a')
{
for (int i = 0; i < 5; i++)
{
cout << pt[i].fulllname << endl;
}
}
else if(ch == 'b')
{
for (int i = 0; i < 5; i++)
{
cout << pt[i].title << endl;
}
}
else if (ch == 'c')
{
for (int i = 0; i < 5; i++)
{
cout << pt[i].bopname << endl;
}
}
else if (ch == 'd')
{
for (int i = 0; i < 5; i++)
{
Show_preference(&pt[i]); //传入结构体数组里面的一个结构体的结构体指针
}
}
cout << "********************" << endl;
}
7.编写一个程序,每次读取一个单词,直到用户输入q,然后程序指出有多少个单词以元音打头,
多少单词用辅音开头,有多少个单词不属于这两类。
其他练习
(1)switch
#include <iostream>
#include <vector>
#include<array>
using namespace std;
void showmenu();
void report();
void comfort();
int main()
{
showmenu();
int choice;
cin >> choice;
while (choice != 5)
{
switch (choice)
{
case 1: cout << "\a\n" << endl;
break;
case 2: report();
break;
case 3: cout << "boss was in all day" << endl;
break;
case 4: comfort();
break;
default:cout << "out" << endl;
}
showmenu();
cin >> choice;
}
cout << " BYy" << endl;
return 0;
}
void showmenu()
{
cout << "please enter 1 2 3 4 or 5:" << endl;
cout << "1)alarm " << "2)report" << endl;
cout << "3)alib " << "4)comfort" << endl;
cout << "5)quit " << endl;
}
void report()
{
cout << "for business,120%" << endl;
}
void comfort()
{
cout << "very good" << endl;
}
(2)cin >> 应用 :对用户输入的数字进行计算,输入非数字,循环结束
例题:每天只能抓5条鱼,计算他们的平均重量,
const int Max = 5;
int main()
{
double weight[5];
double sum = 0;
int i = 0;
cout << "enter the weight of " << i + 1 << " fish" << endl;
while (i <= Max-1 && cin >> weight[i])
{
sum += weight[i];
i += 1;
if (i != Max)
{
cout << "enter the weight of " << i + 1 << " fish" << endl;
}
}
if (i == 0)
{
cout << "no fish" << endl;
}
else
{
cout << "the average of fish : " << sum /5 << endl;
}
return 0;
}
(3)文件的输入输出
写入到文本文件
int main()
{
char automobile[50]; //创建的是一个字符数组,存放的是字符
int year;
double a_price;
double d_price;
ofstream outFile; // create object for output
outFile.open("Z:/wang/重建/interior recon/text.txt");
cout << " enter model info:" << endl;
cin.getline(automobile, 50); //这里可以直接将输入的字符存入数组
cout << "enter the year : " << endl;
cin >> year;
cout << "enter the original price : " << endl;
cin >> a_price;
d_price = a_price * 0.8;
//显示信息
cout << " model info:" << automobile << endl;
cout << " year : " << year << endl;
cout << " original price : " << a_price << endl;
cout << "discount price : " << d_price <<endl;
cout << "#############################" << endl;
//显示file里面的信息
outFile << " model info:" << automobile << endl;
outFile << " year : " << year << endl;
outFile << " original price : " << a_price << endl;
outFile << "discount price : " << d_price << endl;
outFile.close();
return 0;
}
读取文本文件内全部内容
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string automobile; //创建的是一个字符数组,存放的是字符
int year;
double a_price;
double d_price;
//显示信息
//cout << " model info:" << automobile << endl;
//cout << " year : " << year << endl;
//cout << " original price : " << a_price << endl;
//cout << "discount price : " << d_price <<endl;
//cout << "#############################" << endl;
//文件读取
ifstream inFile;
inFile.open("Z:/wang/重建/interior recon/text.txt");
if (!inFile.is_open())
{
cout << " open fault" << endl;
exit(EXIT_FAILURE);
}
//获取文件内所有的信息
while(getline(inFile, automobile)) // line中不包括每行的换行符
{
cout << automobile << endl;
}
inFile.close();
return 0;
}