#include #include #include #include #include void main(int argc, char *argv[]) { unsigned char ch; const int SizeLine = 200; const int SizeBuf = 3000; const int SizeProb = 10; const int SizeHead = 2000; char FileName[40], OneLine[SizeLine], NameTask[SizeLine], Buffer[SizeBuf]; char Head[SizeHead]; int LenBuf, EndLine, LenHead; char *ptr; int NPeople, NProblems, NTasks[SizeProb], i, j, kstart, k, Okay; int *Variant[SizeProb]; char **Task[SizeProb]; FILE *in, *out, *inhead, *outlog; if (argc == 1) { printf("\nEvgenii Rudnyi - Department of Chemistry, \n\ Moscow State University, 1993. \n\n\ Prepares task sheets for students from the problem base file. \n\n\ See file READ.ME for instructions. \n\n\ Usage: TASK [filename]\n"); return;} strcpy(FileName, argv[1]); ptr = strchr(FileName, '.'); if (ptr == NULL) strcat(FileName, ".chi"); randomize(); in = fopen(FileName, "r"); if (!in) { printf("Can't open %s for intput\n", FileName); return; } inhead = fopen("header.chi", "r"); strcpy(Head, ""); LenHead = 0; if (inhead) { while (fgets(OneLine, SizeLine, inhead)) if (strncmp(OneLine, "\\pTS", 4) == 0) break; while (fgets(OneLine, SizeLine, inhead)) { if (strncmpi(OneLine, "\\=", 2) == 0) break; LenHead += strlen(OneLine); if (LenHead > SizeHead) { printf("too long header - truncated\n"); break; } strcat(Head, OneLine); } fclose(inhead); } out = fopen("tskstd.chi", "w"); if (!out) { printf("Can't open %s for outtput\n", "tskstd.chi"); return; } outlog = fopen("tskstd.log", "w"); if (!out) { printf("Can't open %s for outtput\n", "tskstd.out"); return; } while (fgets(OneLine, SizeLine, in)) { if (strncmpi(OneLine, "*NAME", 5) == 0 || strncmpi(OneLine + 2, "*NAME", 5) == 0) { printf("\n\n%sDo you want this task? ", OneLine); ch = getche(); if (ch == 'Y' || ch == 'y') break; } } if (feof(in)) return; strcpy(NameTask, OneLine); printf("\nNumber of people "); scanf("%d", &NPeople); if (NPeople < 1) NPeople = 1; if (NPeople > 40) NPeople = 40; fprintf(outlog, " "); for (i = 1; i <= NPeople; i++) fprintf(outlog, "%3d", i); fprintf(outlog, "\n"); i = -1; while (strncmpi(OneLine, "*END NAME", 9) != 0 && strncmpi(OneLine + 2, "*END NAME", 9) != 0) { if (fgets(OneLine, SizeLine, in) == NULL || strncmpi(OneLine, "*NAME", 5) == 0 || strncmpi(OneLine + 2, "*NAME", 5) == 0) { printf("\nnot found the line with *END NAME"); return; } if (strncmpi(OneLine, "*PROBLEM", 8) == 0 || strncmpi(OneLine + 2, "*PROBLEM", 8) == 0) { if (i == (SizeProb - 1)) break; i = i + 1; printf("Problem %2d", i+1); fprintf(outlog, "Prb. %2d", i+1); NTasks[i] = 0; while (strncmpi(OneLine, "*END PROBLEM", 12) != 0 && strncmpi(OneLine + 2, "*END PROBLEM", 12) != 0) { if (fgets(OneLine, SizeLine, in) == NULL || strncmpi(OneLine, "*PROBLEM", 8) == 0 || strncmpi(OneLine + 2, "*PROBLEM", 8) == 0) { printf("\nnot found the line with *END PROBLEM"); return; } if (strncmpi(OneLine, "*TASK", 5) == 0 || strncmpi(OneLine + 2, "*TASK", 5) == 0) { NTasks[i]++; while (strncmpi(OneLine, "*END TASK", 9) != 0 && strncmpi(OneLine + 2, "*END TASK", 9) != 0) { if (fgets(OneLine, SizeLine, in) == NULL || strncmpi(OneLine, "*TASK", 5) == 0 || strncmpi(OneLine + 2, "*TASK", 5) == 0) { printf("\nnot found the line with *END TASK"); return; } } } } if (NTasks[i] == 0) { printf("no tasks\n"); return; } printf(" - %d tasks ", NTasks[i]); fprintf(outlog, " - %2d tsk ", NTasks[i]); Variant[i] = new int[NPeople]; if (Variant[i] == NULL) { printf("\nnot enough memory\n"); return; } for (j = 0; j < NPeople; j++) { kstart = j / NTasks[i] * NTasks[i]; do { Okay = 1; *(Variant[i] + j) = random(NTasks[i]) + 1; for (k = kstart; k < j; k++) if (*(Variant[i] + k) == *(Variant[i] + j)) Okay = 0; } while (!Okay); printf("%2d ", *(Variant[i] + j)); fprintf(outlog, "%2d ", *(Variant[i] + j)); } printf("\n"); fprintf(outlog, "\n"); } } NProblems = i + 1; if (NProblems == 0) { printf("\nnothing to do\n"); return; } rewind(in); while (fgets(OneLine, SizeLine, in)) { if (fputs(OneLine, out) == EOF) return; if (strncmp(OneLine, "\\pTS", 4) == 0) break; } if (feof(in)) return; while (fgets(OneLine, SizeLine, in)) if (strcmp(OneLine, NameTask) == 0) break; for (i = 0; i < NProblems; i++) { Task[i] = new char*[NTasks[i]]; if (Task[i] == NULL) { printf("\nnot enough memory\n"); return; } for (j = 0; j < NTasks[i]; j++) { while (fgets(OneLine, SizeLine, in)) if (strncmpi(OneLine, "*TASK", 5) == 0 || strncmpi(OneLine + 2, "*TASK", 5) == 0) { while (fgets(OneLine, SizeLine, in)) if (strncmp(OneLine, "\\-", 2) != 0) break; break; } LenBuf = 0; EndLine = 0; strcpy(Buffer, ""); for (;;) { for (;;) { if (strncmp(OneLine, "\\+", 2) != 0) break; LenBuf += strlen(OneLine); if (LenBuf > (SizeBuf - 1)) { printf("\ntoo long task - truncated\n"); break; } strcat(Buffer, OneLine); fgets(OneLine, SizeLine, in); } if (strncmpi(OneLine, "*END TASK", 9) == 0 || strncmpi(OneLine + 2, "*END TASK", 9) == 0) break; LenBuf += strlen(OneLine); if (LenBuf > (SizeBuf - 1)) { printf("\ntoo long task - truncated\n"); break; } strcat(Buffer, OneLine); fgets(OneLine, SizeLine, in); for (;;) { if (strncmp(OneLine, "\\-", 2) != 0) break; LenBuf += strlen(OneLine); if (LenBuf > (SizeBuf - 1)) { printf("\ntoo long task - truncated\n"); break; } strcat(Buffer, OneLine); fgets(OneLine, SizeLine, in); } EndLine = strlen(Buffer); } Buffer[EndLine] = '\0'; *(Task[i] + j) = new char[EndLine + 1]; if (*(Task[i] + j) == NULL) { printf("\nnot enough memory\n"); return; } strcpy(*(Task[i] + j), Buffer); } } for (k = 0; k < NPeople; k++) { fputs("\\+\n", out); fprintf(out, "\\1 N %2d \n", k+1); fputs(Head, out); for (i = 0; i < NProblems; i++) { if (fputs("\\+\n", out) == EOF) return; fprintf(out, "\\1 TASK %2d\\,\n", i+1); if (fputs(*(Task[i] + *(Variant[i] + k) - 1), out) == EOF) return; } if (fputs("\\+\n", out) == EOF) return; if (fputs("----------------------------------------------------------\\,\f\n", out) == EOF) return; } fclose(in); fclose(out); fclose(outlog); }