文章目錄
實例11:輸入一個數,將各位數字翻轉後輸出
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int n, right_digit, newnum = 0;
cout << "Enter the number:\n";
cin >> n;
cout << "The number in reverse order is" << endl;
do {
right_digit = n % 10;
cout << right_digit;
n /= 10;//相當於n=n/10;
} while (n != 0);
cout << endl;
return 0;
}
結果:
Enter the number:
123
The number in reverse order is
321
實例12:利用do—while語句,求自然數1~10之和
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int i = 1,sum = 0;
do {
sum += i;
i++;
} while (i<=10);
cout << "sum = " << sum << endl;
return 0;
}
結果:
sum = 55
實例13:輸入一個整數,求出它的的所有因子
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a positive integer:";
cin >> n;
cout << "Number:" << n << "\nFactors: ";
for (int k = 1; k <= n; k++) {
if (n % k == 0)
cout << k << " ";
}
return 0;
}
結果:
Enter a positive integer:10
Number:10
Factors: 1 2 5 10
實例14:輸入一系列整數,統計正負整數的個數i,就,讀入0則結束
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int i = 0, j = 0, n;
cout << "Enter some integers please(enter 0 to quit):" << endl;
cin >> n;
while (n != 0) {
if (n > 0)
i += 1;
else
j += 1;
cin >> n;
}
cout << "正整數個數:" << i << endl;
cout << "負整數個數:" << j << endl;
return 0;
}
結果:
Enter some integers please(enter 0 to quit):
10 20 -30 -80 2 0
正整數個數:3
負整數個數:2
實例15:某次比賽的四種結果的可能
某次比賽的四種結果的可能:勝、負、平局、比賽取消,編寫程序順序輸出這四種情況。
#include "stdafx.h"
#include <iostream>
using namespace std;
//枚舉類型定義,默認情況WIN=0,LOSE=1,TIE=2,CANCEL=3
enum GameResult{WIN,LOSE,TIE,CANCEL};
int main()
{
//枚舉類型變量定義
GameResult result;
enum GameResult omit = CANCEL;
for (int count = WIN; count <= CANCEL; count++) {
//枚舉類型運算時,需要將int轉換爲枚舉類型,再計算。
result = GameResult(count);
if (result == omit)
cout << "這場比賽將取消!" << endl;
else {
cout << "這場比賽將舉行!" << endl;
if (result == WIN)
cout << "贏了!" << endl;
else if (result==LOSE)
cout << "輸了!" << endl;
else
cout << "平局!" << endl;
}
}
return 0;
}
結果:
這場比賽將舉行!
贏了!
這場比賽將舉行!
輸了!
這場比賽將舉行!
平局!
這場比賽將取消!
函數
實例16:編寫一個求x的n次方的函數
#include "stdafx.h"
#include <iostream>
using namespace std;
//計算x的n次方
double power(double x, int n) {
double val = 1.0;
while (n--)
val *= x;
return val;
}
int main()
{
cout << "5的2次方爲:" << power(5, 2) << endl;
return 0;
}
結果:
5的2次方爲:25
實例17:距離計算
寫一個名爲 distance 的函數,該函數有 3 個輸入和 1 個輸出。輸入爲速度、加速度和時間。輸出爲隨時間推移經過的距離。距離的計算公式爲:
#include "stdafx.h"
#include <iostream>
using namespace std;
float distance(float velocity, float elapsedime,float acceleration);
int main()
{
int x = 25;
double y = 61.4;
double z = 199.2;
double division = y/z;
cout << distance(3,4,5) << "\n";
cout << distance(7.0,2.1,5.4) << "\n";
return 0;
}
float distance(float velocity, float elapsedtime,float acceleration)
{
return velocity*elapsedtime+0.5*acceleration*elapsedtime*elapsedtime;
}
結果:
52
26.607
實例18:二進制轉十進制
輸入一個8位的二進制數,將其轉化爲十進制數輸出。
例如:從鍵盤輸入00001101
00001101(B)=1×23+1×22+0×21+1×20(D)=8+4+1(D)=13(D)
所以程序應輸出13.
#include "stdafx.h"
#include <iostream>
using namespace std;
double power(double x,int n);//計算x的n次方
int main()
{
int value = 0;
cout << "Enter an 8 bit binary number:";
for (int i = 7; i >= 0; i--){
char ch;
cin >> ch;
if (ch == '1')
//強制轉換爲int
value += static_cast<int>(power(2, i));
}
cout << "Decimal value is: " << value << endl;
return 0;
}
double power(double x, int n) {
double val = 1.0;
while (n--)
val *= x;
return val;
}
結果:
Enter an 8 bit binary number:01101001
Decimal value is: 105
注意:
- 對於**ch == ‘1’**的理解,假設ch=01,‘1’與ch中0比較(比較順序從左到右),結果爲False(不執行if內的語句);‘1’與ch中1比較,結果爲True,執行if中的操作。這裏應該用到了ASCll碼的比較。
- static_cast (expr): static_cast 運算符執行非動態轉換,沒有運行時類檢查來保證轉換的安全性。將double強制轉換爲int。
實例19:求pi的值
pi的計算公式:
其中arctan用如下級數計算:
直到級數某項絕對值不大於10^(-15)爲止,pi和x均爲double類型。
#include "stdafx.h"
#include <iostream>
using namespace std;
double arctan(double x) {
double sqr = x*x;
double e = x;
double r = 0;
int i = 1;
while (e / i > 1e-15) {
double f = e / i;
r = (i % 4 == 1) ? r + f : r - f;
e = e*sqr;
i += 2;
}
return r;
}
int main()
{
double a = 16.0*arctan(1 / 5.0);
double b = 4.0*arctan(1 / 239.0);
cout << "PI = " << a - b << endl;
return 0;
}
結果:
PI = 3.14159
實例20:尋找並輸出11~999之間的m,m2和m3均爲迴文數。
例如:11,121,1331
分析:
-
循環遍歷11-999
-
判斷迴文數: a.求數的反序 (除以10取餘數)見實例11
b.判斷反序的數與原來的數是否相等
#include "stdafx.h"
#include <iostream>
using namespace std;
//判斷n是否爲迴文數
bool symm(unsigned n) {
unsigned i = n;
unsigned m = 0;
while (i > 0) {
m = m * 10 + i % 10;
i /= 10;
}
return m == n;
}
int main()
{
for (unsigned m = 11; m < 1000; m++)
if (symm(m) && symm(m*m) && symm(m*m*m)) {
cout << "m = " << m;
cout << " m*m = " << m*m;
cout << " m*m*m = " << m*m*m << endl;
}
return 0;
}
結果:
m = 11 m*m = 121 m*m*m = 1331
m = 101 m*m = 10201 m*m*m = 1030301
m = 111 m*m = 12321 m*m*m = 1367631