/*1
编写一个小程序
读取键盘输入,直到遇见@符号为止 并且回显输入(数字除外)
同时将大写字符转换成小写 将小写字符转化成大写 别忘记cctype函数系列
*/
//#include<iostream>
//
//using namespace std;
//
//int main() {
//
// char ch;
//
// while(cin.get(ch) && ch !='@'){
// while (cin.get(ch) && ch != '@') //输入流缓冲
// {
// // 三目 是 否
// ch = (islower(ch) ? toupper(ch) : tolower(ch)); ///islower小写字母
//
// if (!isdigit(ch))//不是数字
// {
// cout.put(ch);//输出流缓冲
// }
// }
// cout << "\nPlease excuse the slight confusion." << endl;
//
// }
//
//
// return 0;
//}
/*
* 编写一个程序,最多将10个donation值 读入到一个数组中
* (可以使用array) 程序遇到非数字时停止输入,计算平均值
* 且输出大于平均值的个数
*/
//#include<iostream>
//#include<array>
//
//using namespace std;
//int main(){
//
// double donation1[10];
// array<double,10> donation2;
// double ave;
// int count = 0,sum=0,count1=0;
// char num;
// while (count < 10) {
// cin >> num;
// if (!isdigit(num)) {
// break;
// }
// else {
// donation1[count] = num - '0';
// donation2[count] = num - '0';
// sum = sum + num - '0';
// count++;
// }
// }
//
// ave = sum / count;
// cout << "均值" <<ave<< endl;
// for (int i = 0; i < count; i++) {
// cout << donation1[i] << endl;
// cout << donation2[i] << endl;
// if (donation2[i] >= ave) {
// count1++;
// }
// }
// cout <<"ddd" << count1;
//
//
// return 0;
//}
/*3
3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。
如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。
然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices :
c) carnivore p) pianist
t) tree g) game
f
Please enter a c, p, t, or g: qPlease enter a c. p. t,or g: tmaple is a tree.
*/
//#include<iostream>
//
//using namespace std;
//int main() {
//
// char choice;
//
// int flag = 1;
//
// cout << "Please enter one of the following choices :" << endl;
//
// while (flag) {
// cin >> choice;
// switch (choice) {
// case 'a':
// cout << "tmaple is a tree" << endl;
// flag = 0;
// break;
// case 'c':
// cout << "tmaple is a tree" << endl;
// flag = 0;
// break;
// case 'p':
// cout << "tmaple is a tree" << endl;
// flag = 0;
// break;
// case 't':
// cout << "tmaple is a tree" << endl;
// flag = 0;
// break;
// case 'g':
// cout << "tmaple is a tree" << endl;
// flag = 0;
// break;
// default:
// cout << "Please enter a c, p, t, or g:" ;
// flag = 1;
// break;
// }
// }
//
//
//
// return 0;
//}
/*
4.加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。
请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
l/ Benevolent Order of Programmers name structure
struct bop {
char fullname[ strsize]; // real namechar
title[strsize];// job title
char bopname[strsize];// secret BOP name
int preference;// 0 = fullname,1 - title,2 - bopname
};
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name
b. display by title
c. display by bopname
d. display by preference
q. quit
注意,“display by preference"并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。
如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:
Benevolent Order of Programmers Report
a. display by name
b. display by title
c. display by bopname
d. display by preference
q. quit
Enter your choice: a
wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice: d
wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOPY
Next choice: q
Bye!
*/
//#include<iostream>
//
//using namespace std;
//
//const int strsize = 20;
//
//struct bop
//{
// char fullname[strsize]; // real name
// char title[strsize];// job title
// char bopname[strsize];// secret BOP name
// int preference;// 0 = fullname,1 - title,2 - bopname
//
//};
//
//int main(){
//
//
// bop* x = new bop[3];
//
// //初始化
// x[0] = {"小黄","医生","钢铁侠",0};
// x[1] = { "小青","教师","猪猪侠",1 };
// x[2] = { "小侯","魔法师","卡米尔",2 };
//
// cout << "Benevolent Order of Programmers Report" << endl;
// cout << "a. display by name" << endl;
// cout << "b. display by title" << endl;
// cout << "c. display by bopname " << endl;
// cout << "d. display by preference" << endl;
// cout << "q. quit" << endl;
//
// char choice;
//
// while(1){
// cout << "Enter your choice:";
// cin >> choice;
//
// switch (choice) {
// case 'a':
// for (int i = 0; i < 3; i++) {
// cout << x[i].fullname<<endl;
// }
// break;
// case 'b':
// for (int i = 0; i < 3; i++) {
// cout << x[i].title << endl;
// }
// break;
// case 'c':
// for (int i = 0; i < 3; i++) {
// cout << x[i].bopname << endl;
// }
// break;
// case 'd':
// for (int i = 0; i < 3; i++) {
// if (x[i].preference == 0) {
// cout << x[i].fullname << endl;
// }else if(x[i].preference == 1){
// cout << x[i].title << endl;
// }
// else {
// cout << x[i].bopname << endl;
// }
// }
// break;
// case 'q':
// cout << "bye" << endl;
// exit(0);
// break;
// default:
// cout << "Please enter a b, c, d, or q:" ;
//
// break;
// }
//
// }
//
// return 0;
//}
/*5.在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps : 不收税
5001~15000 tvarps : 10 %
15001~35000 tvarps : 15 %
35000 tvarps以上 : 20 %
例如,收入为38000 tvarps时,所得税为5000 0.00 + 10000 0.10 + 20000 0.15 + 3000 0.20,即4600tvarps。
请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。
*/
//#include <iostream>
//using namespace std;
//
//const double TVARPS_5000 = 0.0;
//const double TVARPS_5001_to_15000 = 0.1;
//const double TVARPS_15001_to_35000 = 0.15;
//const double TVARPS_35000 = 0.2;
//
//int main()
//{
// double wage, tax;
//
// cout << "Please enter your wage (q or <0 to quit): ";
// while (cin >> wage && wage > 0)
// {
// cout << "Your wage: " << wage << " tvarps.\n";
// if (wage < 5000)
// {
// tax = 0.0;
// }
// else if (wage < 15000)
// {
// tax = (wage - 5000) * TVARPS_5001_to_15000;
// }
// else if (wage < 35000)
// {
// tax = (wage - 15000) * TVARPS_15001_to_35000 + 10000 * TVARPS_5001_to_15000;
// }
// else
// {
// tax = (wage - 35000) * TVARPS_35000 + 20000 * TVARPS_15001_to_35000 + 10000 * TVARPS_5001_to_15000;
// }
// cout << "Your tax: " << tax << " tvarps.\n";
// cout << "Next wage (q or <0 to quit): ";
// }
// cout << "Bye." << endl;
//
// return 0;
//}
/*
6.编写一个程序,记录捐助给"维护合法权利团体"的资金。
该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。
这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。
读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。
然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
*/
//#include <iostream>
//#include <string>
//using namespace std;
//
//const int HIGH_MONEY = 10000;
//
//struct corporation
//{
// string name;
// double money;
//};
//
//int main()
//{
// int i, num;
// unsigned int patrons = 0;
// unsigned int grand_patrons = 0;
//
// cout << "Please enter the number of donators: ";
// (cin >> num).get();
// corporation* people = new corporation[num];
//
// for (i = 0; i < num; i++)
// {
// cout << "Please enter name #" << i + 1 << ": ";
// getline(cin, people[i].name);
// cout << "Please enter the amount of donation #" << i + 1 << ": ";
// while (!(cin >> people[i].money))
// {
// cin.clear();
// while (cin.get() != '\n')
// {
// continue;
// }
// cout << "Please enter a number: ";
// }
// cin.get();
// }
// for (i = 0; i < num; i++)
// {
// (HIGH_MONEY < people[i].money) ? ++grand_patrons : ++patrons;
// }
//
// cout << "\nGrand Patrons:" << endl;
// if (grand_patrons != 0)
// {
// for (i = 0; i < num; i++)
// {
// if (people[i].money > HIGH_MONEY)
// {
// cout << "Name: " << people[i].name;
// cout << "\nMoney: " << people[i].money << endl;
// }
// }
// }
// else
// {
// cout << "none" << endl;
// }
// cout << "\nPatrons:" << endl;
// if (patrons != 0)
// {
// for (i = 0; i < num; i++)
// {
// if (people[i].money < HIGH_MONEY)
// {
// cout << "Name: " << people[i].name;
// cout << "\nMoney: " << people[i].money << endl;
// }
// }
// }
// else
// {
// cout << "none" << endl;
// }
// delete[] people;
//
// return 0;
//}
/*7.编写一个程序,它每次读取一个单词,直到用户只输入q。
然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。
为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,
然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words(q to quit) : The 12 awesome oxen ambled
quietly across 15 meters of lawn.q5 words beginning with vowels
4 words beginning with consonants2 others
*/
//#include <iostream>
//#include <string>
//#include <cctype>
//using namespace std;
//
//int main()
//{
// string words;
// unsigned int vowels = 0;
// unsigned int consonants = 0;
// unsigned int others = 0;
//
// cout << "Enter words (q to quit):" << endl;
// while (cin >> words, words != "q")
// {
// if (isalpha(words[0]))
// {
// switch (tolower(words[0]))
// {
// case 'a':
// case 'e':
// case 'i':
// case 'o':
// case 'u':
// {
// ++vowels;
// break;
// }
// default:
// {
// ++consonants;
// break;
// }
// }
// }
// else
// {
// ++others;
// }
// }
// 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 <string>
//#include <cstdlib>
//using namespace std;
//
//int main()
//{
// char ch;
// ifstream infile;
// string filename;
// unsigned int count = 0;
//
// cout << "Please enter name of data file: ";
// getline(cin, filename);
// infile.open(filename);
//
// if (!infile.is_open())
// {
// cout << "Could not open the file " << filename << endl;
// cout << "Program terminating." << endl;
// exit(EXIT_FAILURE);
// }
// while (infile.get(ch), infile.good())
// {
// ++count;
// cout.put(ch);
// }
// if (count > 0)
// {
// cout << count << " characters in the file " << filename << endl;
// }
// else
// {
// cout << "No data processed." << endl;
// }
// infile.close();
//
// return 0;
//}
/*9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。
在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
Sam Stone
200e
Freida Flass10050e
Tammy Tubbs5e08
Rich Raptor55eee
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int HIGH_MONEY = 10000;
struct corporation
{
string name;
double money;
};
int main()
{
int i, num;
string filename;
ifstream infile;
unsigned int patrons = 0;
unsigned int grand_patrons = 0;
cout << "Please enter name of data file: ";
getline(cin, filename);
infile.open(filename);
if (!infile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating." << endl;
exit(EXIT_FAILURE);
}
(infile >> num).get();//读取行数 写入name
corporation* people = new corporation[num];
for (i = 0; i < num && infile.good(); i++)
{
getline(infile, people[i].name);
while (!(infile >> people[i].money))
{
infile.clear();
while (infile.get() != '\n')
{
continue;
}
}
while (infile.get() != '\n')
{
continue;
}
}
infile.close();
for (i = 0; i < num; i++)
{
(HIGH_MONEY < people[i].money) ? ++grand_patrons : ++patrons;
}
cout << "\nGrand Patrons:" << endl;
if (grand_patrons != 0)
{
for (i = 0; i < num; i++)
{
if (people[i].money > HIGH_MONEY)
{
cout << "Name: " << people[i].name;
cout << "\nMoney: " << people[i].money << endl;
}
}
}
else
{
cout << "none" << endl;
}
cout << "\nPatrons:" << endl;
if (patrons != 0)
{
for (i = 0; i < num; i++)
{
if (people[i].money < HIGH_MONEY)
{
cout << "Name: " << people[i].name;
cout << "\nMoney: " << people[i].money << endl;
}
}
}
else
{
cout << "none" << endl;
}
delete[] people;
return 0;
}