2023年4月25日发(作者:人力资源管理概念)稀疏矩阵的应用(十字链表)
一、 设计要求
1.问题描述
设计程序用十字链表实现稀疏矩阵的加、乘、转置。
2.需求分析
(1)设计函数建立稀疏矩阵,初始化值。
(2)设计函数输出稀疏矩阵的值。
(3)构造函数进行两个稀疏矩阵相加,输出最终的稀疏矩阵。
(4)构造函数进行两个稀疏矩阵的相乘,输出最终的稀疏矩阵。
(5)构造函数进行冰激凌的英语
稀疏矩阵的转置,并输出结果。
(6)退出系统。
二、 概要设计
为了实现以上功能,可以从3个方面着手设计。
1.主界面设计
为了实现对稀疏矩阵的多种算法功能的管理,首先设计一个含有多个菜单项的主控菜单子程序以链接系统的各项子功能,方便用户交互式使用本系统。本系统主控菜单运行界面如图(1-1)所示。
图 1-1 主界面
2.存储结构设计
本系统采用单链表结构(single list)存储稀疏矩阵的具体信息。其中:全部结点的信息用头结点为指针数组的单链表存储;每个结点里面包含行下标(row),列下标(colum)和对应的数值(value),它们是整型数据,还有两个指针(right)、(down),属于data结构体。全部的信息用结构体(crosslist)包含,包括指针数组(element * 和*)和总共的行数(row_size),列数(colum_size)以及非零元素的个数(non_zero_amount)。
3.系统功能设计
本系统除了要完成稀疏矩阵的初始化功能外还设置了4个子功能菜单。稀疏矩阵的初始化由函数init_matrix( )实现。建立稀疏矩阵用creat_matrix( )实现,依据读入的行数和列数以及非零元素的个数,分别设定每个非零元素的信息。4个子功能的设计描述如下。
(1)稀疏矩阵的加法:
此功能由函数add_matrix( )实现,当用户选择该功能,系统即提示用户初始化要进行加法的两个矩阵的信息。然后进行加法,最后输出结果。
(2)稀疏矩阵的乘法:
此功能由函数multi_matrix( )实现。当用户选择该功能,系统提示输入要进行相乘的两个矩阵的详细信息。然后进行相乘,最后得到结果。
(3)稀疏矩阵的转置:
此功能由函数trans_matrix( )实现。当用户选择该功能,系统提示用户初始化一个矩阵,然后进行转置,最终输出结果。
(4)退出:
即退出稀疏矩阵的应用系统。由exit(1)函数实现,但用户选择此功能时,系统会提示你是否确实想退出,如果是,则退出,否则继续。
三、 模块设计
1.模块设计
本程序包含2个模块:主程序模块和各功能实现模块。调用关系如图7-8所示。
主程序模块
各功能实现模块
图1-2 模块调用示意图
2.系统子程序
及功能设计
本系统共设置7个子程序,各子程序的函数名及功能说明如下。
(1)int init_matrix(crosslist &one) // 初始化矩阵one
(2)_int creat_matrix(crosslist &one) //建立矩阵one
(3)int print_matrix(crosslist &one) // 输出矩阵one的信息
以下编号(4)-(6)是稀疏矩阵的基本操作。依次是:相加,相乘,转置等。
(4)int add_matrix(crosslist one,crosslist two, crosslist &three)
//把one 和two两个矩阵相加,结果是three
(5)int multi_matrix(crosslist one, crosslist two,crosslist &three)
//把one和two两个矩阵相乘,结果是three
(6)int trans_matrix(crosslist & one)
// 把one转置
(7)int main( )
// 主函数。设定界面的颜色,大小和窗口的标题,调用工作区模块函数
4.函数主要调用关系图
稀疏矩阵应用系统7个子程序之间的主要调用关系如图1-3所示。图中数字是各函数的编号。 1
2
1
2
1
2
3
3
7 main ( )
4
5
6
3
四、 详细设计
1.数据类型定义
图1-3 系统函数调用关系图
(1)稀疏矩阵的定义:
typedef struct list
{//node structure //结点结构
int row;//row subscript //行标
int colum;//colum subscript //列标
int value;//value of the node //值
struct list *right;//the next element have the same row //同一行的下一个结点
struct list *down;//the next element have the same colum //同一列的下一个结点
}node,*element;
typedef struct link
{//cross list structure
int row_size;//row size of the matrix //行数
int colum_size;//colum size of the matrix //列数
int non_zero_amount;//the number of non zero element //非零元素个数
element *rhead;//the ba of the row //行指针基指
element *chead;//the ba of the colum //列指针基指
}crosslist;
2.系统主要子程序详细设计
(1)主程序模块设计及用户工作区模块设计
主函数。设定操作区用户界面的颜色,大小和窗口的标题,调用工作区模块函数。
int main(void)
{
/*declare three matrix*/
crosslist one,two,three; //声明三个十字链表链表
int choice;//as a mark of lection //选择的
标志
char flag;//lection mark 论文的标准格式
//选择的标志
while(OK) //死循环
{
system("cls"); //清屏
system("color a4"); //界面颜色设定
system("mode con cols=80 lines=400"); //界面大小设定
system("title #Crosslist To Deal With Spar Matrix#");//窗口标题设定
//下面是主界面 printf("t@*************************************************************@
");
putchar('
');
printf("tt %c---Spar-Matrix's--Application-System---%c
",2,2);
putchar('
');
printf("t@*************************************************************@
");
printf("t$*************************************************************$
");
printf("tt %c----------------Functions----------------%c
",2,2);
putchar('
');
printf("tt %c-----------------------------------------%c
",2,2);
printf("tt %c <1>spar--matrix---addition %c
",2,2);
printf("tt %c-----------------------------------------%c
",2,2);
printf("tt %c <2>spar--matrix---multiplication %c
",2,2);
printf("tt %c-----------------------------------------%c
",2,2);
printf("tt %c <3>spar--matrix---transposition %c
",2,2);
printf("tt %c-----------------------------------------%c
",2,2);
printf("tt %c <4>exit-----quit--the--system %c
",2,2);
printf("tt %c-----------------------------------------%c
",2,2);
putchar('
');
printf("tt %c-----------Crosslist Row First-----------%c
",2,2);
printf("t$*************************************************************$
");
printf("tt!!Tips:if you want to break just press Ctrl +C
");
printf("t$*************************************************************$
");
printf("Plea choo a choice :(1--4)
");
fflush(stdin); //清除缓存区
printf("Your choice is:");
scanf("%d",&choice); //输入选择
switch(choice)
{//下面是每项的功能
ca 1: printf("t
"); //加法
putchar('
');
printf("t
");
init_matrix(one); //初始化矩阵
putchar('
');
printf("t
");
creat_matrix(one); //建立矩阵
putchar('
');
printf("Print the first matrix
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(one); //输出矩阵
printf("t
");
init_matrix(two);
putchar('
');
printf("t
");
creat_matrix(two);
putchar('
');
printf("Print the cond matrix
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(two);
/*add the two matrix*/
printf("Add the two matrix
");
init_matrix(three);
putchar('
');
add_matrix(one,two,three); //矩阵相加
printf("The result is below:
");
Sleep(1000); //停顿一秒
print_matrix(three);
system("pau"); //暂停
break;
ca 2: printf("t
"); //乘法
putchar('
');
printf("t
");
init_matrix(one);
putchar('
');
printf("t
");
creat_matrix(one);
putchar('
');
printf("Print the first matr四年级古诗大全
ix
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(one);
printf("t
");
init_matrix(two);
putchar('
');
printf("t
");
creat_matrix(two);
putchar('
');
printf("Print the cond matrix
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(two);
/*multiply the two matrix*/
printf("Multiply the two matrix
");
init_matrix(three);
multi_matrix(one,two,three); //矩阵相乘
printf("The result is below:
");
Sleep(1000);
print_matrix(three);
system("pau");
break;
ca 3: printf("t
"); //转置
printf("
");
printf("t
");
init_matrix(one);
putchar('
');
printf("t
");
creat_matrix(one);
printf("Print the matrix:
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(one);
/*transpa the matrix*/
printf("Transpa the matrix
");
putchar('
');
printf("The result is below:
");
Sleep(1000);
trans_matrix(one); //矩阵转置
system("pau");
break;
ca 4: printf("Are you sure to quit the system?
");//退出系统
fflush(stdin);
scanf("%c",&flag);
if(flag=='y'||flag=='Y'||flag=='
')
{
printf("tt%c-------%c-------%c-------%c-------%c
",2,2,2,2,2);
putchar('
');
printf("tt(^_^)Thanks For Using This!(^_^)
");
putchar('
');
printf("tt%c-------%c-------%c-------%c-------%c
",2,2,2,2,2);
putchar('
');
Sleep(2000);
exit(OK);
}
el
{
printf("………………continue………………
");
Sleep(2000);
}
break;
default:printf("Plea input a valid choice from 1 to 4!
");//选择无效的选择
Sleep(2000);
break;
}//switch
}//while
return OK;
}
以下是稀疏矩阵操作各子函数的定义:
(1)初始化稀疏矩阵:
Int init_matrix(crosslist &one)
{//初始化
_size=0;
_size=0;
_zero_amount=0;
=NULL;
=NULL;
return OK;
}//init_matrix
(2)建立稀疏矩阵:
int creat_matrix(crosslist &one)
{//赋值
int i;//循环中的计数
element news,temp;
/*输入行数和列数以及非零元素的个数*/
printf("Input the row size of the matrix:");
scanf("%d",&_size);
printf("Input the colum size of the matrix:");
scanf("%d",&_size);
printf("Input the non zero amount of the matrix:");
scanf("%d",&_zero_amount);
/*分配内存,第一个元素不用*/
=(element*)malloc(sizeof(element)*(_size+1));
asrcmos放电
t(!=NULL);//asrt have space
=(element*)malloc(sizeof(element)*(_size+1));
asrt(!=NULL);
/*初始化指针为NULL*/
for(i=1;i<=_size;i++)
[i]=NULL;
for(i=1;i<=_size;i++)
[i]=NULL;
printf("/**************************************/
");
/*赋值*/
for(i=1;i<=_zero_amount;i++)
{//插入所有非零元素
/*建立新的结点并且赋值*/
news=(element)malloc(sizeof(node));
asrt(news!=NULL);
do
{//保证行下标有效
printf("Input the script of the row:");
scanf("%d",&news->row);
}while(news->row>_size);
do
{//保证列下标有效
printf("Input the script of the colum:");
scanf("%d",&news->colum);
}while(news->colum>_size);
printf("Input the value of the node:");
scanf("%d",&news->value);
/*把结点插入到尾部*/
/*连接行指针*/
if(![news->row])
{//如果行中没有结点
news->right=NULL;
[news->row]=news;
}
el
{
for(temp=[news->row];temp->right!=NULL;temp=temp->right)
NULL;
news->right=temp->right;
temp->right=news;
}
/*列指针连接*/
if(![news->colum])
{
news->down=NULL;
[news->colum]=news;
}
e去成语接龙
l
{
for(temp=[news->colum];temp->down!=NULL;temp=temp->down)
NULL;
news->down=temp->down;
temp->down=news;
}
printf("/**************************************/
");
}//for
return OK;
}//creat_matrix
(3)输出稀疏矩阵:
int print_matrix(crosslist &one)
{//输出稀疏矩阵one的信息
element temp;
int count;
for(count=1;count<=_size;count++)
{
if(![count])
continue;
el
{
for(temp=[count];temp!=NULL;temp=temp->right)
{
printf("tt%dttt%dttt%d
",temp->row,temp->colum,temp->value);
printf("tt-------------------------------------------------
");
Sleep(500); //停顿半秒
}
}//el
}//for
return OK;
}//print_matrix
(4)矩阵相加:
int add_matrix(crosslist one,crosslist two,crosslist &three)
{//将one和two矩阵相加得到three矩阵
/*断言两个矩阵可以相加*/
asrt(_size==_size&&_size==_size);
int i,j;//循环中的计数
element inrt;//要插入到结点
element pone,ptwo;
element prow;//每行的最后一个结点
element *pcolum;//含有每列最后一个结点的指针数组
/*赋值*/
_size=_size;
_size=_size;
_zero_amount=0;
/*动态分配内存*/
=(element*)malloc(sizeof(element)*(_size+1));
asrt(!=NULL);
=(element*)malloc(sizeof(element)*(_size+1));
asrt(!=NULL);
pcolum=(element*)malloc(sizeof(element)*(_size+1));
asrt(pcolum!=NULL);
/*初始化指针*/
for(i=1;i<=_size;i++)
[i]=NULL;
for(i=1;i<=_size;i++)
[i]=NULL;
for(i=1;i<=_size;i++)
pcolum[i]=NULL;
/*按照行序开始相加*/
for(i=1;i<=_size;i++)
{
pone=[i];
ptwo=[i];
while(pone!=NULL&&ptwo!=NULL)
{//both have nodes in the same row
if(pone->columcolum)
点插入*/
while(pone!=NULL)
{
inrt=(element)malloc(sizeof(node));
asrt(inrt!=NULL);
inrt->row=i;
inrt->colum=pone->colum;
inrt->value=pone->value;
inrt->right=NULL;
pone=pone->right;
_zero_amount++;
/*row link*/
if([i]==NULL)
[i]=prow=inrt;
el
{
prow->right=inrt;
prow=inrt;
}
/*colum link */
if([inrt->colum]==NULL)
[inrt->colum]=pcolum[inrt->colum]=inrt;
el
{
pcolum[inrt->colum]->down=inrt;
pcolum[inrt->colum]=inrt;
}
}//while
/*把two中的其他结点插入*/
while(ptwo!=NULL)
{
inrt=(element)malloc(sizeof(node));
asrt(inrt!=NULL);
inrt->row=i;
inrt->colum=ptwo->colum;
inrt->value=ptwo->value;
inrt->right=NULL;
ptwo=ptwo->right;
_zero_amount++;
/*row link*/
if([i]==NULL)
[i]=prow=inrt;
el
{
prow->right=inrt;
prow=inrt;
}
/*colum link */
if([inrt->colum]==NULL)
[inrt->colum]=pcolum[inrt->colum]=inrt;
el
{
pcolum[inrt->colum]->down=inrt;
pcolum[inrt->colum]=inrt;
}
}//while
}//for
_zero_amount=_zero_amount;
for(j=1;j<=_size;j++)
{//将所有不是NULL的pcolum值的down设定为NULL
if(pcolum[j]!=NULL)
pcolum[j]->down=NULL;
}
/*释放内存*/
free(pcolum);
return OK;
}//add_m朝花夕拾作者简介
atrixx[i].row;
(5)稀疏矩阵的乘法:
int multi_matrix(crosslist one,crosslist two,crosslist &three)
{//one与two相乘得到three
/*断言连个矩阵可以相乘*/
asrt(_size==_size);
int i,j; //循环中的计数
int value; //暂时存储总值
element inrt; //要插入到three的结点
element pone,ptwo; //用来遍历one和two矩阵结点的标志
element prow,pcolum; //标志行和列最后一个结点
/*赋值*/
_size=_size;
_size=_size;
_zero_amount=0;
/*分配内存*/
=(element*)malloc(sizeof(element)*(_size+1));
asrt(!=NULL);
=(element*)malloc(sizeof(element)*(_size+1));
asrt(!=NULL);
/*指针初始化*/
for(i=1;i<=_size;i++)
[i]=NULL;
for(i=1;i<=_size;i++)
[i]=NULL;
/*开始相乘*/
for(i=1;i<=_size;i++)
{//遍历one中每行的结点
for(j=1;j<=_size;j++)
{//遍历two每列的结点
pone=[i];
ptwo=[j];
value=0;
while(pone!=NULL&&ptwo!=NULL)
{//行和列不为空
if(pone->colum==ptwo->row)
{//可以相乘
value+=pone->value*ptwo->value;
pone=pone->right; //指针后移
ptwo=ptwo->down; //指针下移
while(pone!=NULL&&ptwo!=NULL)
{//为了加上所有的值
if(pone->colum==ptwo->row)
{
value+=pone->value*ptwo->value;
pone=pone->right;
ptwo=ptwo->down;
}
el if(pone->colum>ptwo->row)
{
ptwo=ptwo->down;
continue;
}
el
{
pone=pone->right;
continue;
}
}//while
if(value==0)
break;
inrt=(element)malloc(sizeof(node));
asrt(inrt!=NULL);
inrt->row=i;
inrt->colum=j;
inrt->value=value;
inrt->right=NULL;
inrt->down=NULL;
_zero_amount++;
/*插入到three中并且连接行指针*/
if([i]==NULL)
[i]=prow=inrt;
el
{
prow->right=inrt;
prow=inrt;
}
/*列指针连接*/
if([j]==NULL)
[j]=pcolum=inrt;
el
{
pcolum->down=inrt;
pcolum=inrt;
}
}//if (colum==row)
el if(pone->colum>ptwo->row)
{
ptwo=ptwo->down;
continue;
}
el
{
pone=pone->right;
continue;
}
}//while
}//inner for
}//outter for
return OK;
}//multi_matrix
(6)稀疏矩阵的转置:
int trans_matrix(crosslist &one)
{//只需要按照列为主输出就可以
element temp;
for(int count=1;count<=_size;count++)
{//按照列为主输出
if(![count])
continue;
el
{//遍历one的结点
for(temp=[count];temp!=NULL;temp=temp->down)
{//行列相交换
printf("tt%dttt%dttt%d
",temp->colum,temp->row,temp->value);
printf("tt-------------------------------------------------
");
Sleep(1000);
}
}//el
}//for
return OK;
}//trans_matrix
五、 测试分析
系统运行主界面如图7-4所示。
图7-4 稀疏矩阵应用系统主菜单
各子功能测试运行结果如下。
1.稀疏矩阵相加
在主菜单下,用户输入1回车,根据提示输入第一个和第二个矩阵的初始化,运行结果如图7-5所示。然后根据提示按任意键,返回主菜单。注意:当你输入的两个矩阵不满足相加矩阵的性质时候,会出现图7-6的提示,程序异常终止。图7-7 是出现错误的原因。说明你输入的两个矩阵不符合相加的条件。重新开始使用系统。
图7-5 稀疏矩阵相加
图7-6 错误提示
图7-7 错误原因
2.稀疏矩阵相乘
在主菜单下,用户输入.2回车,根据屏幕提示初始化两个矩阵。运行结果如图7-8所示。然后根据提示按任意键,返回主菜单。注意:当你输入的两个矩阵不满足相加矩阵的性质时候,会出现图7-9的提示,并终止。错误原因如图7-10所示。重新开始系统。
图7-8 稀疏矩阵的乘法结果示意图
图7-9 错误提示
图7-10 错误原因
3.稀疏矩阵的转置
在主菜单下,用户输入3回车,根据屏幕提示初始化一个稀疏矩阵,按enter键,运行结果如图7-11所示。
图7-11 稀疏矩阵的转置结果示意图
4.退出
在主菜单下,用户输入4回车,询问是否真的退出系统,如果是,则即显示图7-12所示图示并在停顿后退出系统,否则显示……continue……并且返回主菜单。
图7-12 结束语
六、 源程序清单
/**********************************************
** @copyright rerved by the author
**author: xuxuelong
**data: 20009/12/26
**IDE: visula c++ 6.0
**major: 08-CS-software engineering
**email address: xuxuelong0214@
**attention: if you find a bug in the
**.......... operation ,just email me!
**********************************************/
#include
#include
#include
#include
#define OK 1
#define ERROR 0
typedef struct list
{//node structure
int row;//row subscript
int colum;//colum subscript
int value;//value of the node
struct list *right;//the next element have the same row
struct list *down;//the next element have the same colum
}node,*element;
typedef struct link
{//cross list structure
int row_size;//row size of the matrix
int colum_size;//colum size of the matrix
int non_zero_amount;//the number of non zero element
element浪人情歌简谱
*rhead;//the ba of the row
element *chead;//the ba of the colum
}crosslist;
/********function definition********/
int init_matrix(crosslist &one)
{//initialization
_size=0;
_size=0;
_zero_amount=0;
=NULL;
=NULL;
return OK;
}//init_matrix
int creat_matrix(crosslist &one)
{//assignment
int i;//as count in the loop
element news,temp;
/*input row size ,colum size and non zero amount*/
printf("Input the row size of the matrix:");
scanf("%d",&_size);
printf("Input the colum size of the matrix:");
scanf("%d",&_size);
printf("Input the non zero amount of the matrix:");
scanf("%d",&_zero_amount);
/*allocate memory and the first memory not u*/
=(element*)malloc(sizeof(element)*(_size+1));
asrt(!=NULL);//asrt have space
=(element*)malloc(sizeof(element)*(_size+1));
asrt(!=NULL);
/*t all the pointer to NULL*/
for(i=1;i<=_size;i++)
[i]=NULL;
for(i=1;i<=_size;i++)
[i]=NULL;
printf("/**************************************/
");
/*assignment*/
for(i=1;i<=_zero_amount;i++)
{//inrt all non zero elements
/*creat a new no
de and assign value to it*/
news=(element)malloc(sizeof(node));
asrt(news!=NULL);
do
{//insure the row script is valid
printf("Input the script of the row:");
scanf("%d",&news->row);
}while(news->row>_size);
do
{//insure the colum script is valid
printf("Input the script of the colum:");
scanf("%d",&news->colum);
}while(news->colum>_size);
printf("Input the value of the node:");
scanf("%d",&news->value);
/*inrt the node to the rear of the list*/
/*row link*/
if(![news->row])
{//if there's no node in the row
news->right=NULL;
[news->row]=news;
}
el
{
for(temp=[news->row];temp->right!=NULL;temp=temp->right)
NULL;
news->right=temp->right;
temp->right=news;
}
/*colum link*/
if(![news->colum])
{
news->down=NULL;
[news->colum]=news;
}
el
{
for(temp=[news->colum];temp->down!=NULL;temp=temp->down)
NULL;
news->down=temp->down;
temp->down=news;
}
printf("/**************************************/
");
}//for
return OK;
}//creat_matrix
int print_matrix(crosslist &one)
{//print out the info of matrix one
element temp;
int count;
for(count=1;count<=_size;count++)
{
if(![count])
continue;
el
{
for(temp=[count];temp!=NULL;temp=temp->right)
{
printf("tt%dttt%dttt%d
",temp->row,temp->colum,temp->value);
printf("tt-------------------------------------------------
");
Sleep(500);
}
}//el
}//for
return OK;
}//print_matrix
int add_
t->right=NULL;
ptwo=ptwo->right;
_zero_amount++;
}
el
if(pone->value+ptwo->value!=0)
{//the result of the addition is nonzero
inrt=(element)malloc(sizeof(node));
asrt(inrt!=NULL);
inrt->row=i;
inrt->colum=ptwo->colum;
inrt->value=pone->value+ptwo->value;
inrt->right=NULL;
pone=pone->right;
ptwo=ptwo->right;
_zero_amount++;
}
el
{//the addition is zero
pone=pone->right;
ptwo=ptwo->right;
continue;
}
/*inrt into three and link row*/
if([i]==NULL)
[i]=prow=inrt;
el
{
prow->right=inrt;
prow=inrt;
}
/*colum link*/
if([inrt->colum]==NULL)
[inrt->colum]=pcolum[inrt->colum]=inrt;
el
{
pcolum[inrt->colum]->down=inrt;
pcolum[inrt->colum]=inrt;
}
}//while
/*inrt the other nodes in one*/
while(pone!=NULL)
{
inrt=(element)malloc(sizeof(node));
asrt(inrt!=NULL);
inrt->row=i;
inrt->colum=pone->colum;
inrt->value=pone->value;
inrt->right=NULL;
pone=pone->right;
_zero_amount++;
/*row link*/
if([i]==NULL)
[i]=prow=inrt;
el
{
_size=_size;
_size=_size;
_zero_amount=0;
/*allocate memroy*/
=(element*)malloc(sizeof(element)*(_size+1));
asrt(!=NULL);
=(element*)malloc(英文句
sizeof(element)*(_size+1));
asrt(!=NULL);
/*initialization*/
for(i=1;i<=_size;i++)
[i]=NULL;
for(i=1;i<=_size;i++)
[i]=NULL;
/*start multiplication*/
for(i=1;i<=_size;i++)
{//traver every row of nodes of one
for(j=1;j<=_size;j++)
{//traver every colum of nodes of two
pone=[i];
ptwo=[j]; 高考信息
value=0;
while(pone!=NULL&&ptwo!=NULL)
{//row and colum are not empty
if(pone->colum==ptwo->row)
{//is valid to multiply
value+=pone->value*ptwo->value;
pone=pone->right;//to the next node node in same row
ptwo=ptwo->down;//point to the next node in same colum
while(pone!=NULL&&ptwo!=NULL)
{//for the emtire row and colum
if(pone->colum==ptwo->row)
{
value+=pone->value*ptwo->value;
pone=pone->right;
ptwo=ptwo->down;
}
el if(pone->colum>ptwo->row)
{
ptwo=ptwo->down;
continue;
}
el
{
pone=pone->right;
continue;
}
}//while
if(value==0)
break;
inrt=(element)malloc(sizeof(node));
asrt(inrt!=NULL);
inrt->row=i;
inrt->colum=j;
inrt->value=value;
inrt->right=NULL;
inrt->down=NULL;
_zero_amount++;
/*inrt into c and row link*/
if([i]==NULL)
[i]=prow=inrt;
el
{
prow->right=inrt;
prow=inrt;
}
/*colum link*/
if([j]==NULL)
[j]=pcolum=inrt;
el
{
pcolum->down=inrt;
pcolum=inrt;
}
}//if (colum==row)
el if(pone->colum>ptwo->row)
{
ptwo=ptwo->down;
continue;
}
el
{
pone=pone->right;
continue;
}
}//while
}//inner for
}//outter for
return OK;
}//multi_matrix
int trans_matrix(crosslist &one)
{//just print the matrix in colum order
element temp;
for(int count=1;count<=_size;count++)
{//print in colum order
if(![count])
continue;
el
{//traver the elements in one
for(temp=[count];temp!=NULL;temp=temp->down)
{//the row script equal to the colum script
printf("tt%dttt%dttt%d
",temp->colum,temp->row,temp->value);
printf("tt-------------------------------------------------
");
Sleep(1000);
}
}//el
}//for
re安全生产工作会议
turn OK;
}//trans_matrix
/*****the main function*****/
/*****the main function*****/
int main(void)
{
/*declare three matrix*/
crosslist one,two,three;
int choice;//as a mark of lection
char flag;//lection mark
while(OK)
{
system("cls");
system("color a4");
system("mode con cols=80 lines=400");
system("title #Crosslist To Deal With Spar Matrix#");
printf("t@*************************************************************@
");
putchar('
');
printf("tt %c---Spar-Matrix's--Application-System---%c
",2,2);
putchar('
');
printf("t@*************************************************************@
");
printf("t$*************************************************************$
");
printf("tt %c----------------Functions----------------%c
",2,2);
putchar('
');
printf("tt %c-----------------------------------------%c
",2,2);
printf("tt %c <1>spar--matrix---addition %c
",2,2);
printf("tt %c-----------------------------------------%c
",2,2);
printf("tt %c <2>spar--matrix---multiplication %c
",2,2);
printf("tt %c-----------------------------------------%c
",2,2);
printf("tt %c <3>spar--matrix---transposition %c
",2,2);
printf("tt %c-----------------------------------------%c
",2,2);
printf("tt %c <4>exit-----quit--the--system %c
",2,2);
printf("tt %c-----------------------------------------%c
",2,2);
putchar('
');
printf("tt %c-----------Crosslist Row First-----------%c
",2,2);
printf("t$*************************************
************************$
");
printf("tt!!Tips:if you want to break just press Ctrl +C
");
printf("t$*************************************************************$
");
printf("Plea choo a choice :(1--4)
");
fflush(stdin);
printf("Your choice is:");
scanf("%d",&choice);
switch(choice)
{
ca 1: printf("t
");
putchar('
');
printf("t
");
init_matrix(one);
putchar('
');
printf("t
");
creat_matrix(one);
putchar('
');
printf("Print the first matrix
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(one);
printf("t
");
init_matrix(two);
putchar('
');
printf("t
");
creat_matrix(two);
putchar('
');
printf("Print the cond matrix
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(two);
/*add the two matrix*/
printf("Add the two matrix
");
init_matrix(three);
putchar('
');
add_matrix(one,two,three);
printf("The result is below:
");
Sleep(1000);
print_matrix(three);
system("pau");
break;
ca 2: printf("t
");
putchar('
');
printf("t
");
init_matrix(one);
putchar('
');
printf("t
");
creat_matrix(one);
putchar('
');
printf("Print the first matrix
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(one);
printf("t
");
init_matrix(two);
putchar('
');
printf("t
");
creat_matrix(two);
putchar('
');
printf("Print the cond matrix
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(two);
/*multiply the two matrix*/
printf("Multiply the two matrix
");
init_matrix(three);
multi_matrix(one,two,three);
printf("The result is below:
");
Sleep(1000);
print_matrix(three);
system("pau");
break;
ca 3: printf("t
");
printf("
");
printf("t
");
init_matrix(one);
putchar('
');
printf("t
");
creat_matrix(one);
printf("Print the matrix:
");
printf("ttRowtttColumtttValue
");
printf("tt-------------------------------------------------
");
print_matrix(one);
/*transpa the matrix*/
printf("Transpa the matrix
");
putchar('
');
printf("The result is below:
");
Sleep(1000);
trans_matrix(one);
system("pau");
break;
ca 4: printf("Are you sure to quit the system?
");
fflush(stdin);
scanf("%c",&flag);
if(flag=='y'||flag=='Y'||flag=='
')
{
printf("tt%c-------%c-------%c-------%c-------%c
",2,2,2,2,2);
putchar('
');
printf("tt(^_^)Thanks For Using This!(^_^)
");
putchar('
');
printf("tt%c-------%c-------%c-------%c-------%c
",2,2,2,2,2);
putchar('
');
Sleep(2000);
exit(OK);
}
el
{
printf("………………continue………………
");
Sleep(2000);
}
break;
default:printf("Plea input a valid choice from 1 to 4!
");
Sleep(2000);
break;
}//switch
}//while
return OK;
}
七、 用户手册
1.本程序执行文件为:
2.进入本系统之后,随即显示系统主菜单界面。用户可在该界面下输入各子菜单前对应的数字并按回车,执行相应子菜单命令。
八、总结与提示
本系统除了十字链表的加法参考了书本之外其他都是是本人原创,若有雷同,纯属巧合。版权所有,任何转载须注明出处及作者信息。
提示:由于源程序编写过程中中英文转换麻烦,所以本版本是英文版本,请谅解!
总结:由于本人能力有限,在保证功能的基础上,算法可能不是最优的,如果有什么好的算法,Email 我!