gpt4 book ai didi

c - 硬件帮助今晚到期。 Char To Int(帮助返回 Null)

转载 作者:行者123 更新时间:2023-11-30 20:59:39 26 4
gpt4 key购买 nike

我有一个程序应该将 char 整数转换为实际整数。它必须与教授编写的 Main 方法兼容。我想我有一个实际转换的解决方案,但我不断陷入无限循环。我知道我应该返回 Null 但我不知道如何。请问有人可以帮忙吗?此函数还应该忽略字符字母,并在每个字符字母后有一个新空格。因此 123fgf456 将打印 123(换行符)456。

The function I need help with.

#include <stddef.h>
/*
* Scans inputString, ignoring leading whitespace (spaces, tabs, and newlines)
* to find the first decimal digit, which it interprets as the most significant
* digit of a decimal number, and continues scanning until finding the first
* non-decimal digit. The digits found are converted to an integer, which is
* stored in the location pointed to by integerPtr. This function returns a
* pointer to the first non-digit after the first digit, unless a
* non-whitespace, non-digit is encountered before a digit, in which case,
* NULL is returned and the location pointed to by integerPtr is not changed.
*
* @param integerPtr A pointer to the integer in which to store the integer
* converted from the ASCII string
* @return a pointer to the first non-digit character found if a number was
* successfully converted, NULL if not
*/

char * asciiToInteger(char *inputString, int *integerPtr) {
int i =0; int j=0; int num =0; char terminate;
if(inputString[i] == '\0') return NULL;
if(inputString[i] == ' '){return NULL;}
while(*inputString != '\0')
{

if(*inputString >= '0' && *inputString <= '9')
{
if(inputString[i] == ' '){break;}
num = num *10 + inputString[i]- '0';
*integerPtr = num;
}
inputString++;

}

return inputString;
}




#include <stdio.h>
#include <stddef.h> // for definition of NULL

char * asciiToInteger(char inputString[], int *integerPtr);
int main() {
char inputBuffer[1024];
char *ptr = NULL;
int integer = 8888;
while(fgets(inputBuffer, sizeof(inputBuffer), stdin)) {
ptr = inputBuffer;
int done = 0;
while(!done) {
char *newPtr = asciiToInteger(ptr, &integer);
if(newPtr == NULL) {
if(*ptr != '\0')
++ptr; // Skip over offending character
else
done = 1;
} else {
printf("%d\n", integer);
ptr = newPtr;
}
}
}

return 0;
}

最佳答案

char * asciiToInteger(char *inputString, int *integerPtr) {
int n = 0;
int index = 0;
while(*inputString != NULL) {
if (*inputString >= '0' && *inputString <= '9') {
n = n * 10 + *inputString - '0';
++inputString;
integerPtr = &n;
index += 1;
} else if(*inputString < '0' || *inputString > '9') {
if(*integerPtr) std::cout << *integerPtr << std::endl;
n = 0;
++inputString;
} else {
if (*integerPtr) std::cout << *integerPtr << std::endl;
++inputString;
}
}
if(*integerPtr) std::cout << *integerPtr << std::endl;
return inputString;
}

上面是一个 C++ 实现,下面是使用 printf 的实现:

char * asciiToInteger(char *inputString, int *integerPtr) {
int n = 0;
int index = 0;
while(*inputString != NULL) {
if (*inputString >= '0' && *inputString <= '9') {
n = n * 10 + *inputString - '0';
++inputString;
integerPtr = &n;
index += 1;
} else if(*inputString < '0' || *inputString > '9') {
if(*integerPtr) printf("%d\n", *integerPtr);
n = 0;
++inputString;
} else {
if (*integerPtr) printf("%d\n", *integerPtr);
++inputString;
}
}
if(*integerPtr) printf("%d\n", *integerPtr);
return inputString;
}

我发现您的提示相当模糊,但这应该通过这些测试用例。现在还要求您返回指向初始数字后的第一个非数字的指针。您可以返回 &inputString[index - 1],但是,您不能提前返回。

关于c - 硬件帮助今晚到期。 Char To Int(帮助返回 Null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46270045/

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