我有一个字符串数组,我想在用户输入字符串时动态更改。此2D数组具有固定大小,例如char A[N][BUFF_MAX]
将存储N
个字符串,每个字符串最多为BUFF_MAX
。我希望这个数组循环,也就是说,最新的输入应该存储在A[0]
中,依此类推。
到目前为止,我在实现上述方面没有任何问题。当用户输入超过N
个字符串时,我的问题就出现了。此时我想存储最后 N
个输入,并将其余输出丢弃为仅使用N
个单元格。可以使用strcpy
进行移位。
现在,我想用字符串编号打印这个数组。例如,如果用户输入M
字符串,其中M < N
字符串,则应打印
1 string_1
2 string_2
: :
M string_M
但是,如果M > N
则应打印
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();
}
}
感谢您的帮助。
答案 0 :(得分:1)
这是您的代码,但有一些更改
#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;
}
请记住,您的代码没有断点才能退出程序。您需要在break
循环中添加return
或goto
或while
才能完成执行!!
答案 1 :(得分:1)
这就是我的想法。请注意,我避免“无限循环”并且程序检测到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;
}
示例运行:
Line 1
hist
1 Line 1
line 2
hist
1 Line 1
2 line 2
line 3
xyz 4
asd 5
hist
1 Line 1
2 line 2
3 line 3
4 xyz 4
5 asd 5
Emergency 6
Helpline 7
hist
1 Line 1
2 line 2
3 line 3
4 xyz 4
5 asd 5
6 Emergency 6
7 Helpline 7
Looping Later 8
Last Line Before Recycling 9
hist
1 Line 1
2 line 2
3 line 3
4 xyz 4
5 asd 5
6 Emergency 6
7 Helpline 7
8 Looping Later 8
9 Last Line Before Recycling 9
First lot of recycling 10
hist
2 line 2
3 line 3
4 xyz 4
5 asd 5
6 Emergency 6
7 Helpline 7
8 Looping Later 8
9 Last Line Before Recycling 9
10 First lot of recycling 10
Keep going
Dozens of lines
Bad Luck
A fortnight
hist
6 Emergency 6
7 Helpline 7
8 Looping Later 8
9 Last Line Before Recycling 9
10 First lot of recycling 10
11 Keep going
12 Dozens of lines
13 Bad Luck
14 A fortnight