鱼C论坛

 找回密码
 立即注册
查看: 2496|回复: 17

求助(段错误)

[复制链接]
发表于 2021-9-20 23:24:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 三刀流.索隆 于 2021-9-20 23:26 编辑

就是程序运行不了,老提示段错误(核心已转储)
/********************/
/***功能:符号三角形**/
/***日期:2021.9.20****/
/*******************/

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

struct Queue
{
        char data;
        struct Queue *next;
};
typedef struct Queue Qnode;

typedef struct
{
        Qnode *front,*rear;
}Queue;

void InitQueue(Queue *s);
void InsertQueue(Queue *s,char e);
void DeleteQueue(Queue *s,char *e);

int main()
{
                Queue s;
                Qnode* ptr1,*ptr2;
                char c;
                int i = 0,j = 0,mem;
                
                printf("请合法输入-或+,#表示结束输入:");
                InitQueue(&s);
                scanf("%c",&c);

                while (c != '#')             //入队操作,#表示结束
                {
                        InsertQueue(&s,c);
                        i++;        
                        scanf("%c",&c);
                }
                
                //while ()
                printf("\n正在生成三角形...");

                ptr1 = s.front->next;
                ptr2 = s.rear;

                while (ptr1 != s.rear)      //遍历队列,并生成新节点,直到ptr1指针等于队尾
                
                        if (ptr1 == ptr2)
                        {
                                ptr2 = s.rear;
                                ptr1 = ptr1->next;
                        }
                        if (ptr1->data == ptr1->next->data)
                        {
                                InsertQueue(&s,'+');
                        }
                        else
                        {
                                InsertQueue(&s,'-');
                        }

                        ptr1 = ptr1->next;
                }
                        
                printf("\n即将为您打印三角形...\n");         //规律打印队列
                for (mem = i;mem >= 0;mem--,i = mem,j++)
                {
                        for (;i >= 0;i--)
                        {        
                                DeleteQueue(&s,&c);
                                printf("%c ",c);
                        }
                        printf("\n");
                        for (;j >= 0;)
                        {
                                printf(" ");
                        }
                
                }
                return 0;
        }

void InitQueue(Queue *s)//初始化队列函数
{
        s->front = s->rear = (Qnode*)malloc(sizeof(Qnode));
        if (s->front == NULL)
        {
                printf("内存分配失败!!!\n");
                exit(0);
        }
        s->rear->next = NULL;
}

void InsertQueue(Queue *s,char e)//入队函数
{
        //if (s->rear - s->front == maxSize)
        //        s->front = (Qnode*)realloc(s->rear,maxSize * sizeof(Qnode));

        s->rear->next = (Qnode*)malloc(sizeof(Qnode));
        if (s->rear->next == NULL)
        {
                printf("错误!");
                exit(0);
        }
        s->rear = s->rear->next;
        s->rear->data = e;
        s->rear->next = NULL;
}

