1,cin的用法
char ch;
cin.get(ch); //僅僅接受一個字元(輸入12,則ch僅僅賦值為1)
cout<<ch;
2,對於字元型變數ch,++ch與ch+1的區別
<strong>int main()<br />{<br />char ch;<br />cin.get(ch);<br />while(ch!='.')<br />{<br />if(ch=='\n')<br />cout<<ch;<br />else<br />cout<<ch+1;<br />cin.get(ch);<br />}<br />}</strong>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中,並在末尾加上一個Null 字元。
cin.getline(word,50);//cin不斷讀取,直到遇到分行符號。讀取38.5 19.2
10,使用檔案輸出的步驟
1)包含標頭檔fstream
2)建立一個ofstream
3)將該ofstream對象仝一個檔案關聯起來。
4)就像使用cout一樣使用ofstream
原始碼:
#include <iostream><br />#include <fstream><br />int main()<br />{<br />using namespace std;</p><p>char automobile[50];<br />int year;<br />double a_price;<br />double d_price;</p><p>ofstream outFile;<br />outFile.open("tianshuai.txt");</p><p>cout<<"Enter the make and model of automobile:";<br />cin.getline(automobile,50);<br />cout<<"Enter the model year:";<br />cin>>year;<br />cout<<"Enter the original asking price:";<br />cin>>a_price;<br />d_price=09.13*a_price;</p><p>cout<<fixed;//輸出浮點數形式輸出變數<br />cout.precision(2);//設定精度 為小數點後兩位<br />cout.setf(ios_base::showpoint);//強制顯示 小數點<br />cout<<"Make and model:"<<automobile<<endl;<br />cout<<"Year:"<<year<<endl;<br />cout<<"Was asking {1}quot;<<a_price<<endl;<br />cout<<"Now asking {1}quot;<<d_price<<endl;</p><p>outFile<<fixed;<br />outFile.precision(2);<br />outFile.setf(ios_base::showpoint);<br />outFile<<"Make and model:"<<automobile<<endl;<br />outFile<<"Year:"<<year<<endl;<br />outFile<<"Was asking {1}quot;<<a_price<<endl;<br />outFile<<"Now asking {1}quot;<<d_price<<endl;</p><p>outFile.close();//程式使用完檔案後關閉<br />return 0;<br />}
11,讀取文字檔
#include <iostream><br />#include <fstream><br />#include <cstdlib><br />const int SIZE=60;<br />int main()<br />{<br />using namespace std;<br />char filename[SIZE];<br />ifstream inFile;</p><p>cout<<"Enter name of data file:";<br />cin.getline(filename,SIZE);<br />inFile.open(filename);//開啟檔案<br />if(!inFile.is_open())//如果打不開檔案<br />{<br />cout<<"Could not open the file "<<filename<<endl;<br />cout<<"Program terminating.\n";<br />exit(EXIT_FAILURE);<br />}<br />double value;<br />double sum=0.0;<br />int count=0;</p><p>inFile>>value;//讀取檔案中數值<br />while(inFile.good())<br />{<br />++count; //數值個數<br />sum+=value;//求數值總和<br />inFile>>value;//繼續讀取數值<br />}<br />if(inFile.eof())//檔案結尾<br /> cout<<"End of file reached.\n";<br /> else if(inFile.fail())<br /> cout<<"Input terminated by data mismatch.\n";<br /> else<br /> cout<<"Input termiated for unknown reason.\n";<br /> if(count==0)<br /> cout<<"No data processed .\n";<br /> else<br /> {<br /> cout<<"Items read:"<<count<<endl;<br /> cout<<"Sum:"<<sum<<endl;<br /> cout<<"Average:"<<sum/count<<endl;<br /> }</p><p>inFile.close();</p><p>return 0;<br />} 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等,程式會報錯。
所以採用字元比較好。