1,cin的用法
char ch;
cin.get(ch); //仅仅接受一个字符(输入12,则ch仅仅赋值为1)
cout<<ch;
2,对于字符型变量ch,++ch与ch+1的区别
int main()
{
char ch;
cin.get(ch);
while(ch!='.')
{
if(ch=='\n')
cout<<ch;
else
cout<<ch+1;
cin.get(ch);
}
}
cout<<++ch;输出的是 相应ASCCI码加一之后的 字符cout<<ch+1;输出的是 相应ASCCI码加一之后的 ASCCI码
3,if条件判断句的巧妙写法
if(i==0)写法时候,往往因为忘记一个“=”而令代码出现难以查证的逻辑错误。
if(0==i)写法,当忘记一个"="的时候,代码编译时候会报错,所以极易查找错误来源。
4,逻辑表达式(||、&&、!)
逻辑表达式的优先级比关系表达式低,先修改左侧的值然后对右侧的值进行判定。如:i++<6||i==j
测试一个变量的范围的写法:if(age>15&&age<35)
错误写法:if(15<age<35)
分析:15<age为true时,表达式值为1 1<35恒成立
15<age为false时,表达式值为0 0<35恒成立
5,字符函数库 cctype(头文件 cctype.h)
判断是否为字母字符:if(isalpha(ch))
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
isalnum()//判断是否为字母数组
isblank()//判断是否为空格活水平制表符
isdigit()//判断是否为数字
6,? :操作符
int i=5>3?1:0;//给i赋值为1
7,break和continue语句
break语句的执行表示:跳出循环体,执行循环体以外的下一条语句。
countinue语句的执行表示:跳过本次循环,但不会跳过循环更新表达式
8,读取数字的循环
cin>>i;用来接收用户的输入。当用户发现输入错误时候应采取三个步骤
1)重置cin 以接受新的输入
2)删除错误输入cin.clear();
3)提示用户再输入
例:
cout<<请输入年龄<<endl;
int age;
while(!(cin>>age))
{
cin.clear();
cout<<请再次输入<<endl;
}
9,简单文件输入/输出
文本I/O:使用cin进行输入时,程序将输入视为一系列的字节,其中每个字节都被解释为字符编码。不管目标数据类型是什么,输入一开始都是字符数据-文本数据。然后,cin对象负责将文本转换为其他类型。
38.5 19.2
char ch;
cin>>ch;//第一个字符3 被赋值给ch,字符编码(二进制)被存储在变量ch中。输入和目标都是字符,不需要转换。
int n;
cin>>n;//cin不断读取,直到遇到非数字字符。读取38,因此将38的二进制编码复制到变量n中。
double x;
cin>>x;//cin不断读取,直到遇到第一个不属于浮点数的字符。读取38.5
char word[50];
cin>>word;//cin不断读取,直到遇到空白字符。读取38.5,然后将38.5字符编码存储到数组word中,并在末尾加上一个空字符。
cin.getline(word,50);//cin不断读取,直到遇到换行符。读取38.5 19.2
10,使用文件输出的步骤
1)包含头文件fstream
2)创建一个ofstream
3)将该ofstream对象仝一个文件关联起来。
4)就像使用cout一样使用ofstream
源代码:
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile;
outFile.open("tianshuai.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;
d_price=09.13*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 {1}quot;<<a_price<<endl;
cout<<"Now asking {1}quot;<<d_price<<endl;
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile<<"Make and model:"<<automobile<<endl;
outFile<<"Year:"<<year<<endl;
outFile<<"Was asking {1}quot;<<a_price<<endl;
outFile<<"Now asking {1}quot;<<d_price<<endl;
outFile.close();//程序使用完文件后关闭
return 0;
}
11,读取文本文件
#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE=60;
int main()
{
using namespace std;
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.\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<<"Input termiated for unknown reason.\n";
if(count==0)
cout<<"No data processed .\n";
else
{
cout<<"Items read:"<<count<<endl;
cout<<"Sum:"<<sum<<endl;
cout<<"Average:"<<sum/count<<endl;
}
inFile.close();
return 0;
}
12,非常值得注意的两个地方
char ch;
ch+1 //表示char+int =int 型
ch++ //表示char型
在 switch(char)
{ case a: break;
case b:break;
default:break;}语句中,采用字符表示菜单选项和case标签比采用数字有什么优点呢?
采用字符(a,b,c……):如果输入了数字,3、4、5等,程序会按照字符处理,而程序本身不会报错。
采用数字(1,2,3……):如果输入了字符,a、b、c等,程序会报错。
所以采用字符比较好。