| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 702 人关注过本帖
标题:请各位高手帮忙看看,为什么队列的打印和求最大值不能正确执行?
只看楼主 加入收藏
ice_callous
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-10-14
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:8 
请各位高手帮忙看看,为什么队列的打印和求最大值不能正确执行?
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdarg.h>

#define N 2


typedef float elemtype;
typedef struct
{
    elemtype *base;
    int dim;
    int *bounds;
    int *constants;
}Array;


void InitArray(Array &A)
{
    A.dim=3;
    int elemtotal;
    A.bounds=(int *)malloc(A.dim*sizeof(int));
    A.bounds[0]=N;
    A.bounds[1]=N;
    A.bounds[2]=N;
    elemtotal=N*N*N;
    A.base=(elemtype*)malloc(elemtotal*sizeof(elemtype));
    A.constants=(int*)malloc(A.dim*sizeof(int));
    A.constants[2]=sizeof(elemtype);
    A.constants[1]=A.constants[2]*N;
    A.constants[0]=A.constants[1]*N;
}


void DataIn_Array(Array &A)
{
    elemtype *y;
    int j1,j2,j3;
    float t;
    for(j1=0;j1<A.bounds[0];j1++)
        for(j2=0;j2<A.bounds[1];j2++)
            for(j3=0;j3<A.bounds[2];j3++)
            {
                scanf("%f",&t);
                y=A.base+A.constants[0]*j1+A.constants[1]*j2+A.constants[2]*j3;
                *y=t;
            }
}

void Print_Array (Array &A)
{
    int counter=0,i;
    int j2=0;
    for(i=0;i<A.bounds[0]*A.bounds[1]*A.bounds[2];i++)
    {
        counter++;
        j2++;
        printf("\n");
        printf("第%d层,第%d行,第%d列元素为:",(counter-1)/(A.bounds[1]*A.bounds[2]),
            (j2-1)/A.bounds[2],(counter-1)%A.bounds[2]);
        printf("%f",A.base[i]);
        if(counter%(A.bounds[1]*A.bounds[2])==0)
            j2=0;
    }

}


void MaxTem(Array &A)
{
    float max=-300.0;
    int i,j;
    for(i=0;i<A.bounds[0]*A.bounds[1]*A.bounds[2];i++)
    {
        if(A.base[i]>max)
        {
            max=A.base[i];
            j=i;
        }
    printf("\n最大值在%d层,%d行,%d列",(j-1)/(A.bounds[1]*A.bounds[2]),((j-1)%(A.bounds[1]*A.bounds[2]))/A.bounds[2],(j-1)%A.bounds[2]);
    }
}



void main()
{
    Array MyArry;
    int x;
    InitArray(MyArry);
    printf("\n***********************\n");
    printf("*1.读入数据 2.输出数据*\n");
    printf("*3.求最大值 4.退出    *\n");
    printf("***********************\n");
a:  printf("请输入你的选择\n");
    scanf("%d",&x);
    switch(x)
    {
    case 1:printf("\n向数组中读入数据为:\n");
           DataIn_Array(MyArry);
           printf("\n");
           break;
   
    case 2:printf("\n数组中的数据为:");
           Print_Array(MyArry);
           printf("\n");
           break;
   
    case 3:printf("\n当前数组中的最大值为:");
           MaxTem(MyArry);
           printf("\n");
           break;
   
    case 4:exit(0);break;
           
   
    default: printf("Error\n");
    }
    goto a;
}
搜索更多相关主题的帖子: 最大值 队列 打印 
2010-11-04 22:37
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:10 
头晕眼花
2010-11-05 15:18
ice_callous
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-10-14
收藏
得分:0 
回复 2楼 寒风中的细雨
麻烦帮忙看看,新手,谢谢了,真的不知道哪里出错了

2010-11-05 18:09
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:10 
typedef struct
{
    elemtype *base;//
    int dim;//
    int *bounds;//
    int *constants;//
}Array;
给标注下是什么意思  在下文要表示
2010-11-05 22:23
ice_callous
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-10-14
收藏
得分:0 
回复 4楼 诸葛修勤
typedef struct
{
    elemtype *base;//数组元素基址   
    int dim;//数组维数
    int *bounds;//数组维界基址   
    int *constants;//数组映像函数常量基址
}Array;


