《C++primerplus》第6章练习题
本来前面五题都做完了,写博客时没保存好草稿= =,写了个整合版的程序,实现前五题的关键部分。
1.定义一个叫jojo的结构,存储姓名、替身和力量值,使用动态结构数组初始化二乔、承太郎和乔鲁诺乔巴纳等人的信息。循环地用菜单化的选项提示用户输入,选项1:显示所有人的替身;选项2:按一定比率强化白金之星的力量值,并输出当前所有人力量的平均值;选项3:要求用户输入一系列字符,然后返回相同的字符,其中转换字母大小写,遇到“@”就停止;选项4:退出。如果输入1-4以外的数字,提示用户重新输入,如果输入的不是数字,则提示失败退出程序。
#include<iostream>
#include<cctype>
using namespace std; const double ratio = 0.1;
const int strsize = 100; struct jojos
{
char name[20];
char stand[20];
double power;
}; int main()
{
jojos *joptr = new jojos[4]; joptr[0] =
{
"Joseph",
"Hermit Purple",
6
}; joptr[1] = { "Jotaro","Star Platinum",9 }; joptr[2] =
{
"Giorno Giovanna",
"Gold Experience",
8
}; joptr[3] = { "me","Repeater",1 }; int choice;
int flag = 1;
char store[strsize]; char prmt[] =
{
"Make your choice:\n1)show the stand \t 2)power up \n3)my stand \t 4)quit\n"
}; while (flag)
{
cout << prmt;
if (cin >> choice)
{
switch (choice)
{
case 1:
{
for (int i = 0; i < 4; i++)
{
cout << joptr[i].name << ":" << joptr[i].stand << endl;
}
cout << "\n";
break;
}
case 2:
{
cout << "Power up!\n";
joptr[1].power = joptr[1].power*(1 + ratio);
double sum = 0;
for (int i = 0; i < 4; i++)
{
sum += joptr[i].power;
}
cout << "Jotaro's star platinum:" << joptr[1].power << endl;
cout << "Power average:" << sum / 4 << endl;
cout << "\n";
break;
}
case 3:
{
cin.get();
cout << "Enter some characters: ";
cin.get(store, strsize);
for (int i = 0; store[i] != '\0'; i++)
{
if (isdigit(store[i]))
continue;
else if (store[i] != '@')
{
if (isupper(store[i]))
store[i] = tolower(store[i]);
else if (islower(store[i]))
store[i] = toupper(store[i]);
cout << store[i];
}
else if (store[i] == '@')
{
break;
}
else {};
}
cout << "\n\n";
break;
}
case 4:
{
flag = 0;
break;
}
default:
{
cout << "Please select from 1 to 4.\n\n";
}
}
}
else
{
cout << "Bad Input!\n";
flag = 0;
}
} delete []joptr;
cout << "Bye.\n";
system("pause");
}
2.编写程序记录一系列捐款者和捐款数目,将所有信息存储在动态结构数组中。首先要求用户输入有几个捐款者,然后循环读取捐款者姓名和捐款数目。读取完数据后,输出所有捐款额超过10000的人的姓名和捐款额,并标记为重要捐款人“Grand Patrons”,然后再输出其它捐款者的姓名,标记为“Patrons”。如果对应标记没有捐款人,则输出“none”。
#include<iostream>
using namespace std; struct donator
{
char name[20];
int donation;
}; int main()
{
int counts, counts_temp = 0; cout << "How many donators: ";
cin >> counts; donator *ptr = new donator[counts]; for (int i = 0; i < counts; i++)
{
cout << "#donator " << i + 1 << "#\n";
cout << "name:";
cin.get();
cin.get(ptr[i].name, 20);
cin.get();
cout << "donation:";
cin >> ptr[i].donation;
} cout << "*Grand Patrons:";
for (int i = 0; i < counts; i++)
{
if (ptr[i].donation >= 10000)
{
cout << ptr[i].name << " $" << ptr[i].donation << " ";
counts_temp++;
}
else {};
} if (counts_temp == 0)
cout << "none.\n";
else
counts_temp = 0; cout << "\nPatrons:";
for (int i = 0; i < counts; i++)
{
if (ptr[i].donation < 10000)
{
cout << ptr[i].name<<";";
counts_temp++;
}
else {};
} if (counts_temp == 0)
cout << "none.\n";
else {} delete[]ptr;
system("pause");
}
3.编写程序,打开一个文件,逐个字符读取该文件,直到文件末尾,然后指出其中包含的字符数。
#include<iostream>
#include<fstream>
using namespace std; const int MAXSIZE = 100; int main()
{
char store[MAXSIZE] ;
int counts_all = 0, counts_space = 0; ifstream infile;
infile.open("test.txt"); //打开文件 infile.get(store, MAXSIZE); //infile此时可以当cin用 for (int i = 0; store[i]!='\0'; i++)
{
counts_all++;
if (store[i] == ' ')
counts_space++;
else {}
} cout << "Total characters(include spaces):" << counts_all << endl;
cout << "Total characters(without spaces):" << counts_all - counts_space << endl; infile.close(); system("pause");
}
4.修改程序2,这次使用文件存储所有信息,从文件里读取。
文件的格式:
4
Sam Stone
2000
Freida Flass
100500
...
第一行为捐款人数量,从第二行开始,一行写姓名,一行写捐款额。
程序如下。
#include<iostream>
#include<fstream>
using namespace std; struct donator
{
char name[20];
int donation;
}; int main()
{
int counts, counts_temp = 0; int test; ifstream infile;
infile.open("test.txt"); cout << "How many donators: ";
infile >> counts;
cout << counts << endl; //每次读入以后输出一下,查看是否正确 donator *ptr = new donator[counts]; //infile会逐行读入,遇到换行符结束,且换行符不会留在缓冲区
for (int i = 0; i < counts; i++)
{
cout << "#donator " << i + 1 << "#\n";
cout << "name:";
infile >> ptr[i].name;
cout << ptr[i].name << endl;
cout << "donation:";
infile >> ptr[i].donation;
cout << ptr[i].donation << endl;
} cout << "*Grand Patrons:";
for (int i = 0; i < counts; i++)
{
if (ptr[i].donation >= 10000)
{
cout << ptr[i].name << " $" << ptr[i].donation << " ";
counts_temp++;
}
else {};
} if (counts_temp == 0)
cout << "none.\n";
else
counts_temp = 0; cout << "\nPatrons:";
for (int i = 0; i < counts; i++)
{
if (ptr[i].donation < 10000)
{
cout << ptr[i].name << ";";
counts_temp++;
}
else {};
} if (counts_temp == 0)
cout << "none.\n";
else {} delete[]ptr;
infile.close();
system("pause");
}
*经测试,infile对象是逐行读入的,每次遇到换行符(回车)就停止一次输入,并且不会在缓冲区留下换行符,所以不用像原先代码那样用cin.get()来清除。
《C++primerplus》第6章练习题的更多相关文章
- 《C++ primerplus》第13章练习题
1.对CD类的派生练习.基类CD类存储作者和作品号等信息,派生类Classic额外增加一格"主要作品"的信息.主函数使用拷贝构造函数.按引用传递参数的函数和指针来测试基类和派生类的 ...
- 《C++primerplus》第12章练习题
做一下倒数两题,都是在队列模拟的程序基础上做点修改测试. 5.找出平均等候时间为1分钟时,每小时到达的客户数为多少(试验时间不少于100小时). 指定队伍最大长度10人,模拟100小时.粗略估计答案在 ...
- 《C++primerplus》第11章练习题
1.修改程序清单11.5(随机漫步),使之以特定的格式将结果写入文件中. //vector.h -- Vector Class #ifndef _VECTOR_H_ #define _VECTOR_H ...
- 《C++primerplus》第10章练习题
1.定义一个类表示银行账户.数据成员包括姓名,账号和存款.成员函数可以执行初始化数据.显示数据和取款存款的功能. //Bank.cpp #include<iostream> #includ ...
- 《C++primerplus》第9章练习题
1.(未使用原书例题)练习多文件组织.在一个头文件中定义一种学生的结构体,存储姓名和年龄,声明三个函数分别用于询问有多少个学生,输入学生的信息和展示学生的信息.在另一个源文件中给出所有函数的定义.在主 ...
- 《C++primerplus》第8章练习题
1.(简单用一下引用变量,没有采用书中的题目)定义一个替身结构体,存储名字(char[])和力量值(int).使用结构体引用作为形参写两个函数,一个不加const,使得能对定义的结构体做修改,另一个加 ...
- 《C++primerplus》第7章练习题
1.用户不断输入两个数,计算调和平均数,直到其中一个数为0. #include<iostream> using namespace std; double harm_mean(double ...
- 《C++primerplus》第4章练习题
注:略过部分题目,修改了题设要求,实现差不多的功能 1.使用字符数组.要求用户输入姓名,等第和年龄,输出其姓名和年龄,等第降一级(即字母高一级). #include<iostream> u ...
- python第一章练习题
本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...
随机推荐
- android studio生成aar包
android studio生成aar包并在其他工程引用aar包 http://blog.csdn.net/getchance/article/details/47257389 用Android st ...
- KMP - NOI2014 动物园
单题分析:NOI2014 动物园. 题目分析:很明显题目已明确指出这是有关KMP的题,思考KMP.本题与普通KMP不同之处在于它求的是不相交最长相同前缀后缀. 如何处理不相交: 1.暴力 2. ...
- [java]将多个文件压缩成一个zip文件
此文进阶请见:https://www.cnblogs.com/xiandedanteng/p/12155957.html 方法: package zip; import java.io.Buffere ...
- leetcode刷题-59螺旋矩阵2
题目 给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 思路 与螺旋矩阵题完全一致 实现 class Solution: def generateM ...
- Spring的循环依赖,学就完事了【附源码】
目录 啥是循环依赖? Spring可以解决循环依赖的条件 Spring如何去解决循环依赖 SpringBean的创建流程 Spring维护的三级缓存 getSingleton getSingleton ...
- shell 设置进程数运行
问题描述 在服务器上提交任务时,需要限制运行的核的数目.程序本身是单线程的,但是不同的输入参数需要跑很多组,粗暴的方法是开多个终端,不断地去提交任务.但这比较麻烦,可以用 shell 实现. 基础 首 ...
- Readme for Software engineering
作业任务: 软件工程 软件工程 作业要求 作业要求 作业目标 博客园.github注册 自我介绍 软工5问 自我介绍: 广东工业大学计算机学院18级信息安全二班 广东工业大学AD攻防工作室成员& ...
- 图解 Await 和 Async
原文链接:Await and Async Explained with Diagrams and Examples 文章目录 简介 Promise 问题:组合 Promise Async 函数 Awa ...
- Georgia and Bob(POJ 1704)
原题如下: Georgia and Bob Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12712 Accepted: ...
- unittest单元测试执行用例的顺序
打印结果如下: