| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 304 人关注过本帖
标题:请帮忙看一下程序
只看楼主 加入收藏
carol7220
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-10-5
收藏
 问题点数:0 回复次数:0 
请帮忙看一下程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream.h>
#include <string.h>

#define MAXNUM 100

#define MaxSize 40


#define TRUE  1
#define FALSE 0
#define OK    1
#define ERROR 0

typedef struct

{

 int num;  //学号

 char name[10]; //姓名

} student;

typedef student EType;


student std[31];


int HeapSize;

void MaxHeapInit (EType a[],int size)
{//对数组a中的数据初始化为一个最大堆
    EType *heap=a;
    HeapSize=size;
    for (int i=HeapSize/2;i>=1;i--)
    {
        heap[0]=heap[i];
        int son=2*i;
        while(son<=HeapSize)
        {//找左右孩子中较大的结点
                if (son < HeapSize && heap[son].num < heap[son +1].num)
                son++;//son<HeapSize时存在右孩子,如果做孩子小于右孩子,son指向右孩子
            if(heap[0].num>=heap[son].num)
                break;
            heap[son/2]=heap[son];//将大孩子上移至双亲位置
            son*=2;//son下移至原大孩子的位置
        }
        heap[son/2]=heap[0];//将heap[0]村放到目标位置

    }
}

bool MaxHeapInsert(EType a[],EType &x)
{//插入值为X的结点到最大对中,MaxSize是空间最大容量
    EType *heap=a;
    if(HeapSize==MaxSize)
        return ERROR;
    int i=++HeapSize;
    while(i!=1 && x.num > heap[i/2].num)//寻找x的插入点
    {
        heap[i]=heap[i/2];
        i=i/2;//结点下移
    }
    heap[i]=x;
    return OK;
}

bool MaxHeapDelete(EType a[],EType &x)
{//删除堆顶结点
    EType *heap=a;
    if(HeapSize==0)
        return ERROR;//堆空
    x=heap[1];//最大结点寻放到x
    heap[0]=heap[HeapSize--];//最后一个节点村放到heap[0],调整堆中元素的个数
    int i=1,son=2*i;
    while(son<=HeapSize)
    {
        if(son<=HeapSize&&heap[son].num<heap[son+1].num)
            son++;
        if(heap[0].num>=heap[son].num)
            break;
        heap[i]=heap[son];
        i=son;
        son=son*2;
    }
    heap[i]=heap[0];
    return OK;
}

void Displayarray(EType a[])
{
    int i;
    for(i=1;i<31;i++)
        printf("%d ",a[i]);
    printf("\n");
}

void HeapSort(EType a[],int n)
{//利用堆对a[]数组中的数据排序
    EType *heap=a;
    MaxHeapInit(heap, n);
    EType x;
    int j;
    j=0;
    for(int i=n-1;i>=1;i--)
    {
        j++;
        MaxHeapDelete(heap,x);
        heap[i+1]=x;
        printf("\n第%d堆排序:",j);
        Displayarray(a);
        system("pause");
    }
}


char re_choose[]={"\n选择非法,请输入正确的编号...\n"};

void Menu_name()
     //作者信息
{   
    printf("\n\n\n\n\n\n\n");
    printf("              *************************************************\n");
    printf("                           学生信息的堆树的排序\n\n");
    printf("                           制作:xxx\n");
    printf("                           班级:xxx班\n");
    printf("                           学号: xxxx\n");
    printf("                           指导老师: xxxxx\n");
    printf("              **************************************************\n");
    printf("\n\n\n\t\t");
}



void Menu()    //菜单函数
{
    
//    cout <<"\n\n\t\t"<<"=============线性表的链式存储=============="<<endl;
    cout <<"\n\t\t"<<"请选择以下一个功能:"<<endl;
    cout <<"\t\t1.初始化前的树." << endl;
    cout <<"\t\t2.初始化后的最大树." << endl;
    cout <<"\t\t3.在最大堆树中插入一个元素." << endl;
    cout <<"\t\t4.删除堆顶点." << endl;
    cout <<"\t\t5.堆排序." << endl;
    cout <<"\t\t0.退出.\n"<<endl;
    cout <<"\t\t===============================\n"<<endl;    

}
void main_switch(char j)//操作选择函数
{

    EType std[31];
    EType x;


    switch(j)
    {
    
    case '1' :
                system("cls");
                printf("\n初始前数组std中的的数据排列:\n");
                Displayarray(std);
                system("pause");
                system("cls");
                break;                
                
    case '2':
            system("cls");
            printf("\n初始化为一个最大堆:\n");
            MaxHeapInit (std,31);
            Displayarray(std);
            system("pause");
            system("cls");
            break;
    case '3':
        system("cls");
        printf("请输入插入的元素的学号/n");
        cin>>x.num;
        printf("请输入插入的元素的姓名/n");
        cin>>x.name;
        MaxHeapInsert(std,x);
        Displayarray(std);
        system("pause");
        system("cls");
        break;
    case '4':
            system("cls");
            printf("\n删除堆顶点:\n");
            MaxHeapDelete (std,x);
            Displayarray(std);
            system("pause");
            system("cls");
            break;
    case '5':
            system("cls");
            printf("\n堆排序:\n");
            MaxHeapDelete (std,x);
            system("pause");
            system("cls");
            break;
    case '0':
            exit(0);
            break;
    default  :
        cout <<re_choose<<endl;
        system("pause");
        system("cls");
        break;
        
    }//end switch
}


 void main()
 {
     char Name[31][10]={"ro","阿布","安安","阿城","阿辉","阿雅","阿特","胡恩","奈叶","银河","宥利",
                        "阿里","阿忆","安红","阿飞","阿强","安庆","小平","小胖","小雷","小雨",
                        "小雪","韩星","小菜","小红","小凯","小樱","小迪","小天","萧然","如意"};
     char a[100];
     system("cls");
    Menu_name();
    system("pause");
    system("cls");    

     srand( (unsigned)time(NULL));

     for(int i = 1; i<31;i++)

    {

        int tmp=MAXNUM+1;

        while(tmp>MAXNUM)

        tmp=rand();

        std[i]. num=tmp;
        strcpy(std[i].name,Name[i]);
     }





while(1)
    {
        
        system("cls");
        Menu();    
        printf("\n\t请输入功能编号:");
        gets(a);
        
        if(a[1]!='\0')
        {
            cout <<"\n"<<re_choose<<endl;
            system("pause");
            system("cls");
            continue;
        }
        else
        {
            if(a[0]=='0')
                break;    
            main_switch(a[0]);        
            
        }

    }

    
 
     
}
为什么编译时没错,而链接时出错了?????
搜索更多相关主题的帖子: 姓名 include 
2008-12-02 23:34
快速回复:请帮忙看一下程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016413 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved