本来前面五题都做完了,写博客时没保存好草稿= =,写了个整合版的程序,实现前五题的关键部分。

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章练习题的更多相关文章

  1. 《C++ primerplus》第13章练习题

    1.对CD类的派生练习.基类CD类存储作者和作品号等信息,派生类Classic额外增加一格"主要作品"的信息.主函数使用拷贝构造函数.按引用传递参数的函数和指针来测试基类和派生类的 ...

  2. 《C++primerplus》第12章练习题

    做一下倒数两题,都是在队列模拟的程序基础上做点修改测试. 5.找出平均等候时间为1分钟时,每小时到达的客户数为多少(试验时间不少于100小时). 指定队伍最大长度10人,模拟100小时.粗略估计答案在 ...

  3. 《C++primerplus》第11章练习题

    1.修改程序清单11.5(随机漫步),使之以特定的格式将结果写入文件中. //vector.h -- Vector Class #ifndef _VECTOR_H_ #define _VECTOR_H ...

  4. 《C++primerplus》第10章练习题

    1.定义一个类表示银行账户.数据成员包括姓名,账号和存款.成员函数可以执行初始化数据.显示数据和取款存款的功能. //Bank.cpp #include<iostream> #includ ...

  5. 《C++primerplus》第9章练习题

    1.(未使用原书例题)练习多文件组织.在一个头文件中定义一种学生的结构体,存储姓名和年龄,声明三个函数分别用于询问有多少个学生,输入学生的信息和展示学生的信息.在另一个源文件中给出所有函数的定义.在主 ...

  6. 《C++primerplus》第8章练习题

    1.(简单用一下引用变量,没有采用书中的题目)定义一个替身结构体,存储名字(char[])和力量值(int).使用结构体引用作为形参写两个函数,一个不加const,使得能对定义的结构体做修改,另一个加 ...

  7. 《C++primerplus》第7章练习题

    1.用户不断输入两个数,计算调和平均数,直到其中一个数为0. #include<iostream> using namespace std; double harm_mean(double ...

  8. 《C++primerplus》第4章练习题

    注:略过部分题目,修改了题设要求,实现差不多的功能 1.使用字符数组.要求用户输入姓名,等第和年龄,输出其姓名和年龄,等第降一级(即字母高一级). #include<iostream> u ...

  9. python第一章练习题

    本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...

随机推荐

  1. Python超级码力在线编程大赛初赛题解

    P1 三角魔法 描述小栖必须在一个三角形中才能施展魔法,现在他知道自己的坐标和三个点的坐标,他想知道他能否施展魔法 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后, ...

  2. android开发之splash闪屏页判断是否第一次进入app代码

    package com.david.david.zhankudemo.activity; import android.app.Activity; import android.content.Con ...

  3. JAVA线程池原理与源码分析

    1.线程池常用接口介绍 1.1.Executor public interface Executor { void execute(Runnable command); } 执行提交的Runnable ...

  4. Eclipse的安装和配置

    1. 下载Eclipse 前往Eclipse官网(https://www.eclipse.org/downloads/packages/)下载Eclipse: 这里下载的版本为: 这里给出该版本的百度 ...

  5. 两篇好文 清晰地描述bug 技术总监的忠告

    如何清晰的描述一个bug 一个技术总监的忠告 --2020-02-26--

  6. linux 用户、用户组的操作

    1.查看linux系统下的所有用户 方式一:grep bash /etc/passwd 方式二:查看etc/passwd文件,其他第三个参数大于500的都是用户自己新增的 vim /etc/passw ...

  7. Mybatis项目构建和CURD操作

    Mybatis入门 一.使用SqlSession对象创建Dao接口代理对象进行持久化操作 1.使用maven构建java项目 2.修改pom.xml配置,添加所需jar包坐标 <?xml ver ...

  8. [源码分析] OpenTracing之跟踪Redis

    [源码分析] OpenTracing之跟踪Redis 目录 [源码分析] OpenTracing之跟踪Redis 0x00 摘要 0x01 总体逻辑 1.1 相关概念 1.2 埋点插件 1.3 总体逻 ...

  9. Django设置前端背景图片

    设置 setting.py 文件 STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static&qu ...

  10. 秒懂JVM的三大参数类型,就靠这十个小实验了

    秒懂JVM的三大参数类型,就靠这十个小实验了 你好,我是悟空哥,「7年项目开发经验,全栈工程师,开发组长,超喜欢图解编程底层原理」.手写了2个小程序,Java刷题小程序,PMP刷题小程序,已发布到公众 ...