读书人

一个结构体的有关问题,十分感谢

发布时间: 2012-02-08 19:52:21 作者: rapoo

一个结构体的问题,十分感谢

弄了好长时间都没解决,大家能帮我看一个错在哪了吗,十分感谢
#include <stdio.h>
#define SMAX 100 /* 允许的最大非零元素个数 */
typedef struct
{ int i,j; /* 非零元素的行号和列号 */
int v; /* 非零元素的值 */
}triple;
typedef struct
{ int mu, nu, tu; /*矩阵的行数、列数和非零元个数*/
triple data[SMAX]; /*非零元素三元组长*/
}spmatrix;
void InitTriTupleNode (spmatrix a) /*建立三元组表结构*/
{ a.mu=a.nu=6;
a.tu=8;
a.data[a.tu]={{1,1,15},{1,4,22},{1,6,15},{2,2,11},{2,3,3},{3,4,6},{5,1,91},{6,3,28}};
}

void showMatrix(spmatrix a) /*输出稀疏矩阵*/
{int m,n,k;
for (m=1;m <=6;m++)
{for (n=1;n <=6;n++)
{ for (k=0;k <8;k++)
{if(a.data[k].i==m&&a.data[k].j==n)
printf ( "%d ",a.data[k].v);
else
printf ( "0 ");
}
}
}
}
void main ()
{ int choice;
spmatrix a;
printf ( "1 InitTriTuple\n ");
printf ( "2 Output the matrix\n ");
printf ( "please choice: ");
choice=getchar();
while (choice)
{ printf ( "1 InitTriTuple\n ");
printf ( "2 Output the matrix\n ");
printf ( "please choice: ");
choice=getchar();
switch (choice)
{case 1: InitTriTupleNode (a); break;
case 2: showMatrix(a); break;
}
}
getch();

}



------解决方案--------------------


a.data[a.tu]={{1,1,15},{1,4,22},{1,6,15},{2,2,11},{2,3,3},{3,4,6},{5,1,91},{6,3,28}};

这一句问题很多.
spmatrix中已经定义了 data[100],a.data[a.tu]只是对a.data[8]赋值.
你没有定义方法,将{1,4,22}转换成triple,不能赋值,编译报错.
[解决办法]
修正后的程序:

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

#define SMAX 100 /* 允许的最大非零元素个数 */

typedef struct
{
int i,j; /* 非零元素的行号和列号 */
int v; /* 非零元素的值 */
}triple;

typedef struct
{
int mu, nu, tu; /*矩阵的行数、列数和非零元个数*/
triple data[SMAX]; /*非零元素三元组长*/
}spmatrix;

void InitTriTupleNode (spmatrix *a) /*建立三元组表结构*/
{
triple tmp[]={{1,1,15},{1,4,22},{1,6,15},{2,2,11},{2,3,3},{3,4,6},{5,1,91},{6,3,28}};
int i;

a-> mu=a-> nu=6;
a-> tu=8;
for(i=0; i <8; i++)
a-> data[i]=tmp[i];
}

void showMatrix(spmatrix a) /*输出稀疏矩阵*/
{
int m,n,k;
for(m=1;m <=6;m++)
{
for(n=1;n <=6;n++)
{
for(k=0;k <8;k++)
if(a.data[k].i==m&&a.data[k].j==n)break;

if(k <8)
printf ( "%-4d ",a.data[k].v);
else
printf ( "0 ");
}
printf( "\n ");
}
}
int main()
{
int choice;
spmatrix a;
printf ( "0 Exit\n ");
printf ( "1 InitTriTuple\n ");
printf ( "2 Output the matrix\n ");
printf ( "please choice: ");
scanf( "%d%*c ", &choice);
while (choice!=0)
{
switch (choice)
{
case 1: InitTriTupleNode (&a); break;
case 2: showMatrix(a); break;
}
printf ( "\n\n0 Exit\n ");
printf ( "1 InitTriTuple\n ");
printf ( "2 Output the matrix\n ");
printf ( "please choice: ");
scanf( "%d%*c ", &choice);
}
system( "pause ");
return 0;
}
[解决办法]

#include <stdio.h>

#define SMAX 100 /* 允许的最大非零元素个数 */
typedef struct
{ int i,j; /* 非零元素的行号和列号 */
int v; /* 非零元素的值 */
}triple;

typedef struct
{ int mu, nu, tu; /*矩阵的行数、列数和非零元个数*/
triple data[SMAX]; /*非零元素三元组长*/
}spmatrix;
void InitTriTupleNode (spmatrix a) /*建立三元组表结构*/
{ a.mu=a.nu=6;
a.tu=8;
triple mTriple[SMAX] = {{1,1,15},{1,4,22},{1,6,15},{2,2,11},{2,3,3},{3,4,6},{5,1,91},{6,3,28}};
// a.data[a.tu]={{1,1,15},{1,4,22},{1,6,15},{2,2,11},{2,3,3},{3,4,6},{5,1,91},{6,3,28}};

for (int i = 0; i < a.tu; i++)
{
a.data[i] = mTriple[i];
}
}

void showMatrix(spmatrix a) /*输出稀疏矩阵*/
{int m,n,k;
for (m=1;m <=6;m++)
{for (n=1;n <=6;n++)
{ for (k=0;k <8;k++)
{if(a.data[k].i==m&&a.data[k].j==n)
printf ( "%d ",a.data[k].v);
else
printf ( "0 ");
}
}
}
}
void main ()
{ int choice;
spmatrix a;
printf ( "1 InitTriTuple\n ");
printf ( "2 Output the matrix\n ");
printf ( "please choice: ");


choice=getchar();
while (choice)
{ printf ( "1 InitTriTuple\n ");
printf ( "2 Output the matrix\n ");
printf ( "please choice: ");
choice=getchar();
switch (choice)
{case 1: InitTriTupleNode (a); break;
case 2: showMatrix(a); break;
}
}
// getch();

}

读书人网 >C语言

热点推荐