簡體   English   中英

在c中打印字符串指針

[英]Printing string pointers in c

因此,基本上我有兩個文件:

文件1:

//
//  main.c
//  frederickterry
//
//  Created by Rick Terry on 1/15/15.
//  Copyright (c) 2015 Rick Terry. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int size (char *g) {
    int ofs = 0;

    while (*(g+ofs) != '\0') {
        ++ofs;
    }

    return ofs;
}

int parse(char *g) {
    // Setup
    char binaryConnective;
    int negated = 0;

    // Looking for propositions
    int fmlaLength = size(g);
    if(fmlaLength == 0) {
        return 1;
    }


    if(fmlaLength == 1) {
        if(g[0] == 'p') {
            return 1;
        } else if (g[0] == 'q') {
            return 1;
        } else if (g[0] == 'r') {
            return 1;
        } else {
            return 0;
        }
    }
    // Now looking for negated preposition

    if(fmlaLength == 2) {
        char temp[100];
        strcpy(temp, g);
        if(g[0] == '-') {
            negated = 1;
            int negatedprop = parse(g+1);
            if(negatedprop == 1) {
                return 2;
            }
        }
    }

    // Checking if Binary Formula
    char arrayleft[50];
    char arrayright[50];
    char *left = "";
    char *right = "";
    int numLeft = 0;
    int numRight = 0;
    int bclocation = 0;
    int binarypresent = 0;



    if(fmlaLength != 1 && fmlaLength != 2) {
        if(g[0] == '-') {
            int negatedBinary = parse(g+1);
            if(negatedBinary == 1 || negatedBinary == 2 || negatedBinary == 3) {
                return 2;
            } else {
                return 0;
            }
        }
        int i = 0;
        int l = 0;
        int p = strlen(g);
        for(l = 0; l < strlen(g)/2; l++) {
            if(g[l] == '(' && g[p-l-1] == ')') {
                i++;
            }
        }


        for(int q = i; q < strlen(g); q++) {
            if(g[q] == '(') {
                numLeft++;
            } else if(g[q] == ')') {
                numRight++;
            }
            arrayleft[q] = g[q];

            //printf("%c", arrayleft[i]);
            //printf("%s", left);


            if((numRight == numLeft) && (g[q+1] == 'v' || g[q+1] == '>' || g[q+1] == '^')) {
                arrayleft[q+1] = '\0';
                bclocation = q+1;
                binaryConnective = g[q+1];
                binarypresent = 1;
                //                    printf("The binary connecive is: %c\n", binaryConnective);
                break;
            }

        }
        if(binarypresent == 0) {
            return 0;
        }

        int j = 0;
        for(int i = bclocation+1; i < strlen(g)-1; i++) {
            arrayright[j] = g[i];
            j++;
        }

        arrayright[j] = '\0';

        left = &arrayleft[1];
        right = &arrayright[0];
        //printf("Printed a second time, fmla 1 is: %s", left);
        int parseleft = parse(left);
        //        printf("Parse left result: %d\n", parseleft);
        if(parseleft == 0) {
            return 0;
        }
        int parseright = parse(right);

        if(parseright == 0) {
            return 0;
        }
        //        printf("Parse right result: %d\n", parseleft);
        if(negated == 1) {
            return 2;
        } else {
            return 3;
        }
    }

    return 0;
}

int type(char *g) {
    if(parse(g) == 1 ||parse(g) == 2 || parse(g) == 3) {
        if(parse(g) == 1) {
            return 1;
        }
        /* Literals, Positive and Negative */
        if(parse(g) == 2 && size(g) == 2) {
            return 1;
        }
        /* Double Negations */
        if(g[0] == '-' && g[1] == '-') {
            return 4;
        }
        /* Alpha & Beta Formulas */
        char binaryConnective;
        int numLeft = 0;
        int numRight = 0;
        int bclocation = 0;
        int binarypresent = 0;

        int i = 0;
        if(g[0] == '(') {
            i++;
        }


        if(g[0] == '-') {
            i++;
            if(g[1] == '(') {
                i++;
            }
        }

        for(i; i < strlen(g); ++i) {
            if(g[i] == '(') {
                numLeft++;
            } else if(g[i] == ')') {
                numRight++;
            }

            if(numRight == numLeft) {
                if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
                    bclocation = i+1;
                    binaryConnective = g[i+1];
                    binarypresent = 1;
                    break;
                }
            }
        }

        /* Connective established */
        if(binaryConnective == '^') {
            if(g[0] == '-') {
                return 3;
            } else {
                return 2;
            }
        } else if(binaryConnective == '>') {
            if(g[0] == '-') {
                return 2;
            } else {
                return 3;
            }
        } else if (binaryConnective == 'v') {
            if(g[0] == '-') {
                return 2;
            } else {
                return 3;
            }
        }
    }
    return 0;
}

