iT邦幫忙

2021 iThome 鐵人賽

DAY 22
0
自我挑戰組

腦力激盪C++系列 第 22

[Day-22] 呼叫自訂函式小練習

  • 分享至 

  • twitterImage
  •  

上次練習完了自訂函式的基礎
今天就要來練習相關題目囉~
/images/emoticon/emoticon07.gif

題目:
1.計算:鍵盤輸入任一整數a,計算2乘以2+(2乘以 a 乘以10)+10乘以10,將結果顯示於第一行
2.顯示圖案加文字:顯示一個長方形的外框,將結果顯示於第1題下面,並將文字「練習結束囉」放置長方形外框裡面

Input:任一整數
5
Output:
第一行 → 計算「2×2+(2×a×10)+10×10」
第一行後面 
■ □ ■ □ ■ □ ■ □ ■
練習結束囉
■ □ ■ □ ■ □ ■ □ ■

程式碼:

#include <iostream>
using namespace std;
int calculation(int a);
void shape();

int main(void){
	int a;
	cout << "Please input a integer."<< '\n' ;
	cin >> a; 
	endl(cout);
	calculation(a);
	endl(cout);
	shape();
	
}

int calculation(int a){
	a = 2*2+(2*a*10)+10*10;
	cout << a;
}

void shape(){
    cout << "■□■□■□■□■";
	endl(cout); 
    cout << "  練習結束囉   "; 
    endl(cout);
    cout << "■□■□■□■□■"; 
}

執行結果:

Please input a integer.
5

204
■ □ ■ □ ■ □ ■ □ ■
       練習結束囉     
■ □ ■ □ ■ □ ■ □ ■
--------------------------------
Process exited after 0.08787 seconds with return value 0
請按任意鍵繼續...

程式碼解釋:
先宣告函式變數的名稱
定義函式
◆第1個結果:主程式會先由cin取得鍵盤輸入的值,並把值存放於變數a,再呼叫函式calculation(),將鍵盤輸入的值丟進函式內去做運算,運算完後直接由函式將結果顯示在命令提示字元上。
◆第2個結果:我使用較簡單的土法煉鋼法,就是單純的打字

以上就是我今天練習的成果~
/images/emoticon/emoticon08.gif

-End-


上一篇
[Day-21] 呼叫自訂函式(二)
下一篇
[Day-23] 小練習二進制轉十進制
系列文
腦力激盪C++30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言