6.9 condit.cpp
#include <iostream>
int main()
{
using namespace std;
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The larger of " << a << " and " << b;
int c = a > b ? a : b; // c = a if a > b, else c = b
cout << " is " << c << endl;
// cin.get();
// cin.get();
return 0;
}
- 该代码展示了三目运算符的使用
-
首先是代码cin >> a >> b,输入ab两个值,先a后b
- a>b?a:b 判断a是否大于b,如果a大于b 则把a的值赋给c,如果a小于b 则把b的值赋给c
6.10 switch.cpp
// switch.cpp -- using the switch statement
#include <iostream>
using namespace std;
void showmenu(); // function prototypes
void report();
void comfort();
int main()
{
showmenu();
int choice;
cin >> choice;
while (choice != 5)
{
switch(choice)
{
case 1 : cout << "\a\n";
break;
case 2 : report();
break;
case 3 : cout << "The boss was in all day.\n";
break;
case 4 : comfort();
break;
default : cout << "That's not a choice.\n";
}
showmenu();
cin >> choice;
}
cout << "Bye!\n";
// cin.get();
// cin.get();
return 0;
}
void showmenu()
{
cout << "Please enter 1, 2, 3, 4, or 5:\n"
"1) alarm 2) report\n"
"3) alibi 4) comfort\n"
"5) quit\n";
}
void report()
{
cout << "It's been an excellent week for business.\n"
"Sales are up 120%. Expenses are down 35%.\n";
}
void comfort()
{
cout << "Your employees think you are the finest CEO\n"
"in the industry. The board of directors think\n"
"you are the finest CEO in the industry.\n";
}
- 该代码讲解了c++中switch的使用方法,与c语言相同,用于从列表中进行选择
- 首先是while循环起手,输入5则结束,输入1-4则进入不同的case
- 其他的值则直接default
6.11 enum.cpp
#include <iostream>
// create named constants for 0 - 6
enum {red, orange, yellow, green, blue, violet, indigo};
int main()
{
using namespace std;
cout << "Enter color code (0-6): ";
int code;
cin >> code;
while (code >= red && code <= indigo)
{
switch (code)
{
case red : cout << "Her lips were red.\n"; break;
case orange : cout << "Her hair was orange.\n"; break;
case yellow : cout << "Her shoes were yellow.\n"; break;
case green : cout << "Her nails were green.\n"; break;
case blue : cout << "Her sweatsuit was blue.\n"; break;
case violet : cout << "Her eyes were violet.\n"; break;
case indigo : cout << "Her mood was indigo.\n"; break;
}
cout << "Enter color code (0-6): ";
cin >> code;
}
cout << "Bye\n";
// cin.get();
// cin.get();
return 0;
}
- 该代码展现了将枚举量作为标签
- 首先使用enum定义一组相关的常量,然后在switch语句中使用这些常量
- 输入一个整数,在while循环测试条件中,将枚举量提升为int类型
- switch中将int值和枚举量进行比较时,也将枚举量提升为int
6.12 jump.cpp
#include <iostream>
const int ArSize = 80;
int main()
{
using namespace std;
char line[ArSize];
int spaces = 0;
cout << "Enter a line of text:\n";
cin.get(line, ArSize);
cout << "Complete line:\n" << line << endl;
cout << "Line through first period:\n";
for (int i = 0; line[i] != '\0'; i++)
{
cout << line[i]; // display character
if (line[i] == '.') // quit if it's a period
break;
if (line[i] != ' ') // skip rest of loop
continue;
spaces++;
}
cout << "\n" << spaces << " spaces\n";
cout << "Done.\n";
// cin.get();
// cin.get();
return 0;
}
- 该代码展示了continue和break的异同
- 首先cin.get(name,ArSize)也是面向行的输入,该函数不是丢弃换行符,而是将其留在输入队列中
- i为.时,break跳出for循环
- 当i为 时,continue继续进行下一个for循环