#include #include #include #define MAXDIM 16 #define MAXLEN 16 #define MAXENT 256 typedef struct { int rowdim; int coldim; int data[MAXDIM][MAXDIM]; } matrix; typedef struct { char name[MAXLEN]; } varname; typedef struct { varname ID; matrix M; } stentry; stentry ST[MAXENT]; int nstentry = 0; matrix readmatrix ( char *str ) { matrix M; char *numstr, c; int i, j, a; while ((*str == ' ') || (*str == '\t')) ++str; if ( (*str != '[') || (strchr(str,']') == NULL) ) { fprintf(stderr, "*** Invalid input matrix\n"); M.rowdim = M.coldim = 0; return M; } ++str; M.rowdim = 1; M.coldim = 0; i = j = 0; while (1) { while ((*str == ' ') || (*str == '\t')) ++str; numstr = str; if ((*str == '-') || (*str == '+')) ++str; while ((*str >= '0') && (*str <= '9')) ++str; c = *str; *str = 0; a = atoi(numstr); M.data[i][j] = a; ++j; if (j > M.coldim) M.coldim = j; *str = c; while ((*str == ' ') || (*str == '\t')) ++str; if (*str == ']') break; if (*str == ',') { ++str; continue; } if (*str == ';') { ++str; ++M.rowdim; ++i; j = 0; continue; } if (!((*str >= '0') && (*str <= '9'))) { fprintf(stderr, "*** Invalid input matrix\n"); M.rowdim = M.coldim = 0; return M; } } return M; } varname readvarname ( char *str, int *skip ) { varname v; int i; *skip = 0; while ((*str == ' ') || (*str == '\t')) { ++str; ++(*skip); } if ( ((*str >= 'A') && (*str <= 'Z')) || ((*str >= 'a') && (*str <= 'z')) ) { v.name[0] = *str; ++str; ++(*skip); i = 1; while ( ((*str >= 'A') && (*str <= 'Z')) || ((*str >= 'a') && (*str <= 'z')) || ((*str >= '0') && (*str <= '9')) || (*str == '_') ) { v.name[i] = *str; ++str; ++(*skip); ++i; } v.name[i] = '\0'; return v; } fprintf(stderr, "*** Invalid variable\n"); v.name[0] = '\0'; return v; } int stref ( varname v, int CREATE ) { int i; for (i=0; i 1) fp = (FILE *)fopen(argv[1],"r"); else fp = NULL; while (1) { printf("$ "); if (fp == NULL) fgets(line,512,stdin); else { fgets(line,512,fp); printf("%s", line); } line[strlen(line)-1] = '\0'; str = line; while ((*str == ' ') || (*str == '\t')) ++str; if (*str == '\0') continue; if (!strncmp(str,"exit",4)) break; lval = readvarname(str, &skip); str += skip; if (lval.name[0] == '\0') continue; while ((*str == ' ') || (*str == '\t')) ++str; if (*str != '=') { fprintf(stderr, "*** Invalid input line\n"); continue; } ++str; while ((*str == ' ') || (*str == '\t')) ++str; l = stref(lval,1); if (*str == '[') { ST[l].M = readmatrix(str); matprn(ST[l]); continue; } rval1 = readvarname(str, &skip); str += skip; r1 = stref(rval1,0); if (r1 < 0) { fprintf(stderr, "*** Bad variable \"%s\"\n", rval1.name); matprn(ST[l]); continue; } while ((*str == ' ') || (*str == '\t')) ++str; op = *str; ++str; if (op == '\0') { ST[l].M = ST[r1].M; matprn(ST[l]); continue; } while ((*str == ' ') || (*str == '\t')) ++str; rval2 = readvarname(str, &skip); str += skip; if ((op != '+') && (op != '-') && (op != '*')) { fprintf(stderr, "*** Invalid input line\n"); continue; } r2 = stref(rval2,0); if (r2 < 0) { fprintf(stderr, "*** Bad variable \"%s\"\n", rval2.name); matprn(ST[l]); continue; } switch (op) { case '+': ST[l].M = matadd(ST[r1].M,ST[r2].M); break; case '-': ST[l].M = matsub(ST[r1].M,ST[r2].M); break; case '*': ST[l].M = matmul(ST[r1].M,ST[r2].M); break; } matprn(ST[l]); } if (fp != NULL) fclose(fp); exit(0); }