目录
1.cpp
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char ch;
cin.get(ch);
while (isalpha(ch))
{
if (islower(ch))
cout << char(toupper(ch)) <<endl;
if (isupper(ch))
cout << char(tolower(ch)) <<endl;
if (ch == '@')
break;
cin.get(ch);
cin.get(ch);
}
return 0;
}
2.cpp
#include<iostream>
using namespace std;
int main()
{
const int Arsize = 20;
double li[Arsize] ;
int i = 0;
while (cin >> li[i])
{
i++;
if (i == 10)
break;
}
double sum = 0;
for (int j = 0; j < i; j++)
{
sum += li[j];
}
double average = sum/i;
int count = 0;
for (int j = 0; j < i; j++)
{
if (li[j] > average)
count++;
}
cout << "average:" << average << "有" << count << "个数字大于平均值" <<endl;
return 0;
}
3.cpp
#include<iostream>
using namespace std;
int main()
{
cout << "Please enter one of the following choice:" <<endl;
cout << "c) carnivore p) pianist" <<endl;
cout << "t) tree g) game" <<endl;
char ch;
cin >> ch;
while (1)
{
if (ch == 'c' || ch == 'p' || ch == 't' || ch == 'g')
break;
else
{
cout << "Please enter a c, p, t, or g:";
cin >> ch;
}
}
switch (ch)
{
case 'c':
cout << "is a c" <<endl;
break;
case 'p':
cout << "is a p" <<endl;
break;
case 't':
cout << "is a t" <<endl;
break;
case 'g':
cout << "is a g" <<endl;
default:
break;
}
return 0;
}
4.cpp
#include<iostream>
using namespace std;
const int Arsize = 20;
struct bop
{
char fullname[Arsize]; // real name
char title[Arsize]; // job title
char bopname[Arsize]; // secret BOP name
int preference; // 0 = fullname, 1 = title, 2= bopname
/* data */
};
int main()
{
bop* B = new bop[3]
{
{"aa_bb","aa","bb",2},
{"cc_dd","cc","dd",1},
{"ee_ff","ee","ff",0}
};
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:";
while (1)
{
/* code */
char ch;
cin >> ch;
switch (ch)
{
case 'a':
for (int i = 0; i < 3; i++)
{
cout << B[i].fullname <<endl;
}
break;
case 'b':
for (int i = 0; i < 3; i++)
{
cout << B[i].title <<endl;
}
break;
case 'c':
for (int i = 0; i < 3; i++)
{
cout << B[i].bopname <<endl;
}
break;
case 'd':
for (int i = 0; i < 3; i++)
{
if (B[i].preference == 0)
cout << B[i].fullname <<endl;
else if (B[i].preference == 1)
{
cout << B[i].title <<endl;
}
else if (B[i].preference == 2)
{
cout << B[i].bopname <<endl;
}
}
break;
case 'q':
cout << "Bye!" <<endl;
break;
default:
break;
}
if (ch == 'q')
break;
else
cout << "Next choice:";
}
return 0;
}
5.cpp
#include<iostream>
using namespace std;
int main()
{
double money;
double tax = 0;
cout << "请输入收入:";
while (cin >> money)
{
if (money < 0)
break;
if (money <= 5000)
{
cout << "税收:" << tax <<endl;
}
else if ((money > 5000) && (money <= 15000))
{
tax = (money - 5000)*0.1;
cout << "税收:" << tax <<endl;
}
else if ((money > 15000) && (money <= 35000))
{
tax = 10000*0.1 + (money - 15000)*0.15;
cout << "税收:" << tax <<endl;
}
else if ((money > 35000))
{
tax = 10000*0.1 + 20000*0.15 + (money - 35000)*0.2;
cout << "税收:" << tax <<endl;
}
cout << "请输入收入:";
/* code */
}
return 0;
}
6.cpp
#include<iostream>
#include<string>
using namespace std;
struct donor
{
string name;
double money;
/* data */
};
int main()
{
int number;
cout << "请输入捐献者数目:";
cin >> number;
cin.get();
donor* d = new donor[number];
for (int i = 0; i < number; i++)
{
cout << "请输入第" << i+1 << "个捐献者的姓名:";
getline(cin,d[i].name);
cout << "请输入第" << i+1 << "个捐献者的款项:";
cin >> d[i].money;
cin.get();
}
int sign_grand = 0;
int sign = 0;
for (int i = 0; i < number; i++)
{
if (d[i].money > 10000)
{
cout << "Grand Partons " << d[i].name << " " << d[i].money <<endl;
sign_grand = 1;
}
}
if (sign_grand == 0)
cout << "Grand Partons none" <<endl;
for (int i = 0; i < number; i++)
{
if (d[i].money <= 10000)
{
cout << "Partons " << d[i].name << " " << d[i].money <<endl;
sign = 1;
}
}
if (sign == 0)
cout << "Partons none" <<endl;
return 0;
}
7.cpp
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;
/* 元音 a e i o u*/
int main()
{
cout << "Enter words (q to quit)";
const int Arsize = 20;
char word[Arsize];
cin >> word;
int count_v = 0;
int count_c = 0;
int count = 0;
while (strcmp(word,"q"))
{
if (isalpha(word[0]))
{
if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
count_v++;
else
count_c++;
}
else
count++;
cin >> word;
}
cout << count_v << " words beginning with vowels" <<endl;
cout << count_c << " words beginning with consonants" <<endl;
cout << count << " others" <<endl;
return 0;
}
8.cpp
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("8.txt");
char ch;
int count = 0;
while (inFile >> ch)
{
count++;
}
cout << count;
inFile.close();
return 0;
}
9.cpp
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct donor
{
string name;
double money;
/* data */
};
int main()
{
int number;
ifstream inFile;
inFile.open("9.txt");
inFile >> number;
inFile.get();
donor* d = new donor[number];
for (int i = 0; i < number; i++)
{
getline(inFile,d[i].name);
inFile >> d[i].money;
inFile.get();
}
inFile.close();
int sign_grand = 0;
int sign = 0;
for (int i = 0; i < number; i++)
{
if (d[i].money > 10000)
{
cout << "Grand Partons " << d[i].name << " " << d[i].money <<endl;
sign_grand = 1;
}
}
if (sign_grand == 0)
cout << "Grand Partons none" <<endl;
for (int i = 0; i < number; i++)
{
if (d[i].money <= 10000)
{
cout << "Partons " << d[i].name << " " << d[i].money <<endl;
sign = 1;
}
}
if (sign == 0)
cout << "Partons none" <<endl;
return 0;
}