迴圈:
while迴圈 計數控制迴圈 counter-controlled loop
標記控制的迴圈: sentinel-conrolled loop
在寫迴圈的過程中容易出現多一次或者少一次的錯誤 , 即差一錯誤 (off-by-one error)
計算減法--簡單**:
猜數字:#include #include // rand and srand function
#include // for time function
using namespace std;
int main(int argc, char *argv)
// swap
cout << "what is " << number1 << " - " << number2 << " ? " << endl;
int answer;
cin >> answer;
while(answer != (number1 - number2))
cout << "you git it!" << endl;
return 0;
}
先隨機生成乙個[0, 100]之間的整數,由使用者數輸入猜測數字,每次給出猜測的結果(偏大還是偏小,直到猜中為止)
不要要浮點數作為迴圈的判斷條件,因為浮點運算是一種近似的表示,程式可能會陷入死迴圈。#include #include // rand and srand function
#include // for time function
using namespace std;
int main(int argc, char *argv)
else
cout << "enter your guess again: ";
cin >> guess;
} cout << "yes, the number is " << guess << "!" << endl;
return 0;
}
2.輸入輸出重定向
如果有許多資料需要輸入,可以將檔案儲存在檔案中,使用空格分開, 如input.txt,執行:
sentinevalue.exe < input.txt 輸入重定向命令, 從檔案獲取輸入
輸出重定向命令 sentinevalue > output.txt , 可以將輸出傳送到檔案中
輸入輸出重定向可以在同乙個命令中:
sentinevalue.exe < input.txt >output.txt
將input中的資料讀取併發送到output中
(1)利用迴圈讀取檔案中的資料
do-while 迴圈:while迴圈的變體#include #include // rand and srand function
#include // for time function
#include // for file operation
using namespace std;
int main(int argc, char *argv)
output << endl;
output << 3 << " " << 5 << endl;
output.close();
int number_1;
int sum=0;
// ifstream input("number.txt");
ifstream input;
input.open("number.txt");
while(!input.eof()) // eof()[end of file]測試是否讀到了檔案末尾
cout << "the sum is " << sum << endl;
return 0;
}
for 迴圈: 簡潔
先驗迴圈: while, for 迴圈, 迴圈的條件檢驗在迴圈體之前進行
後驗迴圈: do while迴圈
執行結果:#include #include // rand and srand function
#include // for time function
#include // for file operation
#include // format outpit
using namespace std;
int main(int argc, char *argv)
cout << endl;
} return 0;
}
求最大公約數
蒙特卡洛模擬:#include #include // rand and srand function
#include // for time function
#include // for file operation
#include // for foemat output
using namespace std;
int main(int argc, char *argv)
cout << "after " << year << " year(s) " << " the tuition double" << endl;
return 0;
}
估計pi的值,作邊長為1的正方形和它的內切圓。
輸出素數#include #include // rand and srand function
#include // for time function
#include // for file operation
#include // for foemat output
#include // for general math function
using namespace std;
const float rand_max_float = static_cast(rand_max);
int main(int argc, char *argv)
cout << tmp << " to hex is " << hex << endl;
return 0;
}
#include #include #include using namespace std;
const int number_of_prime = 50;
const int number_of_prime_per_line = 10;
int main(int argc, char *argv)
} if(isprime)
else
}number++;
}return 0;
}
C 學習筆記 2
ref 和out 都是是傳遞引用,out是返回值,兩者有一定的相同之處,不過也有不同點。使用ref 前必須對變數賦值,out不用。out的函式會清空變數,即使變數已經賦值也不行,退出函式時所有 out引用的變數都要賦值,ref引用的可以修改,也可以不修改。下面是使用 out和 ref進行陣列修改的例...
C 學習筆記(2)
定義 namespace a namespace a 系統會自動合併這兩個 使用時在前面加上using namespace a 或者a var來呼叫命名空間a中的變數和函式。wchar t雙位元組型變數,佔據2位元組,而char只佔據1位元組 wchar t wc l 中國 setlocale lc...
c 學習筆記(2)
筆記 1.相同型別的類物件通過拷貝建構函式完成整個複製過程。2.編譯器會自動生成乙個拷貝建構函式,即預設拷貝建構函式,這個建構函式很簡單,僅僅使用老物件的資料成員的值對新物件的資料成員一一賦值。預設拷貝建構函式執行的是淺拷貝。3.在深拷貝的情況下,對於物件中的動態成員,就不能僅僅簡單的賦值了,而應該...