回复 12楼 TonyDeng
do{
ch = _getch();
if (isdigit(ch) || ((index == 0) && (ch == '-')))
{
_putch(ch);
textBuffer[index++] = ch;
}
} while ((index < sizeof(textBuffer) - 1) && (ch != K_ENTER));
textBuffer[index] = '\0';
可以说下这段是什么意思吗?
2015-03-12 20:35
2015-03-12 20:42
2015-03-12 21:45
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define VALINT -20150312
#define VALDOUBLE -20.150312
#define INTLEN 10
#define DOUBLELEN 16
void getInt(int *n) {
int ch, count = 0;
char num[INTLEN + 1] = {0};
char *end, *p = num;
if(ch = getchar(), isdigit(ch) ||
ch == '-' || ch == '+') {
sprintf(p, "%c", ch);
count++;
while((ch = getchar()) != '\n' && count < INTLEN) {
if(isdigit(ch)) {
sprintf(++p, "%c", ch);
count++;
} else {
fflush(stdin);
*n = VALINT;
return;
}
}
} else {
fflush(stdin);
*n = VALINT;
return;
}
fflush(stdin);
*n = strtol(num, &end, 10);
}
void getDouble(double *n) {
int ch, flag = 0, count = 0;
char num[DOUBLELEN + 1] = {0};
char *end, *p = num;
if(ch = getchar(),
ch == '-' || ch == '+' ||
ch == '.' || isdigit(ch)) {
sprintf(p, "%c", ch);
count++;
if(ch == '.') flag++;
while((ch = getchar()) != '\n' && count < DOUBLELEN) {
if(ch == '.') {
flag++;
if(flag > 1) {
fflush(stdin);
*n = VALDOUBLE;
return;
} else {
sprintf(++p, "%c", ch);
count++;
}
} else if(isdigit(ch)) {
sprintf(++p, "%c", ch);
count++;
} else {
fflush(stdin);
*n = VALDOUBLE;
return;
}
}
} else {
fflush(stdin);
*n = VALDOUBLE;
return;
}
fflush(stdin);
*n = strtod(num, &end);
}
int main(void) {
int x = 0;
double y = 0;
puts("输入一个整形数 x\n"
"回车换行后再输入一个浮点数 y\n"
"x 与 y 都为预设值时退出");
do {
getInt(&x);
getDouble(&y);
printf("x = %d\ty = %f\n", x, y);
} while(x != VALINT || y != VALDOUBLE);
return 0;
}

2015-03-12 22:01
2015-03-12 22:02

2015-03-13 15:37