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