Computer programming test 1 #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { cout << “Hello world”; return 0; } int main() { int number1; int number2; int sum; cout << “Enter first integer: ”; cin >> number1; cout << “Enter second integer: ”; cin >> number2; sum = number1 + number2; cout << “Sum is “ << sum << endl; } int main() { int number1; int number2; cout << “Enter two integer to compare: “; cin >> number1 >> number2; if(number1 == number2) cout << number1 << “==” << number2 << endl; if(number1 != number2) cout << number1 << “!=” << number2 << endl; if(number1 < number2) cout << number1 << “<” << number2 << endl; if(number1 > number2) cout << number1 << “>” << number2 << endl; } cout << ((grade >= 60) ? ”Passed” : “Failed“); or (grade >= 60 ? cout << ”Passed” : cout << “Failed“); 意思: if ( grade >= 60 ) cout << "Passed"; else cout << "Failed"; if ( studentGrade >= 90 ) // 90 and above gets "A" cout << "A"; else if ( studentGrade >= 80 ) // 80-89 gets "B" cout << "B"; else if ( studentGrade >= 70 ) // 70-79 gets "C" cout << "C"; else if ( studentGrade >= 60 ) // 60-69 gets "D" cout << "D"; else // less than 60 gets "F" cout << "F"; if ( x > 5 ) { if ( y > 5 ) cout << "x and y are > 5"; } else cout << "x is <= 5"; setprecision(2) Rounded to 2 digits of precision to the right of the decimal point (e.g., 92.37, 12345.78, 0.17) Stream manipulator fixed: output in fixed-point format, as opposed to scientific notation (scientific,e.g. 9.24e+001, 1.23e+004, 1.73e-001) Also possible to force a decimal point to appear by using stream manipulator showpoint. 当使用 showpoint 时,表示打印浮点数的小数点和小数位数,即使 显示的数值没有小数点。 cout << fixed << showpoint << setprecision(2); c = c + 3; 等於 c += 3; int main() { int A=2; int B=2; A++; ++B; printf("A=%d return 0; B=%d \n",A,B); } 兩者 A 與 B 的輸出均是 3 但只要我們是以表示式來使用,效果就不一樣了。 int main() { int A = 2; int B = 2; int C = A++; int D = ++B; printf("C=%d D=%d \n",C,D); return 0; } A++:意思是先把 A 放到 C,再執行 A=A+1 的動作。 因為 A 先放到 C,所以 C 的值會是 2 ++B:意思是先再執行 B=B+1,再把 B 放到 D。 因為 B 會先執行 B=B+1 的動作,所以 D 的值會是 3 pow(x, y) calculates the value of x raised to the yth power setw() 函数只对紧接着的输出产生作用。 当后面紧跟着的输出字段长度小于 n 的时候,在该字段前面用空格补齐,当输 出字段长度大于 n 时,全部整体输出。