请各位高手帮忙看看,为什么队列的打印和求最大值不能正确执行?
#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;
}