gpt4 book ai didi

c - 我没有正确处理多个 block

转载 作者:行者123 更新时间:2023-11-30 18:53:37 24 4
gpt4 key购买 nike

我有一个作业,我需要读取输入行,例如

2
67 5 100 1 11 97 98 10 1 110
15 72 10 101 47 67 88 20 94 6 22 11

4
61 11 93 4 73 39 78 34 17 104
23 43 11 93 65 52 20 96 66 31 86 24 40 61 102 13 50 51
73 43 28 73 8 89 31 68 77 27 24 77 42 72 15 24 64 51
25 75 7 90 10 111 17 16

......

处理每两个整数( block 中的第一行仅告诉我们将处理多少个单词),将它们相加然后匹配将它们转换为相应的 ASCII 字符。上面的例子是两个 block 。输出应该是:

 Decoded messages:
Hello World!
Happy Bhutanese teacher's day!

在处理具有多个 block (超过 20 个 block 等)且在一个输入文本上遵循相同格式的文件时,我遇到了问题。我可以很好地处理一个 block ,也可以处理两个 block ,但不能很好,因为我的程序似乎没有结束。每行长度不得超过 256 个字符。 numOfPuzzl 指的是我们每个 block 处理的单词数量。我非常感谢任何帮助。我附上了我的代码并尽可能多地发表了评论。

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

int main(int argc, char* argv[])
{
//user will type in file name they choose to process and we allocated in filename[]
char filename[256];
printf("Enter filename: ");
scanf("%s", filename);

//process filename username typed in
FILE *pFile;
pFile = fopen(filename, "r");

//if there's nothong to read
if (pFile == NULL){
exit(1);
}

printf("Decoded messages:\n");

//create array we will putting lines into
char myString[256];
//simply gets the first line, which is always a lone integer
fgets(myString, 256, pFile);

int numOfPuzzl;
sscanf(myString, "%d", &numOfPuzzl);
//printf("puzzles to solve: %d\n", numOfPuzzl);
int wordsProcessed = 0;

//just remember that myString has entire line

//start processing the lines that follow, a line is a word
while (fgets(myString, 256, pFile) != NULL){
int num = 0; //first integer in line
int secondNum = 0; //second int. in line
int tot = 0; //how many bytes
int bytes_read = 0; //bytes processed

char *endOfStrAdr = strchr(myString, '\0'); //returns address of end terminating char.
int endOfStrIdx = endOfStrAdr - myString; //this will give me the index of where the terminating char. occurs within my array

//start scanning integers within array making sure to not sccan out of bounds
while (tot < endOfStrIdx){

//first integer allocated as well as how many byes it was
sscanf(myString + tot, "%d %n", &num, &bytes_read);

tot += bytes_read; //keeps tab so we don't have to read from the begn. of array everytime

//second integer allocated as well as how many byes it was

sscanf(myString + tot, "%d %n", &secondNum, &bytes_read);

tot += bytes_read; ////keeps tab so we don't have to read from the begn. of array everytime

printf("%c", (char) num + secondNum); //add the two integers and cast them to char

//we want to check if we are the end of the string, our word
if (tot == endOfStrIdx){

printf(" ");
wordsProcessed++;
//we want to print a new line char. if we finished processing all the words for the puzzle
if (wordsProcessed == numOfPuzzl){
printf("\n");
fgets(myString, 256, pFile);
}
}
}
}
fclose(pFile);
}

最佳答案

  • 忽略拼图之间的空行。
  • 在处理新谜题之前重置参数(numOfPuzzlwordsProcessed)。

要存档它,请更改

if (wordsProcessed == numOfPuzzl) {
printf("\n");
fgets(myString, 256, pFile);
}

进入

if (wordsProcessed == numOfPuzzl) {
printf("\n");
while ( fgets(myString, 256, pFile) != NULL ){
if ( sscanf(myString, "%d", &numOfPuzzl) == 1 )
break;
}
wordsProcessed = 0;
}

关于c - 我没有正确处理多个 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32643703/

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