1.编写一个程序,读取键盘输入,知道遇到@符号为止,并显示输入,同时将大写字符转化为小写字符,将小写转化为大写。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "please enter zifu ,zhi dao @ break";
char ch;
cin.get(ch);
while (ch !=' @')
{
if (isupper(ch))
{
cout << char(ch + 'a' - 'A');
}
if (islower(ch))
{
cout << char(ch + 'A' - 'a');
}
if (isspace(ch))
{
cout << ch;
}
cin.get(ch);
}
system("pause");
return 0;
}
2.编写一个程序,最多将10个donation值读入到一个double数组中。程序遇到非数字输入是将结束输入,并报告这些数字的平均值以及数组中有多少数字大于平均值。
#include <iostream>
#include <cctype>
#include <array>
using namespace std;
int main()
{
cout << "please enter 10 donation : ";
array<double, 10>kk;
double sum=0,ave;
int i;
for (i = 0; i < 10; i++)
{
cin >> kk[i];
cin.get();
sum = sum + kk[i];
if (isdigit(kk[i]))
{
break;
}
}
ave = sum / i;
cout << "ping jun zhi wei :" << ave << endl;
cout << "da yu zhi de you :";
for (int j = 0; j <= i; j++)
{
if (kk[j]>ave)
{
cout << kk[j] << " ";
}
}
system("pause");
return 0;
}
3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项——每个选项用一个字母标记。如果用户使用有效选项外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样子做为止。然后,该程序使用一条switch语句,根据用户选择执行一个简单的操作。该程序的运行情况为:
#include <iostream>
using namespace std;
int main()
{
cout << "please enter one of the following chioces: " << endl;
cout << "c) carnivore p)pianist" << endl;
cout << "t) tree g)game" << endl;
cout << "please enter a c,p,t,g: ";
char j;
while (1)
{
cin >> j;
switch (j)
{
case 'c':cout << "A maple is a carnivore "; break;
case 'p':cout << "A maple is a pianist "; break;
case 't':cout << "A maple is a tree"; break;
case 'g':cout << "A maple is a game"; break;
default:cout << "please enter a c,p,t,g: ";break;
}
if (j != 'c'&&j != 'p'&&j != 't'&&j != 'g')
{
continue;
}
else
break;
}
system("pause");
return 0;
}
4.加入Benevolent Order of Programmer 后,在BOP大会上,人们便可以通过加入者的真实姓名,头衔或者密码BOP姓名来了解他,请编写一个程序,可以使用真实姓名,头衔,秘密姓名或者成员偏好来列出成员。编写该程序时,使用结构函数。
#include <iostream>
#define strsize 100
using namespace std;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main()
{
cout << "Benevlent Order of Programmers Report" << endl;
cout << "a. display by name b.display by title" << endl;
cout << "c.display by bopname d.display by preference" << endl;
cout << "q.quit" << endl;
cout << "Enter your choice :";
bop *usre = new bop[4];
usre[0] = { "Winm Macho", "Winm Macho","Winm Macho", 0};
usre[1] = { "Raki Rhodes", "Raki Rhodes", "Raki Rhodes", 1 };
usre[2] = { "Celia Laiter", "Celia Laiter", "Celia Laiter", 2 };
usre[3] = { "Pat Hand", "Pat Hand", "Pat Hand", 3 };
char kk;
while (1)
{
cin >> kk;
for (int i = 0; i < 4; i++)
{
switch (kk)
{
case 'a':case 'b':case 'c':cout << usre[i].fullname; break;
case 'd':cout << usre[i].preference; break;
default:
break;
}
}
cout << "Next choice ";
if (kk == 'q')
{
cout << "Bye!";
break;
}
}
system("pause");
return 0;
}
5.在Neutronia王国,货币单位是tvarp,收入税计算为。
5000以下:不收税
5001-15000:收超过部分10%
15001-35000:收上一级全部和超过15000部分的15%
35000以上:收上一级全部超过35000部分的20%
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
double k=0;
cout << "enter your shou ru:";
cin >> k;
double j;
while ( k >= 0)
{
if (k <= 5000)j = 0;
if (k > 5000 && k <= 15000)j = (k - 5000) *0.1 ;
if (k > 15000 && k < 35000)j = 1000 + (k + 15000)*0.15;
if (k>35000)j = 3250 + (k - 35000)*0.2;
cout << "your de shui shou wei : " << j;
cout << "enter your shou ru: ";
cin >> k;
if (cin.fail())//判断是否输入非数字
break;
}
system("pause");
return 0;
}
6.编写一个程序记录捐助给“维护合法权利团队”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用于储存姓名的字符数组和用于存储款项的double成员。读取所有数据后,程序将显示所有捐款超过10000的捐款者的姓名极其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要的捐款者(Grand Patrons)。然后程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include <iostream>
#include <string>
using namespace std;
struct MyStruct
{
string name;
double momey;
};
int main()
{
cout << "please enter ren shu";
int k, j = 0;
cin >> k;
cin.get();
MyStruct *qq = new MyStruct[k];
for (int i = 0; i < k; i++)
{
getline(cin, qq[i].name);
cin >> qq[i].momey;
cin.get();
}
cout << "Grand Patrons" << endl;
for (int i = 0; i < k; i++)
{
if (qq[i].momey >= 10000)
{
cout << qq[i].name << " " << qq[i].momey << endl;
j++;
}
}
if (j == 0)
cout << "none" << endl;
for (int i = 0; i < j; i++)
{
}
cout << "Patrons" << endl;
if (j == k)
cout << "none" << endl;
if (j != k)
{
for (int i = 0; i < k; i++)
{
if (qq[i].momey < 10000)
cout << qq[i].name << " " << qq[i].momey << endl;
}
}
system("pause");
return 0;
}
7.编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少单词以元音大头,有多少单词以辅音打头,还有多少个单词不属于这两类,为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了usalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter words (q to quit):" << endl;
int yuan=0, fu=0, other=0;
string k;
cin >> k;
while (k [0]!= 'q')
{
if (isalpha(k[0]))
{
if (k[0] == 'a' || k[0] == 'e' || k[0] == 'i' || k[0] == 'o' || k[0] == 'u')
{
yuan++;
}
else
fu++;
}
else
{
other++;
}
cin >> k;
}
cout << "yuan yin :" << yuan << " fu yin :" << fu << " other :" << other;
system("pause");
return 0;
}
8.编写一个程序,它打开一个文本文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件包含多少个字符。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
char fine[30];
cout << "enter your name ";
cin.getline(fine, 30);//输入项目文件夹下面的相对位置的文件名,带后缀。
infile.open(fine);
if (!infile.is_open())
{
exit(EXIT_FAILURE);
}
int count = 0;
char str;
infile >> str;
while (infile.good())
{
count++;
infile >> str;
}
if (infile.eof())
{
cout << "end";
infile.close();
cout << count << endl;
}
system("pause");
return 0;
}
9.完成编写练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对,第一行为捐款人姓名,第二行为捐款数额。既该文件类似于下面:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct MyStruct
{
char name[20];
double k;
};
int main()
{
ifstream infile;
cout << "please enter your wenjian name: ";
string wenname;
getline(cin, wenname);//输入文件名字
infile.open(wenname);
if (!infile.is_open())
{
exit(EXIT_FAILURE);
}
int num;
infile >> num;
infile.get();//数字的换行符
string wname;
double momey;
MyStruct *p = new MyStruct[num];
for (int i = 0; i < num; i++)
{
infile.getline( p[i].name,20);
infile >> p[i].k;
infile.get();
}
int j = 0;
cout << "Grand Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (p[i].k >= 10000)
{
cout << p[i].name << " " << p[i].k << endl;
j++;
}
}
if (j == 0)
cout << "none" << endl;
for (int i = 0; i < j; i++)
{
}
cout << "Patrons" << endl;
if (j == num)
cout << "none" << endl;
if (j != num)
{
for (int i = 0; i < num; i++)
{
if (p[i].k < 10000)
cout << p[i].name << " " << p[i].k << endl;
}
}
system("pause");
return 0;
}