Course Design: sparse matrix generator (add, subtract, multiply, and inverse) written in C Language)

Source: Internet
Author: User

# Include <stdio. h> # include <stdlib. h> int system (const char * string); # define maxsize 2000 typedef 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 character = '; int I; for (I = 0; I <80; I ++) printf ("% c", callback);} void qiuni (matrix_f * A, matrix_f ** C) {/* parameter Declaration */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; /* variable operation */B = Init_f ();/* Create matrix AI * // * This part has been completed. The following part cannot be modified */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 ++;}/* transformation matrix 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-> l En; I ++) {if (B-> data [I]. row = row & B-> data [I]. col = row) sum = sum * (B-> data [I]. data) ;}} if (sum = 0) printf ("The Matrix you entered has no Inverse Matrix! \ 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. create A matrix A = "); printf (" = 2. create a matrix B = "); printf (" = 3. create a matrix C [dedicated to inverse matrix] = "); printf (" = 4. matrix A + matrix B = "); printf (" = 5. matrix A-matrix B = "); printf (" = 6. matrix A * matrix B = "); printf (" = 7. calculate the inverse matrix = ") of matrix A; printf (" = 8. exit = "); print_menu (); printf (" select the operation: ");} 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 complete; int g = 0; FILE * in2; char ch2; int p = 0; int z = 0; print_menu (); printf ("= 1. manually create = "); printf (" = 2. load = "); print_menu (); printf (" select the matrix creator Formula: "); scanf (" % d ", & j); switch (j) {case 1: {system (" cls "); printf ("================================== enter the number of columns in the matrix and the number of non-zero elements ==== =================================== "); scanf ("% d", & (* A)-> m, & (* A)-> n, & (* A)-> len ); for (I = 0; I <(* A)-> len; I ++) {printf ("Enter the row of the % d Non-zero element :", I + 1); scanf ("% d", & n1); (* A)-> data [I]. row = (n1-1); printf ("Enter the column for The % d Non-zero element:", I + 1); scanf ("% d", & n2 ); (* A)-> data [I]. col = (n2-1); printf ("Enter the value of the % d Non-zero element:", I + 1); scanf ("% d", & (*) -> data [I ]. Data); printf ("\ n ");} printf ("================================================ the matrix is created successfully. ==================================== "); break;} case 2: {system ("cls "); printf ("============================== enter the file name to open = =============================== "); printf (""); scanf ("% s", & getname); if (get = fopen (getname, "r") = NULL) {printf ("An error occurred while opening the file! Check whether the file exists. \ N "); system (" pause "); exit (0) ;}elseprintf (" Open complete! \ N "); printf (" loading matrix from file... \ n "); in1 = fopen (getname," r "); while (! Feof (in1) {response = fgetc (in1); if (response = '\ 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 ("Matrix loaded successfully! \ N "); printf (" \ n: % d \ n ", g); printf (" Number of text columns: % d \ n ", p ); printf ("non-zero element Information: \ 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 ("row: % d", (* A)-> data [m]. row + 1); printf ("column: % d", (* A)-> data [m]. col + 1); printf ("value: % 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 ++ ;}} (*) -> m = g; (* A)-> n = p; (* A)-> len = z ;}} void print (matrix *) {FILE * put; char putname [20]; int I = 0; int row; int col; int j; char Limit = '0'; char ch2 = ''; Printf (" is the result displayed on the screen? (It is recommended that the large matrix be stored in the file .) \ T is [0]/No [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 ("enter the name of the file to be saved:"); scanf ("% s", & putname);/* Write File Section */if (put = fopen (putname, "w") = NULL) {printf ("cannot be opened! "); Exit (0);} else printf (" \ a created or opened the file % s successfully! \ 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 (latency, put); fputc (ch2, put) ;}} fputc (10, put) ;}} fclose (put); printf ("saved! \ N "); break ;}} int value (matrix * a, int I, int j) // retrieves a row or column when the matrix is multiplied. {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 ("the two matrices you entered do not meet the multiplication condition! "); 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 ("the two matrices you entered do not meet the computing conditions! \ 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 success; int g = 0; FILE * in2; char Ch2; int p = 0; int z = 0; print_menu (); printf ("= 1. manually create = "); printf (" = 2. load = "); print_menu (); printf (" select the matrix creation method: "); scanf (" % d ", & j); switch (j) {case 1: {system ("cls "); printf ("================================== enter the number of columns in the matrix and the number of non-zero elements ==== =================================== "); scanf ("% d", & (* A)-> m, & (* A)-> n, & (* A)-> len ); for (I = 0; I <(* A)-> len; I ++) {printf ("Enter the row of the % d Non-zero element :", I + 1); scanf ("% d", & n3); (* A)-> data [I]. row = (n3-1); printf ("Enter the column for The % d Non-zero element: ", I + 1); scanf (" % d ", & n4); (* A)-> data [I]. col = (n4-1); printf ("Enter the value of the % d Non-zero element:", I + 1); scanf ("% f", & (*) -> data [I]. data); printf ("\ n ");} printf ("================================================ the matrix is created successfully. ==================================== "); break;} case 2: {system ("cls "); printf ("============================== enter the file name to open = =============================== "); printf (""); scanf ("% s", & getname); if (get = fopen (getname, "r") = NULL) {printf ("hit An error occurred while opening the file! Check whether the file exists. \ N "); system (" pause "); exit (0) ;}elseprintf (" Open complete! \ N "); printf (" loading matrix from file... \ n "); in1 = fopen (getname," r "); while (! Feof (in1) {response = fgetc (in1); if (response = '\ 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 ("Matrix loaded successfully! \ N "); printf (" \ n: % d \ n ", g); printf (" Number of text columns: % d \ n ", p ); printf ("non-zero element Information: \ 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 ("row: % d", (* A)-> data [m]. row + 1); printf ("column: % d", (* A)-> data [m]. col + 1); printf ("value: % 0.1f \ n", (* A)-> data [m]. data); // checks whether the load is successful. M ++; col ++; z ++; if (col = p) {col = 0; row ++ ;}} else if (n1 = 0) {col ++; if (col = p) {col = 0; row ++ ;}}( * A)-> m = g; (*) -> n = p; (* A)-> len = z ;}} void print_f (matrix_f * A) {FILE * put; char putname [20]; int I = 0; int j; char forward = '0'; char ch2 = ''; int t_row = 0, t_col = 0; printf ("do you want to display the result on the screen? (It is recommended that the large matrix be stored in the file .) \ T is [0]/No [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 ("Enter the file name to save:"); scanf ("% s", & putname ); /* Write File part */if (put = fopen (putname, "w") = NULL) {printf ("cannot be opened! "); Exit (0);} else printf (" \ a created or opened the file % s successfully! \ 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 ("saved! \ 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 ("Create A matrix... \ n "); Creat (& A); system (" pause "); system (" cls "); print_menu2 (); break;} case 2: {system ("cls"); printf ("create matrix B... \ n "); Creat (& B); system (" pause "); system (" cls "); print_menu2 (); break;} Case 3: {system ("cls"); printf ("create matrix S... \ n "); Creat_f (& S); system (" pause "); system (" cls "); print_menu2 (); break;} case 4: {printf ("the addition operation is being executed... \ n "); add (A, B, & C); print (C); system (" pause "); system (" cls "); print_menu2 (); break;} case 5: {printf ("subtraction... \ n "); sub (A, B, & C); print (C); system (" pause "); system (" cls "); print_menu2 (); break;} case 6: {printf ("Multiplication... \ n "); arr (A, B, & C); print (C); system (" pause "); system (" cls "); pr Int_menu2 (); break;} case 7: {if (A-> m! = A-> n) printf ("the number of matrix rows is not equal to the number of columns. No Inverse Matrix exists! \ N "); else {printf (" an inverse operation is being performed... \ n "); qiuni (S, & T); print_f (T); system (" pause "); system (" cls "); print_menu2 ();} break ;} case 8: {exit (0); break ;}// switch ends scanf ("% d", & num) ;}// while ends scanf ("% d ", & num );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.