|
Generated by UTF-8 version of diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. |
../03-03/03-03-sscanf.c | 03-04-SimpleCalculator.c | |||
---|---|---|---|---|
33 lines 1166 bytes Last modified : Thu Dec 26 06:55:05 2013 |
69 lines 2042 bytes Last modified : Sat Jan 4 13:24:08 2014 |
|||
1 | // Keisanki Joron 2 (Introduction to Computing II) | 1 | // Keisanki Joron 2 (Introduction to Computing II) | |
2 | // Dept. of Engineering Systems, University of Tsukuba | 2 | // Dept. of Engineering Systems, University of Tsukuba | |
3 | // [UTF-8 / Unix] | 3 | // [UTF-8 / Unix] | |
4 | // 計算機序論2・実習 (筑波大学工学システム学類) | 4 | // 計算機序論2・実習 (筑波大学工学システム学類) | |
5 | 5 | |||
6 | // 2013/12/24a kameda[at]iit.tsukuba.ac.jp | 6 | // 2013/12/27c kameda[at]iit.tsukuba.ac.jp | |
7 | // 2011/11/25a kameda[at]iit.tsukuba.ac.jp | 7 | // 03.04. 簡易電卓 | |
8 | // 03.03. sscanf()の試運転 | |||
9 | 8 | |||
10 | #include <stdio.h> | 9 | #include <stdio.h> | |
10 | #include <string.h> // strcmp() | |||
11 | ||||
12 | float operation_plus(float v1, float v2) { | |||
13 | return v1 + v2; | |||
14 | } | |||
15 | ||||
16 | float operation_minus(float v1, float v2) { | |||
17 | return v1 - v2; | |||
18 | } | |||
19 | ||||
20 | float operation_mult(float v1, float v2) { | |||
21 | return v1 * v2; | |||
22 | } | |||
23 | ||||
24 | float operation_div(float v1, float v2) { | |||
25 | if (v2 == 0.0) { | |||
26 | printf("Error: Division by Zero.\n"); | |||
27 | return 0.0; | |||
28 | } | |||
29 | return v1 / v2; | |||
30 | } | |||
31 | ||||
32 | float operation_absdiff(float v1, float v2) { | |||
33 | if (v1 > v2) | |||
34 | return v1 - v2; | |||
35 | return v2 - v1; | |||
36 | } | |||
11 | 37 | |||
12 | int main(int argc, char *argv[]){ | 38 | int main(int argc, char *argv[]){ | |
13 | char oneline[80]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | 39 | char oneline[80]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | |
14 | float value1 , value2; // 演算用の浮動小数点数2つ | 40 | float value1 = 0.0 , value2 = 0.0; // 演算用の浮動小数点数2つ | |
15 | char operator[5] = ""; // 4文字から成る演算子まで許容 | 41 | char operator[5] = ""; // 4文字から成る演算子まで許容 | |
16 | int numelements; // 代入に成功した要素数 | 42 | int numelements = 0; // 代入に成功した要素数 | |
43 | float resultvalue = 0.0; // 演算結果 | |||
17 | 44 | |||
18 | while (fgets(oneline, sizeof(oneline), stdin) != NULL) { | 45 | while (fgets(oneline, sizeof(oneline), stdin) != NULL) { | |
19 | value1 = 0; | 46 | value1 = 0.0; | |
20 | value2 = 0; | 47 | value2 = 0.0; | |
21 | numelements = sscanf(oneline, "%f %4s %f", &value1, operator, &value2); | 48 | numelements = sscanf(oneline, "%f %4s %f", &value1, operator, &value2); | |
22 | if (numelements == 3) { | 49 | if (numelements == 3) { | |
23 | printf("Success: Value1 = %g\n", value1); | 50 | if (strcmp(operator, "+") == 0) { | |
24 | printf("Success: Operator = \"%s\"\n", operator); | 51 | resultvalue = operation_plus(value1, value2); | |
25 | printf("Success: Value2 = %g\n", value2); | 52 | } else if (strcmp(operator, "-") == 0) { | |
53 | resultvalue = operation_minus(value1, value2); | |||
54 | } else if (strcmp(operator, "*") == 0) { | |||
55 | resultvalue = operation_mult(value1, value2); | |||
56 | } else if (strcmp(operator, "/") == 0) { | |||
57 | resultvalue = operation_div(value1, value2); | |||
58 | } else if (strcmp(operator, "abs") == 0) { | |||
59 | resultvalue = operation_absdiff(value1, value2); | |||
60 | } | |||
61 | printf("%g %s %g = %g\n", value1, operator, value2, resultvalue); | |||
26 | } else { | 62 | } else { | |
27 | printf("Missing some elements [got only %d item(s)]... %g, \"%s\", %g\n", numelements, value1, operator, value2); | 63 | printf("Missing some elements ... %g, \"%s\", %g\n", value1, operator, value2); | |
28 | } | 64 | } | |
29 | } | 65 | } | |
30 | 66 | |||
31 | return 0; | 67 | return 0; | |
32 | } | 68 | } | |
33 | 69 |