从函数返回值比较 C 中的宏

标签 c unix

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;
}

最佳答案

改变

#define ISADIGIT "Is a digit";

#define ISADIGIT "Is a digit"

在您的情况下,可以比较指向您的字符串常量的指针,这与其他人所说的相反。

关于从函数返回值比较 C 中的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12842978/

相关文章:

sql - 连接到 awk 脚本内的数据库

c - 在库和程序之间共享变量

c - 使用 AVX2 和 SSE2 进行位 vector 运算

regex - 使用 grep 查询选择性文件

mysql - 为什么即使有足够的 RAM 来映射页面,MySQL 服务器仍使用交换空间?

c - 在接收消息时继续使用 select() 计算超时?

c - 4 维数组的指针语法

c - 使用(void **)传递(double ***)指针可以吗?

c - 错误 C2446 : '==' : no conversion from 'int *' to 'int'

ruby - OS X 中内置的 Ruby 扩展可以在 Linux 上运行吗?