void DeleteQueue(Queue *s,char *e)//出队函数
{
        Qnode *temp = s->front;
        s->front = s->front->next;
        *e = (s->front->data);
        free(temp);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-21 07:38:47 | 显示全部楼层
两个结构体名一样
struct Queue
{
        char data;
        struct Queue *next;
};
typedef struct Queue Qnode;

typedef struct
{
        Qnode *front,*rear;
}Queue;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 09:23:17 | 显示全部楼层
jhq999 发表于 2021-9-21 07:38
两个结构体名一样
struct Node
{
        char data;
        struct Node *next;
};
typedef struct Node Qnode;

typedef struct
{
        Qnode *front,*rear;
}Queue;
//改成这样还是一样的错啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-21 10:09:22 | 显示全部楼层
三刀流.索隆 发表于 2021-9-21 09:23
//改成这样还是一样的错啊

while (ptr1 != s.rear)      //遍历队列,并生成新节点,直到ptr1指针等于队尾
{\\你缺个{
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 14:04:10 | 显示全部楼层

可能是因为发帖的时候不小心弄的,编译器里的代码是好的,运行时唯一的报错就是段错误的问题,话说段错误的原因有哪些啊,我可以一个一个排查的,弄清楚了问题的原因,基本就能找到解决方法了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-21 14:29:46 | 显示全部楼层
1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-21 14:31:46 | 显示全部楼层
提供一下这个程序的输入,和输出(你期望的输出,正确的输出)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 17:04:02 | 显示全部楼层
人造人 发表于 2021-9-21 14:31
提供一下这个程序的输入,和输出(你期望的输出,正确的输出)


https://fishc.com.cn/thread-27518-1-1.html

就是小甲鱼的数据结构的一个练习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-21 17:08:05 | 显示全部楼层
三刀流.索隆 发表于 2021-9-21 17:04
https://fishc.com.cn/thread-27518-1-1.html

就是小甲鱼的数据结构的一个练习

你直接提供一个输入和输出就可以了
这个程序的输入是什么?这样?
1 2 3 4 #
如果是这个输入,输出是什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 17:10:23 | 显示全部楼层
本帖最后由 三刀流.索隆 于 2021-9-21 17:15 编辑
人造人 发表于 2021-9-21 17:08
你直接提供一个输入和输出就可以了
这个程序的输入是什么?这样?
1 2 3 4 #


输入:++-+-++#,就是输入一串+和-
输出:+ + - + - + +
           + - -  - - +
            - + + + -
             -  + + -
               - + -
                -  -
                 +
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-21 17:11:31 | 显示全部楼层
好了,知道输入输出了,我研究研究
输入: ++-+-++
输出:
1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 17:18:56 | 显示全部楼层
人造人 发表于 2021-9-21 17:11
好了,知道输入输出了,我研究研究
输入: ++-+-++
输出:

规律就是:+ +    |          - -      |             - +           |         + -
                  -       |          +       |              -             |           -

符号相同在下面输出+,不同为-,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-21 20:53:58 | 显示全部楼层
三刀流.索隆 发表于 2021-9-21 17:18
规律就是:+ +    |          - -      |             - +           |         + -
                   ...

1. 你的代码逻辑不对
2. 申请了内存一定要释放,不释放在一些环境下会报错的
3. 模块化编程,不要在 queue 函数的外面操作 queue 的内部数据结构
/********************/
/***功能:符号三角形**/
/***日期:2021.9.20****/
/*******************/

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

struct node_tag {
    char data;
    struct node_tag *next;
};

typedef struct node_tag Qnode;

typedef struct {
    Qnode *front, *rear;
    size_t size;
} Queue;

void InitQueue(Queue *s);
void InsertQueue(Queue *s, char e);
void DeleteQueue(Queue *s, char *e);
size_t queue_size(Queue *s);
void queue_deinit(Queue *s);
void queue_undelete(Queue *s, char e);
void queue_uninsert(Queue *s, char *e);

int main() {
    Queue s;
    //Qnode *ptr1, *ptr2;
    char c;
    //int i = 0, j = 0, mem;

    printf("请合法输入-或+,#表示结束输入:");
    InitQueue(&s);
    scanf("%c", &c);

    while (c != '#') //入队操作,#表示结束
    {
        InsertQueue(&s, c);
        //i++;
        scanf("%c", &c);
    }

    /*
    // while ()
    printf("\n正在生成三角形...");

    ptr1 = s.front->next;
    ptr2 = s.rear;

    while (ptr1 != s.rear) //遍历队列,并生成新节点,直到ptr1指针等于队尾
    {
        if (ptr1 == ptr2) {
            ptr2 = s.rear;
            ptr1 = ptr1->next;
        }
        if (ptr1->data == ptr1->next->data) {
            InsertQueue(&s, '+');
        } else {
            InsertQueue(&s, '-');
        }

        ptr1 = ptr1->next;
    }

    printf("\n即将为您打印三角形...\n"); //规律打印队列
    for (mem = i; mem >= 0; mem--, i = mem, j++) {
        for (; i >= 0; i--) {
            DeleteQueue(&s, &c);
            printf("%c ", c);
        }
        printf("\n");
        for (; j >= 0;) {
            printf(" ");
        }
    }
    */
    size_t width = queue_size(&s);
    size_t height = width;
    for(size_t y = 0; y < height; ++y) {
        size_t i = y;
        while(i--) printf(" ");
        for(size_t x = 0; x < width - y; ++x) {
            char a, b, c = '-';
            DeleteQueue(&s, &a);
            DeleteQueue(&s, &b);
            queue_undelete(&s, b);
            if(a == b) c = '+';
            InsertQueue(&s, c);
            printf("%c ", a);
        }
        char e;
        queue_uninsert(&s, &e);
        printf("\n");
    }
    queue_deinit(&s);       // 释放函数一定要写
    return 0;
}

void InitQueue(Queue *s) //初始化队列函数
{
    s->front = s->rear = (Qnode *)malloc(sizeof(Qnode));
    if (s->front == NULL) {
        printf("内存分配失败!!!\n");
        exit(0);
    }
    s->rear->next = NULL;
    s->size = 0;
}

void InsertQueue(Queue *s, char e) //入队函数
{
    // if (s->rear - s->front == maxSize)
    //        s->front = (Qnode*)realloc(s->rear,maxSize * sizeof(Qnode));

    s->rear->next = (Qnode *)malloc(sizeof(Qnode));
    if (s->rear->next == NULL) {
        printf("错误!");
        exit(0);
    }
    s->rear = s->rear->next;
    s->rear->data = e;
    s->rear->next = NULL;
    ++s->size;
}

void DeleteQueue(Queue *s, char *e) //出队函数
{
    /*
    Qnode *temp = s->front;
    s->front = s->front->next;
    *e = (s->front->data);
    free(temp);
    */
    if(queue_size(s) == 0) return;
    Qnode *temp = s->front->next;
    s->front->next = s->front->next->next;
    *e = temp->data;
    free(temp);
    if(--s->size == 0) {
        s->rear = s->front;
    }
}

size_t queue_size(Queue *s) {
    return s->size;
}

void queue_deinit(Queue *s) {
    char e;
    while(queue_size(s) != 0) DeleteQueue(s, &e);
    free(s->front);
}

void queue_undelete(Queue *s, char e) {
    Qnode *temp = malloc(sizeof(*temp));
    temp->data = e;
    temp->next = s->front->next;
    s->front->next = temp;
    if(s->size++ == 0) {
        s->rear = temp;
    }
}

void queue_uninsert(Queue *s, char *e) {
    if(queue_size(s) == 0) return;
    Qnode *prev = s->front;
    while(prev->next != s->rear) prev = prev->next;
    s->rear = prev;
    free(s->rear->next);
    s->rear->next = NULL;
    --s->size;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 22:45:37 | 显示全部楼层
人造人 发表于 2021-9-21 20:53
1. 你的代码逻辑不对
2. 申请了内存一定要释放,不释放在一些环境下会报错的
3. 模块化编程,不要在 qu ...

牛逼啊,果然是神仙,嗯~ o(* ̄▽ ̄*)o,我还想问一下,你用的是什么编译器啊,太舒服了吧~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-21 22:48:10 | 显示全部楼层
三刀流.索隆 发表于 2021-9-21 22:45
牛逼啊,果然是神仙,嗯~ o(* ̄▽ ̄*)o,我还想问一下,你用的是什么编译器啊,太舒服了吧~

vim
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 22:55:42 | 显示全部楼层

咦,怎么弄的呀?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-21 23:25:11 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-1 20:06:33 | 显示全部楼层
#include <stdio.h>

int count = 1;
int notDanger(int row,int j,int (*chess)[8])
{
        int i,k,flag1 = 0,flag2 = 0,flag3 = 0,flag4 = 0,flag5 = 0;
        
        //列
        for (i = 0;i < 8;i++)
        {
                if (*(*(chess+i)+j) != 0)
                {
                        flag1 = 1; 
                        break;
                }
        }

        //左上方
        for(i = row,k = j;i >= 0 && k >= 0;i--,k--)
        {
                if (*(*(chess+i)+k) != 0)
                {
                        flag2 = 1;
                        break;
                }
        }

//        右下方
        for(i = row,k = j;i < 8 && k < 8;i++,k++)
        {
                if (*(*(chess+i)+k) != 0)
                {
                        flag3 = 1;
                        break;
                }
        }
        
        //
        for(i = row,k = j;i >= 0 && k < 8;i--,k++)
        {
                if (*(*(chess+i)+k) != 0)
                {
                        flag4 = 1;
                        break;
                }
        }
        
        for(i = row,k = j;i < 8 && k >= 0;i++,k--)
        {
                if (*(*(chess+i)+k) != 0)
                {
                        flag5 = 1;
                        break;
                }
        }
        
        if (flag1||flag3||flag2||flag4||flag5)
                return 0;
        else
                return 1;
}
//row:起始行,
//n:列数
//(*chess)[8]:指向每一行的指针
void EightQueen(int row,int n,int (*chess)[8])
{
        int chess2[8][8],i,j;

        for (i = 0;i < 8;i++)
        {
                for (j = 0;j < 8;j++)
                {
                        chess2[i][j] = chess[i][j];
                }

        }

        if (8 == row)
        {
                printf("第%d种:\n",count);
                for(i = 0;i < 8;i++)
                {
                        for (j = 0;j < 8; j++)
                        {
                                printf("%d ",*(*(chess2+i)+j));
                        }
                        printf("\n");
                }
                printf("\n");
                count++;
        }
        else
        {
                for (j = 0; j < n;j++)
                {
                        if (notDanger(row,j,chess2))
                        {
                                for(i = 0;i < 8;i++)
                                {
                                        *(*(chess2+row)+i) = 0;
                                }
                                
                                *(*(chess2+row)+j) = 1;
                                EightQueen(row+1,n,chess2);
                        }
                }
        }
}

int main()
{
        int chess[8][8],i,j;
        
        for (i = 0;i < 8;j++)
        {
                
                for (j = 0;j < 8;j++)
                {
                        chess[i][j] = 0;
                }
        }

        EightQueen(0,8,chess);
        printf("总共有%d种解决方法!\n\n",count);
        
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 11:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表