使用全局计数器在C数组中循环
我有一个字符串数组,当用户输入字符串时,我希望动态更改该数组。此2D数组具有固定大小,例如使用全局计数器在C数组中循环,c,arrays,string,C,Arrays,String,我有一个字符串数组,当用户输入字符串时,我希望动态更改该数组。此2D数组具有固定大小,例如char a[N][BUFF\u MAX]将存储N字符串,每个字符串的大小最多为BUFF\u MAX。我想让这个数组循环,也就是说,最近的输入应该存储在A[0]中,依此类推 到目前为止,我在实现上述功能方面没有问题。当用户输入的字符串超过N时,我的问题就出现了。此时,我想存储最后的N输入,并放弃其余的,只使用N单元格。可以使用strcpy进行移位 现在,我想用字符串号打印这个数组。例如,如果用户输入M字符串
char a[N][BUFF\u MAX]
将存储N
字符串,每个字符串的大小最多为BUFF\u MAX
。我想让这个数组循环,也就是说,最近的输入应该存储在A[0]
中,依此类推
到目前为止,我在实现上述功能方面没有问题。当用户输入的字符串超过N
时,我的问题就出现了。此时,我想存储最后的N
输入,并放弃其余的,只使用N
单元格。可以使用strcpy
进行移位
现在,我想用字符串号打印这个数组。例如,如果用户输入M
字符串,其中M
字符串,则应打印
1 string_1
2 string_2
: :
M string_M
M-N string_(M-N)
M-N+1 string_(M-N+1)
: :
M string_M
但是,如果M>N
,则应打印
1 string_1
2 string_2
: :
M string_M
M-N string_(M-N)
M-N+1 string_(M-N+1)
: :
M string_M
我试过好几种方法,但都不能完全奏效。这是我的代码的简化版本
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define N 10 /* max size of array */
#define BUFF_MAX 50 /* max size of buffer */
int M = 0; /* input count */
char A[N][BUFF_MAX]; /* should i initialize this? */
void displayArray() {
for (int h = 0; h < M; h++) { /* print the elements */
printf("%2d %s\n", h + 1, A[h]); /* this doesn't number properly */
/* and needs to be modified */
}
}
int main(void) {
while(1) { /* keep reading from the user */
char input[BUFF_MAX];
fgets(input, sizeof(input), stdin); /* read input from user */
strcpy(A[M], input); /* copy input into array */
M++; /* i might need some modulo here */
if (M >= N) { /* if user inputs more than N */
for (int i = N - 1; i > 0; i--) { /* shift the array */
strcpy(A[i], A[i - 1]);
}
}
if (!strcmp(input, "hist"))
displayArray();
}
}
#包括
#包括
#包括
#包括
#定义N 10/*阵列的最大大小*/
#定义缓冲区的BUFF_MAX 50/*最大大小*/
int M=0;/*输入计数*/
字符A[N][BUFF_MAX];/*我应该初始化这个吗*/
void displayArray(){
对于(inth=0;h=N){/*如果用户输入超过N*/
对于(int i=N-1;i>0;i--){/*移动数组*/
strcpy(A[i],A[i-1]);
}
}
如果(!strcmp(输入,“hist”))
displayArray();
}
}
谢谢您的帮助。这是您的代码,有一些改动
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define N 10 /* max size of array */
#define BUFF_MAX 50 /* max size of buffer */
int M = 0; /* input count */
char A[N][BUFF_MAX]; /* should i initialize this? */
void displayArray()
{
int h;
//you need to change the condition of for in order to print
// the adequate number of elements
for ( h = 0; h < (M>N?N:M); h++) /* print the elements */
{
printf("%2d %s\n", h + 1, A[h]); /* this doesn't number properly */
/* and needs to be modified */
}
}
int main(void)
{
while(1) /* keep reading from the user */
{
char input[BUFF_MAX];
fgets(input, sizeof(input), stdin); /* read input from user */
// you need to add the condition below because the fgets include the newline
// if the user hit enter after each input
if(input[strlen(input)-1]=='\n')
input[strlen(input)-1]='\0';
// note that the subscript of the array is changed in order to
// get the desired input A[ M % N ]
// so the sequence of index will be 0 1 2 ... N-1 0 1 2 etc.
strcpy(A[M % N], input); /* copy input into array */
M++; /* i might need some modulo here */
if (!strcmp(input, "hist"))
displayArray();
}
return 0;
}
#包括
#包括
#包括
#包括
#定义N 10/*阵列的最大大小*/
#定义缓冲区的BUFF_MAX 50/*最大大小*/
int M=0;/*输入计数*/
字符A[N][BUFF_MAX];/*我应该初始化这个吗*/
void displayArray()
{
int-h;
//您需要更改的条件才能打印
//足够数量的元素
对于(h=0;h<(M>N?N:M);h++)/*打印元素*/
{
printf(“%2d%s\n”,h+1,A[h]);/*此数字不正确*/
/*需要修改*/
}
}
内部主(空)
{
而(1)/*保持从用户处读取*/
{
字符输入[BUFF_MAX];
fgets(输入,sizeof(输入),标准输入);/*从用户处读取输入*/
//您需要添加以下条件,因为FGET包含换行符
//如果用户在每次输入后按enter键
如果(输入[strlen(输入)-1]='\n')
输入[strlen(输入)-1]='\0';
//请注意,数组的下标已更改,以便
//获取所需的输入A[M%N]
//所以索引的顺序是0112…N-1012等等。
strcpy(一个[M%N],输入);/*将输入复制到数组中*/
M++;/*这里可能需要一些模*/
如果(!strcmp(输入,“hist”))
displayArray();
}
返回0;
}
请记住,您的代码没有断点以退出程序。您需要在
while
循环中添加中断
或返回
或转到
,以完成其执行 这就是我的想法。注意,我避免了“无限循环”,程序检测到EOF并在EOF时停止。输入hist
会触发打印操作,但该行不会保存在历史记录中。此外,如果您观察得很清楚,您会注意到在10行缓冲区中只保存了9个条目。如果必须显示10行,则将N增加1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10 /* max size of array */
#define BUFF_MAX 50 /* max size of buffer */
int M = 0; /* monotonic input count */
int first_valid = 0;
int first_avail = 0;
char A[N][BUFF_MAX];
static
void displayArray(int v1, int v2)
{
int h = v1;
int x = M - N + 1;
if (x < 0)
x = 0;
while (h != v2)
{
printf("%2d %s\n", ++x, A[h++]);
if (h >= N)
h = 0;
}
}
int main(void)
{
char input[BUFF_MAX];
while (fgets(input, sizeof(input), stdin) != 0)
{
int len = strlen(input);
if (input[len-1] == '\n')
input[--len] = '\0';
if (strcmp(input, "hist") == 0)
displayArray(first_valid, first_avail);
else
{
strcpy(A[first_avail++], input);
M++;
if (first_avail >= N)
first_avail = 0;
if (first_avail == first_valid)
{
if (++first_valid >= N)
first_valid = 0;
}
}
}
return 0;
}
这段代码只输入一个字符串(除了不编译)。还有,你得到的是什么而不是预期的结果?@Inspired,这或多或少是伪代码。为了清楚起见,我不想包括所有内容。我会稍微修改一下,这样更容易理解。我正在寻找一种解决这个问题的通用方法,所以这是一个更一般的问题,而不是一个调试问题。我认为如果你给我们完整的代码比伪代码要好得多,因为后者只会让我困惑@我相信我明白这个想法,但我想看看你是怎么做到的,这样就很容易解决了!注意:
#定义N=10代码>是错误的;你应该写enum{N=10}
或#定义n10
。所有的转换都是毫无意义的——浪费能源。保留第一个有效条目和下一个可用条目的记录,以便覆盖。当其中一个数字达到上限时,将其包装回零。当下一个可用条目是第一个有效条目时,覆盖第一个有效条目并增加第一个有效条目跟踪器以转到下一个条目。冲洗并重复。这很有效,但编号仍然是从1到N。我希望它是从M-N到M。我可以很容易地解决这个问题,谢谢大家。这个答案更好,因为在开始时调用hist
不会像其他解决方案那样编号为负数