C - 将字符串添加到数组 x 中并将其从数组 y 中删除

标签 c arrays pointers

我创建了一个游戏平台,我想交易一些游戏。

因此,我设法编写了一个代码,用户可以在其中将他的游戏出售到市场,而另一个用户如果有信用,则可以随时购买它。为了做到这一点,我需要将已售出的游戏添加到买方的库中并将其从卖方的库中删除,但我缺少一些东西。

我写的代码或多或少是:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

typedef struct {
    char username [15];
    char email [25];
    char surname [20];
    char name [20];
    char sex;
    int hours;
    int level;
    float credit;
    char* library[20];
} User;

typedef struct {
    char id_code [7];
    char name [35];
    char developer [30];
    char category [15];
    float price;
    int pegi;
    char seller [15];
} Game;

...

int main() {
    int max=1, z=0, u=0;
    User signed_up [max];
    Game database [20], marketplace [20];
...
system("pause");
return 0;
}

...

void buy_game (int max, int *pz, int *pu, User signed_up[], Game database[], Game marketplace[]) {
    int i=0, j=0, h=0, y=0, x=0, a=0, b=0, w=0, v=0, c=0;
    bool game_found=false, user_found=false, not_owned_game=false;
    char input_game [35], input_user[15];

    v=*pz;
    w=*pu;

    ...

    printf("\n%*sInserisci username dell'utente con cui acquistare il gioco: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    printf("\n");

    for(i=0;i<max-1;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            user_found=true;

            printf("%*sInserisci nome del gioco da acquistare: ", 26, "");
            fflush(stdin);
            fgets(input_game, sizeof (input_game), stdin);
            input_game[strlen(input_game)-1]='\0';
            printf("\n");

            for (j=0;j<w;j++) {
                if (strcmp(input_game, marketplace[j].name)==0) {
                    game_found=true;

                    for (h=0;h<max-1;h++) {
                        if (strcmp(signed_up[h].username, marketplace[j].seller)==0) {
                            for (y=0;y<v;y++) {
                                if (strcmp(marketplace[j].name, signed_up[h].library[y])==0) {
                                    for (a=0;a<v;a++) {
                                        if (strcmp(marketplace[j].name, signed_up[i].library[a])!=0) {
                                            not_owned_game=true;

                                            if (marketplace[j].price<=signed_up[i].credit) {

                                            printf("\n%*sOperazione avvenuta con successo!\n", 26, "");
                                            signed_up[i].credit=signed_up[i].credit-marketplace[j].price;
                                            printf("\n%*sIl credito attuale dell'utente %s %c: EUR %.2f\n", 26, "", signed_up[i].username, 138, signed_up[i].credit);
                                            signed_up[h].credit=signed_up[h].credit+marketplace[j].price;
                                            printf("\n%*sIl credito attuale dell'utente %s %c: EUR %.2f\n\n\n", 26, "", signed_up[h].username, 138, signed_up[h].credit);

                                            signed_up[i].library[y]=marketplace[j].name;
                                            //add game to buyer's library

                                            do {
                                                signed_up[h].library[y]=signed_up[h].library[y+1];
                                                y+=1;
                                            } while (y<v-1);
                                            signed_up[h].library[v-1]="";
                                            //remove game from seller's library

                                            do {
                                                marketplace[j]=marketplace[j+1];
                                                j+=1;
                                            } while (j<w-1);
                                            *pu-=1;
                                            //remove game from marketplace
                                            } else {
                                                printf("%*sCredito acquirente insufficiente! Si prega di ricaricare il conto.\n\n", 26, "");
                                            }
                                        }
                                    } if (not_owned_game==false) {
                                        printf("%*sGioco inserito gi%c presente nella libreria dell'utente!\n\n\n", 26, "", 133);
                                    }
                                }
                            }
                        }
                    }
                }
            } if (game_found==false) {
                printf("%*sGioco inserito non presente nel marketplace!\n\n\n", 26, "");
            }
        }
    } if (user_found==false) {
        printf("\n%*sUsername inserito non presente nel database!\n\n\n", 26, "");
    }
}

我遇到的问题是,当我检查用户的库时,程序有一个奇怪的行为,因为它不能正确记住买家和卖家的库;事实上,该程序拒绝显示其库中的任何游戏。

最佳答案

数组具有固定大小,因此您无法删除条目。您可以覆盖,也可以改变位置,这两种情况都可能导致数组中不再存在特定值。

因此,请考虑当固定大小数组在逻辑上少包含一项时您希望它是什么样子,因为您实际上无法缩小它。然后思考需要进行哪些更改才能将数组突变为所需的状态,最后编写代码。

关于C - 将字符串添加到数组 x 中并将其从数组 y 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46223910/

相关文章:

c++ - 使用递归从子集总和中找到最大总和

javascript - 如何检查子对象中是否包含 id?

c - 为什么在函数中将 char 数组作为参数传递并尝试在函数内部进行修改会显示段错误?

C++:如何提高经常被复制的自定义类的性能?

c - 链表中不兼容指针类型的赋值 (C)

c - 链表不打印所有值

c# - 按自定义顺序对字符串数组进行排序

c - C中的指针。通过函数更改列表指针的开头

c - 为什么 `int *p = (char *) &num' 会导致警告?

c - 与 coccinelle 匹配 *ptr = value