c++ premier Plus書之for, while迴圈, 逗號運算子, 字串比較
// for迴圈 #include "iostream" const int arSize = 16; int main() { using std::cout; using std::endl; long long factorials[arSize]; factorials[0] = factorials[1] = 1; for (int i = 2; i < arSize; i++) factorials[i] = i * factorials[i - 1]; for (int i = 0; i < arSize; i++) cout << "factorials[" << i << "] = " << factorials[i] << endl; return 0; }
執行結果
// for迴圈 // for迴圈 #include "iostream" using namespace std; const int arSize = 16; int main() { string str; cout << "enter a string : " << endl; cin >> str; for (int i = str.size() - 1; i >= 0; i--) cout << str[i]; cout << "\n end " << endl; int guests = 0; while (guests++ < 10) cout << guests << endl; return 0; }
由於guest++ < 10, 是先拿guest和10比較後, 再對guest+1, 然後才執行輸出語句, 所以執行結果如上
基於範圍的for迴圈
注意其中一個能改變prices數組裡的值, 另一個卻不能
double prices[5] = {1.11, 1.22, 1.33, 1.44, 1.55}; for (double x : prices) cout << x << endl; // 這種寫法能夠改變prices裡的數值, 上面的寫法不行 // 符號&表明x是一個引用變數 for (double &x : prices) x = x * 0.8;
再看一下下面這個輸出設計到++運算子
int x = 1;
int y = (4 + x++) + (6 * x++);
cout << x << endl;
cout << y << endl;
執行結果為
3
17
++運算子和指標混合一起使用:
++和指標運算
// for迴圈
#include "iostream"
using namespace std;
const int arSize = 16;
int main() {
double arr[5] = {1.11, 1.22, 1.33, 1.44, 1.55};
// 指標pt指向了陣列的首地址
double* pt = arr;
// 相當於pt向後移動了一個double的長度, 也就是指向了陣列的第二個元素
++pt;
cout << *pt << endl;
double x = *++pt;
// 1.33
cout << x << endl;
// 先是取出pt指標指向的地址裡的數值, 然後+1, 賦值給y
double y = ++*pt;
// 2.33
cout << y << endl;
cout << "arr[2] = " << arr[2] << endl;
cout << "before (*pt)++ , *pt = " << *pt << endl;
double z = *pt++;
cout << z << endl;
cout << *pt << endl;
return 0;
}
程式執行結果為:
1.22
1.33
2.33
arr[2] = 2.33
before (*pt)++ , *pt = 2.33
2.33
1.44
在看一個利用for迴圈做字串反轉
字串翻轉
#include "iostream"
#include "string"
using namespace std;
int main()
{
cout << "Enter a wrod : ";
string word;
cin >> word;
for (int j = 0, i = word.size() - 1; j < i; j++, i--)
{
// temp 在使用完就會回收
char temp;
temp = word[i];
word[i] = word[j];
word[j] = temp;
}
cout << word << endl;
cout << "Enter second wrod : ";
char word2[5];
cin >> word2;
// 這裡之所以寫-2 是因為陣列最後一位應該留給\0, 如果寫-1會直接列印空字串, 因為上來就是\0導致直接結束
for (int j = 0, i = sizeof(word2) - 2; j < i; j++, i--)
{
char temp;
temp = word2[i];
word2[i] = word2[j];
word2[j] = temp;
}
cout << word2 << endl;
return 0;
}
程式執行結果為:
逗號運算子花絮:
#include "iostream"
#include "string"
using namespace std;
int main()
{
int i = 20, j = 2 * i;
cout << "j = " << j << endl;
// c++規定 逗號運算子的值是第二部分的值
int x = (10, 20);
cout << "x = " << x << endl;
int y = 1;
// 注意這種寫法是不對的 int y = 10, 20;
// 逗號運算子的優先順序是最低的, 因此下面這句話被解釋為 (y = 10), 20; 所以20沒有起作用
y = 10, 20;
cout << "y = " << y << endl;
return 0;
}
程式輸出結果:
程式需要注意的都寫在註釋中
字串比較
先看c語言的字串比較
// c風格字串比較
#include "iostream"
#include "cstring"
using namespace std;
int main()
{
char word[5] = "?ate";
for (char c = 'a'; strcmp(word, "mate"); c++)
{
cout << word << endl;
word[0] = c;
}
cout << "finish word is : " << word << endl;
return 0;
}
執行結果為:
c++中使用c語言風格比較兩個字串是否相同要用strcmp函式, 記得需要匯入cstring標頭檔案
strcmp(s1, s2); s1, s2可是是陣列名也可以是指標,
如果s1和s2相同返回0,
如果s1在s2的前面返回-1,
如果s1在s2的後面返回1
c++裡0在bool型別裡是false, 非0是true, 因此在上面的demo中可以使用strcmp(word, "mate");做for迴圈是否結束的判斷
再看使用string類進行字串比較
// string風格字串比較
#include "iostream"
#include "string"
using namespace std;
int main()
{
string word = "?ate";
for (char c = 'a'; word != "mate"; c++)
{
cout << word << endl;
word[0] = c;
}
cout << "finish word is : " << word << endl;
return 0;
}
執行結果:
c++中的string類過載了運算子!=, 使得能夠在至少有一個運算元是string物件, 而另一個運算元可以使string物件, 也可是是c風格字串的時候進行比較兩個字串是否相等.
while迴圈
// while迴圈
#include "iostream"
#include "cstring"
using namespace std;
const int ArrSize = 20;
int main()
{
char name[ArrSize];
cout << "please input your name : ";
cin >> name;
cout << "Here is your name : " << endl;
int i = 0;
while (name[i] != '\0')
{
cout << name[i] << " : " << int(name[i]) << endl;
i++;
}
string name2;
cout << "please input your name2 : ";
cin >> name2;
cout << "Here is your name : " << endl;
i = 0;
while (name2[i] != '\0')
{
cout << name2[i] << " : " << int(name2[i]) << endl;
i++;
}
return 0;
}
執行結果:
利用while迴圈做一個延遲
#include "iostream"
#include "ctime"
using namespace std;
int main() {
cout << "Enter the delay time in secod : " << endl;
float secs;
cin >> secs;
// clock_t 是clock()返回型別的別名
// CLOCKS_PER_SEC是一個常量表示每秒鐘包含的系統時間單位數
// 都包含在ctime標頭檔案中
clock_t delay = secs * CLOCKS_PER_SEC;
cout << "starting\a\n";
clock_t start = clock();
// 利用while做了一個延遲
while (clock() - start < delay);
cout << "done\a\n";
return 0;
}
類型別名
c++為型別建立別名的方式有兩種:
1.使用前處理器
#define BYTE char
這樣前處理器將在編譯程式時使用char代替所有的BYTE, 從而使BYTE稱為char的別名.
2.第二種方法是使用c++的關鍵詞typedef來建立別名
typedef char byte;
typedef char * byte_pointer; // 將byte_pointer宣告為char指標