1
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
while (cin.get(ch) && ch != '@')
{
if (!isdigit(ch))
{
if (isupper(ch))
{
ch = tolower(ch);
}
else if (islower(ch))
{
ch = toupper(ch);
}
cout << ch;
}
}
cout << endl;
system("pause");
return 0;
}
总结:
- 有条件的回显(write)字符,推荐使用cctype函数库。
2
#include <iostream>
using namespace std;
const int ArSize = 10;
int main()
{
double donation[ArSize];
int i = 0;
double aver = 0;
double total = 0;
int count = 0;
while (i < ArSize && cin >> donation[i])
{
if (!cin)
{
break;
}
i++;
}
for (int j = 0; j < i; j++)
{
total += donation[j];
}
aver = total / i;
for (int k = 0; k < i; k++)
{
if (aver < donation[k])
{
count++;
}
}
cout << "Average: " << aver << endl
<< "The amount of large: " << count << endl;
system("pause");
return 0;
}
总结:
- if语句是一种有前提条件的特殊语句。
3
#include <iostream>
using namespace std;
void show();
int main()
{
char ch;
show();
while (cin >> ch)
{
if (ch == 'c' || ch == 'p' || ch == 't' || ch == 'g')
{
switch (ch)
{
case 'c': cout << "carnivore\n"; break;
case 'p': cout << "pianist\n"; break;
case 't': cout << "A maple is a tree\n"; break;
case 'g': cout << "game\n";
}
break;
}
else
{
show();
cin.clear();
}
}
system("pause");