【发布时间】:2015-03-22 15:01:55
【问题描述】:
我想将数组中的元素向右移动,并用零(作为字符串)填充移动的元素。但这比我想象的要难。
这个程序读取一个包含两行的文本文件。每行包含一个整数(定义了最大长度)。它将每一行保存在一个单独的数组中。
稍后我需要将这两个数组转换为整数数组并进行一些算术运算。
为此,我需要确保这两个数组的长度相同
例如我的输入是:
num_first : 1859654
num_second:5654
现在我需要它们:
num_First : 1859654(它大于第二个数字所以它不会改变)
num second: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';
}
}
edit 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);
}
【问题讨论】:
-
那么你的问题是什么?除非您提供明确的问题,否则您可以自己完成其余的工作。
-
已编辑。我想根据输入数字的差异添加那些前导零。
-
如果您能提供解决方案并在代码中使用它然后分享它,我们将不胜感激。我也不想打印任何东西。我只想添加前导零然后保存到数组。
-
@vvvsg 在 C 中,char 或字符串数组几乎相同,在您的情况下也非常相似。我发表评论的目的是帮助您自己解决问题,而不是提供对您帮助较少的直接答案。