本来前面五题都做完了,写博客时没保存好草稿= =,写了个整合版的程序,实现前五题的关键部分。
1.定义一个叫jojo的结构,存储姓名、替身和力量值,使用动态结构数组初始化二乔、承太郎和乔鲁诺乔巴纳等人的信息。循环地用菜单化的选项提示用户输入,选项1:显示所有人的替身;选项2:按一定比率强化白金之星的力量值,并输出当前所有人力量的平均值;选项3:要求用户输入一系列字符,然后返回相同的字符,其中转换字母大小写,遇到“@”就停止;选项4:退出。如果输入1-4以外的数字,提示用户重新输入,如果输入的不是数字,则提示失败退出程序。
#include
#include
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
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
#include
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
#include
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()来清除。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章