Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************/
- /** 20140304 Exam 02 **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2014/03/04 pm.01:30 **/
- /************************************/
- //一樣先引入標頭檔案
- #include <stdio.h>
- #include <stdlib.h>
- //方式一: 使用if .. else if ..
- void method_1()
- {
- /*先行定義變數
- number_1 = 第一運算數
- number_2 = 第二運算數
- select = 選擇運算子 */
- int number_1 = 0, number_2 = 0, select = 0;
- //輸入第一運算數
- printf("Input the first number: ");
- scanf("%d", &number_1);
- //輸入第二運算數
- printf("Input the second number: ");
- scanf("%d", &number_2);
- //輸入選擇運算子, 其規定為[1, 2, 3, 4] is [+, -, *, /]
- printf("// Select your action ");
- printf("[1, 2, 3, 4] is [+, -, *, /] //");
- printf("\nYour choose is: ");
- scanf("%d", &select);
- /*開始進行if 條件運算式, 其使用格式為
- --------------------------------------
- if([條件式])
- {
- [敘述式]
- }
- else if([條件式])
- {
- [敘述式]
- }
- else
- {
- [敘述式]
- }
- --------------------------------------*/
- if(select == 1)
- {
- printf("Answer(+) is: %d\n", number_1 + number_2);
- return;
- }
- else if(select == 2)
- {
- printf("Answer(-) is: %d\n", number_1 - number_2);
- return;
- }
- else if(select == 3)
- {
- printf("Answer(*) is: %d\n", number_1 * number_2);
- return;
- }
- else if(select == 4)
- {
- printf("Answer(/) is: %d .. %d\n", number_1 / number_2, number_1 % number_2);
- return;
- }
- }
- //方式二: 使用switch .. case ..
- void method_2()
- {
- int number_1 = 0, number_2 = 0, select = 0;
- printf("Input the first number: ");
- scanf("%d", &number_1);
- printf("Input the second number: ");
- scanf("%d", &number_2);
- printf("// Select your action ");
- printf("[1, 2, 3, 4] is [+, -, *, /] //");
- printf("\nYour choose is: ");
- scanf("%d", &select);
- /*開始進行switch 條件運算式, 其使用格式為
- ------------------------
- switch([條件式])
- {
- case [值]:
- [運算式]
- }
- ------------------------*/
- switch(select)
- {
- case 1:
- printf("Answer(+) is: %d\n", number_1 + number_2);
- break;
- case 2:
- printf("Answer(-) is: %d\n", number_1 - number_2);
- break;
- case 3:
- printf("Answer(*) is: %d\n", number_1 * number_2);
- break;
- case 4:
- printf("Answer(/) is: %d .. %d\n", number_1 / number_2, number_1 % number_2);
- break;
- }
- }
- void main()
- {
- //method_1();
- //method_2();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment