首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

用C解线性方程组.请.还没有完工,呵 呵

2012-02-26 
用C解线性方程组.请高手指点.还没有完工,呵 呵#includestdio.h#includeconio.h#includemalloc.h#def

用C解线性方程组.请高手指点.还没有完工,呵 呵
#include   <stdio.h>
#include   <conio.h>
#include   <malloc.h>
#define   LEN   sizeof(struct   matrix)
//链表结构
struct   matrix
{
float   arr;
struct   matrix   *   next;
}

//主函数
main()
{
//函数声明
struct   matrix   *   matrix_det(int   row,   int   col);//输入行列式的系数
struct   matrix   *   print(struct   matrix   *   head,   int   rows,   int   cols);//输出行列式的系数
struct   matrix   *   matrix_sort(struct   matrix   *   head,   int   rows,   int   cols);//排序行列式
struct   matrix   *   matrix_ones(int   col);//建立一个空的链表
int   rows,cols;     //定义行和列的变量
struct   matrix   *   tpr;
printf( "请输入行列式的系数:\n ");
printf( "请输入行数: ");scanf( "%d ",&rows);
printf( "请输入列数: ");scanf( "%d ",&cols);
tpr   =   matrix_det(rows,cols);
print(tpr,rows,cols);
save_det(tpr,rows,cols);
tpr   =   matrix_sort(tpr,rows,cols);
printf( "\n排序后: ");
print(tpr,rows,cols);
save_det(tpr,rows,cols);
getch();
}
//用链表动态输入系数行列式
struct   matrix   *   matrix_det(int   row,   int   col)
{
int   i,j,ji;
struct   matrix   *   head,*prf,*prb;
head   =   prf   =   prb   =   (struct   matrix   *)malloc(LEN);
ji=row*col;
for(i=0;i <row;i++)
{
    printf( "第%d行: ",i+1);
    for(j=0;j <col;j++)
    {
      scanf( "%f ",&prf-> arr);
      prf   =   (struct   matrix   *)malloc(LEN);
      prb-> next   =   prf;
      prb   =   prf;
    }
    prb-> next   =   NULL;
}
return   (head);
}
//创建一个空链表
struct   matrix   *   matrix_ones(int   col)
{
int   i;
struct   matrix   *head,   *prf,   *prb;
head   =   prf   =   prb   =   (struct   matrix   *)malloc(LEN);
for(i=0;i <col;i++)
{
    prf-> arr   =   1;
    prf   =   (struct   matrix   *)malloc(LEN);
    prb-> next   =   prf;
    prb   =   prf;
}
prb-> next   =   NULL;
return   (head);
}
//输出系数
struct   matrix   *   print(struct   matrix   *   head,   int   row,   int   col)
{
int   i,j;
struct   matrix   *   tpr;
tpr   =   head;
printf( "\n行列式:\n------\n ");
for(i=0;i <row;i++)
{
    for(j=0;j <col;j++)
    {
      printf( "%12.2f\t ",tpr-> arr);
      tpr   =   tpr-> next;
    }
    printf( "\n ");         //输出换行
}
printf( "------\n ");
return   0;
}
//对行列式进行排序:冒泡法
struct   matrix   *   matrix_sort(struct   matrix   *   head,   int   row,   int   col)
{
int   ir,   ic,   it,i;
float   tf;
struct   matrix   *   pf,   *   pb,*pf1,*pb1;


pf   =   pb   =     head;
for(ic=0;ic <col;ic++)     //移动使指针指向下一行第一个数
{
    pf   =   pf-> next;
}
pf1   =   pf;
pb1   =   pb;
for(ir=0;ir <row;ir++)     //交换两行的系数
{
    pf1   =   pf;
    pb1   =   pb;
    for(it=1;it <row-ir;it++)
    {
      if(pb1-> arr   <   pf1-> arr)
      {
        for(ic=0;ic <col;ic++)
        {
          tf   =   pb1-> arr;
          pb1-> arr   =   pf1-> arr;
          pf1-> arr   =   tf;
          pf1   =   pf1-> next;
          pb1   =   pb1-> next;
        }
      }
      else
      {
      for(i=0;i <col;i++)
      {
        pf1   =   pf1-> next;
        pb1   =   pb1-> next;
      }
      }
    }
}
return   head;
}

//保存矩阵到文件
save_det(struct   matrix   *   head,int   row,   int   col)
{
FILE   *   fp;
int   hang,lie;
struct   matrix   *p;
p   =   head;
fp=fopen( "matrix_det.txt ", "a ");
for(hang=0;   hang <row;hang++)
{
    for(lie=0;   lie <col;   lie++)
    {
      fprintf(fp, "%12.2f ",p-> arr);
      p   =   p-> next;
    }
    fprintf(fp, "\n ");
}
fputs( "------END------\n ",fp);
fclose(fp);
}


用VC6通过了编译.


[解决办法]
函数声明为什么要放到主函数里呢?

热点排行