/* ** e2.c ** programs for exam#2, questions 2, 3, and 4 ** -- written by Jeff Tian, 11/4/99. */ #include #include #include #include "genlib.h" #include "simpio.h" #include "graphics.h" void pfList(int x); void ukFlag(float x0, float y0, float width, float height); void jpFlag(float x0, float y0, float width, float height, float radius); void pName(void); void ignoreline(void); main() { char c; printf("Personal Names (E#2, Q#4)\n"); printf("=========================\n"); printf("\nContinue with personal names [y/n]? "); c = getchar(); ignoreline(); while (c == 'y' || c == 'Y') { printf("Please enter a name: "); pName(); printf("\nContinue with personal names [y/n]? "); c = getchar(); ignoreline(); } printf("\nPrime Factors (E#2, Q#2)\n"); printf("=========================\n"); printf("\nContinue with prime factors [y/n]? "); c = getchar(); ignoreline(); while (c == 'y' || c == 'Y') { printf("Please enter a positive integer: "); pfList(GetInteger()); printf("\nContinue with prime factors [y/n]? "); c = getchar(); ignoreline(); } printf("\nFlags (E#2, Q#3)\n"); printf("=========================\n"); printf("Pause. Press any key to continue.\n"); getchar(); InitGraphics(); ukFlag(1, 1, 3, 2); printf("Pause. Press any key to continue.\n"); getchar(); InitGraphics(); jpFlag(1, 1, 3, 2, 0.75); } /* ** pfList() -- function for Exam#2, Question 2. */ void pfList(int x) { int i=2, limit, first=1, unique=1; printf("The prime factors for %d are: ", x); limit = sqrt(x); while (i <= limit ) { if(x%i == 0) { if(unique) { unique = 0; if(first) { printf("<%d", i); first = 0; } else { printf(", %d", i); } } x = x/i; limit = sqrt(x); } else { i++; unique = 1; } } if(first) printf("<%d>\n", x); else if(i != x) printf(", %d>\n", x); else printf(">\n"); } /* ** ukFlag(): function for Exam#2, Question 3 */ void ukFlag(float x0, float y0, float width, float height) { MovePen(x0, y0); DrawLine(width, 0); DrawLine(0, height); DrawLine(-width, 0); DrawLine(0, -height); DrawLine(width, height); MovePen(x0, y0 + height); DrawLine(width, - height); MovePen(x0 + width/2, y0); DrawLine(0, height); MovePen(x0, y0 + height/2); DrawLine(width, 0); } /* ** jpFlag(): function for Exam#2, Question 3 */ void jpFlag(float x0, float y0, float width, float height, float radius) { MovePen(x0, y0); DrawLine(width, 0); DrawLine(0, height); DrawLine(-width, 0); DrawLine(0, -height); MovePen(x0 + width/2 + radius, y0 + height/2); DrawArc(radius, 0, 360); } /* ** pName(): function for Exam#2, Question 4. */ void pName() { char c; if(isupper(c=getchar())) { while(islower(c=getchar())); if (isspace(c)) { if(isupper(c=getchar())) { while(islower(c=getchar())); if (c == '\n') { printf("Good name.\n"); return; } } } } ignoreline(); printf("Bad name.\n"); } /* ** ignoreline(): ignore all input until the end of the line */ void ignoreline() { while(getchar() != '\n'); }