到目前为止,这是我的循环数组的实现。它应该存储最后输入的5个命令,方法是在第5个命令的位置输入第6个命令,然后丢弃第1个命令。到目前为止,我所做的是,能够存储这5个命令并将它们打印出来。在第六个命令中,我注意到它位于historyArray的第二个位置(k=1),但在调试时,k等于0,这至少会将最后一个命令推入顶部。如果你能让我重回正轨,我将不胜感激。以下是代码的一部分。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
int i=0;
int j=0;
int k=0;
int tempIndex = 0;
int elementCounter = 0;
char inputString[100];
char *result=NULL;
char delims[] = " ";
char historyArray[5][20] = {0};
char tokenArray[20][20] ;
char hCommand[1][20];
do
{
j = 0;
printf("hshell>");
gets(inputString);
//skip writing "history" in historyArray
if (strcmp(inputString,"history")!= 0)
{
strcpy (historyArray[k], inputString);
}
k = (k+1) % 5;
if (elementCounter <= 5)
elementCounter++;
// Break the string into parts
result = strtok(inputString, delims);
while (result!=NULL)
{
strcpy(tokenArray[j], result);
j++;
result= strtok(NULL, delims);
}
if (strcmp(tokenArray[0], "exit") == 0)
return 0;
if (strcmp(tokenArray[0], "history") == 0)
{
if (j>1)
{
tempIndex = atoi(tokenArray[j]);
puts(tempIndex);
}
else
{
for (i=0; i<elementCounter-1;i++)
printf("%i. %s\n", i+1, historyArray[i]);
}
}
else
{
printf("Command not found\n");
}
} while (1);
}建议之后(仍未完成):
j = 0;
//elementCounter = 0;
printf("327>");
gets(inputString);
strcpy (historyArray[k], inputString);
k = (k+1) % 5;
if (elementCounter <= 5)
{
elementCounter++;
}发布于 2013-02-26 04:51:42
您所描述的bug是由于以下几行而发生的:
k = (k + 1) % 5;
elementCounter++;我看到的是:
k initial | calculation | k result | elementCounter
0 (0 + 1) % 5 1 % 5 = 1 1
1 (1 + 1) % 5 2 % 5 = 2 2
...
4 (4 + 1) % 5 5 % 5 = 0 5
0 (0 + 1) % 5 1 % 5 = 1 5据我所知,k的行为和它应该做的一样。但是,当elementCounter为5时,k= 1。
编辑:我看到的问题是最新的命令被添加到位置k,而不是位置0,根据您的实现,它是最近输入的命令(基于各种if子句,比如处理"exit“和"history”命令的子句)。使用您当前的算法尝试这组命令。我希望Command List列的内容是您将看到的...
Command # | Command Text | [Command List]
0 (null) []
1 Login [Login]
2 History [Login,History]
3 Skynet [Login,History,Skynet]
4 ps -al [Login,History,Skynet,ps -al]
5 Skynet [Login,History,Skynet,ps -al,Skynet]
6 Exit [Exit,History,Skynet,ps -al,Skynet]您要做的是复制元素0-3,并将它们移动到元素1-4。然后,在historyArray中的位置0处插入新命令。因此,在适当调整算法后,您的历史应该如下所示:
Command # | Command Text | [Command List]
0 (null) []
1 Login [Login]
2 History [History,Login]
3 Skynet [Skynet,History,Login]
4 ps -al [ps -al,Skynet,History,Login]
5 Skynet [Skynet,ps -al,Skynet,History,Login]
6 Exit [Exit,Skynet,ps -al,Skynet,History]发布于 2013-02-26 19:14:17
这是我尝试过的,它似乎像预期的那样工作:
j = 0;
//elementCounter = 0;
printf("hshell>");
gets(inputString);
strcpy (historyArray[k], inputString);
k = (k+1) % 5;
if (elementCounter <= 5)
{
elementCounter++;
}
if (elementCounter ==6)
{
k = 5;
for (i=0; i<5; i++)
{
strcpy(historyArray[i], historyArray[i+1]);
}
strcpy (historyArray[4], inputString);
}基本上,这将检查elementCounter是否变为6 (意味着已经输入了第六个命令)。如果是,则设置k=5,以便在数组的最后位置输入命令,然后将前4个值上移一个位置,将索引4留空。最后一步用命令填充位置。这不是最优雅的一段代码,但似乎做到了这一点。
发布于 2018-05-30 13:32:27
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
long n;
long k;
long q;
scanf("%ld %ld %ld",&n,&k,&q);
long *a = malloc(sizeof(long) * n);
long *b = malloc(sizeof(long) * n);
for(long a_i = 0; a_i < n; a_i++)
{
scanf("%ld",&a[a_i]);
}
for(long i = 0; i < k; i++)
{
b[0] = a[n-1];
for(long a_i = 1; a_i < n; a_i++)
{
b[a_i] = a[a_i-1];
}
for(long a_i = 0; a_i < n; a_i++)
a[a_i] = b[a_i];
}
for(long a0 = 0; a0 < q; a0++)
{
long m;
scanf("%ld",&m);
printf("%ld\n", b[m]);
}
return 0;
}https://stackoverflow.com/questions/15075873
复制相似问题