我有一个程序可以将char整数转换为实际的整数。它必须与教授编写的Main方法兼容。我想我有一个实际转换的解决方案,但我不断得到一个无限循环。我知道我应该回归Null,但我不知道如何。请任何人可以帮忙吗?此函数也应该忽略char字母,并在每个字母后面都有一个新空格。因此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;
}
答案 0 :(得分:1)
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;
}
我发现你的提示相当模糊,但这应该通过那些测试用例。现在,还要求您在初始数字后返回指向第一个非数字的指针。您可以返回&amp; inputString [index - 1],但是,您不能提前返回。
答案 1 :(得分:0)
你需要一个标记来记住你是否看过任何数字。然后,如果您看到一个非数字,但您还没有看到任何数字,请立即使用browserHistory
进行纾困。