簡體   English   中英

從函數返回值比較C中的宏

[英]Compare macros in C from function return value

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"

在您的情況下,可以將指針與字符串常量進行比較,這與其他人所說的相反。

立即問題的根本原因是宏定義末尾的分號(如piokuc所指出的)。 預處理程序語句不需要分號。 這是您當前實施中要考慮的另一件事:

由於ISADIGIT是宏,因此預處理程序將用指定的字符串替換代碼中ISADIGIT每個實例。 您在多個地方使用ISADIGIT ,每個地方都將替換為字符串。 這些中的每一個都將被視為獨立的獨立字符串文字。 它們的內容恰好相同,但是它們是單獨的對象。 這意味着您的atoi2函數將返回指向該函數內部包含的字符串文字的指針,而您的main函數正在將該指針與指向main內部包含的字符串文字的指針進行比較。 由於這些是指向不同對象的指針,因此它們將始終比較不相等。 就編譯器而言,它們是恰好包含相同內容的字符串這一事實並不重要。

話雖如此,該代碼在某些情況下似乎可以使用。 一些編譯器具有可選的優化功能,可以通過組合相同的字符串文字來節省空間。 在這種情況下, ISADIGIT宏的擴展將歸結為一個指向通用字符串文字的指針,並且您在main中進行的比較可能會起作用。 但是,您永遠不應依賴特定的編譯器優化來使代碼正常工作。 如果這樣做,則在升級編譯器,使用其他編譯器構建,構建“調試”版本而不是“發行”版本等情況下,很有可能會出現代碼中斷的風險。

通常,請勿將字符串用作函數的返回碼。 字符串不是C中的真實對象(與其他語言不同),並且使用它們時涉及很多隱藏的“陷阱”。 從函數返回狀態代碼的傳統方法是使用整數作為返回值。 一個函數成功完成時將返回零,並且非零返回值表示某種錯誤。 由於您將所有錯誤代碼都抽象為宏,因此可以簡單地將#define語句中的字符串替換為數字,並將atoi2的返回類型atoi2int 您唯一需要做的其他更改是在printf("%s") atoi2的返回值所在的atoi2 您將需要一個單獨的方法將整數轉換為字符串。 執行此操作的兩種常見方法是使用函數或表:

// Function method:
// Use like: printf("%s", code_to_string(code));
const char* code_to_string(int code) {
    switch(code) {
        case STARTSWITH0:
            return "Starts with 0";
        case NEGATIVESIGNNOTATBEGINNING:
            return "Negative sign not at beginning";
        ...
        default:
            return "Unknown error";
    }
}

// Table method (assumes error codes are numbered 1,2,3,...
// Use like: printf("%s", error_strings[code]);
const char* error_strings[] = {
    "No error", // 0
    "Starts with 0", // 1
    "Negative sign not at beginning", // 2
    ...
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM