gpt4 book ai didi

c - 移动字符串数组中的元素并用零填充

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:02 25 4
gpt4 key购买 nike

我想将数组中的元素向右移动并用零填充移动后的元素(作为字符串)。但这比我想象的要难一些。

这个程序读取一个包含两行的文本文件。每行包含一个整数(定义了最大长度)。它将每一行保存在一个单独的数组中。

稍后我需要将这两个数组转换为整数数组并进行一些算术运算。

为此,我需要确保这两个数组具有相同的长度

例如我的输入是:

num_first : 1859654

num_second:5654

现在我需要它们是:

num_First : 1859654(它比第二个数字大所以不会改变)

第二个数:0005654(比第一个小,所以我们需要添加那些前导零)

我如何根据两个输入的差异将那些前导零添加到数组?

最后我想将它们保存在数组中(作为字符串或整数)。

#include <stdio.h>
#include <string.h>
#define SIZE_MAX 20

int main()
{

FILE *fPTR;

char num_first[SIZE_MAX]; // string input
char num_second[SIZE_MAX];

if ((fPTR = fopen("input.txt", "r")) == NULL) // our file contains two line of integers. one at each
{
puts("File could not be opened.");
}
else
{
if (fgets(num_first, SIZE_MAX, fPTR) != NULL) // reads first line and saves to num_first
puts(num_first); // prints first number
if (fgets(num_second, SIZE_MAX, fPTR) != NULL) // reads second line and saves to num_second
puts(num_second); // prints second number
fclose(fPTR);
}

// getting strings lengths
int fLEN = strlen(num_first) - 1;
int sLEN = strlen(num_second);


int e = 0; // difference between two string lengths

// here we get the difference and it's the place which i want to shif the arrays

if (fLEN>sLEN) // first string is bigger than second
{
e = fLEN-sLEN;
}
else if (sLEN>fLEN) // second string is bigger than first
{
e = sLEN-fLEN;
}
else // there is no difference between two strings
{
e = fLEN-sLEN;
}

}

编辑:我还有另一个想法,但它没有按预期工作。

if (fLEN>sLEN) // first string is bigger than second
{
e = fLEN-sLEN;
for(i=e;i>=0;i--)
{
num_second[i+1] = num_second[i];
}
for(i=fLEN-e;i>=0;i--)
{
num_second[i] = '0';
}
}

编辑 2:(慢慢开始工作)打印更多零。试图修复它

    #include <stdio.h>
#include <string.h>
#define SIZE_MAX 20

int main()
{

FILE *fPTR;

char num_first[SIZE_MAX]; // string input
char num_second[SIZE_MAX];
char num_second2[SIZE_MAX] = {0};
int i = 0;
char numbers[SIZE_MAX];

if ((fPTR = fopen("input.txt", "r")) == NULL) // our file contains two line of integers. one at each
{
puts("File could not be opened.");
}
else
{
if (fgets(num_first, SIZE_MAX, fPTR) != NULL) // reads first line and saves to num_first
puts(num_first); // prints first number
if (fgets(num_second, SIZE_MAX, fPTR) != NULL) // reads second line and saves to num_second
puts(num_second); // prints second number
fclose(fPTR);
}

// getting strings lengths
int fLEN = strlen(num_first) - 1;
int sLEN = strlen(num_second);


int e = 0; // difference between two string lengths
int h = 4;

// here we get the difference and it's the place which i want to shif the arrays

if (fLEN>sLEN) // first string is bigger than second
{
e = fLEN-sLEN;
while (i>= h)//for(i=e;i>=0;i--)
{
num_second2[i+h] = num_second[i];
i++;
h--;
}
for(i=fLEN-e;i>=0;i--)
{
// num_second[i] = '0';
}
}
else if (sLEN>fLEN) // second string is bigger than first
{
e = sLEN-fLEN;
}
else // there is no difference between two strings
{
e = fLEN-sLEN;
}
printf("\n%d\n", e);
for (i = 0; i < SIZE_MAX; i++) // using c to print
{
printf("%d", num_second2[i]);
}
puts(num_second);
}

最佳答案

#include <stdio.h>
#include <string.h>
#define SIZE_MAX 20

int main()
{

FILE *fPTR;

char num_first[SIZE_MAX]; // string input
char num_second[SIZE_MAX];
char num_zeros[SIZE_MAX];//array for leading zeros
int i = 0;
char numbers[SIZE_MAX];

if ((fPTR = fopen("input.txt", "r")) == NULL) // our file contains two line of integers. one at each
{
puts("File could not be opened.");
}
else
{
if (fgets(num_first, SIZE_MAX, fPTR) != NULL) // reads first line and saves to num_first
puts(num_first); // prints first number
if (fgets(num_second, SIZE_MAX, fPTR) != NULL) // reads second line and saves to num_second
puts(num_second); // prints second number
fclose(fPTR);
}
for ( i = 0; i < SIZE_MAX; i++)
{
num_zeros[i] = '0';//fill array with '0's
}
// getting strings lengths
int fLEN = strlen(num_first);
if ( fLEN && num_first[fLEN - 1] == '\n')
{
num_first[fLEN - 1] = '\0';//remove trailing newline
fLEN--;
}
int sLEN = strlen(num_second);
if ( sLEN && num_second[sLEN - 1] == '\n')
{
num_second[sLEN - 1] = '\0';//remove trailing newline
sLEN--;
}
int e = 0; // difference between two string lengths
// here we get the difference and it's the place which i want to shif the arrays
if (fLEN>sLEN) // first string is bigger than second
{
e = fLEN-sLEN;
num_zeros[e] = '\0';//terminate array leaving e leading zeros
strcat ( num_zeros, num_second);
strcpy ( num_second, num_zeros);
}
else if (sLEN>fLEN) // second string is bigger than first
{
e = sLEN-fLEN;
while ( fLEN >= 0)//start at end of array
{
num_first[fLEN + e] = num_first[fLEN];//copy each element e items from current location
fLEN--;// decrement length
}
while ( e)// number of leading zeros
{
e--;
num_first[e] = '0';// set first e elements to '0'
}
}
else // there is no difference between two strings
{
//e = fLEN-sLEN;
}
puts(num_first);
puts(num_second);
return 0;
}

关于c - 移动字符串数组中的元素并用零填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29198139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com