首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环数组实现

循环数组实现
EN

Stack Overflow用户
提问于 2013-02-26 04:27:54
回答 3查看 12.2K关注 0票数 6

到目前为止,这是我的循环数组的实现。它应该存储最后输入的5个命令,方法是在第5个命令的位置输入第6个命令,然后丢弃第1个命令。到目前为止,我所做的是,能够存储这5个命令并将它们打印出来。在第六个命令中,我注意到它位于historyArray的第二个位置(k=1),但在调试时,k等于0,这至少会将最后一个命令推入顶部。如果你能让我重回正轨,我将不胜感激。以下是代码的一部分。

代码语言:javascript
复制
#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);
}

建议之后(仍未完成):

代码语言:javascript
复制
         j = 0;
         //elementCounter = 0;
         printf("327>");
         gets(inputString);

         strcpy (historyArray[k], inputString);
         k = (k+1) % 5;

        if (elementCounter <= 5)
         {         
          elementCounter++;                
         }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-26 04:51:42

您所描述的bug是由于以下几行而发生的:

代码语言:javascript
复制
k = (k + 1) % 5;
elementCounter++;

我看到的是:

代码语言:javascript
复制
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列的内容是您将看到的...

代码语言:javascript
复制
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处插入新命令。因此,在适当调整算法后,您的历史应该如下所示:

代码语言:javascript
复制
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]
票数 5
EN

Stack Overflow用户

发布于 2013-02-26 19:14:17

这是我尝试过的,它似乎像预期的那样工作:

代码语言:javascript
复制
             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留空。最后一步用命令填充位置。这不是最优雅的一段代码,但似乎做到了这一点。

票数 1
EN

Stack Overflow用户

发布于 2018-05-30 13:32:27

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15075873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档