char bin(char *g) {
    char binaryConnective;
    char arrayLeft[50];
    int numLeft = 0;
    int numRight = 0;
    int bclocation = 0;

    int i = 0;
    if(g[0] == '(') {
        i++;
    }

    if(g[0] == '-') {
        i++;
        if(g[1] == '(') {
            i++;
        }
    }

    for(i; i < strlen(g); ++i) {
        if(g[i] == '(') {
            numLeft++;
        } else if(g[i] == ')') {
            numRight++;
        }
        int j = 0;
        arrayLeft[j++] = g[i];

        if(numRight == numLeft) {
            if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
                arrayLeft[i+1] = '\0';
                bclocation = i+1;
                binaryConnective = g[i+1];
                return binaryConnective;
            }
        }
    }
    return binaryConnective;
}

char *partone(char *g) {
    char binaryConnective;
    char arrayLeft[50];
    char arrayRight[50];
    int numLeft = 0;
    int numRight = 0;
    int bclocation = 0;

    int i = 0;
    if(g[0] == '(') {
        i++;
    }

    if(g[0] == '-') {
        i++;
        if(g[1] == '(') {
            i++;
        }
    }
    int j = 0;
    for(i; i < strlen(g); ++i) {
        if(g[i] == '(') {
            numLeft++;
        } else if(g[i] == ')') {
            numRight++;
        }
        arrayLeft[j] = g[i];

        if(numRight == numLeft) {
            if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
                arrayLeft[j+1] = '\0';
                bclocation = i+1;
                binaryConnective = g[i+1];
                break;
            }
        }
        j++;
    }
    int m = 0;
    for(int k = bclocation+1; k < strlen(g)-1; k++) {
        arrayRight[m] = g[k];
        m++;
    }

    arrayRight[m] = '\0';

    char* leftSide = &arrayLeft[0];
    //    printf("%s\n", leftSide);
    //    printf("%s\n", rightSide);
    int k = 0;
    k++;
    return leftSide;
}

char *parttwo(char *g) {
    char binaryConnective;
    char arrayLeft[50];
    char arrayRight[50];
    int numLeft = 0;
    int numRight = 0;
    int bclocation = 0;

    int i = 0;
    if(g[0] == '(') {
        i++;
    }

    if(g[0] == '-') {
        i++;
        if(g[1] == '(') {
            i++;
        }
    }
    int j = 0;
    for(i; i < strlen(g); ++i) {
        if(g[i] == '(') {
            numLeft++;
        } else if(g[i] == ')') {
            numRight++;
        }
        arrayLeft[j] = g[i];

        if(numRight == numLeft) {
            if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
                arrayLeft[j+1] = '\0';
                bclocation = i+1;
                binaryConnective = g[i+1];
                break;
            }
        }
        j++;
    }
    int m = 0;
    int n = size(g) - 1;
    if(g[strlen(g)-1] != ')') {
        n++;
    }
    for(int k = bclocation+1; k < n; k++) {
        arrayRight[m] = g[k];
        m++;
    }

    arrayRight[m] = '\0';

    char* leftSide = &arrayLeft[0];
    char* rightSide = &arrayRight[0];
    //    printf("%s\n", leftSide);
    //    printf("%s\n", rightSide);

    return rightSide;
}

char *firstexp(char *g) {
    char* left = partone(g);
    char leftArray[50];
    int i = 0;
    for(i; i < strlen(left); i++) {
        leftArray[i] = left[i];
    }
    leftArray[i] = '\0';
    char binConnective = bin(g);
    int typeG = type(g);
    if(typeG == 2) {
        if(binConnective == '^') {
            return &leftArray;
        } else if(binConnective == '>') {
            return &leftArray;
        }
    } else if(typeG == 3) {
        if(binConnective == 'v')
            return &leftArray;
    }
    char temp[50];
    for(int i = 0; i < strlen(leftArray); i++) {
        temp[i+1] = leftArray[i];
    }
    temp[0] = '-';
    char* lefttwo = &temp[0];

    if(typeG == 2) {
        if(binConnective == 'v') {
            return lefttwo;
        }
    } else if(typeG == 3) {
        if(binConnective == '>' || binConnective == '^') {
            return lefttwo;
        }
    }

    return "Hello";
}

char *secondexp(char *g) {
//    char binaryConnective = bin(g);
//    char* right = parttwo(g);
//    char rightArray[50];
//    int i = 0;
//    for(i; i< strlen(right); i++) {
//        rightArray[i+1] = right[i];
//    }
//    rightArray[i] = '\0';
//    int typeG = type(g);
//    if(type(g) == 2) {
//        if(binaryConnective == '^') {
//            return &rightArray;
//        }
//    } else if(type(g) == 3) {
//        if(binaryConnective == 'v' || binaryConnective == '>') {
//            return &rightArray;
//        }
//    }


        return "Hello";
}

typedef struct tableau tableau;
\

\
struct tableau {

    char *root;

    tableau *left;

    tableau *right;

    tableau *parent;

    int closedbranch;

};


int closed(tableau *t) {
    return 0;
}

void complete(tableau *t) {
}

