对称矩阵压缩算法的实现 下载本文

for(i=1;i<=n;i++) {

for(j=1;j<=n;j++)

printf(\ printf(\ }

return ok; }

int JudgeMatrix(Array s)//判断矩阵是否为对称矩阵 {

int i,j,k; k=0;

for(i=1;i<=n;i++) for(j=1;j<=n;j++) {

if(s.M[i][j]!=s.M[j][i]) k++; }

printf(\判断得到不相等元素的对数k=%d\ printf(\ if(k==0) {

printf(\

printf(\

printf(\对称矩阵构建正确!请您选择其他服务!\\n\ } else {

printf(\

printf(\您构建的矩阵不是对称矩阵\ return 1; }

return 0; }

int CompMatrix(Array &s)//对对称矩阵进行压缩存储 {

int i,j,k=0;

for(i=1;i<=n;i++) for(j=1;j<=i;j++)

12

{

s.m[k]=s.M[i][j]; k++; }

printf(\压缩后的矩阵存入一维数组后各元素为:\\n\ printf(\

for(k=0;k

int OutputMatrix(Array s)//按照数学形式输出矩阵 {

int i,j,k=0;

for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(i>=j) {

k=i*(i-1)/2+j-1; s.M[i][j]=s.m[k]; } else {

k=j*(j-1)/2+i-1; s.M[i][j]=s.m[k]; }

printf(\您压缩存储的矩阵按照数学形式输出为:\\n\ printf(\ for(i=1;i<=n;i++) {

for(j=1;j<=n;j++)

printf(\ printf(\ }

return ok; }

int menu_select() //菜单

13

{

char c; do{

printf(\ printf(\对称矩阵压缩算法的实现 *\\n\ printf(\构建任意N*N个元素的矩阵 *\\n\ printf(\判断所建矩阵是否为对称矩阵 *\\n\ printf(\对对称矩阵进行压缩存储 *\\n\ printf(\按照数学形式输出所压缩的矩阵 *\\n\ printf(\退出程序 *\\n\ printf(\ printf(\请您输入选项1-5*\\n\ fflush(stdin); c=getchar();

}

while(c<'1'||c>'5');

return(c); //返回选择 }

void main() //主函数 {

char n='1'; int m;

for(;;) {

switch(menu_select() ) {

case '1':

printf(\

printf(\构建任意n*n个元素的矩阵>\\n\ printf(\ CreatMatrix(s); printf(\

printf(\您已经成功构建*任意*矩阵!请您选择其他服务!\\n\ printf(\ printf(\ system(\ break; case'2':

14

}

}

printf(\

printf(\判断矩阵是否为对称矩阵>\\n\ printf(\ m=JudgeMatrix(s); printf(\ if(m==1)

CreatMatrix(s); printf(\ printf(\ system(\ break; case'3':

printf(\

printf(\对对称矩阵进行压缩存储>\\n\ printf(\

printf(\只存储其下三角各元素:\\n\ printf(\ CompMatrix(s); printf(\ printf(\ system(\ break; case'4':

printf(\

printf(\按照数学形式输出所压缩的矩阵>\\n\ printf(\ OutputMatrix(s); printf(\ printf(\ system(\ break; case'5':

printf(\

printf(\祝您好运!\\n\ printf(\ exit(0); }

15