编程练习1
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Please type here: " ;
cin.get(ch);
while(ch !='@')
{
if (isdigit(ch))
{
cin.get(ch);
continue;
}
else if(isupper(ch))
ch = tolower(ch);
else if(islower(ch))
ch = toupper(ch);
cout << ch;
cin.get(ch);
}
cout << endl;
system("pause");
return 0;
}
编程练习2
#include <iostream>
using namespace std;
const int MAX = 10;
int main()
{
double donation[MAX];
cout << "Please enter the each donation." << endl;
cout << "You may enter up to " << MAX << " times <type alphabeta to terminate> " << endl;
cout << "donation #1: ";
int i = 0;
while(i < MAX && cin>>donation[i]) {
if (++i < MAX)
cout << "donation #" << i+1 << ": ";
}
double total = 0;
for (int j=0; j<i; j++)
total += donation[j];
if (i == 0)
cout << "No donation" << endl;
else
{
int num_above = 0;
for (int j=0; j<i; j++)
{
if (donation[j] >total/i)
num_above++;
}
cout << "average donation of " << i << " donations is " <<total / i << endl;
cout << num_above << " donation are above average" << endl;
}
system("pause");
return 0;
}
编程练习3
#include <iostream>
using namespace std;
int main()
{
char choice;
cout << "Please enter one of the following choices:" << endl;
cout << "c) canivore\t" << "p) pianist\t" << endl;
cout << "t) tree \t" << "g) game\t" << endl;
bool flag = true;
while(flag)
{
cout<<"Please enter c,p,t,or g:";
cin.get(choice);
switch(choice) {
case 'c': cout << " canivore is selected"; flag = false;break;
case 'p': cout << " pianist is selected"; flag = false;break;
case 't': cout << " tree is selected"; flag = false;break;
case 'g': cout << " t is selected"; flag = false;break;
}
}
cout << endl;
system("pause");
return 0;
}
编程练习4
#include <iostream>
const int strsize = 30;
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference; // 0 = fullname; 1 = title; 2 = bopname
};
int main()
{
using namespace std;
bop arrBop[3] = {
{"Wimp Mcho","Junir Programmer", "WM",0},
{"Raki Rhodes","HR", "RR",1},
{"Celia Laiter","General Manager", "CL",2},
};
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name \t" << "b. display by title" << endl;
cout << "c. display by bopname\t" << "d. display by preference" << endl;
cout << "q. quite" << endl;
char choice;
bool flag = true;
while(flag)
{
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 'a':
{
for(int i=0; i<3; i++)
cout << arrBop[i].fullname << endl;
break;
}
case 'b':
{
for(int i=0; i<3; i++)
cout << arrBop[i].title << endl;
break;
}
case 'c':
{
for(int i=0; i<3; i++)
cout << arrBop[i].bopname << endl;
break;
}
case 'd':
{
for(int i=0; i<3; i++)
{
switch (arrBop[i].preference) {
case 0: cout << arrBop[i].fullname << endl;break;
case 1: cout << arrBop[i].title << endl;break;
case 2: cout << arrBop[i].bopname <<endl;break;
}
}
break;
}
case 'q':
{
flag = false;
cout << "\nBye";
break;
}
}
}
system("pause");
return 0;
}
编程练习5
#include <iostream>
int main()
{
using namespace std;
double income;
double tax;
cout << "Please input your incoming: ";
cin >> income;
while(cin && income > 0)
{
if (income <=5000)
{
tax = 0;
}
else if (income > 5000 && income <=15000)
{
tax = (income-5000) * 0.1 ;
}
else if (income > 15000 && income <=35000)
{
tax = (income-15000) * 0.15 + 10000 * 0.1;
}
else if (income >35000)
{
tax = (income-35000) * 0.2 + 20000 * 0.15 + 10000 * 0.1;
}
cout << "You should pay " << tax << " as tax" << endl;
cout << "Please input your incoming: ";
cin >> income;
}
cin.get();
system("pause");
return 0;
}