2010-11-05 23:24
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
.cpp 文件
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdarg.h>

#define N 2

typedef float elemtype;
typedef struct
{
    elemtype *base;
    int dim;
    int *bounds;
    int *constants;
}Array;


void InitArray(Array &A)
{
    A.dim=3;
    int elemtotal;
    A.bounds = (int *)malloc(A.dim*sizeof(int));//分配3个元素单位
    A.bounds[0] = N;
    A.bounds[1] = N;
    A.bounds[2] = N;
    elemtotal = N*N*N;

    A.base = (elemtype*)malloc(elemtotal*sizeof(elemtype));//分配pow(N, 3)个元素单位
    A.constants = (int*)malloc(A.dim*sizeof(int));//分配3个元素单位
    //A.constants[2] = sizeof(elemtype);
    A.constants[2] = 1;
    A.constants[1] = A.constants[2]*N;
    A.constants[0] = A.constants[1]*N;
}


void DataIn_Array(Array &A)
{
    elemtype *y;
    int j1,j2,j3;
    float t;
    for(j1=0;j1<A.bounds[0];j1++)
        for(j2=0;j2<A.bounds[1];j2++)
            for(j3=0;j3<A.bounds[2];j3++)
            {
                scanf("%f",&t);
                y=A.base+A.constants[0]*j1+A.constants[1]*j2+A.constants[2]*j3;
                *y=t;
            }
}

void Print_Array (Array &A)
{
    int counter=0, i;
    int j2 = 0;
    for(i=0; i<A.bounds[0]*A.bounds[1]*A.bounds[2]; i++)
    {
        counter++;
        //j2++;
        printf("\n");
        printf("第%d层,第%d行,第%d列元素为:",j2+1,
            (counter-1)/A.bounds[2] + 1, (counter-1)%A.bounds[2] + 1);
        printf("%f",A.base[i]);
        if(counter%(A.bounds[1]*A.bounds[2]) == 0)
        {
            ++j2;
            counter = 0;
        }
    }

}


void MaxTem(Array &A)
{
    //float max=-300.0;
    float max = A.base[0];
    int i,j;
    for(i=0;i<A.bounds[0]*A.bounds[1]*A.bounds[2];i++)
    {
        if(A.base[i]>max)
        {
            max=A.base[i];
            j=i;
        }
    }
    int dim = j/(A.bounds[1]*A.bounds[2]) + 1;
    int row = (j - (dim-1)*A.bounds[1]*A.bounds[2])/A.bounds[2] + 1;
    int col = (j - (dim-1)*A.bounds[1]*A.bounds[2])%A.bounds[2] + 1;
    printf("\n最大值在%d层,%d行,%d列 %f\n",dim, row, col, max);
}



void main()
{
    Array MyArry;
    int x;
    InitArray(MyArry);
    printf("\n***********************\n");
    printf("*1.读入数据 2.输出数据*\n");
    printf("*3.求最大值 4.退出    *\n");
    printf("***********************\n");
a:  printf("请输入你的选择\n");
    scanf("%d",&x);
    switch(x)
    {
    case 1:printf("\n向数组中读入数据为:\n");
           DataIn_Array(MyArry);
           printf("\n");
           break;
   
    case 2:printf("\n数组中的数据为:");
           Print_Array(MyArry);
           printf("\n");
           break;
   
    case 3:printf("\n当前数组中的最大值为:");
           MaxTem(MyArry);
           printf("\n");
           break;
   
    case 4:exit(0);break;
           
   
    default: printf("Error\n");
    }
    goto a;
}
2010-11-06 09:03
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2010-11-06 09:03
ice_callous
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-10-14
收藏
得分:0 
回复 7楼 寒风中的细雨
非常感谢你的帮助
2010-11-06 09:42
Tveiker
Rank: 2
来 自:湖南省张家界桑植
等 级:论坛游民
帖 子:17
专家分:41
注 册:2010-9-28
收藏
得分:0 
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdarg.h>

#define N 2

//这个程序应该是存储一个多维数组的
typedef float elemtype;//用elemtype替换float,这是程序员的编程习惯,对该程序无关紧要,若不用这句话,只要将以下的elemtype换回即可
typedef struct
{
    elemtype *base;    //存储的数据指针,用于存储数据
    int dim;           //维数
    int *bounds;       //存放每一维的数据个数的指针
    int *constants;    //用于记住每一维的权值
}Array;


