一.if 语句
二.逻辑表达式
1.逻辑OR运算符:||
||的优先级比关系运算符低。
2.逻辑AND运算符:&&
3.逻辑NOT运算符:!
逻辑AND运算符优先级高于逻辑OR运算符。
布尔函数is_int()使用climits文件中定义的两个符号常量INT _MAX和INT _MIN来确定参数是否位于适当的范围内。如果是,该函数返回true,反之返回false。
#include<iostream>
using namespace std;
const Asize=7;
int main()
{
float naaq[Asize];
int temp;
int i=0;
cout<<"Enter the NAAQ!!!"<<endl;
cout<<"First value: ";
cin>>temp;
while(i<Asize && temp>0)
{
naaq[i]=temp;
++i;
if(i<Asize)
{
cout<<"Next value: ";
cin>>temp;
}
}
if(i == 0)
cout<<"No your NAAQ!"<<endl;
else
{
cout<<"Enter you NAAQ: ";
float you;
cin>>you;
int count=0;
for(int j=0; j<i; ++j)
if(naaq[j]>you)
++count;
cout<<count<<" of neighbors beeter you"<<endl;
}
return 0;
}
三.字符库函数
函数原型在头文件cctype中定义。
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
cout<<"Enter text for analysis, and type @ to terminate input."<<endl;
char ch;
int whitespace=0; //空白
int chars=0; //字母
int punct=0; //标点符号
int digits=0; //数字
int others=0; //其他
cin.get(ch);
while(ch != '@')
{
if(isalpha(ch))
++chars;
else if(isdigit(ch))
++digits;
else if(isspace(ch))
++whitespace;
else if(ispunct(ch))
++punct;
else
++others;
cin.get(ch);
}
cout<<chars<<" letters, "<<whitespace<<" space, "
<<punct<<" punct, \n"<<digits<<" digits, "
<<"and "<<others<<" others!!!"<<endl;
return 0;
}
四. ?:运算符
?:运算符常用来代替if else语句。C++中唯一一个需要三个操作数的运算符。
expression1 ? expression2 : expression3
五.switch语句
。。。
将枚举量用作标签: 通常cin无法识别枚举类型(不知其程序员如何定义它),因此程序要求选择选项是输入一个整数。当switch语句将int值和枚举量的标签进行比较时,将枚举量提升为int。(在while循环测试中也会提升为int类型)。
六.break和continue语句
C++中也有goto语句。下面的语句将调到使用Paris作为标签的的位置:
goto Paris;
char ch;
cin >> ch;
if(ch == 'q')
goto Paris;
cout<<...
...
Paris: cout<<"OK"<<ebdl;
七.读取数字的循环
假设编写一个将一系列数字读入到数组序列中去,并允许在用户在数组填满之前结束输入。一种是利用cin:
int n;
cin>>n;
但是用户输入一个单词呢,不是数字,将放生4种情况:
* n的值保持不变;
* 不匹配的输入将被保留在输入队列中;
* cin对象中的一个错误标记被设置;
* 对cin方法的调用将返回false(如果被转换为bool类型)(意味着可以用非数字的输入来结束循环)
非数字输入设置错误标记意味着必须重置该标记,程序才能继续输入。clear()方法重置错误输入标记,同时重置文件尾。
#include<iostream>
const int Max=5;
using namespace std;
int main()
{
double fish[Max];
cout<<"Please enter the weight of your fish.\n"
<<"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<<": ";
//cout<<fish[i-i];
}
double num=0;
for(int j=0; j<i; ++j) //caculate average
num+=fish[j];
if(i==0)
cout<<"No fish"<<endl;
else
cout<<num/i<<" is fishs average weight"<<endl;
cout<<"OK!!!"<<endl;
return 0;
}
在上面程序中表达式cin>>fish[i]实际上是一个cin方法函数调用,该函数返回cin。但如果cin位于测试条件中,则被转换为bool类型(true或false)。
但是如果要求如果用户输入非数字,程序将拒绝,并要求用户继续输入数字。则可以使用cin输入表达式的值来检测输入是不是数字。分为三个步骤:
1.重置cin以接受新的输入。
2.删除错误输入。
3.提示用户在输入。
#include<iostream>
using namespace std;
const int Max=7;
int main()
{
int golf[Max];
cout<<"Please enter your golf scores.\n";
cout<<"You must enter "<<Max<<" round.\n"<<endl;
//get data
int i;
for(i=0; i<Max; ++i)
{
cout<<"round #"<<i+1<<": ";
while(!(cin>>golf[i]))
{
cin.clear(); //rest input
while(cin.get() != '\n')
continue; //get rid of bad input
cout<<"Please enter a number: ";
}
}
//calculate average
double total=0.0;
for(i=0; i<Max; ++i)
total+=golf[i];
//report results
cout<<total/Max<<" is the average score "<<Max<<" rounds"<<endl;
return 0;
}
八.简单文件输入/输出
1.文本I/O的概念:使用cin输入时,程序将输入视为一系列的字节,其中每个字节都被解释为字符编码。无论目标数据类型是什么,输入一开始都是字符数据——文本数据。然后。cin对象负责将文本转换为其他类型。
(IDE中的源代码编译器生成的也是文本文件,事实上,源代码文件就属于文本文件)
2.写入到文本文件中去
对于文件输入,C++使用类似于cout的东西,以下为有关于将cout用于控制台输出的基本事实,为文件输出做准备。
* 必须包含头文件iostream。
* 头文件iostream定义了一个用于处理输出的ostream类。
* 头文件iostream声明了一个名为cout的ostream变量(对象)。
* 必须指明名称空间std.
* 可以结合使用cout和运算符<<来显示各种类型数据。
文件的输出与以上所述极为相似。
* 必须包含头文件fstream。
* 头文件iostream定义了一个用于处理输出的ofstream类。
* 需要声明一个或多个ofstream变量(对象),并以自己喜欢的方式对其命名。但要遵守命名规则。
* 必须指明名称空间std。
* 需要将ofstream对象与文件关联起来,方法之一为使用open()方法。
* 使用完文件后,应使用方法close()将其关闭。
* 可以结合使用cout和运算符<<来显示各种类型数据。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int year;
char autombile[50];
double a_price;
double b_price;
ofstream outFile; //creat object for output;
outFile.open("carinfor.txt"); //associate with a file
cout<<"Pleasse enter the make and of autombile: ";
cin.getline(autombile,50);
cout<<"Enter the model year: ";
cin>>year;
cout<<"Enter the original asking price: ";
cin>>a_price;
b_price = 0.913 * a_price;
//display information on screen with cout
cout<<fixed; //普通方式输出浮点数
cout.precision(2); //设置精确度为2,并返回上一层
cout.setf(ios_base::showpoint); //西安市浮点数后面的0
cout<<"Make and model: "<<autombile<<endl;
cout<<"Year: "<<year<<endl;
cout<<"Was asking $: "<<a_price<<endl;
cout<<"Now asking $: "<<b_price<<endl;
//now do exact same thing using outFile instead of cout
outFile<<fixed; //普通方式输出浮点数
outFile.precision(2); //设置精确度为2,并返回上一层
outFile.setf(ios_base::showpoint); //西安市浮点数后面的0
outFile<<"Make and model: "<<autombile<<endl;
outFile<<"Year: "<<year<<endl;
outFile<<"Was asking $: "<<a_price<<endl;
outFile<<"Now asking $: "<<b_price<<endl;
outFile.close(); //done with file
return 0;
}
注意:在运行程序时,如果文件carinfo.txt存在,此时情况如何呢?默认情况下,open()将首先截断该文件,即将其长度截短到零——丢弃原有内容,然后将新的输出加入到文件中。
3.读取文本文件
* 必须包含头文件iostream。
* 头文件iostream定义了一个用于处理输出的istream类。
* 头文件iostream声明了一个名为cin的istream变量(对象)。
* 必须指明名称空间std.
* 可以结合使用cin和运算符>>来读取各种类型数据。
* 可以使用cin和get()方法来读取一个字符,使用cin和getline()来读取一行字符。
* 可以结合使用cin和eof()、fail()方法来判断输入是否成功。
文件的输出与以上所述极为相似。
* 必须包含头文件fstream。
* 头文件iostream定义了一个用于处理输出的ifstream类。
* 需要声明一个或多个ifstream变量(对象),并以自己喜欢的方式对其命名。但要 遵守命名规则。
* 必须指明名称空间std。
* 需要将ifstream对象与文件关联起来,方法之一为使用open()方法。
* 使用完文件后,应使用方法close()将其关闭。
* 可以结合使用ifstream和运算符>>来读取各种类型数据。
* 可以使用ifstream和get()方法来读取一个字符,使用ifstream和getline()来读取一行字符。
* 可以结合使用ifstream对象和eof()、fail()等方法来判断输入是否成功。
如果试图打开一个不存在的文件用于输入,这种错误会导致后面使用ifstream对象进行输入时失败。检查文件是否被成功的打开首先方法是使用is_open,可以使用类似于下面的代码:
inFile.open("blowing.txt");
if(!inFile.is_open())
{
exit(EXIT_FAILURE);
}
如果文件被成功打开,方法is_ open()将返回true;如果没有被打开,表达式 !inFile.is_open()将为true。
函数exit()原型是在头文件cstdlib中定义的,在该头文件中,还定义了一个用于和操作系统通信的参数EXIT_FAILURE。函数exit()终止程序。
方法is_open().也可用较老的good()来代替。
#include<iostream>
#include<fstream> //file I/O support
#include<cstdlib>
const int SIZE = 60;
using namespace std;
int main()
{
char filename[SIZE];
ifstream inFile;
cout<<"Enter name of data file: ";
cin.getline(filename,60);
inFile.open(filename); //associate inFile with a file
if(!inFile.is_open()) //failed to open file
{
cout<<"Could not open the file "<<filename<<endl;
cout<<"Program terminating.\n";
exit(EXIT_FAILURE); //函数exit()终止程序
}
double value;
double sum=0.0;
int count = 0;
/* inFile>>value;
while(inFile.good()) //while input good and not at EOF
{
++count; //one more item read
sum+=value; //calculate running total
inFile>>value; //get next value
}*/
//以上测试条件可以简化为:
while(inFile>>value)
{
++count;
sum+=value;
}
if(inFile.eof())
cout<<"End of file reached.\n";
else if(inFile.fail())
cout<<"Input terminated by data mismatch.\n";
else
cout<<"Input terminated for unknown reason.\n";
if(count==0)
cout<<"No data processed.\n";
else
{
cout<<"Items read: "<<count<<"\nSum: "
<<sum<<"\nAverage: "<<sum/count<<endl;
}
inFile.close(); //finished with the file
return 0;
}
在读取文件时,有几点需要检查:
<1>程序读取文件时不应超过EOF。如果最后一次读取数据时遇到EOF,方法eof()将返回true。
<2>程序可能遇到类型不匹配,方法fail()将返回true(但如果遇到EOF,该方法也将返回true)。
<3>可能出现意外情况,如文件受损或硬件故障,如果最后一次读取文件时发生了这样的问题,方法bad()将返回true。
以上三种情况使用方法good()发生任何错误时都返回true。