課程設計:用C語言編寫的疏鬆陣列運算器(加、減、乘、求逆)

來源:互聯網
上載者:User

#include<stdio.h>#include<stdlib.h>int system(const char *string);#define maxsize 2000typedef struct{int row;int col;int data;}triple;typedef struct{triple data[maxsize];int m,n,len;}matrix;typedef struct{int row;int col;float data;}triple_f;typedef struct{triple_f data[maxsize];int m,n,len;}matrix_f;matrix_f *Init_f(){int i=0;matrix_f *A;A=(matrix_f*)malloc(sizeof(matrix_f));A->len=0;A->m=0;A->n=0;for(i=0;i<maxsize;i++){A->data[i].col=0;A->data[i].row=0;A->data[i].data=0;}return A;}matrix *Init(){int j=0;matrix *A;A=(matrix*)malloc(sizeof(matrix));A->len=0;A->m=0;A->n=0;for(j=0;j<maxsize;j++){A->data[j].col=0;A->data[j].row=0;A->data[j].data=0;}return A;}void print_menu(){char ch1='=';int i;for(i=0;i<80;i++)printf("%c",ch1);}void qiuni(matrix_f *A,matrix_f **C){/*參數聲明*/int i=0,j=0,m=0,row=0,col=0,r1,k,tz=0,tem_row,tem_col,l;float n1,n2,t,x,n3,sum;matrix_f *B;/*變數運算*/B=Init_f();/*建立矩陣AI*//*這部分已完成,下面部分禁止修改*/m=0;j=0;B->m=A->m; B->n=2*(A->n); B->len=(B->m)*(B->n);tem_row=0; tem_col=0;for(i=0;i<B->len;i++){B->data[i].data=0;B->data[i].col=tem_col;B->data[i].row=tem_row;tem_col++;if(tem_col==B->n){tem_col=0;tem_row++;}}for(i=0;i<A->len;i++)for(j=0;j<B->len;j++)if(B->data[j].row==A->data[i].row&&B->data[j].col==A->data[i].col)B->data[j].data=A->data[i].data;j=0;for(i=0;i<A->m;i++){B->data[j*(B->n)+i+B->m].data=1;j++;}/*變換矩陣AI*/row=0;col=0;i=0;j=0;n1=1.0;n2=1.0;r1=0;k=0;t=1.0;  l=0;x=0;for(col=0;col<B->m;col++) {for(row=0;row<B->m;row++)         {if(row!=col)            {for(i=0;i<B->len;i++)if(B->data[i].row==row&&B->data[i].col==col){n1=B->data[i].data;break;}for(j=0;j<B->len;j++)if(B->data[j].row==col&&B->data[j].col==col){n2=B->data[j].data;break;}t=n1/n2; for(r1=0;r1<(2*B->m);r1++)                { for(k=0;k<B->len;k++)if(B->data[k].row==col&&B->data[k].col==r1){n3=B->data[k].data;break;} x=n3*t; for(l=0;l<B->len;l++)if(B->data[l].row==row&&B->data[l].col==r1){B->data[l].data=B->data[l].data-x;break;}                }             }         } } row=0;j=0;n1=0;col=0;i=0;for(row=0;row<B->m;row++){for(j=0;j<B->len;j++)if(B->data[j].row==row&&B->data[j].col==row){n1=B->data[j].data;break;}for(col=0;col<B->n;col++)for(i=0;i<B->len;i++)if(B->data[i].row==row&&B->data[i].col==col){B->data[i].data=(B->data[i].data)/n1;break;}}row=0;col=0;sum=1;tem_row=0;tem_col=0;for(row=0;row<B->m;row++){for(i=0;i<B->len;i++){if(B->data[i].row==row&&B->data[i].col==row)sum=sum*(B->data[i].data);}}if(sum==0)printf("您所輸入的矩陣沒有逆矩陣!\n");else{(*C)->len=B->len/2;(*C)->m=B->m;(*C)->n=B->m;for(i=0;i<(*C)->len;i++){(*C)->data[i].data=B->data[i+(tem_row+1)*B->m].data;(*C)->data[i].row=B->data[i+(tem_row+1)*B->m].row;(*C)->data[i].col=B->data[i+(tem_row+1)*B->m].col;tem_col++;if(tem_col==(*C)->m){tem_col=0;tem_row++;}}}}void print_menu2(){print_menu();printf("=                            1.建立矩陣A                                       =");printf("=                            2.建立矩陣B                                       =");printf("=                            3.建立矩陣C【求逆矩陣專用】                       =");printf("=                            4.矩陣A + 矩陣B                                   =");printf("=                            5.矩陣A - 矩陣B                                   =");printf("=                            6.矩陣A * 矩陣B                                   =");printf("=                            7.求矩陣A的逆矩陣                                 =");printf("=                            8.退出                                            =");print_menu();printf("請選擇要進行的操作:");}void Creat(matrix **A){int i=0;int n1,n2;int j=0;FILE *get;char getname[20];int m=0;int row=0;int col=0;FILE *in1;char ch1;int g=0;FILE *in2;char ch2;int p=0;int z=0;print_menu();printf("=                            1.手動建立                                        =");printf("=                            2.從檔案載入                                      =");print_menu();printf("請選擇矩陣建立方式:");scanf("%d",&j);switch(j){case 1:{system("cls");printf("=========================請輸入矩陣的行列數及非零元個數=========================");scanf("%d%d%d",&(*A)->m,&(*A)->n,&(*A)->len);for(i=0;i<(*A)->len;i++){printf("請輸入第 %d 個非零元素的行: ",i+1);scanf("%d",&n1);(*A)->data[i].row=(n1-1);printf("請輸入第 %d 個非零元素的列: ",i+1);scanf("%d",&n2);(*A)->data[i].col=(n2-1);printf("請輸入第 %d 個非零元素的值: ",i+1);scanf("%d",&(*A)->data[i].data);printf("\n");}printf("==================================矩陣建立完成==================================");break;}case 2:{system("cls");printf("==============================請輸入要開啟的檔案名稱==============================");printf("                                ");scanf("%s",&getname);if((get=fopen(getname,"r"))==NULL){printf("開啟檔案出錯!請檢查檔案是否存在。\n");  system("pause");exit(0);     }     elseprintf("開啟完成!\n");printf("正在從檔案中載入矩陣...\n");in1=fopen(getname,"r");while(!feof(in1)){ch1=fgetc(in1);if(ch1=='\n')g++;}in2=fopen(getname,"r");while(!feof(in2)){ch2=fgetc(in2);if(ch2==' ')p++;else if(ch2=='\n')break;}p=p+1; g=g+1;printf("矩陣載入成功!\n");printf("\n文本行數為:%d\n",g);printf("文本列數為:%d\n",p);printf("非零元素資訊如下:\n");while(!feof(get)){fscanf(get,"%d",&n1);if(n1!=0){(*A)->data[m].data=n1;(*A)->data[m].row=row;(*A)->data[m].col=col;printf("行:%d    ",(*A)->data[m].row+1);printf("列:%d    ",(*A)->data[m].col+1);printf("值:%d    \n",(*A)->data[m].data);m++;col++;z++;if(col==p){col=0; row++;}}else if(n1==0){col++;if(col==p){col=0;row++;}}}(*A)->m=g;(*A)->n=p;(*A)->len=z;}}}void print(matrix *A){FILE *put;char putname[20];int i=0;int row;int col;int j;char ch1='0';char ch2=' ';printf("是否將結果顯示在螢幕上?(建議大矩陣儲存在檔案中。)\t是[0]/否[1]\n");scanf("%d",&j);switch(j){case 0:{system("cls");for(row=0;row<A->m;row++){for(col=0;col<(A->n);col++){if(A->data[i].col==col&&A->data[i].row==row){printf("%d ",A->data[i].data);i++;}else  printf("0 ");}printf("\n");}break;}case 1:{system("cls");printf("請輸入要儲存的檔案名稱:");scanf("%s",&putname);/*寫入檔案部分*/if((put=fopen(putname,"w"))==NULL){printf("無法開啟!");exit(0);}else printf("\a建立或開啟檔案 %s 成功!\n",putname);{for(row=0;row<A->m;row++){for(col=0;col<A->n;col++){if(A->data[i].col==col&&A->data[i].row==row){fprintf(put,"%d",A->data[i].data);fputc(ch2,put);i++;}else  {fputc(ch1,put);fputc(ch2,put);}}fputc(10,put);}}fclose(put);printf("已儲存!\n");break;}}}int value(matrix *a,int i,int j)  //矩陣相乘時 取出一行或一列{int k=0;while(k<(a->len)&&(a->data[k].row!=i||a->data[k].col!=j))k++;if(k<a->len)return a->data[k].data;elsereturn 0;}void arr(matrix *a,matrix *b,matrix **c){int i=0,j=0,k=0,p=0;int tem;if(a->m!=b->n){printf("您輸入的兩個矩陣不滿足相乘條件!");system("pause");exit(0);}for(i=0;i<a->n;i++){for(j=0;j<b->m;j++){tem=0;for(k=0;k<a->m;k++){tem=tem+value(a,i,k)*value(b,k,j);}if(tem!=0){(*c)->data[p].row=i;(*c)->data[p].col=j;(*c)->data[p].data=tem;p++;}}(*c)->n=a->n;(*c)->m=a->m;(*c)->len=p;}}void add(matrix *a,matrix *b,matrix **c){if(a->m==b->m&&a->n==b->n){int i=0,j=0,k=0;(*c)->m=a->m;(*c)->n=a->n;while(i<a->len||j<b->len){if(i==a->len&&j<b->len){(*c)->data[k].col=b->data[j].col;(*c)->data[k].row=b->data[j].row;(*c)->data[k++].data=b->data[j].data;(*c)->len++;j++;}else if(i<a->len&&j==b->len){(*c)->data[k].col=a->data[i].col;(*c)->data[k].row=a->data[i].row;(*c)->data[k++].data=a->data[i].data;(*c)->len++;i++;}else{if(a->data[i].row>b->data[j].row){(*c)->data[k].col=b->data[j].col;(*c)->data[k].row=b->data[j].row;(*c)->data[k++].data=b->data[j].data;(*c)->len++;j++;}else if(a->data[i].row<b->data[j].row){(*c)->data[k].col=a->data[i].col;(*c)->data[k].row=a->data[i].row;(*c)->data[k++].data=a->data[i].data;(*c)->len++;i++;}else{if(a->data[i].col==b->data[j].col){if(a->data[i].data+b->data[j].data!=0){(*c)->data[k].col=a->data[i].col;(*c)->data[k].row=a->data[i].row;(*c)->data[k++].data=a->data[i].data+b->data[j].data;(*c)->len++;}i++;j++;}else if(a->data[i].col>b->data[j].col){(*c)->data[k].col=b->data[j].col;(*c)->data[k].row=b->data[j].row;(*c)->data[k++].data=b->data[j].data;(*c)->len++;j++;}else if(a->data[i].col<b->data[j].col){(*c)->data[k].col=a->data[i].col;(*c)->data[k].row=a->data[i].row;(*c)->data[k++].data=a->data[i].data;(*c)->len++;i++;}}}}}else {printf("您輸入的兩個矩陣不滿足運算條件!\n");system("pause");}}void sub(matrix *A,matrix *B,matrix **C){int k=0;for(k=0;k<B->len;k++) B->data[k].data=-B->data[k].data;if(A->m==B->m&&A->n==B->n)  add(A,B,C);else {for(k=0;k<B->len;k++)B->data[k].data=-B->data[k].data;system("pause");}}void Creat_f(matrix_f **A){int i=0;float n1;int n3,n4;int j=0;FILE *get;char getname[20];int m=0;int row=0;int col=0;FILE *in1;char ch1;int g=0;FILE *in2;char ch2;int p=0;int z=0;print_menu();printf("=                            1.手動建立                                        =");printf("=                            2.從檔案載入                                      =");print_menu();printf("請選擇矩陣建立方式:");scanf("%d",&j);switch(j){case 1:{system("cls");printf("=========================請輸入矩陣的行列數及非零元個數=========================");scanf("%d%d%d",&(*A)->m,&(*A)->n,&(*A)->len);for(i=0;i<(*A)->len;i++){printf("請輸入第 %d 個非零元素的行: ",i+1);scanf("%d",&n3);(*A)->data[i].row=(n3-1);printf("請輸入第 %d 個非零元素的列: ",i+1);scanf("%d",&n4);(*A)->data[i].col=(n4-1);printf("請輸入第 %d 個非零元素的值: ",i+1);scanf("%f",&(*A)->data[i].data);printf("\n");}printf("==================================矩陣建立完成==================================");break;}case 2:{system("cls");printf("==============================請輸入要開啟的檔案名稱==============================");printf("                                ");scanf("%s",&getname);if((get=fopen(getname,"r"))==NULL){printf("開啟檔案出錯!請檢查檔案是否存在。\n");system("pause");exit(0);     }     elseprintf("開啟完成!\n");printf("正在從檔案中載入矩陣...\n");in1=fopen(getname,"r");while(!feof(in1)){ch1=fgetc(in1);if(ch1=='\n')g++;}in2=fopen(getname,"r");while(!feof(in2)){ch2=fgetc(in2);if(ch2==' ')p++;else if(ch2=='\n')break;}p=p+1; g=g+1;printf("矩陣載入成功!\n");printf("\n文本行數為:%d\n",g);printf("文本列數為:%d\n",p);printf("非零元素資訊如下:\n");while(!feof(get)){fscanf(get,"%f",&n1);if(n1!=0){(*A)->data[m].data=n1;(*A)->data[m].row=row;(*A)->data[m].col=col;printf("行:%d    ",(*A)->data[m].row+1);printf("列:%d    ",(*A)->data[m].col+1);printf("值:%0.1f    \n",(*A)->data[m].data);  //檢測載入是否成功。m++;col++;z++;if(col==p){col=0; row++;}}else if(n1==0){col++;if(col==p){col=0;row++;}}}(*A)->m=g;(*A)->n=p;(*A)->len=z;}}}void print_f(matrix_f *A){FILE *put;char putname[20];int i=0;int j;char ch1='0';char ch2=' ';int t_row=0,t_col=0;printf("是否將結果顯示在螢幕上?(建議大矩陣儲存在檔案中。)\t是[0]/否[1]\n");scanf("%d",&j);switch(j){case 0:{system("cls");for(i=0;i<A->len;i++){printf("%0.1f   ",A->data[i].data);t_col++;if(t_col==A->n){printf("\n");t_col=0;}}break;}case 1:{system("cls");printf("請輸入要儲存的檔案名稱:");scanf("%s",&putname);/*寫入檔案部分*/if((put=fopen(putname,"w"))==NULL){printf("無法開啟!");exit(0);}else printf("\a建立或開啟檔案 %s 成功!\n",putname);{for(i=0;i<A->len;i++){fprintf(put,"%0.1f",A->data[i].data);fputc(ch2,put);fputc(ch2,put);fputc(ch2,put);t_col++;if(t_col==A->n){fputc(10,put);t_col=0;}}}fclose(put);printf("已儲存!\n");break;}}}void main(){int num;matrix *A;matrix *B;matrix *C;matrix_f *S;matrix_f *T;A=Init();B=Init();C=Init();S=Init_f();T=Init_f();print_menu2();scanf("%d",&num);while(num<=8){switch(num){case 1:{system("cls");printf("建立矩陣A...\n");Creat(&A);system("pause");system("cls");print_menu2();break;}case 2:{system("cls");printf("建立矩陣B...\n");Creat(&B);system("pause");system("cls");print_menu2();break;}case 3:{system("cls");printf("建立矩陣S...\n");Creat_f(&S);system("pause");system("cls");print_menu2();break;}case 4:{printf("正在執行加法運算...\n");add(A,B,&C);print(C);system("pause");system("cls");print_menu2();break;}case 5:{printf("正在執行減法運算...\n");sub(A,B,&C);print(C);system("pause");system("cls");print_menu2();break;}case 6:{printf("正在執行乘法運算...\n");arr(A,B,&C);print(C);system("pause");system("cls");print_menu2();break;}case 7:{if(A->m!=A->n)printf("矩陣行數不等於列數,沒有逆矩陣!\n");else{printf("正在執行求逆運算...\n");qiuni(S,&T);print_f(T);system("pause");system("cls");print_menu2();}break;}case 8:{exit( 0);break;}}//switch結束scanf("%d",&num);}//while結束scanf("%d",&num);}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.