跃2.0 2018-07-26 14:01 采纳率: 40%
浏览 680
已采纳

这个代码哪里有问题?为什么输出不正常

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int bool;
struct Array
{
    int * pHead;//头指针
    int len;//数组长度
    int cnt;//当前元素个数
};
void initArray(struct Array *p,unsigned length);//初始化数组函数
void showArray(struct Array *p);//输出数组
bool isEmpty(struct Array *p);//判断数组元素是否为0
bool isFull(struct Array *p);//判断元素是否为满
void appand(struct Array *p,int i);//末尾追加元素
void sort(struct Array *p);//int jj;
int main()
{
    struct Array arr;
    initArray(&arr, 5);
    appand(&arr, 3);
    appand(&arr, 1000);
    appand(&arr, 5);
    printf("sort before:");
    showArray(&arr);
    sort(&arr);
    printf("\nsort after:");
    showArray(&arr);
}//初始化数组
void initArray(struct Array *p,unsigned length)
{
    if(length==0)
    {
        printf("数组长度不合法\n");
        exit(0);
    } //
    p->pHead=(int *)malloc(sizeof(int));
    if((p->pHead=(int *)malloc(sizeof(int)))==NULL)
    {
        printf("初始化失败\n");    //
        exit(0);
    }
    p->len=length;
    p->cnt=0;
   // free(p->pHead);
}
//数组输出函数
void showArray(struct Array *p)
{
   // struct Array *h=(struct Array*)malloc(sizeof(struct Array));
   // h=p;
    int i;
    if(p->len==0) printf(NULL);
    if(p->cnt==0) printf("数组元素为空\n");
    for (i=0; i<p->cnt; i++)
    {
        printf("%d ",p->pHead[i]);
    }
    //return p;

}//判断数组是否为空
bool isEmpty(struct Array *p)
{
    return p->cnt==0?1:0;
}//判断数组是否满
bool isFull(struct Array *p)
{
    return p->cnt==p->len?1:0;
}//末尾追加元素
void appand(struct Array *p,int i)
{
    //判断当前元素是否已满
    if(isFull(p)) printf("元素已满,添加失败\n");
    p->pHead[p->cnt]=i;
    p->cnt=p->cnt+1;
}//排序冒泡
void sort(struct Array *p)
{  int i,j;
    int temp;
    for (i=0; i<=p->cnt-1; i++)
    {
        for (j=0; j<=p->cnt-i-i ; j++)
        {
            if (p->pHead[j]>p->pHead[j+1])
            {
                temp=p->pHead[j];
                p->pHead[j]=p->pHead[j+1];
                p->pHead[j+1]=temp;
            }
        }
    }
}


输出有时候会变 正常应该输出 3 5 1000 但是它输出有时候会变成各种奇怪的值偶尔

  • 写回答

2条回答

  • threenewbee 2018-07-26 15:09
    关注
     // Q695942.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    //typedef int bool;
    struct Array
    {
        int * pHead;//头指针
        int len;//数组长度
        int cnt;//当前元素个数
    };
    void initArray(struct Array *p,unsigned length);//初始化数组函数
    void showArray(struct Array *p);//输出数组
    bool isEmpty(struct Array *p);//判断数组元素是否为0
    bool isFull(struct Array *p);//判断元素是否为满
    void appand(struct Array *p,int i);//末尾追加元素
    void sort(struct Array *p);//int jj;
    int main()
    {
        struct Array arr;
        initArray(&arr, 5);
        appand(&arr, 3);
        appand(&arr, 1000);
        appand(&arr, 5);
        printf("sort before:");
        showArray(&arr);
        sort(&arr);
        printf("\nsort after:");
        showArray(&arr);
    }//初始化数组
    void initArray(struct Array *p,unsigned length)
    {
        if(length==0)
        {
            printf("数组长度不合法\n");
            exit(0);
        } //
        //p->pHead=(int *)malloc(sizeof(int));
        if((p->pHead=(int *)malloc(sizeof(int) * 5))==NULL)
        {
            printf("初始化失败\n");    //
            exit(0);
        }
        else
        {
            memset(p->pHead, 0, 5 * sizeof(int));
        }
        p->len=5;
        p->cnt=0;
       // free(p->pHead);
    }
    //数组输出函数
    void showArray(struct Array *p)
    {
       // struct Array *h=(struct Array*)malloc(sizeof(struct Array));
       // h=p;
        int i;
        if(p->len==0) printf(NULL);
        if(p->cnt==0) printf("数组元素为空\n");
        for (i=0; i<p->cnt; i++)
        {
            printf("%d ",p->pHead[i]);
        }
        //return p;
    
    }//判断数组是否为空
    bool isEmpty(struct Array *p)
    {
        return p->cnt==0?1:0;
    }//判断数组是否满
    bool isFull(struct Array *p)
    {
        return p->cnt==p->len?1:0;
    }//末尾追加元素
    void appand(struct Array *p,int i)
    {
        //判断当前元素是否已满
        if(isFull(p)) printf("元素已满,添加失败\n");
        p->pHead[p->cnt]=i;
        p->cnt=p->cnt+1;
    }//排序冒泡
    void sort(struct Array *p)
    {  int i,j;
        int temp;
        for (i=0; i<=p->cnt-1; i++)
        {
            for (j=0; j<=p->cnt-i-2 ; j++)
            {
                if (p->pHead[j]>p->pHead[j+1])
                {
                    temp=p->pHead[j];
                    p->pHead[j]=p->pHead[j+1];
                    p->pHead[j+1]=temp;
                }
            }
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)