void InitArray(Array &A)
{
    A.dim=3;//定义一个三维数组
    int elemtotal;//统计数据总数用于下句话的开辟空间
    A.bounds=(int *)malloc(A.dim*sizeof(int));
    //为每一维的数据个数赋值
    A.bounds[0]=N;
    A.bounds[1]=N;
    A.bounds[2]=N;
    elemtotal=N*N*N;
    A.base=(elemtype*)malloc(elemtotal*sizeof(elemtype));
    A.constants=(int*)malloc(A.dim*sizeof(int));//为权值分配空间
    A.constants[2]=(N-1);  //确定第2列的权的大小        
    A.constants[1]=A.constants[2]*N;//确定第1列权值大小
    A.constants[0]=A.constants[1]*N;//确定第0列权值大小
    //打个比方,199是十进制数,其百位权值为100,十位为10,各位为1.若与该题对应起来,A[0]=1,A[1]=9,A[2]=9
    //199=A[0]*1+A[1]*10+A[2]*100;不过该题中的数据是N进制的其若按此规则,下标为i的权是N的i-1次方
}


void DataIn_Array(Array &A)
{
    elemtype *y;
    int j1,j2,j3;
    float t;
    for(j1=0;j1<A.bounds[0];j1++)
        for(j2=0;j2<A.bounds[1];j2++)
            for(j3=0;j3<A.bounds[2];j3++)
            {
                scanf("%f",&t);
                y=A.base+A.constants[0]*j1+A.constants[1]*j2+A.constants[2]*j3;//给指针赋值,以便给A.base[i]赋值
                //这句话其实可以直接用*(A.base+A.constants[0]*j1+A.constants[1]*j2+A.constants[2]*j3)=t代替,此时则不需要y
                //即该函数第一句话与最后一句可以省略
                *y=t;
            }
}

void Print_Array (Array &A)
{
    int counter=0,i;
    int j2=0;
    for(i=0;i<A.bounds[0]*A.bounds[1]*A.bounds[2];i++)
    {
        counter++;
        j2++;
        printf("\n");
        //注意下标与每个数据层行列的关系
        printf("第%d层,第%d行,第%d列元素为:",(counter-1)/(A.bounds[1]*A.bounds[2]),
            (j2-1)/A.bounds[1],(counter-1)%A.bounds[2]);
        printf("%f",A.base[i]);
        if(counter%A.constants[0]==0)
             j2=0;
    }

}


void MaxTem(Array &A)
{
    int i=0,j;
    float max=A.base[i];//这里要修改下,不能直接赋-3,如果所有的数比-3小,将出现错误输出
    for(i=0;i<A.bounds[0]*A.bounds[1]*A.bounds[2];i++)
    {
        if(A.base[i]>max)
        {
            max=A.base[i];//将较大者赋给max
            j=i;//记录最大值的下标
        }
    }
    //在打印时注意每个数据的层行列对应的下标关系
    printf("%f\n最大值在%d层,%d行,%d列",A.base[j],(j-1)/(A.bounds[1]*A.bounds[2]),(j%(A.bounds[1]*A.bounds[2]))/A.bounds[2],j%A.bounds[2]);
}



void main()
{
    Array MyArry;
    int x;
    InitArray(MyArry);
    printf("\n***********************\n");
    printf("*1.读入数据 2.输出数据*\n");
    printf("*3.求最大值 4.退出    *\n");
    printf("***********************\n");
a:  printf("请输入你的选择\n");
    scanf("%d",&x);
    switch(x)
    {
    case 1:printf("\n向数组中读入数据为:\n");
           DataIn_Array(MyArry);
           printf("\n");
           break;
   
    case 2:printf("\n数组中的数据为:");
           Print_Array(MyArry);
           printf("\n");
           break;
   
    case 3:printf("\n当前数组中的最大值为:");
           MaxTem(MyArry);
           printf("\n");
           break;
   
    case 4:exit(0);break;
           
   
    default: printf("Error\n");
    }
    goto a;
}
2010-11-06 22:45
快速回复:请各位高手帮忙看看,为什么队列的打印和求最大值不能正确执行?
数据加载中...
 
   



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

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