Baramarがあなたの主な問題を正確かつ完全に説明したように、私はあなたの問題について異なる理解をしていると思います。あなたは、きちんとそのような各3桁の列にフォーマットさ、数の与えられた文字列、例えば:2134567896543245678
をしたいし、単一の列でそれをプリントアウト:
213
456
789
654
324
567
8
を整数の断続的な配列なし。例えばのように行うことができる
:この
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 512
int main()
{
int res;
// for the scanf input, set all to '\0'
char buffer[BUFFER_SIZE + 1] = { '\0' }, *idx;
size_t len, i;
// restrict max-size to BUFFER_SIZE
res = scanf("%512s", buffer);
if (res != 1) {
exit(EXIT_FAILURE);
} else {
if (strcmp(buffer, "exit") == 0) {
exit(EXIT_SUCCESS);
}
// TODO: check if the buffer contains all digits
len = strlen(buffer);
idx = buffer;
for (i = len; i >= 3; i -= 3, idx += 3) {
printf("%.3s\n", idx);
}
// last entries, if any
if (*idx != '\0') {
printf("%s\n", idx);
}
}
exit(EXIT_SUCCESS);
}
あなたは例えばのような行の実際の整数を取得する場合:12 3123 23478 34 5456 567456 567 678
あなたがそのようなものを使用することができます後
EDIT
を浮動小数点を使用するOPのコメント私はフォームの入力を受け入れるようにコードを変更しました:
24722.319352 51433.662233
56087.991042 49357.684934 67875.375848 68421.563197
54521.615295
22744.470483
38097.001461 80878.250982
92131.575748 7217.137271
20750.671365 7620.695008 37118.391541 28655.609469 46885.110202 87114.202312
46462.577299
20557.716648
とコード:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUM 400
int main()
{
int res;
int i = 0, m;
// input and two temporary variables
double in, in1, in2;
for (m = 0; m < MAXNUM; m++) {
res = scanf("%lf", &in);
if (res != 1) {
break;
} else {
switch (i) {
case 0:
// set value of first temporary variable to input
in1 = in;
// increment indicator indicating position in output row
i++;
break;
case 1:
in2 = in;
i++;
break;
case 2:
// print the three numbers and a newline
fprintf(stdout, "%f %f %f\n", in1, in2, in);
// reset counter
i = 0;
break;
}
}
}
// if there are still numbers, print them
if (i != 0) {
if (i == 1) {
fprintf(stdout, "%f\n", in1);
} else {
fprintf(stdout, "%f %f\n", in1, in2);
}
}
exit(EXIT_SUCCESS);
}
あなたがctrl+d
たりして、ほとんどのUnixシェルで起動することができますが、EOF
で終了する必要が400個の未満のエントリを入力した場合
$ gcc-4.9 -O3 -g3 -W -Wall -Wextra -std=c11 sc.c -o sc
$ ./sc < floatin
24722.319352 51433.662233 56087.991042
49357.684934 67875.375848 68421.563197
54521.615295 22744.470483 38097.001461
80878.250982 92131.575748 7217.137271
20750.671365 7620.695008 37118.391541
28655.609469 46885.110202 87114.202312
46462.577299 20557.716648
でそれを試してみてください例えば、-1
のようなエントリを終了させるためのエントリを設定します。すべてが正の数であり、それがループから抜けることを確認します。上の例のようにファイルを提出すると、自動的に動作します。
scanfを間違って使用しているようです。今はnums [i]で指定された書式を変数に読み込もうとしています。また、iとnumの比較が間違っています。 –
あなたは 'nums [i]'にスペースを割り当てていないので、未定義の動作を引き起こしています。 – Barmar