C 移位字符串数组中的元素并用零填充
我想将数组中的元素向右移位,并用零(作为字符串)填充移位后的元素。但这比我想象的要难一点 此程序读取包含两行的文本文件。每行包含一个整数(定义了最大长度)。它将每一行保存在一个单独的数组中 稍后我需要将这两个数组转换为整数数组,并进行一些算术运算 因此,我需要确保这两个数组具有相同的长度 例如,我的输入是: 数量优先:1859654 秒数:5654 现在我需要他们: num_First:1859654(它大于第二个数字,所以不会改变) num second:0005654(比第一个小,所以我们需要添加那些前导零) 如何根据两个输入的差值将这些前导零添加到数组中?? 最后,我想将它们保存在数组中(作为字符串或整数) 编辑2:(慢慢开始工作)打印更多的零。试图修复它C 移位字符串数组中的元素并用零填充,c,arrays,C,Arrays,我想将数组中的元素向右移位,并用零(作为字符串)填充移位后的元素。但这比我想象的要难一点 此程序读取包含两行的文本文件。每行包含一个整数(定义了最大长度)。它将每一行保存在一个单独的数组中 稍后我需要将这两个数组转换为整数数组,并进行一些算术运算 因此,我需要确保这两个数组具有相同的长度 例如,我的输入是: 数量优先:1859654 秒数: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];
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);
}
#包括
#包括
#定义最大尺寸为20
int main()
{
文件*fPTR;
char num_first[SIZE_MAX];//字符串输入
字符数秒[大小最大];
char num_second2[SIZE_MAX]={0};
int i=0;
字符数[SIZE_MAX];
if((fPTR=fopen(“input.txt”,“r”))==NULL)//我们的文件包含两行整数,每行一个
{
puts(“无法打开文件”);
}
其他的
{
if(fgets(num\u first,SIZE\u MAX,fPTR)!=NULL)//读取第一行并首先保存到num\u
put(num_first);//打印第一个数字
if(fgets(num\u second,SIZE\u MAX,fPTR)!=NULL)//读取第二行并保存到num\u second
puts(num_second);//打印第二个数字
fclose(fPTR);
}
//获取字符串长度
int fLEN=strlen(num_first)-1;
int sLEN=strlen(num_秒);
int e=0;//两个字符串长度之间的差值
int h=4;
//这里我们得到了差异,这是我想要移动数组的地方
if(fLEN>sLEN)//第一个字符串大于第二个字符串
{
e=弗伦-斯伦;
而(i>=h)//对于(i=e;i>=0;i--)
{
num_second2[i+h]=num_second[i];
i++;
h--;
}
对于(i=fLEN-e;i>=0;i--)
{
//num_second[i]=“0”;
}
}
else if(sLEN>fLEN)//第二个字符串比第一个字符串大
{
e=sLEN-fLEN;
}
else//两个字符串之间没有区别
{
e=弗伦-斯伦;
}
printf(“\n%d\n”,e);
对于(i=0;i
代码的良好开端
因为您正在读取字符串,所以可以使用字符串操作。
如果你想把它们读作int,你可以用对数函数来确定大小,但那太过分了
您可以将数字另存为整数,但随后必须推迟填充,直到稍后打印它们或将它们保存到文件中
用零左填充数字的最简单方法是使用sprintf()和正确的格式说明符右对齐数字然后您可以迭代结果的每个字符,并将空格(例如“0”)替换为“0”。这将创建左侧0填充的条目。例如,sprintf right在一个缓冲区中调整您的数字,该缓冲区有空间容纳您可以读取的最大大小的数字,并在左侧留下空格
然后在一个循环中,每次索引条目中的一个字符,并基于下面显示的MAX_NUMBER_LEN,跳过左侧不需要零的额外空格(例如MAX_NUMBER_LEN-maxPaddingLenYouCalculateAtRuntime),然后开始用零替换
然后,只需创建一个缓冲区,将其地址传递给sprintf(),该缓冲区分配有足够的空间来保存结果。它必须和你的最大长度一样大或更大。我称之为maxStrLen而不是e,因为为变量的用途命名变量使程序更易于理解和维护
对于如何分配适当大小的缓冲区,您有一些选择,包括使用malloc()。但确定整数的最大大小可能更容易。甚至还有一个C常量,它告诉您什么是32位或64位整数最大值,并根据该大小预先创建固定长度项的字符数组
例如:
#define MAX_ENTRIES = 10
#define MAX_NUMBER_LEN = 15
char numbers[MAX_ENTRIES][MAX_NUMBER_LEN]
这将为您提供存储sprintf()结果的存储空间。
例如:
sprintf(numbers[entryNumber], "*right-justifying format specifier you look up*", numberReadFromFile)
其中entryNumber是数组中要存储结果的插槽
获取sprintf地址时不需要包含的MAX_NUMBER_LEN部分(注意,我只是传入了数字[entryNumber],但不是第二组括号)。因为通过省略第二组括号,您表示希望在数字数组中找到与entryNumber对应的特定[MAX_NUMBER_LEN]块的地址。请注意,numberReadFromFile上也没有&,因为您将它们作为字符串读入字符数组,并且因为您要传递数组的第一个字符的地址,所以只需传递数组名称即可。或者,您可以传入&numberredfromfile[0]以获取要传递给sprintf()的第一个元素的地址
当以这种方式使用数组时,不需要使用&字符来获取要传递给sprintf()的变量地址,因为数组实际上只是C中指针的另一种表示法,理解它的工作原理是在C语言中有效使用的关键,并且值得进行初步的理解
下面是一个如何执行实际零paddi的示例
#define MAX_ENTRIES = 10
#define MAX_NUMBER_LEN = 15
char numbers[MAX_ENTRIES][MAX_NUMBER_LEN]
sprintf(numbers[entryNumber], "*right-justifying format specifier you look up*", numberReadFromFile)
/* numberToPad is a buffer containing a right-justified number, e.g. a number
* shifted-right by sprintf(), in a buffer sized 15 characters,
* space-padded on the left by sprintf's right-justifying format specifier.
* We want to convert the number to a 10-digit zero-padded number
* inside a 15 character field. The result should be 4 spaces, followed
* by some zeroes, followed by non-zero digits, followed by null-terminator.
* example: "ƀƀƀƀ0000012345\0" where ƀ is a space character.
*/
#define MAX_NUMBER_LEN 15 /* note: 15 includes null-terminator of string */
int maxNumberLenActuallyRead = 10;
int startZeroPaddingPos = MAX_NUMBER_LEN - maxNumberLenActuallyRead
char numberToPad[MAX_NUMBER_LEN];
int i;
for (i = 0; i < MAX_NUMBER_LEN; i++) {
if (i < startZeroPaddingPos)
continue;
if (numberToPad[i] == ' ')
numberToPad[i] = '0';
}
#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;
}