實驗結論
編程練習2-28
switch版源碼:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Menu:A(dd) D(elete) S(ort) Q(uit)" << endl;
while (1)
{
char choice;
cin >> choice;
switch (choice)
{
case 'A':
cout << "Data has been added" << endl;
break;
case 'D':
cout << "Data has been deleted" << endl;
break;
case 'S':
cout << "Data has been sorted" << endl;
break;
}
if(choice=='Q')
break;
}
}
if版源碼:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Menu:A(dd) D(elete) S(ort) Q(uit)" << endl;
while (1)
{
char choice;
cin >> choice;
if(choice=='A')
cout << "Data has been added" << endl;
if(choice=='D')
cout << "Data has been deleted" << endl;
if(choice=='S')
cout << "Data has been sorted" << endl;
if(choice=='Q')
break;
}
}
2-28運行截圖:
編程練習2-29
do_while版源碼:
#include <iostream>
using namespace std;
int main(void)
{
int prime = 2;
do
{
int i = 2;
do
{
if (prime % i == 0)
break;
i++;
} while (i <= prime);
if (prime == i)
cout
<< prime << endl;
prime++;
} while (prime <= 100);
cin.get();
}
while版源碼
#include <iostream>
using namespace std;
int main(void)
{
int prime=2;
while (prime <= 100)
{
int i=2;
while (i <= prime)
{
if (prime % i == 0)
break;
i++;
}
if (prime == i)
cout << prime << endl;
prime++;
}
cin.get();
}
for版源碼:
#include <iostream>
using namespace std;
int main(void)
{
int prime;
for (prime = 2; prime <= 100; prime++)
{
int i;
for (i = 2; i <= prime; i++)
{
if (prime % i == 0)
break;
}
if (prime == i)
cout << prime << endl;
}
cin.get();
}
2-29運行截圖:
編程練習2-32
因為程序全程只有while(1)
循環內容 do while與while沒有什么差異 故只寫了一版
源碼:
#include <ctime>
#include <iostream>
#include <random>
#include <windows.h>
using namespace std;
int main(void)
{
cout << "Game Start" << endl;
cout << "Enter\"0\" to exit." << endl;
cout << "Have Fun!" << endl;
Sleep(2000); //暫停2秒
while (1)
{
mt19937 gen(time(NULL)); //用時間作為種子
uniform_int_distribution<> dis(1, 100); //設定隨機數的范圍為1~100
int num2gess = dis(gen);
int numUgess;
cout << "Please enter the number(1~100) you gess" << endl;
while (1)
{
cin >> numUgess;
if (numUgess == 0)
break;
if (numUgess < num2gess)
cout << "The number you gess is too small! Try again!" << endl;
else if (numUgess == num2gess)
{
cout << "Bingo! Congratulation!" << endl;
break;
}
else
cout << "The number you gess is too large! Try again!" << endl;
}
if (numUgess == 0)
break;
}
}
2-32運行截圖:
編程練習2-34
采用順序無關的寫法,並稍加拓展
//原題實現太簡單 現將程序改為從m個顏色不同的小球中取n個(n<=m<=20)
#include <iostream>
using namespace std;
long long int fac(long long int num) //階乘函數
{
long long int r = 1, i;
for (i = 1; i <= num; i++)
{
r *= i;
}
return r;
}
int main(void)
{
long long int Ball, Take;
cout << "Please enter the number of balls and how many balls you want to take." << endl;
cout << "Enter\"0\" to exit" << endl;
while (1)
{
cin >> Ball;
if (Ball == 0)
break;
cin >> Take;
cout << "There are " << fac(Ball) / fac(Take) / fac(Ball - Take) << " possibilities." << endl;
}
}
2-34威力加強版運行截圖:
實驗總結與體會
- 這四道編程練習題帶我們回顧了c++中與c一樣的語法知識,並使用了c++中新添加的特性,如:新添加的輸入輸出流函數與新的頭文件。
- 同時我在翻閱往屆學長學姐的blog時發現了很多新的知識,萬能頭文件
#include<bits/stdc++.h>
在這里推薦給大家 - 我在做編程練習2-23時查閱了一些資料發現在c++11標准中新加了
uniform_int_distribution
函數用於生成整數隨機數,用法我會貼在下方的引用欄中
cppreference:uniform_int_distribution
CSDN:用時間作為生成隨機數的種子
CSDN:如何在c++中暫停特定時間