这是我的 get_polynom 函数的代码,请告诉我如何接受输入和空格并根据问题将它们存储在两个不同的数组中。预先感谢:)
*****注意:除了 printf() 和 scanf() 之外,我不允许使用任何默认的 C 函数。************
void get_polynom( int coeff[ ], int exp[ ] )
{
init_polynom( coeff, exp); /* calling the initialization function*/
int i=1;
int terms = 0;
int inputs=0;
int arr[ASIZE];
scanf("%d", &terms);
for(; i<terms*2; i++){
scanf("%d", &inputs);
arr[i] = inputs;
printf( "Your array has these values: ", arr[i] );
}
for(i=0; i< terms; i++){
coeff[i] = arr[2*i];
exp[i] = arr[2*i +1];
}
最佳答案
您错过了初始化变量 i
在 for 循环中 for(; i<terms*2; i++){
并且您没有指定格式说明符 %d
在这里printf( "Your array has these values: ", arr[i] );
这是工作代码,我还编写了 print_poylynom
功能:-)
`#include<stdio.h>
void print_polynom(int coeff[],int exp[],int terms)
{
int i=0;
printf("the polynomial is \n");
for(i=0;i<terms;i++)
{
printf("%dX^%d",coeff[i],exp[i]);
if(i<terms-1)
printf("+");
}
}
void init_polynom(int coeff[], int exp[])
{
int i=0;
for(i=0; i<100; i++)
{
coeff[i]=0;
exp[i]=0;
}
}
void get_polynom( int coeff[ ], int exp[ ] )
{
init_polynom( coeff, exp); //calling the initialization function
int i=1;
int terms = 0;
int inputs=0;
int arr[100];
scanf("%d", &terms);
for(i=0; i<terms*2; i++)
{
scanf("%d", &inputs);
arr[i] = inputs;
printf( "Your array has these values:%d\n", arr[i] );
}
for(i=0; i< terms; i++)
{
coeff[i] = arr[2*i];
exp[i] = arr[2*i +1];
}
print_polynom(coeff,exp,terms);
}
int main()
{
int i=0;
int coeff[100],exp[100];
get_polynom(coeff,exp);
return 0;
}
`
关于c - 如何读取带有空格的输入并将输入排序到 2 个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266892/