if (ISADIGIT != atoi2(&word[i])){
为什么我不能这样做(见上文)。我得到“预期的表达”?
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#define STARTSWITH0 "Starts with 0";
#define NEGATIVESIGNNOTATBEGINNING "Negative sign not at beginning";
#define ISADIGIT "Is a digit";
#define MORETHANONENEGATIVESIGN "More than one negative sing";
#define MORETHANONEDECIMALPLACE "More than one decimal place";
#define NUMBERTOOLARGEORNUMBERTOOSMALL "Number too large or number too small";
#define CONVERSIONERROR "Conversion Error";
#define CANNOTCONTAINLETTERS "Cannot contain letters";
char* atoi2(char str[]);
int main(int argc, char ** argv){
char c, word[1000]; int count;
printf("enter word: ");
count = 0;
while(1){
c = getchar();
if (c == EOF);
return 0;
if (c == ' ') {
continue;
}
if (c == '\n' || c == '\t'){
word[count] = '\0';
break;
}
word[count++] = c;
}
int i;
for (i = 0; i < count; i++){
if (ISADIGIT != atoi2(&word[i])){
printf("%s\n", atoi2(&word[i]));
return 1;
}
}
int sum;
for (i = 0; i < count; i++){
sum += word[i];
}
printf("sum is: %d", sum);
}
char* atoi2(char * str)
{
int i, v, d, n, errno;
i = v = d = n = 0;
while(str[i] != '\0'){
/* this means the first value is a 0, therefore it should not be numeric */
if (i == 0 && str[i] == '0' && str[i+1] != '.' && str[i+1] != '\0') return STARTSWITH0;
if(!isdigit(str[i])) {
if (isalpha(str[i])) return CANNOTCONTAINLETTERS; /* must make sure the digit we receive is not a letter */
if (str[i] == '-' && i > 0) return NEGATIVESIGNNOTATBEGINNING; /* negative sign must come first */
if (str[i] == '-'){ ++n; ++i; continue; } /* first negative sign, pass through */
if (str[i] == '.') { ++d; } /* periods are ok, just not too many */
}
++i;
}
if (n > 1) return MORETHANONENEGATIVESIGN; /* can't have more than one negative sign */
if (d > 1) return MORETHANONEDECIMALPLACE; /* this means there is more than one decimal place */
return ISADIGIT;
}
感谢您提供的所有精彩意见。这是修改后的程序
#include <ctype.h>
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#define STARTSWITH0 1 /*"Starts with 0"*/
#define NEGATIVESIGNNOTATBEGINNING 2 /*"Negative sign not at beginning"*/
#define MORETHANONENEGATIVESIGN 3 /*"More than one negative sing"*/
#define MORETHANONEDECIMALPLACE 4 /*"More than one decimal place"*/
#define NUMBERTOOLARGE 5
#define CANNOTCONTAINLETTERS 6 /*"Cannot contain letters"*/
#define CONTAINSAPERIOD 7 /*"Integers cannot contain decimal points"*/
int atoi2(char *);
char * geterrorstring(int);
int main(int argc, char ** argv){
char c, nums[1000][1000];
while(1) {
printf("enter numbers: ");
int digits = 0, row = 0, maxrows = 1;
/* gets data from user, puts it into an array */
while(1){
c = getchar();
if (c == EOF) return 0;
if (c == ' ' || c == '\t') { nums[row][digits] = '\0'; row++; maxrows++; digits = 0; }
if (c == '\n'){ nums[row][digits] = '\0'; maxrows++; digits = 0; break; }
nums[row][digits++] = c;
}
int i, j;
int error = 0;
/* sums up data from array and prints error message if digit is invalid */
int sum = 0, msg = 0;
for (i = 0; i < maxrows; i++){
msg = atoi2(nums[i]);
if (msg > 0){
printf("%s\n", geterrorstring(msg));
error = 1;
}
sum += atoi(nums[i]);
}
if (error == 1) continue;
printf("sum is: %d\n", sum);
}
}
char * geterrorstring(int code)
{
switch (code) {
case STARTSWITH0:
return "Starts with 0";
break;
case NEGATIVESIGNNOTATBEGINNING:
return "Negative signs must be at the begging of the number";
break;
case MORETHANONENEGATIVESIGN:
return "You cannot have more than one negative sign in a number";
break;
case MORETHANONEDECIMALPLACE:
return "More than one decimal place";
break;
case NUMBERTOOLARGE:
return "Number too large";
break;
case CANNOTCONTAINLETTERS:
return "Cannot contain letters";
break;
case CONTAINSAPERIOD:
return "Integers do not have decimal places";
default:
return "I have no idea what error you have, but you got one";
break;
}
}
int atoi2(char * str)
{
int i, v, n, errno;
i = v = n = 0;
while(str[i] != '\0'){
/* this means the first value is a 0, therefore it should not be numeric */
if (i == 0 && str[i] == '0' && str[i+1] != '.' && str[i+1] != '\0') return STARTSWITH0;
if(!isdigit(str[i])) {
if (isalpha(str[i])) return CANNOTCONTAINLETTERS; /* must make sure the digit we receive is not a letter */
if (sizeof(atoi(str)) > sizeof(int)) return NUMBERTOOLARGE; /* make sure number is not that large */
if (str[i] == '-' && i > 0) { if (n > 1) { return MORETHANONENEGATIVESIGN; } return NEGATIVESIGNNOTATBEGINNING; } /* negative sign must come first */
if (str[i] == '-'){ ++n; ++i; continue; } /* first negative sign, pass through */
if (str[i] == '.') { return CONTAINSAPERIOD; } /* periods are ok, just not too many */
}
++i;
}
if (n > 1) return MORETHANONENEGATIVESIGN; /* can't have more than one negative sign */
return 0;
}