C程序,将输入字符串的空格分隔整数转换为整数数组

C程序,将输入字符串的空格分隔整数转换为整数数组,c,arrays,io,C,Arrays,Io,问题: #include <stdio.h> int main () { int arr[1000], length = 0, c; while ((c = getchar()) != '\n') { if (c != ' ') { arr[length++] = c - '0'; } } printf("["); for ( int i = 0; i < length-1; i++ ) { printf("%d,", a

问题:

#include <stdio.h>
int main () {
  int arr[1000], length = 0, c;
  while ((c = getchar()) != '\n') {
    if (c != ' ') {
      arr[length++] = c - '0';
    }
  }
  printf("[");
  for ( int i = 0; i < length-1; i++ ) {
    printf("%d,", arr[i]);
  }
  printf("%d]\n", arr[length-1]);
}
我想制作一个C程序,它以一个空格分隔的整数字符串作为输入(正数和负数,可变位数),并将该字符串转换为整数数组

在堆栈溢出时,从字符串输入到数组中读取int还有另一个问题,但它不适用于位数长度大于1或负数的数字

尝试:

#include <stdio.h>
int main () {
  int arr[1000], length = 0, c;
  while ((c = getchar()) != '\n') {
    if (c != ' ') {
      arr[length++] = c - '0';
    }
  }
  printf("[");
  for ( int i = 0; i < length-1; i++ ) {
    printf("%d,", arr[i]);
  }
  printf("%d]\n", arr[length-1]);
}
这是我得到的数组:[2,1,7],而不是[21,7]

如果我输入以下内容:

$ echo "-21 7" | ./run
$ [-3,2,1,7]
我得到:[3,2,1,7],而不是[-21,7],这毫无意义

但是,如果我进入:

$ echo "1 2 3 4 5 6 7" | ./run
$ [1,2,3,4,5,6,7]
注意:我假设输入总是一个空格分隔的整数字符串。

完成程序(改编自)(不再需要无效输入来停止读取输入):


注意:我目前不确定这到底是如何工作的。我会调查的。但是,如果有人理解,请编辑这篇文章或留下评论。

这是一个简单的版本(没有错误检查,数字后面应该留一个空格),我相信你可以从这里了解:

int main(void)
{
    int c;
    int i, num = 0, neg = 0;
    while ((c = getchar()) != EOF) {
        if (c != ' ') {
            if (c == '-') {
                neg = 1;
            } else {
                i = c - '0';
                num = num * 10 + i;
            }
        } else {
            (neg == 1) ? num *= -1 : num;
            printf("%d\n", num + 2);    // this is just to show that you indeed get an integer and addition works
            num = 0;
            neg = 0;
        }
    }
}

我不怎么做c,但我的目标是:)

