📄 code_ez.c
字号:
/* code_ez.c (c) DJCM 96 01 17 from code4r.c makes sparse alist matrix of this form \...\ from a sparse alist matrix of the form ...., by transferring the bottom left triangle to the right hand end and adding 2M diagonal 1s. code_ez -i infile -s style (1/2) -o outfile (Default MNC4/N.t1.t2.seed) style = 1 adds M+1 columns to the matrix, and overwrites a diagonal with 1s style = 2 adds M+2 columns to the matrix, adding two new diagonalseg usage: code_ez -i MNC4RC/12.4.3.111 -s 1 -o tmpo*/#include "./ansi/r.h"#include "./ansi/rand2.h" #include "./ansi/mynr.h"#include "./ansi/cmatrix.h"typedef struct { /* code_control */#include "code_ez_var_str.h"} code_creation ;static void dc_defaults ( code_creation * ) ; static int process_command ( int , char ** , code_creation * ) ; static void print_usage ( char ** , FILE * , code_creation * );static int report_code ( code_creation * , alist_matrix * ) ;static int check_alist ( alist_matrix * , code_creation * ) ;void main ( int , char ** ) ;/* MAIN */void main ( int argc, char *argv[] ){ FILE *fp ; int u , N , M , No , Mo , n , m , offsetR , offsetC ; code_creation dc ; alist_matrix a ; alist_matrix ai ; dc_defaults ( &dc ) ; if ( process_command (argc, argv, &dc ) < 0 ) exit (0) ; if ( read_allocate_alist ( &ai , dc.afile ) < 0 ) exit (0) ; N = ai.N ; M = ai.M ; if ( N < M ) { fprintf ( stderr, "sorry N<M is not handled by this code \n" ) ; exit(0); } No = N + M + dc.style - 1 ; Mo = M ; dc.M = Mo ; dc.N = No ; offsetR = N + dc.style - 1 ; /* how much the triangular block is displaced */ offsetC = dc.style - 1 ; /* how much the centre block is displaced */ dc.t = 2 * ai.biggest_num_n + 2 + 1 ; dc.tr = ai.biggest_num_m + 2 + 1 ; /* the extra 1s give breathing space */ initialize_alist ( &a , No , Mo , dc.t , dc.tr ) ; fprintf(stderr,"code_ez N=%d, M=%d No=%d Mo=%d\n", N , M , No , Mo ) ; fflush(stderr); /* add diagonals to a */ for ( m = 1 , n = N + dc.style ; m <= M ; m++ , n++ ) { add_to_alist ( &a , m , m ) ; add_to_alist ( &a , n , m ) ; } /* run through all elements in ai copying or translating them */ for ( n = 1 ; n <= M ; n ++ ) { for ( u = 1 ; u <= ai.num_nlist[n] ; u ++ ) { m = ai.nlist[n][u] ; if ( m > n ) { /* move it */ add_to_alist ( &a , n + offsetR , m ) ; } else if ( !( (dc.style ==1 ) && (m==n) ) ) { /* avoids repetitions */ add_to_alist ( &a , n + offsetC , m ) ; } } } for ( ; n <= N ; n ++ ) { for ( u = 1 ; u <= ai.num_nlist[n] ; u ++ ) { m = ai.nlist[n][u] ; add_to_alist ( &a , n + offsetC , m ) ; } } finish_off_alist ( &a ) ; if ( check_alist ( &a , &dc ) < 0 ) { report_code ( &dc , &a ) ; printf ( "FAILED\n" ) ; /* exit ( 0 ) ; */ } if ( dc.out ) { fp = fopen ( dc.outfile , "w" ) ; if ( !fp ) { fprintf ( stderr , "can't open %s\n" , dc.outfile ) , exit (0) ; } write_alist ( fp , &a ) ; fclose ( fp ) ; } free_alist ( &a ) ; if ( dc.verbose ) { read_allocate_alist ( &a , dc.outfile ) ; if ( dc.out ) { fp = fopen ( "tmpout" , "w" ) ; if ( !fp ) { fprintf ( stderr , "can't open %s\n" , dc.outfile ) , exit (0) ; } write_alist ( fp , &a ) ; fclose ( fp ) ; } free_alist ( &a ) ; printf ( "diff tmpout %s\n" , dc.outfile ) ; }}static int report_code ( code_creation *dc , alist_matrix *a ) { int status = 0 ; report_alist ( a , dc->verbose + 1 ) ; return status ; }static int check_alist ( alist_matrix *alist , code_creation *dc ){ int status = 0 ; int tmpi = 0 , n , m ; if ( dc->verbose ) printf ( "Checking alist\n" ) ; /* check: every col should have more than 1 1 in it */ for ( n = 1 ; n <= alist->N ; n ++ ) { if ( alist->num_nlist[n] <= 1 ) { tmpi ++ ; fprintf ( stderr , "%d:" , n ) ; } } if ( tmpi > 0 ) { fprintf ( stderr , "Warning unchecked bits n : %d\n", tmpi ) ;/* status -- ; in ez code this is not a failure */ } /* check: every row should have more than 1 1 in it */ tmpi = 0 ; for ( m = 1 ; m <= alist->M ; m ++ ) { if ( alist->num_mlist[m] <= 1 ) { tmpi ++ ; fprintf ( stderr , "%d:" , m ) ; } } if ( tmpi > 0 ) { fprintf ( stderr , "Warning unchecked bits m : %d\n", tmpi ) ; status -= 10 ; } if ( status < 0 && dc->verbose ) { fprintf ( stderr , "Failing ... here is the offending alist\n" ) ; report_alist ( alist , 2 ) ; } else if ( dc->verbose >= 2 ) { report_alist ( alist , dc->verbose ) ; } return status ; }static void dc_defaults ( code_creation *dc ) {#include "code_ez_var_def.c"}static int process_command ( int argc , char **argv , code_creation *dc ) { int p_usage = 0 ; int status = 0 ; int cs , i , fake ; if ( argc < 1 ) { p_usage = 1 ; status -- ; }#define ERROR1 fprintf ( stderr , "arg to `%s' missing\n" , argv[i] ) ; \ status --#define ERROR2 fprintf ( stderr , "args to `%s' missing\n" , argv[i] ) ; \ status -- for (i = 1 ; i < argc; i++) { cs = 1 ; if ( strcmp (argv[i], "-fake") == 0 ) { if ( i + 1 == argc ) { ERROR1; } else cs *= sscanf(argv[++i], "%d", &(fake)); } #include "code_ez_var_clr.c" else { fprintf ( stderr , "arg `%s' not recognised\n" , argv[i] ) ; p_usage = 1 ; status -- ; } if ( cs == 0 ) { fprintf ( stderr , "arg at or before `%s' has incorrect format\n" , argv[i] ) ; p_usage = 1 ; status -- ; } } if ( p_usage ) print_usage ( argv , stderr , dc ) ; return ( status ) ;}#undef ERROR1#undef ERROR2#define DNT fprintf( fp, "\n ")#define NLNE fprintf( fp, "\n")static void print_usage ( char **argv , FILE * fp , code_creation *dc){ fprintf( fp, "Usage: %s ",argv[0]); fprintf( fp, " [optional arguments]");#include "code_ez_var_usg.c" return ;}#undef DNT#undef NLNE/*<!-- hhmts start -->Last modified: Sat Feb 3 18:54:48 1996<!-- hhmts end -->*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -