顺带回应“无功”网友询问在字符串中提取数字的办法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void Pause(const char* message);
bool ScanInteger(const char* str, int* value);
int main(void)
{
const char* str = "asd123qwe45 6mnb789xyz";
int value;
while (ScanInteger(str, &value))
{
printf_s("%d\n", value);
str = NULL;
}
Pause(NULL);
return EXIT_SUCCESS;
}
void Pause(const char* message)
{
if (message == NULL)
{
message = "\nPress <Enter> key to continue...";
}
fflush(stdin);
getchar();
}
bool ScanInteger(const char* str, int* value)
{
char buffer[20];
static const char* ptrStr = NULL;
char* ptrBuffer = buffer;
bool begin = false;
bool end = false;
if (str != NULL)
{
ptrStr = str;
}
while ((*ptrStr++ != '\0') && !end)
{
if (isdigit(*ptrStr))
{
if (!begin)
{
begin = true;
}
*ptrBuffer++ = *ptrStr;
}
else
{
if (begin)
{
end = true;
*ptrBuffer = '\0';
*value = atoi(buffer);
}
}
}
--ptrStr;
return end;
}
[ 本帖最后由 TonyDeng 于 2015-4-15 20:24 编辑 ]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void Pause(const char* message);
bool ScanInteger(const char* str, int* value);
int main(void)
{
const char* str = "asd123qwe45 6mnb789xyz";
int value;
while (ScanInteger(str, &value))
{
printf_s("%d\n", value);
str = NULL;
}
Pause(NULL);
return EXIT_SUCCESS;
}
void Pause(const char* message)
{
if (message == NULL)
{
message = "\nPress <Enter> key to continue...";
}
fflush(stdin);
getchar();
}
bool ScanInteger(const char* str, int* value)
{
char buffer[20];
static const char* ptrStr = NULL;
char* ptrBuffer = buffer;
bool begin = false;
bool end = false;
if (str != NULL)
{
ptrStr = str;
}
while ((*ptrStr++ != '\0') && !end)
{
if (isdigit(*ptrStr))
{
if (!begin)
{
begin = true;
}
*ptrBuffer++ = *ptrStr;
}
else
{
if (begin)
{
end = true;
*ptrBuffer = '\0';
*value = atoi(buffer);
}
}
}
--ptrStr;
return end;
}
[ 本帖最后由 TonyDeng 于 2015-4-15 20:24 编辑 ]
授人以渔,不授人以鱼。