移动元素字符串数组,并用零填充 [英] shifting elements in an array of strings and fill with zeros
问题描述
我想在我的阵列转移元素右零填充移位元素(如字符串)。但它是一个有点难度比我想象。
此程序读取包含两行的文本文件。每一行包含一个整数(最大长度定义)。它保存在一个单独的阵列中的每个行。
我需要这两个数组转换以后整型数组,并做一些算术运算。
所以,我需要确保这两个数组有相同的长度
例如我的输入是:
num_first: 1859654
num_second: 5654
现在我需要他们是:
num_First: 1859654 (它比第二个大数目,因此不会改变)
NUM第二: 0005654 (比最初小,所以我们需要添加这些前导零)
我怎么这些前导零添加到阵列基于两个输入之差??
,最后我想将它们保存在阵列(如字符串或整数)。
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义SIZE_MAX 20诠释的main()
{FILE * FPTR; 烧焦num_first [SIZE_MAX] //字符串输入
烧焦num_second [SIZE_MAX] 如果((FPTR = FOPEN(input.txt的,R))== NULL)//我们的文件包含两行整数。逐个每
{
看跌期权(文件无法打开。);
}
其他
{
如果(与fgets(num_first,SIZE_MAX,FPTR)!= NULL)//读取第一行,并保存到num_first
看跌期权(num_first); //打印第一个数字
如果(与fgets(num_second,SIZE_MAX,FPTR)!= NULL)//读取第二行并保存到num_second
看跌期权(num_second); //打印第二个数字
FCLOSE(FPTR);
} //获取字符串长度
INT弗伦= strlen的(num_first) - 1;
INT SLEN = strlen的(num_second);
INT E = 0; //两个字符串长度的区别 //这里我们得到的差异,这是我想要的SHIF阵列地方 如果(弗伦> SLEN)//第一个字符串比第二大
{
E =弗伦-SLEN;
}
否则,如果(SLEN>弗伦)//第二个字符串比第一大
{
E = SLEN,弗伦;
}
否则//有两个字符串之间无差异
{
E =弗伦-SLEN;
}}
编辑:我也有另外一个想法,但按预期它不工作
如果(弗伦> SLEN)//第一个字符串比第二大
{
E =弗伦-SLEN;
对(我= E; I> = 0;我 - )
{
num_second第[i + 1] = num_second [I];
}
对于(I = FLEN-E; I> = 0;我 - )
{
num_second [I] ='0';
}
}
编辑2:(慢慢开始工作)打印更多为零。试图修复它。
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义SIZE_MAX 20诠释的main()
{FILE * FPTR; 烧焦num_first [SIZE_MAX] //字符串输入
烧焦num_second [SIZE_MAX]
焦炭num_second2 [SIZE_MAX] = {0};
INT I = 0;
字符数[SIZE_MAX] 如果((FPTR = FOPEN(input.txt的,R))== NULL)//我们的文件包含两行整数。逐个每
{
看跌期权(文件无法打开。);
}
其他
{
如果(与fgets(num_first,SIZE_MAX,FPTR)!= NULL)//读取第一行,并保存到num_first
看跌期权(num_first); //打印第一个数字
如果(与fgets(num_second,SIZE_MAX,FPTR)!= NULL)//读取第二行并保存到num_second
看跌期权(num_second); //打印第二个数字
FCLOSE(FPTR);
} //获取字符串长度
INT弗伦= strlen的(num_first) - 1;
INT SLEN = strlen的(num_second);
INT E = 0; //两个字符串长度的区别
INT H = 4; //这里我们得到的差异,这是我想要的SHIF阵列地方 如果(弗伦> SLEN)//第一个字符串比第二大
{
E =弗伦-SLEN;
而(ⅰ&GT = H)//为(ⅰ= E; I> = 0;我 - )
{
num_second2 [I + H] = num_second [I];
我++;
H - ;
}
对于(I = FLEN-E; I> = 0;我 - )
{
// num_second [I] ='0';
}
}
否则,如果(SLEN>弗伦)//第二个字符串比第一大
{
E = SLEN,弗伦;
}
否则//有两个字符串之间无差异
{
E =弗伦-SLEN;
}
的printf(\\ n%d个\\ N,E);
对于(i = 0; I< SIZE_MAX;我++)//用C打印
{
的printf(%d个,num_second2 [I]);
}
看跌期权(num_second);
}
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义SIZE_MAX 20诠释的main()
{ FILE * FPTR; 烧焦num_first [SIZE_MAX] //字符串输入
烧焦num_second [SIZE_MAX]
焦炭num_zeros [SIZE_MAX]; //数组前导零
INT I = 0;
字符数[SIZE_MAX] 如果((FPTR = FOPEN(input.txt的,R))== NULL)//我们的文件包含两行整数。逐个每
{
看跌期权(文件无法打开。);
}
其他
{
如果(与fgets(num_first,SIZE_MAX,FPTR)!= NULL)//读取第一行,并保存到num_first
看跌期权(num_first); //打印第一个数字
如果(与fgets(num_second,SIZE_MAX,FPTR)!= NULL)//读取第二行并保存到num_second
看跌期权(num_second); //打印第二个数字
FCLOSE(FPTR);
}
对于(i = 0; I< SIZE_MAX;我++)
{
num_zeros [I] ='0'; //填入阵列与'0的
}
//获取字符串长度
INT弗伦= strlen的(num_first);
如果(弗伦&安培;&安培; num_first [弗伦 - 1] =='\\ n')
{
num_first [弗伦 - 1] ='\\ 0'; //删除尾随的换行符
fLEN--;
}
INT SLEN = strlen的(num_second);
如果(SLEN和放大器;&安培; num_second [SLEN - 1] =='\\ n')
{
num_second [SLEN - 1] ='\\ 0'; //删除尾随的换行符
sLEN--;
}
INT E = 0; //两个字符串长度的区别
//这里我们得到的差异,这是我想要的SHIF阵列地方
如果(弗伦> SLEN)//第一个字符串比第二大
{
E =弗伦-SLEN;
num_zeros [E] ='\\ 0'; //终止阵列留下Ë前导零
strcat的(num_zeros,num_second);
的strcpy(num_second,num_zeros);
}
否则,如果(SLEN>弗伦)//第二个字符串比第一大
{
E = SLEN,弗伦;
而(弗伦> = 0)//开始在阵列结束
{
num_first [弗伦+ E] = num_first [FLEN]; //复制从当前位置的每个元素e项目
弗伦 - ; //递减长度
}
而(五)//前导零的数
{
E--;
num_first [E] = 0; //设置第一个电子元素,以0
}
}
否则//有两个字符串之间无差异
{
// E = FLEN,SLEN;
}
看跌期权(num_first);
看跌期权(num_second);
返回0;
}
I want to shift elements in my array to right and fill the shifted elements with zero (as string) . but it's a bit more difficult than i thought.
this program reads a text file containing two lines. each line contains of an integer (maximum length is defined). it saves each line in a separate array.
I need to convert these two arrays to integer array later and do some arithmetic operations.
so for that, i need to make sure that these two arrays have the same length
for example my input is:
num_first : 1859654
num_second: 5654
now i need them to be:
num_First : 1859654 (it's larger than second number so it doesn't change)
num second: 0005654 (smaller than first, so we need to add those leading zeros)
how do i add those leading zeros to array based on difference of two input??
and at last i want to save them in arrays (as string or integer).
#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;
}
}
edit: i also got another idea but it doesn't work as intended.
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: (slowly started to working) prints more zero. trying to fix it
#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;
}
这篇关于移动元素字符串数组,并用零填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!