if语句——if和if else
#include <iostream>
using namespace std;
int main(void)
{
int spaces = 0;
int total = 0;
char ch;
cin.get(ch);
while(ch != '.')
{
if(ch == ' ')
++spaces;
++total;
cin.get(ch);
}
cout << "spaces = " << spaces << endl;
cout << "total characters = " << total << endl;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
char ch;
cin.get(ch);
while(ch != '.')
{
if(ch == '\n')
cout << ch;
else
cout << ++ch; //cout << ch + 1
cin.get(ch);
}
cout << endl;
cout << "Please excuse the slight confusion." << endl;
return 0;
}
if_else if_else (注意ch++和ch+1的区别,后者会被整型提升)
#include <iostream>
using namespace std;
const int Fave = 27;
int main(void)
{
int n;
cout << "Enter a number in the range 1~100 to find your 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 the right number." << endl;
}while(n != Fave);
return 0;
}
逻辑表达式
或运算优先级低于关系运算符 ||运算符是个顺序点 先左后右 如果左侧表达式为真则右侧表达式不会被执行
#include <iostream>
using namespace std;
int main(void)
{
cout << "This program may reformat your hard disk, and destory all your data." << endl;
cout << "Do you wish to contunue? <y/n>" << endl;
char ch;
cin >> ch;
if(ch == 'y' || ch == 'Y')
cout << "You were wanrned!" << endl;
else if(ch == 'n' || ch == 'N')
cout << "A wise choice ...." << endl;
else
cout << "That's wasn't a y or n!, Can't follow your instruction!" << endl;
return 0;
}
与运算优先级低于关系运算符 &&运算符是个顺序点 先左后右 如果左侧表达式为假则右侧表达式不会被执行
#include <iostream>
using namespace std;
const int ArSize = 6;
int main(void)
{
float value[ArSize];
float temp;
int i = 0;
cout << "Enter six numbers and compare with your level." << endl;
cout << "Terminate condition, when you make 6 number or enter a nagative number" << endl;
cout << "First value:";
cin >> temp;
while(i < ArSize && temp >= 0)
{
value[i] = temp;
++i;
if(i < ArSize)
{
cout << "Next value:";
cin >> temp;
}
}
if(i == 0)
cout << "No data--byte." << endl;
else
{
cout << "Enter your level:";
float level;
cin >> level;
int count = 0;
for(int j = 0; j < i; j++)
{
if(value[j] > level)
count++;
}
cout << count << " numbers are bigger than your level" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
const char *qualify[5] =
{
"Perfect",
"Great",
"Good",
"Just so so",
"failed"
};
int main(void)
{
int score, index;
cout << "Enter your score:";
cin >> score;
if(score >= 90 && score <= 100)
index = 0;
else if(score >= 80 && score < 90)
index = 1;
else if(score >= 70 && score < 80)
index = 2;
else if(score >= 60 && score < 70)
index = 3;
else
index = 4;
cout << "You qualify: " << qualify[index] << endl;
return 0;
}
逻辑 !运算 !运算符优先级高于所有关系运算符和算数运算符
#include <iostream>
#include <climits>
using namespace std;
bool is_int(double x);
int main(void)
{
double number;
cout << "Enter an integer value:";
cin >> number;
while(!is_int(number))
{
cout << "Out of range, please enter again:";
cin >> number;
}
int value = (int)number;
cout << "You've entered an integer: " << value << endl;
return 0;
}
bool is_int(double x)
{
if(x <= INT_MAX && x >= INT_MIN)
return true;
else
return false;
}
ctype库函数
#include <iostream>
#include <cctype>
using namespace std;
int main(void)
{
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;
char ch;
cout << "Enter text for analysis, and type @ to terminate the input." << endl;
cin.get(ch);
while(ch != '@')
{
if(isalpha(ch))
chars++;
else if(isspace(ch))
whitespace++;
else if(isdigit(ch))
digits++;
else if(ispunct(ch))
punct++;
else
others++;
cin.get(ch);
}
cout << chars << " letters, " << whitespace << " whitespaces, " << digits << " digits, " << punct << " punctuations, " << others << " others." << endl;
return 0;
}
?:运算符
#include <iostream>
using namespace std;
int main(void)
{
int a, b;
int c;
cout << "Enter two ingtegers:";
cin >> a >> b;
c = a > b ? a : b;
cout << "The larger of " << a << " and " << b << " is " << c << endl;
return 0;
}
switch语句
#include <iostream>
using namespace std;
void showmenu(void);
void report(void);
void comfort(void);
int main(void)
{
showmenu();
int choice;
cin >> choice;
while(choice != 5)
{
switch(choice)
{
case 1: cout << "Warning!!!" << endl;
break;
case 2: report();
break;
case 3: cout << "The boss was in all day." << endl;
break;
case 4: comfort();
break;
default: cout << "That is not a choice." << endl;
}
showmenu();
cin >> choice;
}
return 0;
}
void showmenu(void)
{
cout << "Please enter 1, 2, 3, 4, 5 as your choice:" << endl;
cout << "1) alarm 2) report" << endl;
cout << "3) alibi 4) comfort" << endl;
cout << "5) quit" << endl;
}
void report(void)
{
cout << "Sales are up 120%. Expenses are down 35%." << endl;
}
void comfort(void)
{
cout << "You are the best CEO!" << endl;
}
用枚举量作为标签
#include <iostream>
using namespace std;
enum {red, orange, yellow, green, blue, violet, indigo};
int main(void)
{
int code;
cout << "Enter color code (0~6):";
cin >> code;
while(code >= red && code <= indigo)
{
switch(code)
{
case red: cout << "You choose red!"<< endl; break;
case orange: cout << "You choose orange!" << endl; break;
case yellow: cout << "You choose yellow!" << endl; break;
case green: cout << "You choose green!" << endl; break;
case blue: cout << "You choose blue!" << endl; break;
case violet: cout << "You choose violet!" << endl; break;
case indigo: cout << "You choose indigo!" << endl; break;
}
cout << "Enter color code (0~6):";
cin >> code;
}
cout << "Bye!" << endl;
return 0;
}
break和continue
#include <iostream>
using namespace std;
const int ArSize = 80;
int main(void)
{
char line[ArSize];
int spaces = 0;
cout << "Enter a line of text:" << endl;
cin.get(line, ArSize);
cout << "Complete line:" << line << endl;
for(int i = 0; line[i] != '\0'; i++)
{
cout << line[i];
if(line[i] == '.')
break;
if(line[i] != ' ')
continue;
spaces++;
}
cout << endl;
cout << "Spaces = " << spaces << endl;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
int num1, num2;
cout << "First number:";
cin >> num1;
cin.clear(); //reset input
while(cin.get() != '\n'); //get rid of bad input
cout << "Last number:";
cin >> num2;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
return 0;
}
#include <iostream>
using namespace std;
const int Max = 5;
int main(void)
{
double fish[Max];
cout << "Please enter the weight of your fish:" << endl;
cout << "You may enter up to " << Max << " fish <q> to terminate." << endl;
cout << "fish #1:";
int i = 0;
while(i < Max && cin >> fish[i])
{
if(++i < Max)
cout << "fish #" << i+1 << ":";
}
double total = 0.0;
for(int j = 0; j < i; j++)
total += fish[j];
cout << "total = " << total << endl;
if(i == 0)
cout << "No fish" << endl;
else
cout << "Average weight of " << i << " fish: " << total / i << endl;
return 0;
}
#include <iostream>
using namespace std;
const int Max = 5;
int main(void)
{
int golf[Max];
cout << "Please enter your golf scores." << endl;
cout << "You must enter " << Max << " rounds." << endl;
int i;
for(i = 0; i < Max; i++)
{
cout << "Round #" << i + 1 << ": " << endl;
while(!(cin >> golf[i]))
{
cin.clear();
while(cin.get() != '\n');
cout << "Please enter a number:";
}
}
double total = 0.0;
for(i = 0; i < Max; i++)
total += golf[i];
cout << "Average score: " << total / Max << "." << endl;
return 0;
}
简单文件输入输出
写文本
#include <iostream> //ostream->cout istream->cin
#include <fstream> //1. include <fstream> //ofstream ifstream
using namespace std;
int main(void)
{
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile; //2.create object of output file stream(ofstream)
outFile.open("carinfo.txt"); //3.associate with a file
cout << "Enter the make and model of automobile:";
cin.getline(automobile, 50);
cout << "Enter the model year:";
cin >> year;
cout << "Enter the orignal asking price:";
cin >> a_price;
d_price = a_price * 0.913;
//show on screen
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: " << d_price << endl;
//write to carinfo.txt
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "Make and model: " << automobile << endl;
outFile << "Year: " << year << endl;
outFile << "Was asking: " << a_price << endl;
outFile << "Now asking: " << d_price << endl;
outFile.close();
return 0;
}
读文本
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 60;
int main(void)
{
char filename[SIZE];
ifstream inFile;
cout << "Enter 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." << endl;
exit(EXIT_FAILURE);
}
cout << "Suceess open the txt file." << endl;
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." << endl;
else if(inFile.fail())
cout << "Input terminated by data mismachted." << endl;
else
cout << "Input terminated by unkonw reason." << endl;
if(count == 0)
cout << "No data processed." << endl;
else
{
cout << "Items read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close();
return 0;
}