/* ** ** TYPING.C Modeled after TYPING.BAS, a typing timer. ** ** Will Menninger, 6-90 ** ** ANSI C ** */ #include #include #include #include #include #include #define EOS '\0' /* ANSI sequences */ static char *WHITE = "\x1b[1m\x1b[37m"; static char *NORMAL = "\x1b[0m\x1b[37m"; static char *CLS = "\x1b[H\x1b[J"; static char *YELLOW = "\x1b[1m\x1b[33m"; static char *UP = "\x1b[1A"; static char *CLL = "\x1b[K"; static char *start_typing= "Start typing: "; static char *description= "This program prints out phrases for you to type and times your typing\n" "performance. You are allowed to use the backspace key to correct errors\n" "if you desire. You must end each line of type with the key.\n" "If you get some funny characters on your screen or you don't have an\n" "ANSI screen driver installed, enter \"typing -n\" at the command line.\n\n" "Press to begin timing your typing, or to quit: "; static char *phrases[] = { "When you read, you see words that make sense to you; so you", "read meaning and give no thought to the letters that are in", "the words. This is the way you learn to type.", "", "It is not easy at first to think the word and type the word,", "but it is the way you will learn to type with best typing power.", "", "Take it easy. Do not try too hard. You cannot be lazy and", "learn to type with ease, of course; but there is such a thing", "as trying too hard.", "", "Fix your eyes on the material you are typing and hold them", "there. Keep your wrists low. Hold your hands and arms quiet", "and let your fingers do the work.", "", "The boy in my class who always had the highest typing speed and", "the best control told me one day that he had gained his skill", "through typing the same short paragraph over and over.", "", "It will help you to type a drill, exercise, or timed writing", "several times if it is done in just the right way.", "", "Studying and education give you the power to do, the power to", "think, and the power to feel. You must have the power to do", "things if you are to succeed in life.", "", "Everyone loves to win. It is a fine feeling to be on top, to", "be first in whatever we try to do. Often the prize is not", "worth the effort, but still we want to win.", "", "The winner must achieve his victory; it cannot be bestowed on", "him. If you will develop superior ability in the doing of one", "thing, you, too, can be a winner.", "", "You must know when to type as rapidly as possible and when to", "type on the letter level. Many of the short and simple words", "can be read as word wholes and the typing will be quicker.", "", "Just as soon as you come to a long word or a series of", "difficult or unfamiliar words, you may have to make some", "changes in the way you type.", "", "The right kind of practice is that which is done with a", "purpose. You have to know what you are expected to gain from", "typing a word, a line, or a problem once or any number of times.", "", "Your fingers do exactly what you tell them to do. If you do", "not tell them what you want them to do, they may fly all over", "the keyboard with an excess of motion.", "", "When you are to type on the control level, drop back in speed", "five words or so below your skill building rate - a drop back", "of at least ten words below your forced speed.", "", "Control level, or slower more accurate typing, is the level of", "typing you should use when handing in a paper for a mark.", "", "Businessmen and women have a keen interest in the work that", "students and teachers are doing in the classrooms of our", "schools. These students will become the men and women who will", "be the next leaders in business.", "", "As I talk with businessmen and women, I often ask them to tell", "me just what I shall say to young men and women who are now", "enrolled in business education courses.", "", "A typist's position at the machine is of great importance. He", "must sit erect, squarely in front of the machine. Palms must", "not be resting on the keyboard. Such a position will reduce", "tension.", "", "This is an age and an era in which we are quite likely to give", "too much importance to getting money and in which we are prone", "to give too little thought to the real value of money.", "", "In taking stock of our personal assets, we must rank high in", "the list of our possessions those qualities that have to do", "with real values.", "", "Wealth is not made up just of assets known as stocks and bonds", "and cash and real estate. We are beginning to realize that the", "true wealth of man is in his ideas and ideals.", "", "Just a few finger gymnastics when you start your practice will", "free the muscles of stiffness and thus help you get a quicker", "key stroke than you can get with tense muscles.", "", "Begin now to train yourself to think through each problem that", "you do. You will have few puzzled moments if you will take", "extreme care to understand each problem before typing it.", "", "Letters tell the story of what is to be sold, and they handle", "the details of credit and collection. If they are poor, the", "effect will show in the volume of business.", "", "By this time, you should be quite skilled in handling your", "machine. You can type with reasonable speed and a fair degree", "of accuracy.", "", "The First State Bank's index in June stood at 327.5 of the", "'Average' as compared with 298 in May, a jump of 9.9%.", "", "A structure is a collection of one or more variables, possibly", "of different types, grouped together under a single name for", "convenient handling. (Structures are called \"records\" in some", "languages, notably Pascal.)", "", "If 2 lbs. of coffee are the same price as 6.5 liters of soda", "pop, then how much are the bananas in row 3 where the gorilla", "is sitting?", "", "Fred gave Dean his 5 white marbles. In return, Dean gave Fred", "$7.89. Fred felt that his white marbles were worth more than", "that, but Dean could bench press 215 lbs.", "", "And this is the newsstand where Troy dared to ask the question,", "\"Are employee snacks subsidized?\" Tragically, the answer was", "no, and Troy was fired.", "", "The modern dispenser cathode consists, essentially, of a strongly-", "bonded, continuous metallic phase of a refractory metal or metals,", "interspersed uniformly with the emitting material.", "", "Within a few hours (a few days at most) a typist with no math", "or typesetting experience can be taught to input even the most", "complex equations.", "", "English is a straightforward, frank, honest, open-hearted no-", "nonsense language, which has little truck with such devilish devious", "devices as accents; indeed U.S. editors are often thrown into a", "dither when a foreign word insinuates itself into the language.", "", "If sixteen pennies are arranged in the form of a square there will", "be the same number of pennies in every row, every column, and each", "of the two long diagonals. Can you do the same with twenty pennies?", "", "A T.V. commercial man was in the supermarket. He found an", "unsuspecting lady and asked her which brand she preferred.", "She said that they were both disgusting, so the man angrily", "told the camera man to stop filming.", "." }; static void start_timer (void); static float stop_timer (void); static float timer (int); static void get_text (char s[4][100],int count); static int count_phrases (void); static int rnd (int arg); static void count_em(char s[4][100],char s2[4][100],int *chars,int *words, int *cwords,int *errs); static int nextword(char s[4][100],int *si,int *sl,int maxl); static int instr (char *string,char *pattern); void main(int argc,char *argv[]) { char s[4][100]; char s2[4][100]; char inp[80]; float secs; int ansi,words,cwords,chars,errs,i,phrase_count; ansi=1; for (i=1;i:\n\n"); aprintf("%s",WHITE); for (i=0;i<4 && s[i][0]!=EOS;i++) aprintf(" %s\n",s[i]); aprintf("%s",NORMAL); aprintf("%s","\n" "Press to start, to change the phrase, to quit: "); fgets(inp,70,stdin); if (inp[0]=='X' || inp[0]=='x' || inp[0]=='q' || inp[0]=='Q') break; aprintf("%s%s%s%s",UP,start_typing,"\n\n",YELLOW); start_timer(); for (i=0;i<4 && s[i][0]!=EOS;i++) { aprintf(" "); fgets(&s2[i][0],95,stdin); } aprintf("%s",NORMAL); secs=stop_timer(); aprintf("\n"); count_em(s,s2,&chars,&words,&cwords,&errs); aprintf("Seconds: %6.2f Errors: %d\n\n",secs,errs); aprintf("Chars: %3d CPS: %5.2f\n",chars,chars/secs); aprintf("Chars/5: %4.1f CPM/5: %5.1f\n",chars/5., chars*12./secs); aprintf("Words: %2d WPM: %5.1f\n",words, words*60./secs); aprintf("Correct Words: %2d CWPM: %5.1f\n",cwords, cwords*60./secs); aprintf("\n"); if (ansi) { while (1) { aprintf("Enter to continue, to quit: "); fgets(inp,70,stdin); if (inp[0]=='q' || inp[0]=='Q') break; if (inp[0]=='c' || inp[0]=='C') break; } if (inp[0]=='q' || inp[0]=='Q') break; } } if (inp[0]=='q' || inp[0]=='Q') break; } return; } static void start_timer(void) { timer(0); } static float stop_timer(void) { return(timer(1)); } #ifdef unix /* Use wall clock time */ static float timer(int arg) { static time_t time0; time_t time1; if (!arg) time(&time0); else { time(&time1); return(difftime(time1,time0)); } return(0.); } #else /* Use CPU time */ static float timer(int arg) { static clock_t time0; clock_t now; if (!arg) { time0=clock(); return(0.); } now=clock(); return((now-time0)/(float)CLOCKS_PER_SEC); } #endif static void count_em(char s[4][100],char s2[4][100],int *chars, int *words,int *cwords,int *errs) { char bigbuf[500]; char littlebuf[100]; int wc,cwc,errors,cpos,pos,wordlen,s2i,s2l,acc,cc,i,lines; for (i=0;i<4 && s[i][0]!=EOS;i++); lines=i; cc=0; acc=-1; strcpy(bigbuf," "); for (i=0;i0;wc++); cpos=1; errors=0; cwc=0; /* Count all words out of place */ for (s2i=s2l=-1;s2l=0) { pos++; for (i=0;i=cpos) { cpos=pos; cwc++; } else if (pos=maxl) return(-1); if (i<0 || l<0) i=l=0; else { for (;s[l][i]!=' ' && s[l][i]!=EOS && s[l][i]!='\t' && s[l][i]!='\n';i++); if (s[l][i]==EOS) { i=0; l++; } } if (l>=maxl) { (*si)=i; (*sl)=l; return(-1); } for (;l=maxl) return(-1); for (;s[l][i]!=' ' && s[l][i]!=EOS && s[l][i]!='\t' && s[l][i]!='\n';i++); return(i-(*si)); } static int instr(char *string,char *pattern) { int i,j; for (i=0;string[i]!=EOS;i++) { for (j=0;pattern[j]!=EOS;j++) if (string[i+j]!=pattern[j]) break; if (pattern[j]==EOS) return(i); } return(-1); } static void get_text(char s[4][100],int count) { static int last_phrase= -1; int i,j; while ((i=rnd(count))==last_phrase); last_phrase=i; for (j=0;i;j++) if (phrases[j][0]==EOS) i--; for (;phrases[j][0]!=EOS && phrases[j][0]!='.';j++,i++) strcpy(&s[i][0],phrases[j]); for (;i<4;i++) s[i][0]=EOS; } static int count_phrases(void) { int i,count; count=0; for (i=0;phrases[i][0]!='.';i++) { if (phrases[i][0]==EOS) count++; } return(i>0 && phrases[i-1][0]==EOS ? count : count+1); } static int rnd(int arg) { static int seeded=0; if (!seeded) { time_t time0; time(&time0); srand((unsigned int)time0); seeded=1; } return(rand()%arg); }