Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组左移(C)_C - Fatal编程技术网

数组左移(C)

数组左移(C),c,C,我对C中的数组有一个小问题。 我有4个元素的数组。我必须这样做: 初始数组 0 0 0 0 输入1 0 0 0 1 输入2 0 0 1 2 投入3 0 1 2 3 输入4 1 2 3 4 输入5 2 3 4 5 输入6 3 4 5 6 我不知道;你能给我一些提示吗?你可以像这样移动数组中的数据 假设您有一个名为“a”的全局数组变量 您可以调用“add”函数将数据添加到数组的末尾 int a[4]; void add(int element) { for (int

我对C中的数组有一个小问题。 我有4个元素的数组。我必须这样做:

初始数组

0 0 0 0 
输入1

0 0 0 1 
输入2

0 0 1 2
投入3

0 1 2 3
输入4

1 2 3 4 
输入5

2 3 4 5 
输入6

3 4 5 6 

我不知道;你能给我一些提示吗?

你可以像这样移动数组中的数据

假设您有一个名为“a”的全局数组变量 您可以调用“add”函数将数据添加到数组的末尾

int a[4];

void add(int element)
{
    for (int i=0; i<3; i++)
        a[i] = a[i+1];

    a[3] = element;
}

这是我做类似项目的方式。它不是每次按下新键时都移动每个元素,而是在按下时检查从当前位置开始的正确输入代码[]

#include <stdio.h>

#define ALEN 4

// for testing, just use stdout/stdin...
void accept() { printf("\nACCEPT\n"); }
void deny() { printf("\nDENY\n"); }
char input() { return (char)getchar(); }


int main() {
    char a[ALEN];
    char code[ALEN] = {'1','2','3','4'};
    int pos = 0;
    char keypressed;
    while (keypressed = (char)getchar()) {
        if (keypressed == '#') {
            if (a[pos] == code[0] && a[(pos+1)%4] == code[1] && a[(pos+2)%4] == code[2] && a[(pos+3)%4] == code[3]) {
                accept();
            } else {
                deny();
            }
        } else {
            a[pos] = keypressed;
            pos++;
            if (pos == ALEN) {
                pos = 0;
            }
        }
    }
}

你在问什么?你在找人帮你吗?你试过什么吗?张贴代码。我和我的代码打开门,但这不是我想要的。当我按下的数组被重置,下一个4号是密码,并且当我按下的代码被接受时,我的程序被激活。我需要一些不需要重置的东西,当我按下一些随机数字,最后4个数字是我的密码时,我按下并打开门。代码:枚举{A_SIZE=4}是否有优点;int a[a_SIZE];对于int i=0;i//code your initialisation and your key_hook //then call tab = shift_and_add(oldtab, new); with new, your new integer int *shift_and_add(int *oldtab, int new, unisgned int size) { unsigned int i = 1; unsigned int j = 0; newtab = (int *)malloc(sizeof(int) * size); while (i < size) { newtab[j] = oldtab[i]; i++; j++; } newtab[j] = new; return (newtab); }