gpt4 book ai didi

c - 函数 fopen/calloc/malloc 及其替换方法

转载 作者:行者123 更新时间:2023-11-30 16:10:23 25 4
gpt4 key购买 nike

我想制作一个简单的程序(但对我来说很难),它将通过字母(姓名的部分内容使用数字输入)和数字(数字的部分内容)查找联系人。输入 - 来自标准输入(txt 文件)的数字,输出 - 包含这些数字(字母)的联系人。联系人文件如下所示

(姓名)
(数量)
(姓名)
(...)

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

#define MAX_NAME (128)
#define MAX_NUM (32)
/*
IN :
contacts.txt :
Sad Mirrow
38074025
Deniel Kovalski
78032596
Miky Trance
88055535
Martin Worried
77432651

96 [key number from standard entry]

OUT:

Deniel Kovalski
[because 96 matches in his number]
Martin Worried
[96 matches in his name Wo]
*/


typedef struct Contact {
char* name;
char* number;
} Contact;

char matchTable[10][9] = {
"0+", "1", "2abcABC", "3defDEF", "4ghiGHI",
"5jklJKL", "6mnoMNO", "7pqrsPQRS", "8tuvTUV", "9wxyWXY"
};


bool find(char c, char key){

int j = key - '0';
for (int i = 0; matchTable[j][i] != '\0'; i++){
if (c == matchTable[j][i])
return true;
}
return false;

}

bool matches(char* src, char* key){

unsigned int i,j;

for (i = 0; src[i] != '\0'; i++){
int tmp = i;
for (j = 0; key[j] != '\0'; j++){
if (find(src[tmp], key[j]))
tmp++;
else
break;
}
if (j == strlen(key))
return true;
}
return false;
}

int main(){

char key[MAX_NUM];
scanf("%s", key);
size_t arrSize = 32;

Contact* contacts = malloc(arrSize * sizeof(Contact));

int k = 0;
size_t nameSize = MAX_NAME;
size_t numSize = MAX_NUM;
char *nameBuf = malloc(MAX_NAME);
char *numBuf = malloc(MAX_NUM);

FILE* f = fopen("contacts.txt", "r");


while (fgets(nameBuf, nameSize, f)
&& fgets(numBuf, numSize, f)){
contacts[k].name = malloc(MAX_NAME);
contacts[k].number = malloc(MAX_NUM);
strcpy(contacts[k].name, nameBuf);
strcpy(contacts[k].number, numBuf);

k++;
if (k == arrSize);
arrSize <<= 1;
contacts = realloc(contacts, arrSize * sizeof(Contact));
}

for (int i = 0; i < k; i++){
bool matchesName = matches(contacts[i].name, key);
bool matchesNumber = matches(contacts[i].number, key);

if (matchesName || matchesNumber)
printf("%s\n", contacts[i].name);
}

for (int i = 0; i < k; i++){
free(contacts[i].name);
free(contacts[i].number);
}
free(contacts);
free(nameBuf);
free(numBuf);
fclose(f);

return 0;
}

一开始我们尽力了。到了完成任务条件的时候,问题就来了。它需要在没有 malloc/calloc/fopen 的情况下完成。我试图修复所有问题,但遇到了一个问题,即该程序无法运行,在我看来,我很困惑。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_NAME (128)
#define MAX_NUM (64)

struct Folio
{
char name[1000];
char num[1000];
};

static char matchTable[10][9] = {
"0+", "1", "2abcABC", "3defDEF", "4ghiGHI",
"5jklJKL", "6mnoMNO", "7pqrsPQRS", "8tuvTUV", "9wxyWXY"
};


int find(char c, char key){

int j = key - '0';
for (int i = 0; matchTable[j][i] != '\0'; i++){
if (c == matchTable[j][i])
return 1;
}
return 0;

}

int matches(char* src, char* key){

unsigned int i,j;

for (i = 0; src[i] != '\0'; i++){
unsigned int tmp = i;
for (j = 0; key[j] != '\0'; j++){
if (find(src[tmp], key[j]))
tmp++;
else
break;
}
if (j == strlen(key))
return 1;
}
return 0;
}


int main(){

char key[MAX_NUM];
scanf("%s", key);

struct Folio contacts[42]; //Entry entries[42]

int contacts_count = 0; //entries_count = 0;


//FILE* f = fopen("seznam.txt", "r");
char name[1000];
char num[1000]; //number[1000]



while (fgets(name, MAX_NAME, stdin) != NULL && fgets(num, MAX_NUM, stdin) != NULL)
{
// copy to struct
strcpy(contacts[contacts_count].name, name);
strcpy(contacts[contacts_count].num, num);

contacts_count++;
}

for (int i = 0; i < contacts_count; i++){
int matchesName = matches(contacts[contacts_count].name, key);
int matchesNumber = matches(contacts[contacts_count].num, key);

if (matchesName || matchesNumber)
printf("%s%s\n", contacts[contacts_count].name, contacts[contacts_count].num);
}

//fclose(f);

return 0;
}

想请教有经验的程序员。

最佳答案

matchTable[10][9] 太小,无法根据 matchTable[ 的需要将 "7pqrsPQRS" 保存为字符串 j][i] != '\0';。需要 10 个。

建议

//static char matchTable[10][9] = {
// "0+", "1", "2abcABC", "3defDEF", "4ghiGHI",
// "5jklJKL", "6mnoMNO", "7pqrsPQRS", "8tuvTUV", "9wxyWXY"
//};

static char *matchTable[10] = {
"0+", "1", "2abcABC", "3defDEF", "4ghiGHI",
"5jklJKL", "6mnoMNO", "7pqrsPQRS", "8tuvTUV", "9wxyWXY"
};
<小时/>

也许还有其他问题。

关于c - 函数 fopen/calloc/malloc 及其替换方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58900632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com