/*int main(int argc, const char * argv[])
{
    printf("Hello, World!\n");
    printf("%d \n", parse("p^q"));
    printf("%d \n", type("p^q"));
    printf("%c \n", bin("p^q"));
    printf("%s\n", partone("p^q"));
    printf("%s\n", parttwo("p^q"));
    printf("%s\n", firstexp("p^q"));
    printf("Simulation complete");
    return 0;
}*/

檔案2:

#include <stdio.h>
#include <string.h>   /* for all the new-fangled string functions */
#include <stdlib.h>     /* malloc, free, rand */
#include "yourfile.h"

int Fsize = 50;

int  main()

{ /*input a string and check if its a propositional formula */
    char *name = malloc(Fsize);
    printf("Enter a formula:");
    scanf("%s", name);
    int p=parse(name);
    switch(p)
    {case(0): printf("not a formula");break;
        case(1): printf("a proposition");break;
        case(2): printf("a negated formula");break;
        case(3): printf("a binary formula");break;
        default: printf("what the f***!");
    }
    printf("\n");
    if (p==3)
    {
        printf("the first part is %s and the second part is %s", partone(name), parttwo(name));
        printf(" the binary connective is %c \n", bin(name));
    }


    int t =type(name);
    switch(t)
    {case(0):printf("I told you, not a formula");break;
        case(1): printf("A literal");break;
        case(2): printf("An alpha formula, ");break;
        case(3): printf("A beta formula, ");break;
        case(4): printf("Double negation");break;
        default: printf("SOmewthing's wrong");
    }
    if(t==2) printf("first expansion fmla is %s, second expansion fmla is %s\n", firstexp(name), secondexp(name));
    if(t==3) printf("first expansion fmla is %s, second expansion fmla is %s\n", firstexp(name), secondexp(name));




    tableau tab;
    tab.root = name;
    tab.left=0;
    tab.parent=0;
    tab.right=0;
    tab.closedbranch=0;


    complete(&tab);/*expand the root node then recursively expand any child nodes */
    if (closed(&tab)) printf("%s is not satisfiable", name);
    else printf("%s is satisfiable", name);

    return(0);
}

如果查看第一個文件,您將看到一個名為* firstexp(char * g)的方法。

此方法可以完美運行,但前提是已注釋掉另一個名為* secondexp(char * g)的方法。

如果* secondexp(char * g)被注釋掉,那么* firstexp將像這樣運行:

Enter a formula:((pvq)>-p)
a binary formula
the first part is (pvq) and the second part is -p the binary connective is > 
A beta formula, first expansion fmla is -(pvq), second expansion fmla is Hello
((pvq)>-p) is satisfiableProgram ended with exit code: 0

否則,如果* secondexp沒有被注釋掉,它將像這樣運行:

Enter a formula:((pvq)>-p)
a binary formula
the first part is (pvq) and the second part is -p the binary connective is > 
A beta formula, first expansion fmla is \240L, second expansion fmla is (-
((pvq)>-p) is satisfiable. Program ended with exit code: 0

如您所見,盡管輸入相同,但輸出卻完全不同。 有人可以解釋這是怎么回事嗎?

parttwo()函數中,您返回局部變量的地址

return rightSide;

其中rightSide是指向局部變量的指針。

看來您的編譯器向您發出了有關此問題的警告,您可以通過指向本地arrayRight的指針來解決此警告,這可能會使編譯器感到困惑,但結果將是相同的,在函數返回后, arrayRight的數據將不再存在。

您在整個代碼中都這樣做,甚至更糟的是,在secondexp()函數中,您secondexp()地址返回本地變量的地址,不僅將地址返回至本地變量,而且還返回了一個類型與函數的返回類型不兼容。

這是您的代碼可能存在的許多可能的問題之一,但是您需要開始解決該問題才能繼續解決其他可能的問題。

注意 :在編譯器上啟用額外的警告並聆聽它們時,除非您確切知道自己在做什么,否則請不要愚弄編譯器。

secondexpparttwo的注釋掉的部分中,您將返回不應該使用的局部變量的地址

您似乎填充了許多臨時大小的輔助數組。 它們的問題是它們可能對於較大的表達式溢出,並且除非使用malloc在堆上分配它們,否則您將無法返回它們,這也意味着您必須稍后free它們。

乍一看,您要返回的字符串是表達式字符串的子字符串或切片。 這意味着這些字符串的數據已經存在。

您可以(安全地)將指針返回到該字符串中。 就是這樣,例如strchrstrstr 如果您願意修改原始字符串,則還可以在子字符串后放置空終止符'\\0' 這就是strtok所做的事情,它的缺點是您會丟失該位置的信息:如果字符串為a*b並將其修改為a\\0b ,則您將不知道存在哪個運算符。

另一種方法是創建一個結構,該結構將切片作為指針存儲在字符串和長度中:

struct slice {
    const char *p;
    int length;
};

然后,您可以安全地返回原始字符串的切片,而不必擔心額外的內存。

如果您堅持使用strn變體,則在大多數情況下也可以使用標准功能。 打印切片時,可以通過以printf格式指定字段寬度來做到這一點:

printf("Second part: '%.*s'\n", s->length, s->p);

暫無
暫無

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

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