gpt4 book ai didi

C - 创建字符串矩阵时遇到问题

转载 作者:行者123 更新时间:2023-11-30 18:29:10 27 4
gpt4 key购买 nike

更新:弄清楚了...错误代码底部的更新代码按其应有的方式进行。谢谢大家

我正在尝试将 input.txt 文件中的行读入三个矩阵,一个用于名字,一个用于姓氏,一个用于成绩。

成绩矩阵很好,我无法计算出姓名矩阵。

运行时我得到输出:

Row 1, column 1 of grades: 1.30
Row 1, column 1 of first: P
Row 1, column 1 of last: V

这对于字符数组来说是不正确的。

我认为问题在于数组第一个和最后一个如何初始化。第一个例子在这里:

//Store first token in first[][]
if(counter == 1){
first[i][j] = token;
}

这是将名为 token 的字符串存储到数组 first[][] 的索引 [0][0] 中的正确方法吗?

初始化字符串矩阵的正确方法是什么?我需要第一行包含名字,第二行包含第二个名字,等等

我很难想象一个实际的矩阵与它们在 RAM 中的存储方式(我知道数组的名称是指向数组第一个索引的指针,但是当它到达二维数组时,我可以'不要可视化指针在 RAM 中的结构)

输入.txt文件:

Alice Wonderland 1.3 2 3 4 5.5
Bob Marley 7 8 9.8 10 11.5
Charley Manson 12 13.5 14 15 16

代码:

/* Input: input file name, as char array;
matrix of names, and matrix of grades
Functionality: opens the file, extracts names into matrix of names,
extracts grades into the matrix of grades;
Output: 0 if everything went well, -1 otherwise
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUMSTUDENT 100
#define MAXNUMGRADES 100

//#define DEBUG1
#define DEBUG2

char first[MAXNUMSTUDENT][1]; //One first name per row (one column)
char last[MAXNUMSTUDENT][1]; //One last name per row (one column)
float grades[MAXNUMGRADES][MAXNUMGRADES];
float grade;




int counter = 1; //Counts the token number (First two tokens = names, rest = grades)
int i = 0; //Row index
int j = 0; //Column index for first[][], last[][]
int n = 0; //Row index for grades[][]
int m = 0; //Column index for grades[][]

int main(void)
{
//Initialize all arrays to 0

// First[i][j]
for(i=0; i<MAXNUMSTUDENT; i++){
first[i][j] = "0";
for(j=0; j<=1; j++){
first[i][j] = "0";
}
}

// Last[i][j]
for(i=0; i<MAXNUMSTUDENT; i++){
last[i][j] = "0";
for(j=0; j<=1; j++){
last[i][j] = "0";
}

}

// grades[n][m]
for(n=0; n<MAXNUMGRADES; n++){
grades[n][m] = 0;
for(m=0; m<MAXNUMGRADES; m++){
grades[n][m] = 0;
}
}

//Reset indexes
i=0;
j=0;
n=0;
m=0;

FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;

fp = fopen("gradebook.txt", "r"); //Name of input file goes here
if (fp == NULL)
return -1;

//Loops through each line of gradebook (1 loop = 1 line)
while ((read = getline(&line, &len, fp)) != -1) {

/**** AT THIS POINT YOU HAVE A STRING CALLED line *****/


char delim[] = " "; //delimiters are space comma and hyphen

char* token;

//For loop tokenizes string, runs until there are no more tokens
for (token = strtok(line, delim); token; token = strtok(NULL, delim))
{
#ifdef DEBUG1
printf("Retrieved line of length %zu :\n", read);
printf("%s\n", line);
printf("token=%s counter=%d i=%d j=%d n=%d m=%d\n\n", token, counter, i, j, n, m);
#endif

//Store first token in first[][]
if(counter == 1){
first[i][j] = token;
}

//Store second token in last[][]
if(counter == 2){
last[i][j] = token;

//Increment row of first[][] and last[][] (for next line)
i++;
}

//Store the rest of tokens in grades[][]
if(counter > 2){
grade = atof(token);
grades[n][m] = grade;

//Increment column of grades[][]
m++;
}

//Counter for token number
counter++;
}

//Increment row of grades[][]
n++;

//Reset counter
counter = 1;

//Reset column index of grades[][]
m = 0;
}


//Test arrays
printf("Row 1, column 1 of grades: %.2f\n", grades[0][0]);
printf("Row 1, column 1 of first: %c\n", first[0][0]);
printf("Row 1, column 1 of last: %c\n", last[0][0]);

fclose(fp);
if (line)
free(line);
return 0;
}

谢谢。

更新的代码:

/*  File    : extractgrades.c          *
* team : SnS *
* Date : 11-20-16 */

