| 
 | Generated by diff2html.pl © Yves Bailly, MandrakeSoft S.A. 2001, Ryohei Morita 2007 diff2html.pl is licensed under the GNU GPL. | 
| ../03-2/03-2-fgets.c | 03-3-sscanf.c | |||
|---|---|---|---|---|
| 24 lines 710 bytes Last modified : Fri Nov 25 03:52:32 2011 | 32 lines 1122 bytes Last modified : Fri Nov 25 03:12:04 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.2. fgets()の試運転 | 7 | // 03.3. sscanf()の試運転 | |
| 8 | 8 | |||
| 9 | #include <stdio.h> | 9 | #include <stdio.h> | |
| 10 | 10 | |||
| 11 | int main(int argc, char *argv[]){ | 11 | int main(int argc, char *argv[]){ | |
| 12 | char oneline[8]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | 12 | char oneline[80]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | |
| 13 | int i; // バッファ精査ループ用 | 13 | float value1 , value2; // 演算用の浮動小数点数2つ | |
| 14 | char operator[5] = ""; // 4文字から成る演算子まで許容 | |||
| 15 | int numelements; // 代入に成功した要素数 | |||
| 14 | 16 | |||
| 15 | while (fgets(oneline, sizeof(oneline), stdin) != NULL) { | 17 | while (fgets(oneline, sizeof(oneline), stdin) != NULL) { | |
| 16 | for (i = 0; oneline[i] != '\0'; i++) { | 18 | value1 = 0; | |
| 17 | printf("oneline[%3d] = '%c' (%d)\n", i, oneline[i], oneline[i]); | 19 | value2 = 0; | |
| 20 | numelements = sscanf(oneline, "%f %4s %f", &value1, operator, &value2); | |||
| 21 | if (numelements == 3) { | |||
| 22 | printf("Success: Value1 = %g\n", value1); | |||
| 23 | printf("Success: Operator = \"%s\"\n", operator); | |||
| 24 | printf("Success: Value2 = %g\n", value2); | |||
| 25 | } else { | |||
| 26 | printf("Missing some elements [got only %d item(s)]... %g, \"%s\", %g\n", numelements, value1, operator, value2); | |||
| 18 | } | 27 | } | |
| 19 | printf("Total: %d chars ==========\n", i); | |||
| 20 | } | 28 | } | |
| 21 | 29 | |||
| 22 | return 0; | 30 | return 0; | |
| 23 | } | 31 | } | |
| 24 | 32 |