分支语句
#include<iostream>
using namespace std;
/*
int main() {
char ch;
int spaces = 0;
int total = 0;
cin.get(ch);
while (ch != '.') {
if (ch == ' ') {
spaces++;
}
total++;
cin.get(ch);
//联系5.8练习题
//循环内继续读取输入以判定结束标志
}
cout << spaces << " spaces, " << total;
cout << " characters total in sentence\n";
return 0;
}
*/
/*
//if-else结构
int main() {
char ch;
cout << "Type, and I shall repeat.\n";
cin.get(ch);
while (ch != '.') {
if (ch == '\n') {
cout << ch;
}
else {
//cout << ++ch;
cout << ch + 1;
//ch+1结果被提升为int型
}
cin.get(ch);
}
cout << "\nPlease excuse the slight confusion.\n";
return 0;
}*/
//else之后也可以是语句块
//也可以继续添加if-else
//整个结构被视为一条语句
/*
const int Fave = 27;
int main() {
int n;
cout << "Enter a number in the range 1-100 to find ";
cout << "my favorite number: ";
do {
cin >> n;
if (n < Fave) {
cout << "Too low _____ guess again: ";
}
else if (n > Fave) {
cout << "Too high _____ guess again: ";
}
else {
cout << Fave << " is right!";
}
} while (n != Fave);
return 0;
}*/
//switch
/*void showmenu();
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";
return 0;
}
void showmenu() {
cout << "Please enter 1, 2, 3, 4, or 5:\n1)alarm 2)report\n3)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";
}*/
//switch case接受的类型必须是整数常量,int\char,或枚举量
//break表示终止循环,没有break将会继续检测
/*enum { red, orange, yellow, green, blue, violet, indigo };
int main() {
cout << "Enter color code(0-6): ";
int code;
cin >> code;
while (code >= red && code <= indigo) {
switch (code) {
case red:cout << "Her lios 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";
return 0;
}*/
//当选项全为常量且不少于3个时,switch效率高于ifelse
//数字循环
/*const int Max = 5;
int main() {
int golf[Max];
cout << "Please enter your golf scores.\n";
cout << "You must enter " << Max << " rounds.\n";
int i;
for (i = 0; i < Max; i++) {
cout << "round #" << i + 1 << ": ";
while (!(cin >> golf[i])) {
cin.clear();
while (cin.get() != '\n') {
continue;
//会回跳至整个循环的开头
//而非上一级
}
cout << "Please enter a number: ";
}
}
double total = 0.0;
for (i = 0; i < Max; i++) {
total += golf[i];
}
cout << total / Max << " = average score " << Max << " rounds\n";
return 0;
}*/
初识文件写入读取
//文件输入输出
//与iostream不同,文建输出必须声明自己的ofstream对象(cout)
#include<fstream>
/*int main() {
char automobile[50];
int year;
double a_price;
double b_price;
ofstream outFile;
outFile.open("carinfo.txt");
//如果没有,会自动新建
//如果存在,会清空原内容
cout << "Enter the make and model of automobile: ";
cin.getline(automobile, 50);
cout << "Enter the model year: ";
cin >> year;
cout << "Enter the original asking price: ";
cin >> a_price;
b_price = 0.913*a_price;
cout << fixed;
//用一般的方式输出浮点数,而不是科学计数法
cout.precision(2);
//数据精度
cout.setf(ios_base::showpoint);
//显示浮点数小数点后面的零
cout << "Make and model: " << automobile << endl;
cout << "Year: " << year << endl;
cout << "Was asking $" << a_price << endl;
cout << "Now asking $" << b_price << endl;
//cout可替换为outFile
//outFile<<fixed;
outFile.close();
return 0;
}*/
//读取文件ifstream
#include<fstream>
#include<cstdlib>
const int SIZE = 60;
int main() {
char filename[SIZE];
ifstream inFile;
cout << "Enter the name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open()) {
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
inFile >> value;
while (inFile.good()) {
++count;
sum += value;
inFile >> value;
}
if (inFile.eof()) {
cout << "End of file reached.\n";
}
else if (inFile.fail()) {
cout << "Input terminated by data mismatch.\n";
}
else {
cout << "Items read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close();
return 0;
}
逻辑运算符
#include<iostream>
using namespace std;
//or
/*
int main() {
cout << "This program may re format your hard disk\n"
"and destroy all your data.\n"
"Do you still wish to continue?<y/n>";
char ch;
cin.get(ch);
if (ch == 'y' || ch == 'Y') {
cout << "You were warned!\a\a\n";
}
else if (ch == 'n' || ch == 'N') {
cout << "Good dog.\n";
}
else {
cout << "That's wasn't a y or n!Apparently you "
"can't follw\ninstructions,so "
"I'll trash your disk anyway\a\a\a\n";
}
return 0;
}*/
//and
/*const int ArSize = 6;
int main() {
float naaq[ArSize];
cout << "Enter the NAAQs(New Age Awareness Quotients) "
"of\nyour neighbors Program terminates "
"when you make\n" << ArSize << " enteries "
"or enter a negative value.\n";
int i = 0;
float temp;
cout << "First value: ";
cin >> temp;
while (i < ArSize&&temp >= 0) {
naaq[i] = temp;
++i;
if (i < ArSize) {
cout << "Next value: ";
cin >> temp;
}
}
if (i == 0) {
cout << "No data - -bye\n";
}
else {
cout << "Enter your NAAQ: ";
float you;
cin >> you;
int count = 0;
for (int j = 0; j < i; j++) {
if (naaq[j] > you) {
++count;
}
}
cout << count << " of your neighbors have greater awareness of\n"
"the New Age than you do.\n";
}
1 return 0;
}*/
/*int main() {
int a, b;
cout << "Enter 2 integers: ";
cin >> a>>b;
cout << "The larger of " << a << " and " << b;
int c = a > b ? a : b;
等效
int c;
if(a>b){c=a;}
else{c=b;}
cout << " is " << c << endl;
return 0;
}*/