| 
 | Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. | 
| ../03-3/03-3-sscanf.c | 03-4-SimpleCalculator.c | |||
|---|---|---|---|---|
| 32 lines 1122 bytes Last modified : Fri Nov 25 03:12:04 2011 | 71 lines 2115 bytes Last modified : Fri Nov 25 02:50:25 2011 | |||
| 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 | // 2011/11/25a kameda[at]iit.tsukuba.ac.jp | 6 | // 2011/11/25a kameda[at]iit.tsukuba.ac.jp | |
| 7 | // 03.3. sscanf()の試運転 | 7 | // 03.4. 簡易電卓 | |
| 8 | 8 | |||
| 9 | #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 | } | |||
| 10 | 37 | |||
| 11 | int main(int argc, char *argv[]){ | 38 | int main(int argc, char *argv[]){ | |
| 12 | char oneline[80]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | 39 | char oneline[80]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | |
| 13 | float value1 , value2; // 演算用の浮動小数点数2つ | 40 | float value1 , value2; // 演算用の浮動小数点数2つ | |
| 14 | char operator[5] = ""; // 4文字から成る演算子まで許容 | 41 | char operator[5] = ""; // 4文字から成る演算子まで許容 | |
| 15 | int numelements; // 代入に成功した要素数 | 42 | int numelements; // 代入に成功した要素数 | |
| 43 | float resultvalue; // 演算結果 | |||
| 16 | 44 | |||
| 17 | while (fgets(oneline, sizeof(oneline), stdin) != NULL) { | 45 | while (fgets(oneline, sizeof(oneline), stdin) != NULL) { | |
| 18 | value1 = 0; | 46 | value1 = 0; | |
| 19 | value2 = 0; | 47 | value2 = 0; | |
| 20 | numelements = sscanf(oneline, "%f %4s %f", &value1, operator, &value2); | 48 | numelements = sscanf(oneline, "%f %4s %f", &value1, operator, &value2); | |
| 21 | if (numelements == 3) { | 49 | if (numelements == 3) { | |
| 22 | printf("Success: Value1 = %g\n", value1); | 50 | if (strcmp(operator, "+") == 0) { | |
| 23 | printf("Success: Operator = \"%s\"\n", operator); | 51 | resultvalue = operation_plus(value1, value2); | |
| 24 | 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, "/") == 0) { | |||
| 59 | resultvalue = operation_div(value1, value2); | |||
| 60 | } else if (strcmp(operator, "abs") == 0) { | |||
| 61 | resultvalue = operation_absdiff(value1, value2); | |||
| 62 | } | |||
| 63 | printf("%g %s %g = %g\n", value1, operator, value2, resultvalue); | |||
| 25 | } else { | 64 | } else { | |
| 26 | printf("Missing some elements [got only %d item(s)]... %g, \"%s\", %g\n", numelements, value1, operator, value2); | 65 | printf("Missing some elements ... %g, \"%s\", %g\n", value1, operator, value2); | |
| 27 | } | 66 | } | |
| 28 | } | 67 | } | |
| 29 | 68 | |||
| 30 | return 0; | 69 | return 0; | |
| 31 | } | 70 | } | |
| 32 | 71 |