1.编写一个程序,读取键盘输入,知道遇到@符号为止,并回显输入(数字除外),同时转换大小写字符。
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
char ch;
cin.get(ch);
while (ch != '@')
{
if (!isdigit(ch))
{
if (islower(ch))
ch = toupper(ch);
else if (isupper(ch))
ch = tolower(ch);
cout << ch;
}
cin.get(ch);
}
return 0;
}
2.编写一个程序,最多将10个donation值输入到一个double数组中。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include <iostream>
int main()
{
using namespace std;
double donation[10], sum = 0, average;
int i = 0;
cout << "donation 1: ";
while (i < 10 && cin >> donation[i])
{
sum += donation[i];
cout << "donation " << ++ i << ": ";
}
average = sum / i;
cout << "The average of " << i << " donation is: " << average << endl
<< "Donation ";
for (int j = 0; j < i; j ++)
if (donation[i] > average)
cout << i << " ";
cout << "over average." << endl;
return 0;
}
3.编写一个菜单驱动程序的雏形。
#include <iostream>
int main()
{
using namespace std;
char ch;
cout << "Please enter one of the following choices:" << endl
<< "c) carnivore p) pianist" << endl
<< "t) tree g) game " << endl;
cin >> ch;
while (ch != 'c' && ch != 'p' && ch != 't' && ch !='g')
{
cout << "Please enter a c, p, t, or g: ";
cin >> ch;
}
cout << "A maple is a ";
switch (ch)
{
case 'c': cout << "carnivore" << endl;
break;
case 'p': cout << "pianist" << endl;
break;
case 't': cout << "tree" << endl;
break;
case 'g': cout << "game" << endl;
break;
}
return 0;
}
4.编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。
#include <iostream>
using namespace std;
struct bop
{
char fullname[20];
char title[20];
char bopname[20];
int preference;
};
int main()
{
bop a[5] =
{
{"Wimp Macho", "title1", "bopname1", 0},
{"Raki Rhodes", "Junior Programmer", "bopname2", 1},
{"Celia Laiteer", "title3", "MIPS", 2},
{"Hoppy Hipman", "Analyst trainee", "bopname3", 1},
{"Pat Hand", "title4", "LOOPY", 2}
};
char ch;
cout << "Benevolent Order of Programmers Report\n"
<< "a.display by name b.display by title\n"
<< "c.display by bopname d.display by preference\n"
<< "q.quit";
cin >> ch;
while (ch != 'q')
{
while(!(ch >= 'a' && ch <= 'd' || ch == 'q'))
{
cout << "Please enter a a,b,c,d or q: ";
cin. << ch;
}
if (ch =='q')
break;
switch (ch)
{
case 'a': for (int i = 0; i < num; i++)
cout << a[i].fullname << endl;
break;
case 'b': for (int i = 0; i < num; i++)
cout << a[i].title << endl;
break;
case 'c': for (int i = 0; i < num; i++)
cout << a[i].bopname << endl;
break;
case 'd': for (int i = 0; i < num; i++)
{
int j = a[i].preference;
switch (j)
{
case 0: cout a.fullname << endl;
break;
case 1: cout a.title << endl;
break;
case 2: cout a.bopname << endl;
break;
}
}
break;
}
cout << "Next choice: ";
cin >> ch;
}
cout << "Bye!\n";
return 0;
}
5.编写程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。
#include <iostream>
int main()
{
using namespace std;
int income;
double tax;
cout << "Please enter your income: ";
while (cin >> income && income > 0)
{
if (income > 5000)
if (income > 15000)
if (income > 35000)
tax = 4000 + 0.2 * (income - 35000);
else
tax = 1000 + 0.15 * (income - 15000);
else
tax = 0.1 * (income - 5000);
else tax = 0;
cout << "The tax is: " << tax << endl;
}
return 0;
}
6.编写程序,记录捐助资金。
#include <iostream>
struct patrons
{
char name[20];
double money;
};
int main()
{
using namespace std;
int num, high, low;
cout << "Please enter the number of patrons: ";
cin >> num;
cin.get();
patrons * p =new patrons[num];
for (int i = 0; i < num ; i++)
{
cout << "Please enter the name of the " << i + 1 << " patrons: ";
cin.get(p[i].name,20);
cout << "money: ";
cin >> p[i].money;
cin.get();
}
cout << "Grand Patrons are: ";
high = 0;
for (int i = 0; i< num; i ++)
{
if (p[i].money >1000)
{
cout << p[i].name << ":\t" << p[i].money << endl;
high ++;
}
}
if (high == 0)
cout << "none\n";
cout << "Partrons are: " << endl;
low = 0;
for (int i = 0; i < num; i++)
if (p[i].money <= 1000)
{
cout <<p[i].name << ": \t" << p[i].money << endl;
low ++;
}
if (low == 0)
cout << "none\n";
return 0;
}
7.编写一个程序,它每次读取一个单词,知道用户只输入q然后进行分类并输出。
#include <iostream>
#include <cctype>
#include <string>
int main()
{
using namespace std;
string word;
char ch;
int consonants, vowels, others;
consonants = 0;
vowels = 0;
others = 0;
cout << "Enter words (q to quit):\n";
cin >> word;
while (word != "q")
{
if (isalpha(word[0]))
{
ch = tolower (word[0]);
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': vowels ++;
break;
default: consonants ++;
break;
}
}
else
others ++;
cin >> word;
}
cout << vowels << " begining with vowels\n";
cout << consonants << " begining with consonants\n";
cout << others << " others\n";
return 0;
}
8.编写一个程序,它打开一个文件,逐个字符地读取该文件,知道到达文件末尾,然后指出该文件中包含多少个字符。
#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
using namespace std;
char filename[100],ch;
int num = 0;
ifstream infile;
cout << "Please enter the name of the file: ";
cin.getline(filename,100);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file: " << filename << endl;
exit(EXIT_FALURE);
}
inFile >> ch;
while (inFile.good())
{
num++;
inFile >> ch;
}
cout << "You have enter " << num <<" characters." << endl;
inFile.close();
return 0;
}
9.完成编程练习6,但从文件中读取所需信息。
#include <iostream>
#include <fstream>
#include <cstdlib>
struct patrons
{
char name[20];
double money;
};
int main()
{
using namespace std;
char filename[100];
int num, high, low;
ifstream inFile;
cout << "Please enter the name of the file: ";
cin.getline(filename,100);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file: " << filename << endl;
exit(EXIT_FALURE);
}
inFile >> num;
inFile.get();
patrons * p =new patrons[num];
for (int i = 0; i < num ; i++)
{
cout << "Please enter the name of the " << i + 1 << " patrons: ";
cin.get(p[i].name,20);
cout << "money: ";
cin >> p[i].money;
cin.get();
}
cout << "Grand Patrons are: ";
high = 0;
for (int i = 0; i< num; i ++)
{
if (p[i].money >1000)
{
cout << p[i].name << ":\t" << p[i].money << endl;
high ++;
}
}
if (high == 0)
cout << "none\n";
cout << "Partrons are: " << endl;
low = 0;
for (int i = 0; i < num; i++)
if (p[i].money <= 1000)
{
cout <<p[i].name << ": \t" << p[i].money << endl;
low ++;
}
if (low == 0)
cout << "none\n";
inFile.close();
return 0;
}