-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw1-1.c
More file actions
47 lines (41 loc) · 927 Bytes
/
hw1-1.c
File metadata and controls
47 lines (41 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
int main(void){
float left = 0.0, right = 0.0, result = 0.0;
int flag = 0, i = 0;
char op = 0;
printf( "請輸入兩個數字: " );
scanf( " %f %f", &left, &right );
if( right == 0.0 ) { flag = 1; }
printf( "\n計算結果:\n" );
for( i = 0; i < 4; i++ )
{
switch( i ){
case 0:
result = left + right;
op = '+';
break;
case 1:
result = left - right;
op = '-';
break;
case 2:
result = left * right;
op = '*';
break;
case 3:
if( flag ){
printf("除數不得為零!!!");
continue;
}
else{
result = left / right;
op = '/';
break;
}
}
printf( "%.2f %c %.2f = %.2f\n", left, op, right, result);
}
printf("\n");
return 0;
}