/* Input: input file name, as char array;
matrix of names, and matrix of grades
Functionality: opens the file, extracts names into matrix of names,
extracts grades into the matrix of grades;
Output: 0 if everything went well, -1 otherwise
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUMSTUDENT 100
#define MAXNUMGRADES 100

//Comment out to turn off debugging
#define DEBUG1
#define DEBUG2

char first[MAXNUMSTUDENT][MAXNUMSTUDENT];
char last[MAXNUMSTUDENT][MAXNUMSTUDENT];
float grades[MAXNUMGRADES][MAXNUMGRADES];

int numberofscores; //Number of scores
int numberofstudents;
int maxnumberofscores;

int main(void)
{
char first[MAXNUMSTUDENT][MAXNUMSTUDENT] = {{0}};
char last[MAXNUMSTUDENT][MAXNUMSTUDENT] = {{0}};
float grades[MAXNUMGRADES][MAXNUMGRADES] = {{0}};
float grade;

int i; //Row index
int j; //Column index for first[][], last[][]
int n; //Row index for grades[][]
int m; //Column index for grades[][]
int counter; //Counts the token number (First two tokens = names, rest = grades)

//Initialize indexes
counter=1;
numberofscores=0;
i=0;
j=0;
n=0;
m=0;

FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;

fp = fopen("gradebook.txt", "r"); //Name of input file goes here
if (fp == NULL)
return -1;

//Loops through each line of gradebook (1 loop = 1 line)
while ((read = getline(&line, &len, fp)) != -1) {

char delim[] = " "; //delimiters are space comma and hyphen
char * token;

//For loop tokenizes string, runs until there are no more tokens
for (token = strtok(line, delim); token; token = strtok(NULL, delim))
{
#ifdef DEBUG1
printf("Retrieved line of length %zu :\n", read);
printf("%s\n", line);
printf("token=%s counter=%d i=%d j=%d n=%d m=%d\n\n", token, counter, i, j, n, m);
#endif

//Store first token in first[][]
if(counter == 1){
strcpy(first[i], token);
}

//Store second token in last[][]
if(counter == 2){
strcpy(last[i], token);
//Increment row of first[][] and last[][] (for next line)
i++;
}

//Store the rest of tokens in grades[][]
if(counter > 2){
grade = atof(token);
grades[n][m] = grade;

//Increment column of grades[][]
m++;
}

//Counter for token number
counter++;
}

//Save pseudo max number of scores
maxnumberofscores = counter;
if(counter >= maxnumberofscores){
maxnumberofscores = counter;
}
printf("maxnumberofscores: %d\n", maxnumberofscores);

//Increment row of grades[][]
n++;

//Reset column index of grades[][]
m = 0;

//Reset counter
counter = 1;
}

//Get actual max number of scores
numberofscores = maxnumberofscores - 3;

//Get number of students
numberofstudents = i;


//Debug sandwich to test everything
#ifdef DEBUG2
//Test arrays
printf("Row 1, column 1 of grades: %.2f\n", grades[0][0]);
printf("Row 1, column 1 of first: %c\n", first[0][0]);
printf("Row 1, column 1 of last: %c\n", last[0][0]);

//Print all arrays
i=0;
j=0;
n=0;
m=0;

//Print grades[n][m]
for(n=0; n<numberofstudents; n++){
printf("\n");
for(m=0; m<numberofscores; m++){
printf("grades[%d][%d]: %.2f\n", n, m, grades[n][m]);
}
}

//Print first[i][j]
for(i=0; i<numberofstudents; i++){
printf("first[%d]: %s\n", i, first[i]);
}

//Print last[i][j]
for(i=0; i<numberofstudents; i++){
printf("last[%d]: %s\n", i, last[i]);
}

//Print number of scores, number of students
printf("Max number of scores = %d\n", numberofscores);
printf("Number of students: %d\n", numberofstudents);
#endif


fclose(fp);
if (line)
free(line);
return 0;
}

最佳答案

first[i][j] = "0";

first[i][j]是一个char(不是字符串),改为

first[i][j] = '0';

Is this the proper way to store a string called token into the index [0][0] of the array first[][]?

first[i][j] = token;

不,首先,您不能使用 = 分配给数组,您需要使用诸如 strcpy 之类的函数,或者更好的是更安全的版本,例如 strlcpy snprintf

第二,你的数组只有一个字符的空间:

char first[MAXNUMSTUDENT][1];

您需要更多空间:

char first[MAXNUMSTUDENT][100];

此外,您在最后释放了line,但是每次调用getline时都需要调用free以避免内存占用泄漏:

while ((read = getline(&line, &len, fp)) != -1) {
...
free(line);
}

关于C - 创建字符串矩阵时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40690111/

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