稀疏矩阵的应用(十字链表)

稀疏矩阵的应用(十字链表)
一、  设计要求
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 * one.rhead和*lum)和总共的行数(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 base of the row                                        //行指针基指
element *chead;//the base of the colum                                    //列指针基指
}crosslist;
2.系统主要子程序详细设计
(1)主程序模块设计及用户工作区模块设计
主函数。设定操作区用户界面的颜,大小和窗口的标题,调用工作区模块函数。
int main(void)
{
/*declare three matrix*/
crosslist one,two,three;                                            //声明三个十字链表链表
int choice;//as a mark of selection                            //选择的
标志
char flag;//selection mark                                        //选择的标志
while(OK)                                                                  //死循环
{       
system("cls");                                                  //清屏
system("color a4");                                        //界面颜设定
system("mode con cols=80 lines=400");      //界面大小设定
system("title #Crosslist To Deal With Sparse Matrix#");//窗口标题设定
//下面是主界面          printf("\t@*************************************************************@\n");
putchar('\n');             
printf("\t\t  %c---Sparse-Matrix's--Application-System---%c\n",2,2);
putchar('\n');
printf("\t@*************************************************************@\n");   
printf("\t$*************************************************************$\n");
printf("\t\t  %c----------------Functions----------------%c\n",2,2);
putchar('\n');   
printf("\t\t  %c-----------------------------------------%c\n",2,2);
printf("\t\t  %c    <1>sparse--matrix---addition        %c\n",2,2);     
printf("\t\t  %c-----------------------------------------%c\n",2,2);
printf("\t\t  %c    <2>sparse--matrix---multiplication  %c\n",2,2);       
printf("\t\t  %c-----------------------------------------%c\n",2,2);
printf("\t\t  %c    <3>sparse--matrix---transposition    %c\n",2,2);       
printf("\t\t  %c-----------------------------------------%c\n",2,2);
printf("\t\t  %c    <4>exit-----quit--the--system        %c\n",2,2);
printf("\t\t  %c-----------------------------------------%c\n",2,2);
putchar('\n');
printf("\t\t  %c-----------Crosslist Row First-----------%c\n",2,2); 
printf("\t$*************************************************************$\n");
printf("\t\t!!Tips:if you want to break just press Ctrl +C\n");               
printf("\t$*************************************************************$\n");
printf("Please choose a choice :(1--4)\n");
fflush(stdin);                                                    //清除缓存区
printf("Your choice is:");
scanf("%d",&choice);                                    //输入选择
switch(choice)
{//下面是每项的功能
case 1: printf("\t<addition>\n");                            //加法
putchar('\n');
printf("\t<Initialization>\n");
init_matrix(one);                          //初始化矩阵
putchar('\n');
printf("\t<Creat the first matrix>\n");
creat_matrix(one);                        //建立矩阵
putchar('\n');
printf("Print the first matrix\n");
printf("\t\tRow\t\t\tColum\t\t\tValue\n");
printf("\t\t-------------------------------------------------\n");
print_matrix(one);                        //输出矩阵       
printf("\t<Initialization>\n");
init_matrix(two);
putchar('\n');
printf("\t<Creat the second matrix>\n");
creat_matrix(two);
putchar('\n');
printf("Print the second matrix\n");
printf("\t\tRow\t\t\tColum\t\t\tValue\n");
printf("\t\t-------------------------------------------------\n");
print_matrix(two);                                         
李老汉的性 福生活全部/*add the two matrix*/
printf("Add the two matrix\n");
init_matrix(three);
putchar('\n');
add_matrix(one,two,three);                            //矩阵相加
printf("The result is below:\n");
Sleep(1000);                                                      //停顿一秒
print_matrix(three);
system("pause");                                            //暂停
break;
case 2: printf("\t<multiplication>\n");                                    //乘法
putchar('\n');
printf("\t<Initialization>\n");
init_matrix(one);
putchar('\n');
printf("\t<Creat the first matrix>\n");
creat_matrix(one);
putchar('\n');
printf("Print the first matrix\n");
printf("\t\tRow\t\t\tColum\t\t\tValue\n");
printf("\t\t-------------------------------------------------\n");
print_matrix(one);
printf("\t<Initialization>\n");
init_matrix(two);
putchar('\n');
printf("\t<Creat the second matrix>\n");
creat_matrix(two);
putchar('\n');
printf("Print the second matrix\n");
printf("\t\tRow\t\t\tColum\t\t\tValue\n");
printf("\t\t-------------------------------------------------\n");
print_matrix(two);
/
*multiply the two matrix*/
printf("Multiply the two matrix\n");
init_matrix(three);
三国世纪multi_matrix(one,two,three);                                  //矩阵相乘
printf("The result is below:\n");
Sleep(1000);
print_matrix(three);
system("pause");
break;
case 3: printf("\t<transposation>\n");                                              //转置
printf("\n");
printf("\t<Initialization>\n");
init_matrix(one);
putchar('\n');
printf("\t<Creat the matrix>\n");中国领土争端
creat_matrix(one);
printf("Print the matrix:\n");
printf("\t\tRow\t\t\tColum\t\t\tValue\n");
printf("\t\t-------------------------------------------------\n");
print_matrix(one);
/*transpase the matrix*/
printf("Transpase the matrix\n");     
putchar('\n');
printf("The result is below:\n");
Sleep(1000);
trans_matrix(one);                                                  //矩阵转置
system("pause");
break;
case 4:      printf("Are you sure to quit the system<Y/N>?\n");//退出系统
江西大宇学院
fflush(stdin);
scanf("%c",&flag);
if(flag=='y'||flag=='Y'||flag=='\n')
{
printf("\t\t%c-------%c-------%c-------%c-------%c\n",2,2,2,2,2);
putchar('\n');
printf("\t\t(^_^)Thanks  For  Using  This!(^_^)\n");     
putchar('\n');
printf("\t\t%c-------%c-------%c-------%c-------%c\n",2,2,2,2,2);
putchar('\n');
Sleep(2000);
exit(OK);
}
else

本文发布于:2024-09-21 03:11:07,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/53878.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:矩阵   系统   功能   设计   函数
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议