#包括“stdio.h”
#包括“string.h”
#包括“stdlib.h”
#定义块1000000
#定义整数计数10000
内部主(空){
/*获取所有输入*/
char temp_str[块]=“”;
char*full_string=malloc(CHUNK*sizeof(char));
如果(完整字符串==0){
printf(“内存错误\n”);
出口(1);
}
整数计数=2;
做{
fgets(临时、区块、标准数据);
strcat(全字符串、临时字符串);
full_string=realloc(full_string,count*CHUNK*sizeof(char));
如果(完整字符串==0){
printf(“内存错误\n”);
出口(1);
}
计数++;
}while(strlen(temp_str)=CHUNK-1和temp_str[CHUNK-2]!='\n');
//解析输入
char*token=strtok(完整字符串“”);
int*arr=malloc(int_COUNT*sizeof(int)),长度=0;
如果(arr==0){
printf(“内存错误\n”);
出口(1);
}
计数=1;
while(令牌!=0){
arr[length]=atoi(令牌);
令牌=strtok(0,“”);
长度++;
如果(长度==计数*整数计数){
计数++;
arr=realloc(arr,count*INT_count);
如果(arr==0){
printf(“内存错误\n”);
出口(1);
}
}
}
自由(满字符串);
//打印整数
for(int i=0;i
编辑了一点。但还有一个负问题。我过会儿再回头看。如果你对此稍加调整,我想,它可能会起作用。你试试你的方法。我试试我的。但是后来

#include <stdio.h>
int main () {
  int arr[1000]={0}, length = 0, c, i;
  while (1) {
    c = getchar();

if(c=='-')
{
    //minus sign has problem yet. I ll come back once I have better soln.
}
else if(c==' ' || c=='\n')
{
    length-=2;
    arr[length]= arr[length]*10 + arr[length+1];
    length++;
    if(c=='\n')
    {
        break;
    }
}
else if (c != ' ') {
  arr[length++] = c - '0';
    }
 }
  printf("[");
  for (i = 0; i < length-1; i++ ) {
 printf("%d,", arr[i]);
  }
  printf("%d]\n", arr[length-1]);
}
#包括
int main(){
int arr[1000]={0},长度=0,c,i;
而(1){
c=getchar();
如果(c=='-')
{
//负号还没出问题,等我有更好的解决办法再回来。
}
else如果(c=''| | c=''\n')
{
长度-=2;
arr[length]=arr[length]*10+arr[length+1];
长度++;
如果(c=='\n')
{
打破
}
}
否则,如果(c!=''){
arr[length++]=c-'0';
}
}
printf(“[”);
对于(i=0;i
为什么
length
在使用之前没有初始化为某个值?我修复了这个问题。我注意到我使用的是
getchar
,它一次扫描一个字符。
scanf
能帮上忙吗?但这并不能解释为什么我得到
-21
[-3,2,1]
。你得到的是
-3
,因为
-
的ASCII比
0
的ASCII小3
getchar()
只获取一个字符,而不考虑字符的类型,因此您将减号视为一个数字。这非常有意义。有没有办法将
'-'
转换成
-
?或者我应该使用一个完全不同于getchar的方法吗?神奇的是在
sscanf()
调用中,它将指向
char*
字符串的指针作为第一个参数。第二个参数表示应将一个整数读入
c
,并将字节量读入
bytesread
。使用
bytesread
增加指针
input1
。这样,您就可以更改读取下一个整数的位置。
int main(void)
{
    int c;
    int i, num = 0, neg = 0;
    while ((c = getchar()) != EOF) {
        if (c != ' ') {
            if (c == '-') {
                neg = 1;
            } else {
                i = c - '0';
                num = num * 10 + i;
            }
        } else {
            (neg == 1) ? num *= -1 : num;
            printf("%d\n", num + 2);    // this is just to show that you indeed get an integer and addition works
            num = 0;
            neg = 0;
        }
    }
}
#include"stdio.h"
#include"string.h"
#include"stdlib.h"

#define CHUNK 1000000

#define INT_COUNT 10000


int main(void) {
/*get all of the input*/
char temp_str[CHUNK] = "";
char* full_string = malloc(CHUNK * sizeof(char));
if (full_string == 0) {
    printf("Memory Error\n");
    exit(1);
}
int count = 2;
do {
    fgets(temp_str, CHUNK, stdin);
    strcat(full_string, temp_str);
    full_string = realloc(full_string, count * CHUNK * sizeof(char));
    if (full_string == 0) {
        printf("Memory Error\n");
        exit(1);
    }
    count++;
} while (strlen(temp_str) == CHUNK - 1 && temp_str[CHUNK - 2] != '\n');

//parse the input
char* token = strtok(full_string, " ");

int* arr = malloc(INT_COUNT * sizeof(int)), length = 0;
if (arr == 0) {
    printf("Memory Error\n");
    exit(1);
}

count = 1;

while (token != 0) {
    arr[length] = atoi(token);
    token = strtok(0, " ");
    length++;
    if (length == count * INT_COUNT) {
        count++;
        arr = realloc(arr, count * INT_COUNT);
        if(arr == 0) {
            printf("Memory Error\n");
            exit(1);
        }
    }
}

free(full_string);

//print the integers
for (int i = 0; i < length; i++) {
    printf("%d ", arr[i]);
    if (i % 20 == 0 && i != 0) {
        printf("\n");
    }
}

free(arr);

return 0;
}
#include <stdio.h>
int main () {
  int arr[1000]={0}, length = 0, c, i;
  while (1) {
    c = getchar();

if(c=='-')
{
    //minus sign has problem yet. I ll come back once I have better soln.
}
else if(c==' ' || c=='\n')
{
    length-=2;
    arr[length]= arr[length]*10 + arr[length+1];
    length++;
    if(c=='\n')
    {
        break;
    }
}
else if (c != ' ') {
  arr[length++] = c - '0';
    }
 }
  printf("[");
  for (i = 0; i < length-1; i++ ) {
 printf("%d,", arr[i]);
  }
  printf("%d]\n", arr[length-1]);
}