|
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-02/03-02-fgets.c | 03-03-sscanf.c | |||
---|---|---|---|---|
25 lines 818 bytes Last modified : Sat Jan 4 12:12:25 2014 |
33 lines 1166 bytes Last modified : Thu Dec 26 06:55:05 2013 |
|||
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/27a kameda[at]iit.tsukuba.ac.jp | 6 | // 2013/12/24a kameda[at]iit.tsukuba.ac.jp | |
7 | // 2011/11/25a kameda[at]iit.tsukuba.ac.jp | 7 | // 2011/11/25a kameda[at]iit.tsukuba.ac.jp | |
8 | // 03.02. fgets()の試運転 | 8 | // 03.03. sscanf()の試運転 | |
9 | 9 | |||
10 | #include <stdio.h> | 10 | #include <stdio.h> | |
11 | 11 | |||
12 | int main(int argc, char *argv[]){ | 12 | int main(int argc, char *argv[]){ | |
13 | char oneline_static[5]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | 13 | char oneline[80]; // 1行分のバッファ,固定長にしておくとsizeof()が利用可能 | |
14 | int i; // バッファ精査ループ用 | 14 | float value1 , value2; // 演算用の浮動小数点数2つ | |
15 | char operator[5] = ""; // 4文字から成る演算子まで許容 | |||
16 | int numelements; // 代入に成功した要素数 | |||
15 | 17 | |||
16 | while (fgets(oneline_static, sizeof(oneline_static), stdin) != NULL) { | 18 | while (fgets(oneline, sizeof(oneline), stdin) != NULL) { | |
17 | for (i = 0; oneline_static[i] != '\0'; i++) { | 19 | value1 = 0; | |
18 | printf("oneline_static[%3d] = '%c' (%d)\n", i, oneline_static[i], (unsigned char)oneline_static[i]); | 20 | value2 = 0; | |
21 | numelements = sscanf(oneline, "%f %4s %f", &value1, operator, &value2); | |||
22 | if (numelements == 3) { | |||
23 | printf("Success: Value1 = %g\n", value1); | |||
24 | printf("Success: Operator = \"%s\"\n", operator); | |||
25 | printf("Success: Value2 = %g\n", value2); | |||
26 | } else { | |||
27 | printf("Missing some elements [got only %d item(s)]... %g, \"%s\", %g\n", numelements, value1, operator, value2); | |||
19 | } | 28 | } | |
20 | printf("Total: %d chars ==========\n", i); | |||
21 | } | 29 | } | |
22 | 30 | |||
23 | return 0; | 31 | return 0; | |
24 | } | 32 | } | |
25 | 33 |