diff --git a/Mix C v251/ANOTHER.CFG b/Mix C v251/ANOTHER.CFG new file mode 100644 index 0000000..60f4201 Binary files /dev/null and b/Mix C v251/ANOTHER.CFG differ diff --git a/Mix C v251/CC.COM b/Mix C v251/CC.COM new file mode 100644 index 0000000..33caa11 Binary files /dev/null and b/Mix C v251/CC.COM differ diff --git a/Mix C v251/CC.EXE b/Mix C v251/CC.EXE new file mode 100644 index 0000000..854a6fc Binary files /dev/null and b/Mix C v251/CC.EXE differ diff --git a/Mix C v251/CERRORS.DAT b/Mix C v251/CERRORS.DAT new file mode 100644 index 0000000..f0ef4d1 --- /dev/null +++ b/Mix C v251/CERRORS.DAT @@ -0,0 +1,256 @@ +Identifier expected +Identifier expected in a type declaration +Variable or function declaration expected +')' expected +':' expected +Illegal symbol +Invalid preprocessor statement +Unexpected end of file during declaration definition +Right braces expected + + +Right bracket ']' or '.)' expected + +';' expected +Integer expected +'=' expected +Statement expected +Closing single quote expected +Invalid character constant +',' expected +Invalid octal digit +Expression or ';' expected +Invalid hexadecimal digit +Expression expected +Left parenthesis expected +WHILE keyword expected +Lvalue expected (invalid expression on left hand side of assignment) +Invalid type cast in constant expression + + +Left braces expected or semi-colon missing on function declaration + +Exponential part of floating point number expected + + +Lvalue expected for & and * unary operators +'{' not allowed, contents skipped +Matching '}' to error 37, text between has been skipped + + + + + + + + + + + +Constant expected + + + + + + + + + + + + + + + + +Name of typedef expected +Structure contains a reference to itself +Bitfield too wide +Bitfields must be int or unsigned +Invalid reference to bitfield +Too many parameters in macro invocation +#ENDIF without a matching #IF +Unexpected eof with unclosed #IF statment +Preprocessor command expected +Missing parameter in macro invocation +Error opening #include file + +End of file within a comment (missing */) +Open comment within a comment +Unknown option + + + + + + + + + + + + + + + + + + + +Identifier already defined + + +Undeclared identifier + + + + + + + + + +Null array size allowed for first dimension only + + + + +Static function definition must precede use +Static function must be declared static before first use +Pointer to a function expected +';' not allowed before '{' in function definition + + + + + + +Type conflict of operands in an expression +Structure assignment with structures of different sizes +| or & do not apply to float or double operands + +Pointer arithmetic requires int +Illegal type of operands + + +Incompatible pointers +Type of variable is not array + +Type of variable is not structure or union +Type of variable is not pointer + +Constant expression contains illegal operator + + + + + + + + +No such field in this structure + +Function parameter list expected + + + +Redefinition of a global variable +Missing identifier in type definition +Function definition within a function illegal + +Function definition has more than one variable in declaration list + +Parameter declaration expected +Label already defined + +This symbol is not a label +Label not defined + +Too many items in initializer +Initializer contains list to initialize single item +Type not compatible in initializer +Initializer not allowed for this class +Initialization of bitfields not allowed on statics & globals +Can not be initialized by a string constant +& operator does not apply +Pointer required for & in initializers +Expression not allowed for float, double + + + + +Switch selector must be int, unsigned or char + + + +Array subscript must be int +Invalid type in operands of an op= or = operator +Operand to & must be a variable + + + + + + + + + +Feature not implemented + + +String constant cannot span lines +Integer constant too large +Auto variables exceed 32k in a single function + + + + + +Unable to create trace file for debugging + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Too many errors + + + + + diff --git a/Mix C v251/CLIB.MIX b/Mix C v251/CLIB.MIX new file mode 100644 index 0000000..6c9d6bd Binary files /dev/null and b/Mix C v251/CLIB.MIX differ diff --git a/Mix C v251/CONVERT.COM b/Mix C v251/CONVERT.COM new file mode 100644 index 0000000..331bed7 Binary files /dev/null and b/Mix C v251/CONVERT.COM differ diff --git a/Mix C v251/CTR.CFG b/Mix C v251/CTR.CFG new file mode 100644 index 0000000..be4dbe7 Binary files /dev/null and b/Mix C v251/CTR.CFG differ diff --git a/Mix C v251/CTR.EXE b/Mix C v251/CTR.EXE new file mode 100644 index 0000000..66f0e75 Binary files /dev/null and b/Mix C v251/CTR.EXE differ diff --git a/Mix C v251/E.C b/Mix C v251/E.C new file mode 100644 index 0000000..3c6a84d --- /dev/null +++ b/Mix C v251/E.C @@ -0,0 +1,30 @@ +#include + +#define DIGITS_TO_FIND 200 /*9009*/ + +int main() { + + int N = DIGITS_TO_FIND; + int x = 0; + int a[ DIGITS_TO_FIND ]; + int n; + + for (n = N - 1; n > 0; --n) { + a[n] = 1; + } + + a[1] = 2, a[0] = 0; + while (N > 9) { + n = N--; + while (--n) { + a[n] = x % n; + + x = 10 * a[n-1] + x/n; + } + printf("%d", x); + } + + printf( "\ndone\n" ); + + return 0; +} diff --git a/Mix C v251/EXAMPLES/EXAM11.C b/Mix C v251/EXAMPLES/EXAM11.C new file mode 100644 index 0000000..ab1e667 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM11.C @@ -0,0 +1,3 @@ +main() +{ +} diff --git a/Mix C v251/EXAMPLES/EXAM12.C b/Mix C v251/EXAMPLES/EXAM12.C new file mode 100644 index 0000000..f48de70 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM12.C @@ -0,0 +1,5 @@ +main() /* Example 1.2 */ +{ + printf("One small step for C, one giant leap for me."); +} + diff --git a/Mix C v251/EXAMPLES/EXAM13.C b/Mix C v251/EXAMPLES/EXAM13.C new file mode 100644 index 0000000..50f4453 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM13.C @@ -0,0 +1,12 @@ +main() /* Example 1.3 */ +{ + printf("This is the way we wash our face,\n"); + printf("Wash our face,\n"); + printf("Wash our face.\n"); + printf("\n"); + printf("This is the way we wash our face, "); + printf("so early in the morning.\n"); + /* usually sung to young children as one scrubs + the face. */ +} + diff --git a/Mix C v251/EXAMPLES/EXAM14.C b/Mix C v251/EXAMPLES/EXAM14.C new file mode 100644 index 0000000..707c0a9 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM14.C @@ -0,0 +1,26 @@ +main() /* Example 1.4 */ +{ + /* declare integer variables */ + int our_score; + int their_score; + int our_touchdowns, our_field_goals; + int their_tds, their_fgs; + + /* assign values */ + our_score = 24; + their_score = 14; + our_touchdowns = 3; + their_tds = 2; + our_field_goals = 1; + their_fgs = 0; + +/* print the results of the game */ + printf(" Home Visitor\n"); + printf("Score %d %d\n", + our_score, their_score); + printf("Touchdowns %d %d\n", + our_touchdowns, their_tds); + printf("Field Goals %d %d\n", + our_field_goals, their_fgs); + +} diff --git a/Mix C v251/EXAMPLES/EXAM15.C b/Mix C v251/EXAMPLES/EXAM15.C new file mode 100644 index 0000000..d81f758 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM15.C @@ -0,0 +1,29 @@ +main() /* Example 1.5 */ +{ + long total, stock; + unsigned sales; + float price, commission, cost, income; + double profit; + char type; + + sales = 45678; /* items sold */ + stock = 112502; /* items on hand */ + price = 42.50; /* selling price */ + cost = 19.95; /* cost of item */ + type = 'A'; + + /* calculate values */ + total = stock - sales; + income = price * sales; + profit = (price - cost) * sales; + commission = profit * 0.03; + + /* print answers */ + printf( + "total inventory = %ld of type %c\n",total,type); + printf("On sales of %u items: profit = %f\n", + sales, profit); + printf("Commissions at a rate of 0.03 = %f\n", + commission); +} + diff --git a/Mix C v251/EXAMPLES/EXAM21.C b/Mix C v251/EXAMPLES/EXAM21.C new file mode 100644 index 0000000..8800283 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM21.C @@ -0,0 +1,9 @@ +main() /* Example 2.1 */ +{ /* figure distance traveled */ + int mph,time; + printf("Enter your speed in miles per hour: "); + scanf("%d",&mph); + printf("Enter the number of hours: "); + scanf("%d",&time); + printf("Miles traveled = %d \n", mph * time); +} diff --git a/Mix C v251/EXAMPLES/EXAM22.C b/Mix C v251/EXAMPLES/EXAM22.C new file mode 100644 index 0000000..9aedfdf --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM22.C @@ -0,0 +1,40 @@ +main() /* Example 2.2 */ +{ + long total, stock, sales; + float price, commission, cost, income; + double profit; + char type; + + /* input values to be used */ + printf("Please enter the following items "); + printf("to calculate gross profit,\n net profit,"); + printf(" and sales commision on a item \n"); + printf("Enter the type of item (1 char.) : "); + scanf("%c",&type); + printf("Enter the number of items sold : "); + scanf("%ld",&sales); /* items sold */ + printf("Enter the number of items on hand"); + printf(" at the beginning of the month :"); + scanf("%ld",&stock); /* items on hand */ + printf("Enter the current selling price : "); + scanf("%f",&price); /* selling price */ + printf("Enter the cost of the item : "); + scanf("%f",&cost); /* cost of item */ + + /* calculate values */ + total = stock - sales; + income = price * sales; + profit = (price - cost) * sales; + commission = profit * 0.03; + + /* print answers */ + printf( + "End of month total inventory = %ld of type %c\n", + total,type); + printf("On a gross income of "); + printf("%6.2f profit = %6.2f\n", + income, profit); + printf("Commissions at a rate of 0.03 = %6.2f\n", + commission); +} + diff --git a/Mix C v251/EXAMPLES/EXAM23.C b/Mix C v251/EXAMPLES/EXAM23.C new file mode 100644 index 0000000..6a0c50f --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM23.C @@ -0,0 +1,16 @@ +#define TAXRATE 0.06125 +#define READ_LONG(longvar) scanf("%ld", &longvar) +#define READ_FLOAT(floatvar) scanf("%f", &floatvar) + +main() /* Example 2.3 */ +{ + long number; + float price, cost, tax; + printf("Enter number of units: "); + READ_LONG(number); + printf("Enter price per unit: "); + READ_FLOAT(price); + printf("Cost = %8.2f\n", cost = price * number); + printf("Tax = %8.2f\n", tax = cost * TAXRATE); + printf("Total = %8.2f\n", cost + tax); +} diff --git a/Mix C v251/EXAMPLES/EXAM24.C b/Mix C v251/EXAMPLES/EXAM24.C new file mode 100644 index 0000000..0e9c9f0 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM24.C @@ -0,0 +1,7 @@ +main() /* Example 2.4 */ +{ + int a; + + a = 3.4 * 5; + printf("a = %d \n",a); +} diff --git a/Mix C v251/EXAMPLES/EXAM25.C b/Mix C v251/EXAMPLES/EXAM25.C new file mode 100644 index 0000000..063dee3 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM25.C @@ -0,0 +1,34 @@ +main() /* Example 2.5 */ +{ + char c1, c2, c3; + int i1, i2, i3; + float f1, f2, f3; + long dl; + + /* char converts to int */ + c1 = 'c'; + i1 = c1 - 'a' + 'A'; + c3 = i1; /* truncate to character */ + printf("c1 = %c, i1 = %d, c3 = %c\n", c1,i1,c3); + i1 = 321; + c2 = i1; /* convert integer to char */ + c3 = i1 + 1; /* truncates value */ + printf("i1 = %d, c2 = %c, c3 = %c\n", + i1,c2,c3); + + /* automatic conversion from int to float */ + f1 = 200; /* converted to float */ + f2 = 350 * f1; /* 350 converted to float */ + /* 7 converted to float- result truncated */ + i3 = 3.4 * 7; + /* 350 converted to float - result truncated */ + i1 = f3 = f1 / 350; + printf("f1 = %f, f2 = %f,\n",f1,f2); + printf("i3 = %d, f3 = %f, i1 = %d\n", + i3,f3,i1); + /* values produced in the following + assume a 16 bit integer */ + dl = 69631; /* In hex 10FFF */ + i2 =dl; /* truncates to 0FFF */ + printf("i2 = %d\n",i2); +} diff --git a/Mix C v251/EXAMPLES/EXAM26.C b/Mix C v251/EXAMPLES/EXAM26.C new file mode 100644 index 0000000..da859e4 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM26.C @@ -0,0 +1,19 @@ +main() /* Example 2.6 */ +{ + int xx,ii; + int yy,jj; + + jj = ii = 0; + + xx = jj++ + ++jj; + printf("xx = %d, jj = %d, ii = %d\n",xx,jj,ii); + + yy = jj *= xx++ + ++ii + 3; + printf("yy = %d, jj = %d, xx = %d, ii = %d\n", + yy,jj,xx,ii); + + ii = ++ii; + printf("ii = %d\n",ii); + + printf("1st --yy = %d, 2nd --yy = %d",--yy,--yy); +} diff --git a/Mix C v251/EXAMPLES/EXAM31.C b/Mix C v251/EXAMPLES/EXAM31.C new file mode 100644 index 0000000..e3f6526 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM31.C @@ -0,0 +1,11 @@ +main() /* Example 3.1 */ +{ + int mph,time,distance; /* figure distance traveled */ + printf("Enter your speed in miles per hour: "); + scanf("%d",&mph); + printf( + "Enter the number of hours you traveled the above speed: "); + scanf("%d",&time); + distance = mph * time; + printf("Miles traveled = %d \n",distance); +} diff --git a/Mix C v251/EXAMPLES/EXAM32.C b/Mix C v251/EXAMPLES/EXAM32.C new file mode 100644 index 0000000..0f5ee45 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM32.C @@ -0,0 +1,14 @@ +main() /* Example 3.2 */ +{ + int birth_yr, cur_yr; + + printf("Enter the year of your birth: "); + scanf("%d",&birth_yr); + if (birth_yr <= 0) + printf(" Invalid year entered \n"); + printf("Enter the current year: "); + scanf("%d",&cur_yr); + if (cur_yr <= 0 ) + printf(" Invalid year entered \n"); + printf(" Your current age = %d \n",cur_yr - birth_yr); +} diff --git a/Mix C v251/EXAMPLES/EXAM33.C b/Mix C v251/EXAMPLES/EXAM33.C new file mode 100644 index 0000000..ea2d95e --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM33.C @@ -0,0 +1,18 @@ +main() /* Example 3.3 */ +{ + int birth_yr, cur_yr; + + printf("Enter the year of your birth: "); + scanf("%d",&birth_yr); + if (birth_yr <= 0) + printf(" Invalid year entered \n"); + else { + printf("Enter the current year: "); + scanf("%d",&cur_yr); + if (cur_yr <= 0 ) + printf(" Invalid year entered \n"); + else printf(" Your current age = %d \n", + cur_yr - birth_yr); + } +} + diff --git a/Mix C v251/EXAMPLES/EXAM34.C b/Mix C v251/EXAMPLES/EXAM34.C new file mode 100644 index 0000000..0b7adb2 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM34.C @@ -0,0 +1,24 @@ +main() /* Example 3.4 */ +{ + /* y */ + /* calculate x */ + /* */ + /* where x and y are integers and y >= 0 */ + + int base, power; + long result = 1; + int counter = 0; + + printf("Enter the base number : "); + scanf("%d",&base); + printf("Enter the nth power to "); + printf("which base will be raised:"); + scanf("%d",&power); + + while (counter++ < power) + result = result * base; + + printf("The base of %d raised to",base); + printf(" the %dth power is %ld\n", + power,result); +} diff --git a/Mix C v251/EXAMPLES/EXAM35.C b/Mix C v251/EXAMPLES/EXAM35.C new file mode 100644 index 0000000..114e9e4 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM35.C @@ -0,0 +1,14 @@ +main() /* Example 3.5 */ +{ + int sum = 0; + int number, ok = 1; + /* try (ok = 1) */ + while (ok == 1) { + printf("Enter a number to sum"); + printf("(zero will terminate): "); + scanf("%d",&number); + if (number == 0) ok = 0; + else sum = sum + number; + } + printf("The total sum is %d\n",sum); +} diff --git a/Mix C v251/EXAMPLES/EXAM36.C b/Mix C v251/EXAMPLES/EXAM36.C new file mode 100644 index 0000000..d889b5a --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM36.C @@ -0,0 +1,16 @@ +main() /* Example 3.6*/ +{ + char in = 'a'; + + printf("Test for upper or lowercase letters,"); + printf(" terminate on non-alpha\n"); + while((in <= 'z' && in >= 'a') || + (in <= 'Z' && in >= 'A')){ + printf("Enter a character and"); + printf(" press the enter key: "); + scanf("%c%*c",&in); + printf("Character read is %c\n",in); + } + printf("Program Terminated - "); + printf("non-alphabetic character entered\n"); +} diff --git a/Mix C v251/EXAMPLES/EXAM37.C b/Mix C v251/EXAMPLES/EXAM37.C new file mode 100644 index 0000000..f09ee73 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM37.C @@ -0,0 +1,12 @@ +main() /* Example 3.7 */ +{ + int sum, count, num; + printf("This program sums 5 integer numbers \n"); + sum = 0; + for (count=0; count < 5; count++) { + printf("Enter a number : "); + scanf("%d",&num); + sum += num; + } + printf("Total of the five numbers = %d\n",sum); +} diff --git a/Mix C v251/EXAMPLES/EXAM38.C b/Mix C v251/EXAMPLES/EXAM38.C new file mode 100644 index 0000000..aa24344 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM38.C @@ -0,0 +1,22 @@ +#define TRUE 1 +main() /* Example 3.8 */ +{ + int sum, count, num; /* declarations */ + int go = TRUE; + + printf("This program sums 5 integer numbers \n"); + /* initialize go so that we fall through loop at least once */ + + while (go) { + for (sum = 0,count=0 ; count < 5; count++) { + printf("Enter a number : "); + scanf("%d",&num); + sum += num; + } + printf("Total of the five numbers = %d\n",sum); + printf( + "Do you have any more sets of 5 numbers to sum?\n"); + printf("Enter 1 for yes and 0 for no: "); + scanf("%d",&go); + } +} diff --git a/Mix C v251/EXAMPLES/EXAM39.C b/Mix C v251/EXAMPLES/EXAM39.C new file mode 100644 index 0000000..03b43f3 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM39.C @@ -0,0 +1,18 @@ +main() /* Example 3.9 */ +{ + int sum, count, num; + int go; + printf("This program sums 5 integer numbers \n"); + do { + for (sum = 0,count=0 ; count < 5; count++) { + printf("Enter a number : "); + scanf("%d",&num); + sum += num; + } + printf("Total of the five numbers = %d\n",sum); + printf("Do you have any more sets of 5"); + printf(" numbers to sum?\n"); + printf("Enter 1 for yes and 0 for no\n"); + scanf("%d",&go); + } while (go); +} diff --git a/Mix C v251/EXAMPLES/EXAM41.C b/Mix C v251/EXAMPLES/EXAM41.C new file mode 100644 index 0000000..1664f89 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM41.C @@ -0,0 +1,33 @@ +main() +{ int Score, Avg_Score, No_of_Students; + long Total_Score; + prompt1(); + No_of_Students = 0; + Total_Score = 0; + prompt2(); + scanf("%d", &Score); + while (Score != -1) { + Total_Score = Total_Score + Score; + ++No_of_Students; + prompt2(); + scanf("%d", &Score); + } + printf("You Entered Scores for %d Students.\n", + No_of_Students); + if (No_of_Students > 0) { + Avg_Score = Total_Score/No_of_Students; + printf("Average Score was %d.", Avg_Score); + } +} + +prompt1() +{ + Printf("*** Program to Compute Average Test Score ***\n"); + Printf(" A Test Score of -1 Terminates Input\n"); +} + +prompt2() +{ + printf("Enter Test Score: "); +} + diff --git a/Mix C v251/EXAMPLES/EXAM42.C b/Mix C v251/EXAMPLES/EXAM42.C new file mode 100644 index 0000000..b44fd9b --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM42.C @@ -0,0 +1,27 @@ +/* This is a complete listing of example 4.2 */ +main() +{ + int i,sq,cu; + + printf("This function prints the square"); + printf(" and the cube of a number\n"); + printf("Enter the number now: "); + scanf("%d",&i); + printf("\nThe value entered = %d\n",i); + sq = square(i); + printf("\nThe value squared = %d\n",sq); + cu = cube(i); + printf("\nThe value cubed = %d\n",cu); +} + +square(ii) +int ii; +{ + return(ii * ii); +} + +cube(iii) +int iii; +{ + return(iii * square(iii)); +} diff --git a/Mix C v251/EXAMPLES/EXAM43.C b/Mix C v251/EXAMPLES/EXAM43.C new file mode 100644 index 0000000..0415f15 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM43.C @@ -0,0 +1,18 @@ +double cir_area(radius) /* function definition */ +double radius; /* Example 4.3 */ +{ + return(3.14159 * radius * radius); +} +main() +{ + double cir_area(); /* function declaration */ + double radius; + double area; + printf("This program calculates the"); + printf(" area of a circle\n"); + printf("Enter the radius of the circle: "); + scanf("%lf",&radius); + area = cir_area(radius); + printf(" The area = %f ",area); +} + diff --git a/Mix C v251/EXAMPLES/EXAM44.C b/Mix C v251/EXAMPLES/EXAM44.C new file mode 100644 index 0000000..974a323 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM44.C @@ -0,0 +1,69 @@ +#define TRUE 1 +main() /* Example 4.4 */ +{ +/* print a number in one of several formats */ + int go = TRUE,type,dnum; + float fnum; + void fltout(), intout(); + + printf("Print a number as "); + printf("binary or hexadecimal\n"); + while(go) { + printf("Is your number floating point ?\n"); + printf("Enter 0 for no, "); + printf("anything else for yes: "); + scanf("%d",&type); + if(type) { + printf("Enter your floating number: "); + scanf("%f",&fnum); + fltout(fnum); + } + else { + printf("Enter your integer number : "); + scanf("%d",&dnum); + intout(dnum,'D'); + intout(dnum,'B'); + intout(dnum,'H'); + } + printf("Do you wish to enter another number?\n"); + printf("Enter 0 for no, anything else for yes\n"); + scanf("%d",&go); + } +} + +void fltout(dnum) +double dnum; +{ + float fnum; + + printf("\nFloating Point number: %f\n",dnum); + printf("in e format: %e\n",dnum); +} + +void intout(inum,type) +int inum,type; +{ + void binary(); + + if (inum < 0) { + printf("Only positive numbers accepted\n"); + inum = - inum; + } + if (type == 'B') { + printf("Binary: "); + binary(inum); + printf("\n"); + } + else if (type == 'H') + printf("Hex: %x\n",inum); + else + printf("Decimal: %d\n",inum); + +} + +void binary(inum) +int inum; +{ + if (inum > 1) binary(inum/2); + printf("%d",inum%2); +} diff --git a/Mix C v251/EXAMPLES/EXAM51.C b/Mix C v251/EXAMPLES/EXAM51.C new file mode 100644 index 0000000..e166ef3 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM51.C @@ -0,0 +1,9 @@ +#include "file1.c" +main() +{ + #include "file2.c" + a = 'a'; + one = 1; + flt1 = 3.567; + printf("This example shows include files only\n"); +} diff --git a/Mix C v251/EXAMPLES/EXAM52.C b/Mix C v251/EXAMPLES/EXAM52.C new file mode 100644 index 0000000..313835b --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM52.C @@ -0,0 +1,15 @@ +/*include standard I/O header file */ +#include + +main () /* Example 5.2 */ +{ + /* this program reads characters from stdin + and writes them to stdout */ + + int charin; + + /* Type Ctrl Z to terminate the program */ + + while ( (charin = getc(stdin)) != EOF ) + putc(charin, stdout); +} diff --git a/Mix C v251/EXAMPLES/EXAM53.C b/Mix C v251/EXAMPLES/EXAM53.C new file mode 100644 index 0000000..22fbb28 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM53.C @@ -0,0 +1,11 @@ +#include + +main () /* Example 5.3 */ +{ + /* copy a file from stdin to stdout */ + + int charin; + + while ( (charin = getchar()) != EOF ) + putchar(charin); +} diff --git a/Mix C v251/EXAMPLES/EXAM54A.C b/Mix C v251/EXAMPLES/EXAM54A.C new file mode 100644 index 0000000..4024e10 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM54A.C @@ -0,0 +1,27 @@ +#include +main() /* Example 5.4a */ +{ + int count = 0, charin; + int digit = 0, other = 0; + int alpha = 0, space = 0; + + printf("\nThis program will count "); + printf("occurrences of digits,\n"); + printf("alphabetic characters, and whitespace\n"); + printf("Enter EOF to terminate the program\n"); + printf("Reading input.......\n"); + for(count=0;;++count) { + charin = getchar(); + if (isalpha(charin)) alpha++; + else if (isdigit(charin)) digit++; + else if (isspace(charin)) space++; + else if (charin == EOF) break; + else other++; + } + printf("\nSUMMARY\n"); + printf("\nThere were %d digits entered\n",digit); + printf("Along with %d alphabetic characters,\n",alpha); + printf(" %d whitespace characters,\n",space); + printf("and %d other characters.\n",other); + printf("For a total of %d characters read.\n",count); +} diff --git a/Mix C v251/EXAMPLES/EXAM54B.C b/Mix C v251/EXAMPLES/EXAM54B.C new file mode 100644 index 0000000..0358435 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM54B.C @@ -0,0 +1,29 @@ +#include +main() /* Example 5.4b */ +{ + + int count = 0,charin; + int digit = 0,other = 0; + int alpha = 0,space = 0; + + printf("\nThis program will count "); + printf("occurrences of digits,\n"); + printf("alphabetic characters, and whitespace\n"); + printf("Enter EOF to terminate the program\n"); + printf("Reading input.......\n"); + for(count=0;;++count) { + charin = getchar(); + if (isalpha(charin)) alpha++; + else if (isdigit(charin)) digit++; + else if (isspace(charin)) space++; + else if (charin == EOF) goto summarize; + else other++; + } +summarize: + printf("\nSUMMARY\n"); + printf("\nThere were %d digits entered\n",digit); + printf("Along with %d alphabetic characters,\n",alpha); + printf(" %d whitespace characters,\n",space); + printf("and %d other characters.\n",other); + printf("For a total of %d characters read.\n",count); +} diff --git a/Mix C v251/EXAMPLES/EXAM55.C b/Mix C v251/EXAMPLES/EXAM55.C new file mode 100644 index 0000000..3752e2a --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM55.C @@ -0,0 +1,16 @@ +#define TAB 8 /* Example 5.5 */ +#include +main () +{ + /* Convert tab characters to blanks + The tab character is generated + from the keyboard by Ctrl I */ + int c,i; + while((c = getchar()) != EOF) { + if ( c == '\t') { + for ( i = 0; i < TAB; i++) putchar(' '); + continue; + } + putchar(c); + } +} diff --git a/Mix C v251/EXAMPLES/EXAM56.C b/Mix C v251/EXAMPLES/EXAM56.C new file mode 100644 index 0000000..d3fb52b --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM56.C @@ -0,0 +1,9 @@ +main () /* Example 5.6 */ +{ + int largest, i1, i2; + + printf("Enter 2 integer numbers: "); + scanf("%d%d",&i1,&i2); + largest = i1 > i2 ? i1 : i2; + printf("The largest number is %d",largest); +} diff --git a/Mix C v251/EXAMPLES/EXAM57.C b/Mix C v251/EXAMPLES/EXAM57.C new file mode 100644 index 0000000..9202682 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM57.C @@ -0,0 +1,36 @@ +#include +main() /* Example 5.7 */ +{ + int digit = 0, other = 0, space = 0; + int charin; + + printf("\nThis program will count "); + printf("occurrences of digits,\n"); + printf("whitespace and other characters\n"); + printf("Enter EOF to terminate the program\n"); + printf("Reading input ..........\n"); + while((charin = getchar()) != EOF) + switch(charin) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': case '9': digit++; + break; /* exit switch */ + case '\t': + case '\n': + case ' ': space++; + break; /* exit switch */ + default: other++; + break; /* exit switch */ + } + printf("\n\n SUMMARY\n"); + printf("There were %d digits entered\n", digit); + printf("Along with %d whitespace characters\n", + space); + printf(" and %d other characters.\n",other); +} diff --git a/Mix C v251/EXAMPLES/EXAM58.C b/Mix C v251/EXAMPLES/EXAM58.C new file mode 100644 index 0000000..786fa97 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM58.C @@ -0,0 +1,65 @@ +#define DAYPHCORR 10; +#define LENCYCLE 28.3; + +main() /* Example 5.8 */ +{ + int daynumber, intphase; + int startphase, phase, month, day, year; + double realphase, phasecorrection; + + printf(" *** Lunar Phase calculation program ***\n"); + printf(" Enter the date (mm/dd/yy):"); + scanf("%d/%d/%d ",&month,&day,&year); + startphase = ((year - 78) * 365) + DAYPHCORR; + switch(month) { + case 1: daynumber = 1; + break; + case 2: daynumber = 32; + break; + case 3: daynumber = 60; + break; + case 4: daynumber = 91; + break; + case 5: daynumber = 121; + break; + case 6: daynumber = 152; + break; + case 7: daynumber = 182; + break; + case 8: daynumber = 213; + break; + case 9: daynumber = 243; + break; + case 10: daynumber = 274; + break; + case 11: daynumber = 304; + break; + case 12: daynumber = 334; + break; + } + + startphase += daynumber + day; + realphase = startphase / LENCYCLE; + intphase = realphase; /* truncate the result */ + realphase -= intphase; + phase = realphase * LENCYCLE; + switch (phase){ + case 1: case 2: case 3: case 4: case 5: case 6: case 7: + printf("The moon is in its first quarter.\n"); + break; + case 15: case 16: case 17: case 18: case 19: + case 20: case 21: + printf("The moon is in its third quarter.\n"); + break; + /* labels don't have to be in order */ + case 8: case 9: case 10: case 11: case 12: + case 13: case 14: + printf("The moon is in its second quarter.\n"); + break; + case 22: case 23: case 24: case 25: case 26: + case 27: case 28: + printf("The moon is in its fourth quarter.\n"); + break; + } +} + diff --git a/Mix C v251/EXAMPLES/EXAM61.C b/Mix C v251/EXAMPLES/EXAM61.C new file mode 100644 index 0000000..21ce73e --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM61.C @@ -0,0 +1,27 @@ +main() /* Example 6.1 */ +{ + int *ptr1, xxx; + int old, new; + int *savptr; + + xxx = 9; + ptr1 = &xxx; /* ptr1 contains address of xxx */ + printf("ptr1 = %x \n",ptr1); + + /* get the contents of what is pointed to by ptr1 */ + old = *ptr1; /* old now has the value of xxx */ + printf("old = %d \n",old); + + new = *ptr1 + 1; /* new contains xxx + 1 */ + printf("new = %d\n",new); + + savptr = ptr1; /* save the old address */ + printf("savptr = %x\n",savptr); + + ptr1 = &new; /* make ptr1 point to new */ + printf("ptr1 = %x\n",ptr1); + + old = *ptr1 + 1; /* an updated old value */ + printf("old = %d\n",old); +} + diff --git a/Mix C v251/EXAMPLES/EXAM62.C b/Mix C v251/EXAMPLES/EXAM62.C new file mode 100644 index 0000000..f57786e --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM62.C @@ -0,0 +1,22 @@ +main() /* Example 6.2 */ +{ + int var1, var2; + + var1 = 25; + var2 = 99; + printf("Variables before swap,"); + printf("var1 = %d , var2 = %d\n",var1,var2); + + swap(&var1,&var2); /* swap these variables */ + printf("Variables after swap,"); + printf("var1 = %d, var2 = %d\n",var1,var2); +} + +swap(ptr1,ptr2) + int *ptr1,*ptr2; /* arguments passed by reference */ +{ + int temp; + temp = *ptr1; /* save value of 1st argument */ + *ptr1 = *ptr2; /* 1st argument gets value of 2nd */ + *ptr2 = temp; /* 2nd argument gets value of 1st */ +} diff --git a/Mix C v251/EXAMPLES/EXAM63.C b/Mix C v251/EXAMPLES/EXAM63.C new file mode 100644 index 0000000..ff239d0 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM63.C @@ -0,0 +1,29 @@ +#define MAX 10 /* bound of the array */ + +#include /* standard header file */ +main() /* Example 6.3 */ +{ + /* Counts occurrences of numbers in input */ + + int digit_count[MAX], i; + int char_in; /* What happens if this is type char? */ + + /* initialize the array to zeros */ + + for(i = 0; i < MAX; i++) + digit_count[i] = 0; + + /* find digits in input and count occurrences */ + + while ( (char_in = getchar()) != EOF) { + if (char_in >= '0' && char_in <= '9') + digit_count[char_in - '0']++; + } + + /* print out information collected */ + + for(i = 0; i < MAX; i++) + printf( + "There are %d occurrences of %d in the input\n", + digit_count[i],i); +} diff --git a/Mix C v251/EXAMPLES/EXAM64.C b/Mix C v251/EXAMPLES/EXAM64.C new file mode 100644 index 0000000..549624a --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM64.C @@ -0,0 +1,13 @@ +main() /* Example 6.4 */ +{ + char name[45], first[15], middle[15], last[15]; + char *format; + strcpy(first, "Billy "); + strcpy(middle, "Bob "); + strcpy(last, "Texas "); + strcat(name, first); + strcat(name, middle); + strcat(name, last); + format = "The complete name is %s\n"; + printf(format, name); +} diff --git a/Mix C v251/EXAMPLES/EXAM65.C b/Mix C v251/EXAMPLES/EXAM65.C new file mode 100644 index 0000000..a7fb38c --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM65.C @@ -0,0 +1,10 @@ +#include +#define MAXLINE 81 /* 80 characters plus NULL terminator */ +main() /* Example 6.5 */ +{ + char line[MAXLINE]; + + printf("File copy from stdin to stdout\n"); + printf("Input terminates with EOF"); + while(gets(line) != NULL) puts(line); +} diff --git a/Mix C v251/EXAMPLES/EXAM66.C b/Mix C v251/EXAMPLES/EXAM66.C new file mode 100644 index 0000000..1b6fad2 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM66.C @@ -0,0 +1,31 @@ +#define MAX 10 +main() /* Example 6.6 */ +{ + float count[MAX]; + float *ptr; + int *iptr; + int icount[MAX], i; + + /* initialize both arrays */ + for (i=0; i < MAX; i++) { + count[i] = 0.3; + icount[i] = 1; + } + + /* make ptr point to sixth element of array */ + ptr = &count[5]; + + *ptr = 1.; /* sets count[5] to 1. */ + *(ptr - 1) = .9; /* sets count[4] to .9 */ + *(ptr + 1) = 1.1; /* sets count[6] to 1.1 */ + + iptr = &icount[5]; + *iptr = 0; /* sets count[5] to 0 */ + *(iptr - 1) = -1; /* sets count[4] to -1 */ + *(iptr + 1) = 2; /* sets count[6] to 2 */ + + for (i=0; i < MAX; i++) { /* print both arrays */ + printf("count[%d] = %f ",i,*(count+i)); + printf("icount[%d] = %d\n",i,icount[i]); + } +} diff --git a/Mix C v251/EXAMPLES/EXAM67.C b/Mix C v251/EXAMPLES/EXAM67.C new file mode 100644 index 0000000..d9d0e64 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM67.C @@ -0,0 +1,59 @@ +/* NO_OF_NAMES is the maximum number of names */ +/* SIZE is the maximum number of characters in each name */ + +#define NO_OF_NAMES 50 +#define SIZE 31 + +main() +{ /* Example 6.7 */ + /* counter */ + int i; + /* number of names read */ + int number; + /* 2 dimension array of names */ + char name[NO_OF_NAMES][SIZE]; + /* array of pointers to names */ + char *nameptr[NO_OF_NAMES]; + + /* read the names into the two dimension array */ + printf("--- Enter one name per line, "); + printf(" EOF to terminate ---\n"); + for (number=0; gets(name[number]) && + number 0) { + notsorted = TRUE; + ptr = names[i]; /* swap the two pointers */ + names[i] = names[i+1]; + names[i+1] = ptr; + } + } +} diff --git a/Mix C v251/EXAMPLES/EXAM68.C b/Mix C v251/EXAMPLES/EXAM68.C new file mode 100644 index 0000000..a1d91a0 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM68.C @@ -0,0 +1,19 @@ +#define PI 3.141592654 + +main() /* Example 6.8 */ +{ + int i; + double sin(); /* standard library function for sine */ + double cos(); /* standard library function for cosine */ + double log(); /* standard library function for logarithm */ + + /* 3 element array of pointers to functions returning double */ + double (*trig_function[3])(); + + trig_function[0] = sin; + trig_function[1] = cos; + trig_function[2] = log; + + /* print the values of sin(PI), cos(PI), and log(PI) */ + for (i=0; i<3; i++) printf("%f\n", (*trig_function[i])(PI)); +} diff --git a/Mix C v251/EXAMPLES/EXAM69.C b/Mix C v251/EXAMPLES/EXAM69.C new file mode 100644 index 0000000..8f38fd8 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM69.C @@ -0,0 +1,108 @@ +#include + +main() /* Example 6.9 */ +{ + double amount, rate, factor; + int option, months; + int i; + + /* declaration of functions */ + int present_value(); /* present value of future payment */ + int future_value(); /* future value of present payment */ + int monthly(); /* monthly installments on a present payment */ + int monthly_value(); /* present value of monthly installments */ + + /* declaration of array of pointers to functions */ + int (*function[4])() = {present_value, future_value, + monthly, monthly_value}; + + menu(&option,&months,&rate,&amount); /* display menu */ + + for(;;) { /* loop forever */ + for (factor = 1+rate, i = 1; i < months; i++) + factor = factor * (1 + rate); + /* call appropriate function */ + (*function[option])(factor, amount, rate, months); + menu(&option,&months,&rate,&amount); + } +} + +menu(option,months,rate,amount) /* display menu and collect + the input values */ +int *option, *months; +double *rate, *amount; +{ + printf( + "\n1) present value of a single future payment\n"); + printf( + "2) future value of a single present payment\n"); + printf( + "3) monthly installments on a present payment\n"); + printf( + "4) present value of monthly installments\n"); + printf( + "5) <>\n\n"); + printf(" enter option --> "); + scanf("%d",option); + if (*option<0 || *option>4) exit(0); /* exit program */ + else (*option)--; /* make option 0..3 */ + printf("\nenter # of months --> "); + scanf("%d",months); + printf("enter annual interest rate (%%) --> "); + scanf("%lf",rate); + *rate = *rate/1200; + printf("enter dollar amount --> "); + scanf("%lf%*c",amount); +} + +present_value(factor, amount) +double factor, amount; +{ + printf("the present value is %9.2f\n", amount/factor); +} + +future_value(factor, amount) +double factor, amount; +{ + printf("the future value is %9.2f\n", amount*factor); +} + +monthly(factor, amount, rate, months) +double factor, amount, rate; +int months; +{ + int answer, i; + double result; + double accum_interest = 0; + double owed, interest, principle; + + if (rate == 0) result = amount/months; + else result = factor * amount * rate / (factor-1); + printf("the monthly payment is %9.2f\n", result); + printf("do you wish to see the table (y or n)? "); + answer = getchar(); + putchar('\n'); + if( answer == 'y' || answer == 'Y') { + accum_interest = 0; + owed = amount; + for (i = 1; i<=months; i++) { + interest = rate * owed; + principle = result - interest; + owed = owed - principle; + accum_interest = accum_interest + interest; + printf(" month principle interest "); + printf(" amount owed accumulated interest\n"); + printf(" %2d %9.2f %9.2f", + i,principle,interest); + printf(" %9.2f %9.2f\n", + owed,accum_interest); + } + } +} + +monthly_value(factor, amount, rate) +double factor, amount, rate; +{ + printf("the present value is %9.2f\n", + rate == 0 ? amount : amount * (factor-1)/(rate * factor)); +} diff --git a/Mix C v251/EXAMPLES/EXAM71.C b/Mix C v251/EXAMPLES/EXAM71.C new file mode 100644 index 0000000..c11c443 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM71.C @@ -0,0 +1,31 @@ +int a1 = 1; /* global variable a1 */ + +main() /* Example 7.1 */ +{ + a1 = 2; /* changes the global value */ + printf("a1 inside main function = %d\n",a1); + next(); + printf("After call to next, a1 = %d\n",a1); + next1(); + printf("After call to next1, a1 = %d\n",a1); +} + +int b1; /* b1 is extern int */ + +next() +{ + char a1; /* in next a1 is auto character */ + a1 = 'a'; + printf("a1 inside next function = %c\n",a1); + b1 = 77; + printf("b1 inside next function = %d\n",b1); +} + +next1() +{ + float b1; /* b1 is auto char */ + b1 = 19.3; + printf("a1 inside next1 function = %d\n",a1); + printf("b1 inside next1 function = %6.2f\n",b1); + a1 = 13; +} diff --git a/Mix C v251/EXAMPLES/EXAM72A.C b/Mix C v251/EXAMPLES/EXAM72A.C new file mode 100644 index 0000000..8cea1cd --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM72A.C @@ -0,0 +1,11 @@ +int a1 = 1; /* global variable a1 */ + +main() /* Example 7.2 - part A */ +{ + a1 = 2; /* changes the global value */ + printf("a1 inside main function = %d\n",a1); + next(); + printf("After call to next, a1 = %d\n",a1); + next1(); + printf("After call to next1, a1 = %d\n",a1); +} diff --git a/Mix C v251/EXAMPLES/EXAM72B.C b/Mix C v251/EXAMPLES/EXAM72B.C new file mode 100644 index 0000000..1dbac77 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM72B.C @@ -0,0 +1,11 @@ +int b1; /* b1 is extern int */ + +next() /* Example 7.2 - part B */ +{ + char a1; /* in next a1 is auto character */ + a1 = 'a'; + printf("a1 inside next function = '%c'\n",a1); + b1 = 77; + printf("b1 inside next function = %d\n",b1); +} + diff --git a/Mix C v251/EXAMPLES/EXAM72C.C b/Mix C v251/EXAMPLES/EXAM72C.C new file mode 100644 index 0000000..638d9bd --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM72C.C @@ -0,0 +1,9 @@ +next1() /* Example 7.2 - part C */ +{ + extern int a1; + float b1; /* b1 is auto float */ + b1 = 19.3; + printf("a1 inside next1 function = %d\n",a1); + printf("b1 inside next1 function = %6.2f\n",b1); + a1 = 13; +} diff --git a/Mix C v251/EXAMPLES/EXAM73A.C b/Mix C v251/EXAMPLES/EXAM73A.C new file mode 100644 index 0000000..65bc483 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM73A.C @@ -0,0 +1,44 @@ +/* Example 7.3 - part A */ + +#define STKMAX 20 /* maximum size of the stack */ + +static topptr = 0; /* stack pointer */ +static stack[STKMAX]; /* the stack itself */ + +push(val1) /* push value onto stack */ +int val1; +{ + if (topptr < STKMAX) + return( stack[topptr++] = val1); + else { + printf("Error - stack overflow\n"); + return(-1); + } +} + +pop () +{ + if (topptr > 0) + return(stack[--topptr]); + else { + printf("Error - stack empty\n"); + return(-1); + } +} + +top_reset() /* reset stack */ +{ + topptr = 0; +} + +stk_lst() +{ + int count; + + printf("\nThe stack contains the following"); + printf(", starting at the top:\n"); + if (topptr == 0) printf("empty\n"); + else for(count = topptr - 1; count > -1; count--) + printf("stack[%d] = %d\n",count,stack[count]); +} + diff --git a/Mix C v251/EXAMPLES/EXAM73B.C b/Mix C v251/EXAMPLES/EXAM73B.C new file mode 100644 index 0000000..c30d4a2 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM73B.C @@ -0,0 +1,12 @@ +main() /* Example 7.3 - part B */ +{ + int count; /* test push, pop, & top_reset */ + int pop(),push(),stk_lst(),top_reset(); + + top_reset(); /* start off with fresh stack */ + stk_lst(); /* see if stack is empty */ + for(count=0; push(count)> -1; count++) ; + stk_lst(); /* list current contents */ + while (pop() > -1); + stk_lst(); /* see if stack empty */ +} diff --git a/Mix C v251/EXAMPLES/EXAM74.C b/Mix C v251/EXAMPLES/EXAM74.C new file mode 100644 index 0000000..8dc440e --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM74.C @@ -0,0 +1,22 @@ +#define MAX 5 + +main() /* Example 7.4 */ +{ + int count; + + printf("Please enter 5 numbers to be summed:\n"); + for (count = 0; count < MAX; count++) + sumit(); + printf("Program completed\n"); +} + +sumit() +{ + static sum; + int num; + + printf("Enter a number:\n"); + scanf("%d",&num); + sum += num; + printf("The total is %d \n",sum); +} diff --git a/Mix C v251/EXAMPLES/EXAM75A.C b/Mix C v251/EXAMPLES/EXAM75A.C new file mode 100644 index 0000000..872b570 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM75A.C @@ -0,0 +1,33 @@ +/* Example 7.5 - part A */ +#define TRUE 1 +#define FALSE 0 +#define MAX 100 +static long bin[MAX]; +static int bin_number = 0; +static int first = TRUE; + +static init() +{ + int i; + for(i=0; i < MAX; i++) + bin[i] = -1; + first = FALSE; +} + +allocbin(partno) +long partno; +{ + if (first) init(); + if (bin_number < MAX) + bin[bin_number++] = partno; + else + printf("Error - Out of Part Bins\n"); +} + +printbin() +{ + int i; + for (i=0; i +main() /* Example 8.1 */ +{ + struct { + int hours; + int minutes; + int temp; + } input_rec; + double convert(),ctemp; + char c; + + printf("This program calculates military time"); + printf(" and centigrade temperatures\n"); + printf("Enter current time (hh:mm) : "); + scanf("%d:%d%*c",&input_rec.hours, + &input_rec.minutes); + printf("Is it PM ? (Y or N) "); + c = getchar(); + if (c == 'Y' || c == 'y') + input_rec.hours = input_rec.hours + 12; + printf("Enter Fahrenheit temperature : "); + scanf("%d", &input_rec.temp); + ctemp = convert(input_rec.temp); + printf("Time using 24-hr clock = %02d:%02d\n", + input_rec.hours,input_rec.minutes); + printf("Temperature is %3.1f degrees Celsius. \n", + ctemp); +} + +double convert(ftemp) +int ftemp; +{ + return((ftemp - 32.) * 5./9.); +} diff --git a/Mix C v251/EXAMPLES/EXAM82.C b/Mix C v251/EXAMPLES/EXAM82.C new file mode 100644 index 0000000..18058e7 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM82.C @@ -0,0 +1,49 @@ +struct _name { + char last[15]; + char first[15]; +}; + +struct _address { + char street[25]; + char city[15]; + char state[15]; + long zip; +}; + +struct label { + struct _name name; + struct _address address; +}; + +main() +{ + struct label customer; + getlabel(&customer); + putlabel(&customer); +} + +getlabel(customer) +struct label *customer; +{ + printf("Enter Name : "); + scanf("%s%s%*c", customer->name.first, + customer->name.last); + printf("Enter street : "); + gets(customer->address.street); + printf("Enter city, state & zip : "); + scanf("%s%s%ld%*c", customer->address.city, + customer->address.state, + &customer->address.zip); +} + +putlabel(customer) +struct label *customer; +{ + printf("\n%s %s\n%s\n%s %s %ld\n", + customer->name.first, + customer->name.last, + customer->address.street, + customer->address.city, + customer->address.state, + customer->address.zip); +} diff --git a/Mix C v251/EXAMPLES/EXAM83.C b/Mix C v251/EXAMPLES/EXAM83.C new file mode 100644 index 0000000..4870061 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM83.C @@ -0,0 +1,29 @@ +#define MAX 100 + +#include /* include standard IO header */ +#include "structs.h" /* include structure header */ +#include "labelio.c" /* include getlabel & putlabel */ +#include "sort.c" /* include sort function */ + +main() /* Example 8.3 */ +{ + struct label customer[MAX]; + int i, number = 0; + char more = 'Y'; + char *names[MAX]; + while (more == 'Y' || more == 'y') { + if (number < MAX) { + names[number] = customer[number].name.last; + getlabel(&customer[number++]); + printf("More labels? (Y/N): "); + more = getchar(); + } + else { + printf("* Maximum number of labels is %d *\n", + MAX); + break; + } + } + sort(names, number); + for (i=0; i /* include standard IO header */ +#include "structs.h" /* include structure header */ +#include "labelio.c" /* include getlabel & putlabel */ +#include "sort.c" /* include sort function */ + +main() /* Example 8.4 */ +{ + int i, number = 0; + char more = 'Y'; + char *names[MAX]; + while (more == 'Y' || more == 'y') { + if (number < MAX) { + names[number] = calloc(1, sizeof(struct label)); + if (names[number] != NULL) + getlabel(names[number++]); + else { + printf("<<< Out of Memory >>>\n"); + break; + } + printf("More labels? (Y/N): "); + more = getchar(); + } + else { + printf("<<< Maximum No. of Labels is %d >>>\n", + MAX); + break; + } + } + sort(names, number); + for (i=0; i /* include standard IO header */ +#include "struct2.h" /* include structure header */ +#include "labelio.c" /* include getlabel & putlabel */ + +menu(option) +char *option; +{ + puts("\n\nA) add a label"); + puts("D) delete a label"); + puts("P) print labels"); + puts("Q) quit\n"); + printf(" Enter option --> "); + scanf("%c%*c", option); +} + +struct label *first_label; + +main() +{ + char option; + for(;;) { /* loop forever */ + menu(&option); + switch (toupper(option)) { + case 'A' : add_label() ; break; + case 'D' : delete_label(); break; + case 'P' : print_labels(); break; + case 'Q' : exit(0); + default : puts("*** invalid option ***"); + } + } +} + +add_label() +{ + struct label *new_label, *current_label; + new_label = calloc(1, sizeof(struct label)); + if (new_label != NULL) { + new_label->next = NULL; + getlabel(new_label); + if (first_label == NULL) first_label = new_label; + else { + current_label = first_label; + while (current_label->next != NULL) + current_label = current_label->next; + current_label->next = new_label; + } + } + else printf("<<< Out of Memory >>>\n"); +} + +delete_label() +{ + struct label *current_label, *previous_label; + char first[15], last[15]; + printf("Enter the name : "); + scanf("%s%s%*c", first, last); + current_label = first_label; + while (current_label != NULL) { + if (strcmp(current_label->name.last, last) == 0 && + strcmp(current_label->name.first, first) == 0) break; + else { + previous_label = current_label; + current_label = current_label->next; + } + } + if (current_label == NULL) puts("*** Label not found ***"); + else { + if (current_label == first_label) + first_label = first_label->next; + else + previous_label->next = current_label->next; + cfree(current_label); + } +} + +print_labels() +{ + struct label *current_label; + current_label = first_label; + puts("\n"); + while (current_label != NULL) { + putlabel(current_label); + current_label = current_label->next; + } +} diff --git a/Mix C v251/EXAMPLES/EXAM86.C b/Mix C v251/EXAMPLES/EXAM86.C new file mode 100644 index 0000000..1f4d17c --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM86.C @@ -0,0 +1,22 @@ +add_label() +{ struct label *new_label, *current_label, *previous_label; + new_label = calloc(1, sizeof(struct label)); + if (new_label != NULL) { + getlabel(new_label); + new_label->next = NULL; + if (first_label == NULL) first_label = new_label; + else { + current_label = first_label; + while (current_label != NULL && + strcmp(new_label->name.last, + current_label->name.last) > 0) { + previous_label = current_label; + current_label = current_label->next; } + if (current_label == first_label) + first_label = new_label; + else previous_label->next = new_label; + new_label->next = current_label; + } + } + else printf("<<< Out of Memory >>>\n"); +} diff --git a/Mix C v251/EXAMPLES/EXAM91.C b/Mix C v251/EXAMPLES/EXAM91.C new file mode 100644 index 0000000..b86ad54 --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM91.C @@ -0,0 +1,30 @@ +#include + +main() +{ + FILE *input_file, *output_file; + char input_name[15], output_name[15]; + int c; + + puts("*** File Copy Program ***"); + + printf("Enter the name of the input file : "); + scanf("%s", input_name); + printf("Enter the name of the output file : "); + scanf("%s", output_name); + + input_file = fopen(input_name, "r"); + if (input_file == NULL) { + puts("*** Can't open input file ***"); + exit(0); + } + + output_file = fopen(output_name, "w"); + if (output_file == NULL) { + puts("*** Can't open output file ***"); + exit(0); + } + + while ((c = getc(input_file)) != EOF) + putc(c, output_file); +} diff --git a/Mix C v251/EXAMPLES/EXAM92.C b/Mix C v251/EXAMPLES/EXAM92.C new file mode 100644 index 0000000..a5559fb --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM92.C @@ -0,0 +1,28 @@ +#include +#define BUFSIZE 81 + +main() +{ + FILE *input_file, *output_file; + char input_name[15], output_name[15], buffer[BUFSIZE]; + int c; + fputs("*** File Copy Program ***\n", stdout); + + fprintf(stdout, "Enter the name of the input file : "); + fscanf(stdin, "%s", input_name); + fprintf(stdout, "Enter the name of the output file : "); + fscanf(stdin, "%s", output_name); + + input_file = fopen(input_name, "r"); + if (input_file == NULL) { + fputs("*** Can't open input file ***", stdout); + exit(0); + } + output_file = fopen(output_name, "w"); + if (output_file == NULL) { + fputs("*** Can't open output file ***", stdout); + exit(0); + } + while (fgets(buffer, BUFSIZE, input_file) != NULL) + fputs(buffer, output_file); +} diff --git a/Mix C v251/EXAMPLES/EXAM93.C b/Mix C v251/EXAMPLES/EXAM93.C new file mode 100644 index 0000000..dfa148a --- /dev/null +++ b/Mix C v251/EXAMPLES/EXAM93.C @@ -0,0 +1,94 @@ +#define MAX 100 +#include /* include standard IO header */ +#include "sort.c" /* include sort function */ + +typedef struct { + char last[15]; + char first[15]; + } NAME; + +typedef struct { + char street[25]; + char city[15]; + char state[15]; + long zip; + } ADDRESS; + +typedef struct { + NAME name; + ADDRESS address; + } LABEL; + +getlabel(fp, customer) +FILE *fp; +LABEL *customer; +{ + if (fp == stdin) printf("Enter Name : "); + fscanf(fp, "%s%s%*c", customer->name.first, + customer->name.last); + if (fp == stdin) printf("Enter street : "); + fgets(customer->address.street, 25, fp); + if (fp == stdin) printf("Enter city, state & zip : "); + return fscanf(fp, "%s%s%ld%*c", customer->address.city, + customer->address.state, + &customer->address.zip); +} + +putlabel(fp, customer) +FILE *fp; +LABEL *customer; +{ + fprintf(fp, "\n%s %s\n%s%s %s %ld\n", + customer->name.first, + customer->name.last, + customer->address.street, + customer->address.city, + customer->address.state, + customer->address.zip); +} + +main() +{ + int i, number = 0; + char more = 'Y', *names[MAX], in_name[15], out_name[15]; + FILE *in_file, *out_file; + puts("*** Label Sorting Program ***"); + puts("press the enter key to map files to terminal\n"); + printf("Input file containing unsorted labels : "); + gets(in_name); + printf("Output file to contain sorted labels : "); + gets(out_name); + if (strlen(in_name) == 0) in_file = stdin; + else { + in_file = fopen(in_name, "r"); + if (in_file == NULL) + {printf("Can't open input: %s", in_name); exit(0);} + } + if (strlen(out_name) == 0) out_file = stdout; + else { + out_file = fopen(out_name, "w"); + if (out_file == NULL) { + printf("Can't open output: %s", out_name); exit(0); + } + } + while (more == 'Y' || more == 'y') { + if (number < MAX) { + names[number] = calloc(1, sizeof(LABEL)); + if (names[number] != NULL) { + if (getlabel(in_file, names[number]) == EOF) + {cfree(names[number]); break;} + ++number; + if (in_file == stdin) { + printf("More labels? (Y/N): "); + more = getchar(); + } + } + else {printf("<<< Out of Memory >>>\n"); break;} + } + else + {printf("<<< Maximum No. of Labels is %d >>>\n", + MAX); break;} + } + sort(names, number); + for (i=0; i + +main() +{ + char input_name[15], output_name[15]; + + puts("*** Append File Example ***"); + fputs("Enter the input file name : ", stdout); + gets(input_name); + fputs("Enter the output file name: ", stdout); + gets(output_name); + if (append(input_name, output_name) == NULL) + puts("Dismal Failure"); + else + puts("Extremely Successful"); +} + +append(name1, name2) +char *name1, *name2; +{ + #define FAILURE 0 + #define SUCCESS 1 + FILE *input, *output; + int c; + + input = fopen(name1, "r"); + output = fopen(name2, "a"); + if (input == NULL) { + fprintf(stderr, "Can't open input: %s\n", name1); + return FAILURE; + } + if (output == NULL) { + fprintf(stderr, "Can't open output: %s\n", name2); + return FAILURE; + } + while ((c = getc(input)) != EOF) putc(c, output); + fclose(input); + fclose(output); + return SUCCESS; +} diff --git a/Mix C v251/EXAMPLES/FILE1.C b/Mix C v251/EXAMPLES/FILE1.C new file mode 100644 index 0000000..810e9d6 --- /dev/null +++ b/Mix C v251/EXAMPLES/FILE1.C @@ -0,0 +1,7 @@ +/* common preprocessor definitions from FILE1.C */ +#define FALSE 0 +#define TRUE 1 +#define YES 1 +#define NULL '\0' +#define ALL 1 + diff --git a/Mix C v251/EXAMPLES/FILE2.C b/Mix C v251/EXAMPLES/FILE2.C new file mode 100644 index 0000000..d76e523 --- /dev/null +++ b/Mix C v251/EXAMPLES/FILE2.C @@ -0,0 +1,5 @@ +/* common declarations from FILE2.C */ +int one,two,three; +char a,b,c; +float flt1,flt2; + diff --git a/Mix C v251/EXAMPLES/LABELIO.C b/Mix C v251/EXAMPLES/LABELIO.C new file mode 100644 index 0000000..81d57e1 --- /dev/null +++ b/Mix C v251/EXAMPLES/LABELIO.C @@ -0,0 +1,27 @@ + +getlabel(customer) +struct label *customer; +{ + printf("Enter Name : "); + scanf("%s%s%*c", customer->name.first, + customer->name.last); + printf("Enter street : "); + gets(customer->address.street); + printf("Enter city, state & zip : "); + scanf("%s%s%ld%*c", customer->address.city, + customer->address.state, + &customer->address.zip); +} + +putlabel(customer) +struct label *customer; +{ + printf("\n%s %s\n%s\n%s %s %ld\n", + customer->name.first, + customer->name.last, + customer->address.street, + customer->address.city, + customer->address.state, + customer->address.zip); +} + diff --git a/Mix C v251/EXAMPLES/SORT.C b/Mix C v251/EXAMPLES/SORT.C new file mode 100644 index 0000000..f27568d --- /dev/null +++ b/Mix C v251/EXAMPLES/SORT.C @@ -0,0 +1,26 @@ + +sort(names, number) +char *names[]; /* array of pointers to names */ +int number; /* number of names */ +{ + #define TRUE 1 + #define FALSE 0 + int notsorted = TRUE; + int i; + char *ptr; + + /* sort the names by sorting the array of pointers */ + /* sort using a bubble sort algorithm */ + --number; + while (notsorted) { + notsorted = FALSE; + for (i=0; i 0) { + notsorted = TRUE; + ptr = names[i]; /* swap the two pointers */ + names[i] = names[i+1]; + names[i+1] = ptr; + } + } +} + diff --git a/Mix C v251/EXAMPLES/STRUCT2.H b/Mix C v251/EXAMPLES/STRUCT2.H new file mode 100644 index 0000000..06ecf1c --- /dev/null +++ b/Mix C v251/EXAMPLES/STRUCT2.H @@ -0,0 +1,17 @@ +struct _name { + char last[15]; + char first[15]; +}; + +struct _address { + char street[25]; + char city[15]; + char state[15]; + long zip; +}; + +struct label { + struct _name name; + struct _address address; + struct label *next; +}; diff --git a/Mix C v251/EXAMPLES/STRUCTS.H b/Mix C v251/EXAMPLES/STRUCTS.H new file mode 100644 index 0000000..40fabfe --- /dev/null +++ b/Mix C v251/EXAMPLES/STRUCTS.H @@ -0,0 +1,17 @@ +struct _name { + char last[15]; + char first[15]; +}; + +struct _address { + char street[25]; + char city[15]; + char state[15]; + long zip; +}; + +struct label { + struct _name name; + struct _address address; +}; + diff --git a/Mix C v251/JUMP.C b/Mix C v251/JUMP.C new file mode 100644 index 0000000..c2f1db2 --- /dev/null +++ b/Mix C v251/JUMP.C @@ -0,0 +1,47 @@ +#include + +jmp_buf env; + +main() +{ + puts("Example of using setjmp and longjmp"); + + for (;;) { + switch (setjmp(env)) { + case 0: func1(3); + case 1: puts("returned from func1"); break; + case 2: puts("returned from func2"); func1(1); + case 3: puts("returned from func3"); func1(2); + default: puts("never prints this message"); + } + puts("----------------------------"); + } + + exit(0); +} + +func1(val) +int val; +{ + puts("inside func1"); + if (val == 1) longjmp(env, 1); + else func2(val); + puts("never prints this message"); +} + +func2(val) +int val; +{ + puts("inside func2"); + if (val == 2) longjmp(env, 2); + else func3(val); + puts("never prints this message"); +} + +func3(val) +int val; +{ + puts("inside func3"); + longjmp(env, 3); + puts("never prints this message"); +} diff --git a/Mix C v251/LINKER.COM b/Mix C v251/LINKER.COM new file mode 100644 index 0000000..daa25e7 Binary files /dev/null and b/Mix C v251/LINKER.COM differ diff --git a/Mix C v251/MATRIX.C b/Mix C v251/MATRIX.C new file mode 100644 index 0000000..7af98fc --- /dev/null +++ b/Mix C v251/MATRIX.C @@ -0,0 +1,110 @@ +main() +{ + long float f, t, x[4][4], y[4][4]; + long float x1=1.2; + long float x3, x2=.76; + int n, a, n1, n2, n3, k9, k6, k8; + n=0; + /* x3 is for testing */ + x3 = x1/x2; + x3 = 9.9999987654321e60; + printf("M A T R I X I N V E R S I O N"); + k6=0; + for(k8=0;;k8++) { + if(k8==10) break; +/*-slash is at left hand end */ + if(k8<5); else k6++; + } + k8=0; while(1) { + if(k8==10) break; + k8++; + } + for(k8=0; ;k8++) if(k8==10) break; + k8=0;while(1) { + k8++; + if(k8>10) break; + } + +/* this is the real start of the matrix inversion */ + + for(k9=0; + ; + k9++) { + a=4; + n++; + if(n==1) + n=n; + if(n==2) + n=n; + if(n==3) + n=n; + if(n==4) + n=n; + if(n==5) + n=n; + if(n==6) + n=n; + if(n==7) + n=n; + if(n==8) + n=n; + if(n==9) + n=n; + if(n==10) + n=n; + if(n==50) + n=n; + x[0][0]=0.0; + x[0][0]=1.123456789111111111111; + x[0][0]=1.23456789;x[0][0]=12.3456789; + printf("\n\n\nRun number is %d",n); + x[0][0]= 1.;x[0][1]=.04;x[0][2]=.03;x[0][3]=.02; + x[1][0]= .02;x[1][1]=1.;x[1][2]=.03;x[1][3]=.01; + x[2][0]= .01;x[2][1]=.01;x[2][2]=1.;x[2][3]=.01; + x[3][0]=-.02;x[3][1]=.02;x[3][2]=.03;x[3][3]=1.; + printf("\n\nThe X matrix is"); + for(n1=0;n1n1) + x[n2][n3] = x[n2][n3]-f*x[n1][n3]; + if(n3<=n1) + y[n2][n3] = y[n2][n3]-f*y[n1][n3]; + } + } + } + } + for(n1=0;n1length; + bufptr = dynamic->string; + break; + + default : /* format string character */ + if ((*putc)(c, fp) != EOF) continue; + else return EOF; + } + if (precision >= 0 && length > precision) length = precision; + width = width - length; + if (!ljflag) { + if (padchar == '0' && *bufptr == '-') { + --length; + if ((*putc)(*bufptr++, fp) == EOF) return EOF; + } + while (width-- > 0) + if ((*putc)(padchar, fp) == EOF) return EOF; + } + if ((*write)(fp,bufptr,length) == EOF) return EOF; + while (width-- > 0) + if ((*putc)(padchar,fp) == EOF) return EOF; + --parms; + } + return 0; +} + +_eint(iptr, bufptr, base, sf) /* encode an integer*/ +int *iptr; /* pointer to integer */ +char *bufptr; /* pointer to encode buffer */ +int base; /* number base */ +int sf; /* signed or unsigned flag */ +{ + if (*iptr < 0 && sf) { + *bufptr++ = '-'; + *iptr = -*iptr; + } + _itoa(*iptr, &bufptr, base); /* convert integer to string */ + *bufptr = '\0'; +} + +_elong(lptr, bufptr, base, sf) /* encode a long */ +long *lptr; /* pointer to long */ +char *bufptr; /* pointer to encode buffer */ +int base; /* number base */ +int sf; /* sign flag */ +{ + if (*lptr < 0 && sf) { + *bufptr++ = '-'; + *lptr = -*lptr; + } + _ltostr(*lptr, &bufptr, base); /* convert long to string */ + *bufptr = '\0'; +} + +_itoa(n, bufptr, base) /* integer encode routine */ +unsigned n; /* unsigned integer */ +char **bufptr; /* pointer to the buffer pointer */ +int base; /* number base */ +{ + if (n < base) { + *(*bufptr)++ = (n < 10) ? n + 48 : n + 55; + return; + } + _itoa(n/base, bufptr, base); + _itoa(n%base, bufptr, base); +} + +_ltostr(lptr, bufptr, base) /* long encode routine */ +long lptr; /* long integer */ +char **bufptr; /* pointer to the buffer pointer */ +int base; /* number base */ +{ + if (lptr < base) { + *(*bufptr)++ = (lptr < 10) ? lptr + '0' : lptr + 55; + return; + } + _ltostr(lptr/base, bufptr, base); + _ltostr(lptr%base, bufptr, base); +} + +_edouble(dptr, bufptr, flag, left, right) /* encode a double */ +double *dptr; /* pointer to double */ +char *bufptr; /* pointer to encode buffer */ +int flag; /* encode format flag */ +int left; /* # of digits left of decimal */ +int right; /* # of digits right of decimal */ +{ + ftoa(*dptr, bufptr, flag, left, right); +} + +_mputc(c, fp) /* write c to fp */ +int c; +char **fp; +{ + *(*fp)++ = c; + return c; +} + +_mwrite(fp, s, n) +char **fp; +char *s; +int n; +{ + int i; + for (i=0; i < n; i++) *(*fp)++ = *s++; + return n; +} + +_getformat(dptr) /* get format flag */ +double *dptr; /* for _edouble */ +{ + if (*dptr < 100000.0) return 2; /* fixed format */ + else return 3; /* exponential format */ +} + diff --git a/Mix C v251/RUNTIME.OVY b/Mix C v251/RUNTIME.OVY new file mode 100644 index 0000000..f478bd7 Binary files /dev/null and b/Mix C v251/RUNTIME.OVY differ diff --git a/Mix C v251/SCANF.C b/Mix C v251/SCANF.C new file mode 100644 index 0000000..d84f5b2 --- /dev/null +++ b/Mix C v251/SCANF.C @@ -0,0 +1,350 @@ +#include "stdio" + +scanf(fs) /* standard routine */ +char *fs; +{ + int getc(); + int ungetc(); + return _input(stdin, &fs, getc, ungetc); +} + +fscanf(fp, fs) /* standard routine */ +char *fp, *fs; +{ + int getc(); + int ungetc(); + return _input(fp, &fs, getc, ungetc); +} + +sscanf(s,fs) /* standard routine */ +char *s, *fs; +{ + int _mread(); + int _mungetc(); + return _input(&s, &fs, _mread, _mungetc); +} + +_input(fp, parms, read, ungetc) /* input routine for */ + /* scanf, sscanf, fscanf */ +char *fp; /* pointer to input */ +char **parms; /* pointer to addr of parameters */ +int (*read)(); /* character read function */ +int (*ungetc)(); /* character unget function */ +{ + union { /* buffer for numeric & string storage */ + int integer; + char string[81]; + } buffer; + char *format; /* format string pointer */ + char c; /* format string character */ + char **ptr; /* pointer to pointer */ + int base; /* number base */ + int value; /* binary value of digit */ + int pflag; /* real precision flag */ + int asflag; /* assignment supression flag */ + int sflag; /* sign flag */ + int ch; /* input character */ + int temp; /* temporary storage */ + int i; /* loop counter */ + int count; /* character count */ + int width; /* maximum field width */ + int status; /* status returned from the read */ + int valid; /* valid real number format */ + int (*acc)(); /* pointer to accumulate function */ + int (*neg)(); /* pointer to negate function */ + int (*asn)(); /* pointer to assign function */ + int _digit(); /* decode digit function */ + int _dint(); /* decode integer function */ + int _dlong(); /* decode long function */ + int _negint(); /* negate integer function */ + int _neglong(); /* negate long function */ + int _aint(); /* assign integer function */ + int _along(); /* assign long function */ + int _atoi(); /* convert string to integer */ + int isspace(); /* whitespace function */ + int isdigit(); /* digit function */ + STRING *stods(); /* convert string to dynamic string */ + + format = *parms--; /* format points to format string */ + count = 0; /* number of parameters read */ + while (c = *format++) { + if (isspace(c)) continue; /* skip white space */ + if (c != '%') { + while (isspace(ch = (*read)(fp))); + if (ch == c) continue; + else return (ch == EOF) ? EOF : count; + } + pflag = sflag = asflag = 0; + if ((*format) == '*') { /* check for assignment suppression */ + ++asflag; + ++format; + } + acc = _dint; + neg = _negint; + asn = _aint; + width = isdigit(*format) ? _atoi(&format) : -1; + if (toupper(*format) == 'L') { /* check for double precision */ + ++pflag; + ++format; + acc = _dlong; + neg = _neglong; + asn = _along; + } + c = *format++; + switch (toupper(c)) { + + case 'H': /* signed decimal */ + pflag++; + case 'D': + case 'U': + base = 10; + goto decode; + + case 'O': /* unsigned octal */ + base = 8; + goto decode; + + case 'X': /* unsigned hexadecimal */ + base = 16; + decode: + for (i=0; i<5; ++i) buffer.string[i] = '\0'; + while ((ch=(*read)(fp))==' ' || ch == '\t' + || ch == '\n'); + if (ch == EOF) return EOF; + if (width && (ch == '+' || ch == '-')) { + if (ch == '-') ++sflag; + ch = (*read)(fp); + --width; + } + if (width && base == 16 && ch == '0') { + temp = (*read)(fp); + --width; + if (toupper(temp) == 'X' && width) { + ch = (*read)(fp); + --width; + } + else { + (*ungetc)(temp, fp); + ++width; + } + } + if ((value = _digit(ch, base)) == -1) return count; + while (width && (value != -1)) { + (*acc)(&buffer.integer, value, base); + ch = (*read)(fp); + --width; + value = _digit(ch,base); + } + (*ungetc)(ch, fp); + if (sflag) (*neg)(&buffer.integer); + if (!asflag) { + (*asn)(&buffer.integer, *parms, pflag); + ++count; + --parms; + } + break; + + case 'S': /* string */ + while ((ch=(*read)(fp))==' ' || ch == '\t' + || ch == '\n'); + if (ch == EOF) return EOF; + while (width && ch != ' ' && ch != '\t' + && ch != '\n' && ch != EOF) { + if (!asflag) *(*parms)++ = ch; + ch = (*read)(fp); + --width; + } + (*ungetc)(ch, fp); + if (!asflag) { + *(*parms) = '\0'; + ++count; + --parms; + } + break; + + case 'C': /* character */ + if ((ch = (*read)(fp)) == EOF) return EOF; + else + if (!asflag) { + *(*parms) = ch; + ++count; + --parms; + } + break; + + case 'E': + case 'F': /* floating point */ + while ((ch=(*read)(fp))==' ' || ch == '\t' + || ch == '\n'); + if (ch == EOF) return EOF; + i = 0; + valid = 0; + if (width && (ch == '+' || ch == '-')) { + buffer.string[i++] = ch; + ch = (*read)(fp); + --width; + } + while (width && ch >= '0' && ch <= '9') { + buffer.string[i++] = ch; + ch = (*read)(fp); + --width; + ++valid; + } + if (width && ch == '.') { + buffer.string[i++] = ch; + ch = (*read)(fp); + --width; + while (width && ch >= '0' && ch <= '9') { + buffer.string[i++] = ch; + ch = (*read)(fp); + --width; + ++valid; + } + } + if (width && (ch == 'E' || ch == 'e' || ch == 'D')) { + if (!valid) return count; + valid = 0; + buffer.string[i++] = 'E'; + ch = (*read)(fp); + --width; + if (width && (ch == '+' || ch == '-')) { + buffer.string[i++] = ch; + ch = (*read)(fp); + --width; + } + while (width && isdigit(ch)) { + buffer.string[i++] = ch; + ch = (*read)(fp); + --width; + ++valid; + } + } + (*ungetc)(ch, fp); + if (!valid) return count; + buffer.string[i] = '\0'; + if (!asflag) { + _dreal(buffer.string, *parms, pflag); + ++count; + --parms; + } + break; + + case 'Y': /* dynamic string */ + while ((ch=(*read)(fp))==' ' || ch == '\t' + || ch == '\n'); + if (ch == EOF) return EOF; + i = 0; + while (width && ch != ' ' && ch != '\t' + && ch != '\n' && ch != EOF) { + if (i < 80) buffer.string[i++] = ch; + ch = (*read)(fp); + --width; + } + buffer.string[i] = '\0'; + (*ungetc)(ch,fp); + if (!asflag) { + ptr = *parms; + *ptr = stods(buffer.string); + ++count; + --parms; + } + break; + + default : + return count; + } + } + return count; +} + +_negint(iptr) /* negate integer */ +int *iptr; /* pointer to integer */ +{ + *iptr = -*iptr; +} + +_aint(iptr1, iptr2, pflag) /* assign integer */ +int *iptr1; /* pointer to fp */ +int *iptr2; /* pointer to destination */ +int pflag; /* short integer flag */ +{ + short *sptr; + if (pflag) { + sptr = iptr2; + *sptr = *iptr1; + } + else *iptr2 = *iptr1; +} + +_neglong(lptr) /* negate long */ +long *lptr; /* pointer to long integer */ +{ + *lptr = -*lptr; +} + +_along(lptr1, lptr2) /* assign long */ +long *lptr1; /* pointer to fp */ +long *lptr2; /* pointer to destination */ +{ + *lptr2 = *lptr1; +} + +_digit(ch, base) /* decode ch to binary */ +int ch; /* character to decode */ +int base; /* number base */ +{ + if (ch >= '0' && ch <= '9') ch -= 48; + else + if (isalpha(ch = toupper(ch))) ch -= 55; + else + return -1; + if (ch < base) return ch; + else return -1; +} + +_dint(iptr, digit, base) /* decode an integer */ +int *iptr; /* pointer to integer */ +int digit; /* digit to add to integer */ +int base; /* number base */ +{ + *iptr = *iptr * base + digit; +} + +_dlong(lptr, digit, base) /* decode a long */ +long *lptr; /* pointer to long */ +int digit; /* digit to add to long */ +int base; /* number base */ +{ + *lptr = *lptr * base + digit; +} + +_dreal(s, fptr, pflag) /* decode a real */ +char *s; /* pointer to decode string */ +float *fptr; /* pointer to float */ +int pflag; /* precision flag */ +{ + double atof(); /* string to double function */ + double *dptr; /* pointer to double */ + if (pflag) { + dptr = fptr; + *dptr = atof(s); + } + else + *fptr = (float) atof(s); +} + +_mread(s) /* read character from string */ +char **s; /* pointer to string */ +{ + if (*(*s) != '\0') return *(*s)++; + else return EOF; +} + +_mungetc(c,s) /* unget character to string */ +int c; /* dumy parameter */ +char **s; /* pointer to string pointer */ +{ + --(*s); + return c; +} + diff --git a/Mix C v251/SHRINK.COM b/Mix C v251/SHRINK.COM new file mode 100644 index 0000000..8b77e7f Binary files /dev/null and b/Mix C v251/SHRINK.COM differ diff --git a/Mix C v251/SIEVE.C b/Mix C v251/SIEVE.C new file mode 100644 index 0000000..236f1d4 --- /dev/null +++ b/Mix C v251/SIEVE.C @@ -0,0 +1,35 @@ +/* sieve.c */ + +/* Eratosthenes Sieve Prime Number Program in C from Byte Jan 1983 + to compare the speed. */ + +#include + +#define TRUE 1 +#define FALSE 0 +#define SIZE 8190 +typedef int bool; + +char flags[SIZE+1]; + +int main() + { + int i,k; + int prime,count,iter; + + for (iter = 1; iter <= 10; iter++) { /* do program 10 times */ + count = 0; /* initialize prime counter */ + for (i = 0; i <= SIZE; i++) /* set all flags TRUE */ + flags[i] = TRUE; + for (i = 0; i <= SIZE; i++) { + if (flags[i]) { /* found a prime */ + prime = i + i + 3; /* twice index + 3 */ + for (k = i + prime; k <= SIZE; k += prime) + flags[k] = FALSE; /* kill all multiples */ + count++; /* primes found */ + } + } + } + printf("%d primes.\n",count); /*primes found in 10th pass */ + return 0; + } diff --git a/Mix C v251/SMALLCOM.OVY b/Mix C v251/SMALLCOM.OVY new file mode 100644 index 0000000..daaa7ad Binary files /dev/null and b/Mix C v251/SMALLCOM.OVY differ diff --git a/Mix C v251/SPEEDUP.COM b/Mix C v251/SPEEDUP.COM new file mode 100644 index 0000000..4118a38 Binary files /dev/null and b/Mix C v251/SPEEDUP.COM differ diff --git a/Mix C v251/STDIO.H b/Mix C v251/STDIO.H new file mode 100644 index 0000000..ee13bf5 --- /dev/null +++ b/Mix C v251/STDIO.H @@ -0,0 +1,26 @@ +/*$no list*//*$no trace <<< start of stdio.h >>> */ + +#define MAXFILES 20 +#define BUFSIZ 512 +#define EOF -1 +#define NULL 0 +#define stdin _iob[0] +#define stdout _iob[1] +#define stderr _iob[2] +#define getchar() getc(stdin) +#define putchar(c) putc(c,stdout) + +typedef struct { + char file[32]; /* file descriptor */ + int fd; /* file descriptor number */ +} FILE; + +extern FILE *_iob[MAXFILES]; + +typedef struct { + int length; + char string[80]; +} STRING; + +/*$list *//*$trace <<< end of stdio.h >>> */ + diff --git a/Mix C v251/STDLIB.C b/Mix C v251/STDLIB.C new file mode 100644 index 0000000..0bb4f19 --- /dev/null +++ b/Mix C v251/STDLIB.C @@ -0,0 +1,1118 @@ +#include +#include + +#define MAXARGS 20 +#define FALSE 0 +#define TRUE 1 + +_initio(stdfiles) +int stdfiles; /*stdfiles = 1 says open standard files*/ +{ + extern char *_flagptr; + extern char *_procptr; + extern int _seed; + char indevice[] = ":i"; + char outdevice[] = ":o"; + char errdevice[] = ":e"; + char *STDIN, *STDOUT, *STDERR; + char cmdbuffer[128]; + char *cmdline; + char *argv[MAXARGS]; /*pointers to command line arguments*/ + char nullchar = NULL; + char mode[] = "w"; + int argc; /*number of command line arguments*/ + int i; + int count; + int fopen(); + int main(); + int exit(); + int movmem(); + int filetrap(); + int heaptrap(); + int mathtrap(); + int _getflag(); + int _getproc(); + + _flagptr = _getflag(); + _procptr = _getproc(); + _seed = 13849; + heaptrap(0); + filetrap(0); + mathtrap(0); + STDIN = indevice; + STDOUT = outdevice; + STDERR = errdevice; + for (i=0; i < MAXFILES; i++) _iob[i] = NULL; + movmem(0x80, cmdbuffer, 128); + count = cmdbuffer[0]; + cmdline = &cmdbuffer[1]; + argv[0] = &nullchar; + argc = 1; + while (count > 0 && argc < MAXARGS) { + while (*cmdline == ' ' && count) { + count--; + cmdline++; + } + if (count) { + if (*cmdline == '<') { + STDIN = ++cmdline; + count--; + } + else if (*cmdline == '>') { + cmdline++; + count--; + if (*cmdline == '>') { + cmdline++; + count--; + mode[0] = 'a'; + } + STDOUT = cmdline; + } + else { + argv[argc] = cmdline; + argc++; + } + while (*cmdline != ' ' && count) { + cmdline++; + count--; + } + *cmdline++ = '\0'; + count--; + } + } + if (stdfiles == 1) { + fopen(STDIN,"r"); + fopen(STDOUT,mode); + fopen(STDERR,"w"); + } + exit(main(argc,argv)); +} + +abs(i) +int i; +{ + if (i < 0) return -i; + else return i; +} + +atoi(s) /* decode an integer */ +char *s; /* pointer to integer string */ +{ + int sflag = 1, value = 0; + while (*s == ' ' || *s == '\t' || *s == '\n') ++s; + if (*s == '+' || *s == '-') + if (*s++ == '-') sflag = -1; + while (*s > 47 && *s < 58) value = 10 * value + (*s++ - 48); + return value * sflag; +} + +long atol(s) /* decode a long integer */ +char *s; /* pointer to integer string */ +{ + int sflag = 1; + long value = 0, base = 10; + while (*s == 32 || *s == 9 || *s == 13) ++s; + if (*s == '+' || *s == '-') + if (*s++ == '-') sflag = -1; + while (*s > 47 && *s < 58) value = base * value + (*s++ - 48); + return value * sflag; +} + +itoa(n, s) +int n; +char *s; +{ + char buffer[7], flag = '\0', *ptr; + buffer[6] = '\0'; + ptr = &buffer[5]; + if (n < 0) { + if (n == -32768) { + strcpy(s, "-32768"); + return; + } + ++flag; + n = -n; + } + do { + *(ptr--) = n % 10 + 48; + } while ((n /= 10) > 0); + if (flag) *ptr = '-'; + else ptr++; + strcpy(s, ptr); + return; +} + +ltoa(n, s) +long n; +char *s; +{ + char buffer[12], flag, *ptr; + flag = '\0'; + buffer[11] = '\0'; + ptr = &buffer[10]; + if (n < 0) { + if (n == -2147483648) { + strcpy(s, "-2147483648"); + return; + } + ++flag; + n = -n; + } + do { + *(ptr--) = n % 10 + 48; + } while ((n /= 10) > 0); + if (flag) *ptr = '-'; + else ptr++; + strcpy(s, ptr); + return; +} + +_atoi(ptr) /* decode an integer */ +char **ptr; /* pointer to addr of 1st digit */ +{ + int n = 0; + while (*(*ptr) > 47 && *(*ptr) < 58) n = 10 * n + *(*ptr)++ - 48; + return n; +} + +rand() +{ + extern long _seed; + _seed = _seed * 15790321; + return (int) ((_seed >> 17) & 0x7FFF); +} + +ungetc(c,fp) +int c; +FILE *fp; +{ + if (c != EOF) { + --fp->file[12]; + *((char *)(*(int *)&fp->file[10])+fp->file[12]) = c; + } +} + +execl(filename, args) +char *filename, *args; +{ + return _execlv(filename, &args, -1); +} + +execv(filename, args) +char *filename, **args; +{ + return _execlv(filename, args, 1); +} + +chain(filename) +char filename[]; +{ + char cmdlin[129]; + char *arg[65]; + int i = 0, c = 0; + strncpy(cmdlin, filename, 128); + cmdlin[128] = 0; + while (cmdlin[i]) { + while (cmdlin[i] == ' ') i++; + if (cmdlin[i]) arg[c++] = &cmdlin[i]; + while (cmdlin[i] != ' ' && cmdlin[i] != 0) i++; + if (cmdlin[i]) cmdlin[i++] = 0; + } + arg[c] = NULL; + return _execlv(arg[0], arg, 1); +} + +_execlv(filename, argptr, increment) +char *filename, **argptr; +int increment; +{ + char *sptr; + char c, name[64]; + char cmdlin[128]; + FILE *fp, *fopen(); + int n = 0, extension = 0; + while ((c = *filename++) && n < 63) { + name[n++] = c; + if (c == '.') ++extension; + } + if (!extension && n < 61) strcpy(&name[n], ".COM"); + else name[n] = '\0'; + fp = fopen(name, "r"); + if (fp != NULL) { + cmdlin[0] = '\0'; + n = 1; + if (*(argptr)) argptr = argptr + increment; + while (sptr = *(argptr)) { + argptr = argptr + increment; + while ((c = *sptr++) && n < 126) cmdlin[n++] = c; + cmdlin[0] = n - 1; + cmdlin[n++] = ' '; + } + cmdlin[n] = '\r'; + _chain(fp, cmdlin); + } + else return -1; +} + +FILE *fdopen(fd, type) +int fd; +char *type; +{ + FILE *fp; + int c; + fp = _iob[fd]; + if (fp != NULL) { + c = tolower(*type); + if ((c == 'w') || (c == 'a')) { + if (fp->file[16]) return fp; + else return NULL; + } + if ((c == 'r') && (!fp->file[16])) return fp; + else return NULL; + } + else return NULL; +} + +FILE *freopen(filename, type, old_fp) +char *filename, *type; +FILE *old_fp; +{ + FILE *fopen(); + if (fclose(old_fp)) return NULL; + else return fopen(filename, type); +} + +FILE *fopen(name,modes) +char *name, *modes; +{ + char c; + int fd, mode = 0; + void _creat(), _openr(), _opena(), _fopenrw(), _openrw(), (*funcptr)(); + long lseek(); + int iomode = 0; + FILE *fp; + while ((c = tolower(*modes++))) { + switch(c) { + case 'w': funcptr = _creat; iomode = 1; break; + case 'a': funcptr = _opena; iomode = 2; break; + case 'r': funcptr = _openr; break; + case '+': + if (iomode == 0) funcptr = _openrw; + else funcptr = _fopenrw; + break; + case 'b': mode = 0x8000; + } + } + if ((fd = _open(name, funcptr, mode)) == -1) return NULL; + if (iomode == 2) lseek(fd,0l,2); + return _iob[fd]; +} + +creat(name, mode) +char *name; +int mode; +{ + int _creat(); + return _open(name, _creat, mode); +} + +open(name, mode) +char *name; +int mode; +{ + void _openr(), _openw(), _openrw(), (*funcptr)(); + int modemask; + modemask = mode & 0x7FFF; + if (modemask == 0) funcptr = _openr; + else + if (modemask == 1) funcptr = _openw; + else + funcptr = _openrw; + return _open(name, funcptr, mode); +} + +_open(name, funcptr, mode) +char name[]; +void (*funcptr)(); +int mode; +{ + extern int errno; + int fd; + int len; + char c,id[8],*buffer; + FILE *fp; + void _setname(); + fd = 0; + while (fd < MAXFILES && _iob[fd] != NULL) fd++; + if (fd < MAXFILES) { + id[0] = 'F'; + for (len=0; (c = name[len]) != '\0'; len++) + if (len < 7) id[len+1] = c; + _iob[fd] = fp = calloc(1, sizeof(FILE)); + if (fp) { + fp->fd = fd; + _setname(fp, name, len, id); + if (!(fp->file[18])) { + (*funcptr)(mode, fp, id); + if (fp->file[17] != '\0') { + if (mode < 0) fp->file[23] = '\2'; + return fd; + } + else { + _iob[fd] = NULL; + buffer = *((char **) &fp->file[8]); + free(*((char **) (buffer - 11))); + free(buffer - 16); + free(*((char **) &fp->file[21])); + free(fp); + return -1; + } + } + else { + errno = fp->file[18]; + return -1; + } + } + else return -1; + } + else return -1; +} + +close(fd) +int fd; +{ + return fclose(_iob[fd]); +} + +fclose(fp) +FILE *fp; +{ + void _close(); + _close(fp); + if (fp->file[17]) return -1; + else { + _iob[fp->fd] = NULL; + free(fp); + return 0; + } +} + +exit(status) +int status; +{ + int i; + int _exit(); + void _close(); + i = 0; + while (i < MAXFILES) { + if (_iob[i] != NULL) _close(_iob[i]->file); + i++; + } + _exit(status); +} + +isalpha(c) /* returns c is alphabetic */ +int c; /* else returns 0 */ +{ + if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') return 1; + else return 0; +} + +isdigit(c) +int c; +{ + if (c >= '0' && c <= '9') return 1; + else return 0; +} + +isspace(c) +int c; +{ + if (c == ' ' || c == '\t' || c == '\n') return 1; + else return 0; +} + +islower(c) +int c; +{ + if (c >= 'a' && c <= 'z') return 1; + else return 0; +} + +isupper(c) +int c; +{ + if (c >= 'A' && c <= 'Z') return 1; + else return 0; +} + +isalnum(c) +int c; +{ + if ((c >= 48 && c <= 57) || + (c >= 65 && c <= 90) || + (c >= 97 && c <= 122)) return 1; + else return 0; +} + +isascii(c) +int c; +{ + if (c < 128) return 1; + else return 0; +} + +iscntrl(c) +int c; +{ + if ((c < 32) || (c == 127)) return 1; + else return 0; +} + +isprint(c) +int c; +{ + if ((c > 31) && (c < 127)) return 1; + else return 0; +} + +ispunct(c) +int c; +{ + if ((c > 31 && c < 48) || + (c > 57 && c < 65) || + (c > 90 && c < 97) || + (c >122 && c < 127)) return 1; + else return 0; +} + +toupper(c) +int c; +{ + if (c >= 'a' && c <= 'z') c -= 32; + return c; +} + +tolower(c) +int c; +{ + if (c >= 'A' && c <= 'Z') c += 32; + return c; +} + +char *index(s, c) +char *s; +int c; +{ + while (*s != c && *s != 0) s++; + if (*s) return s; + else return NULL; +} + +char *rindex(s, c) +char *s; +int c; +{ + int len; + len = strlen(s); + if (len) { + s = s + len - 1; + while (*s != c && len) { + s--; + len--; + } + if (len) return s; + } + return NULL; +} + +char *strsave(s) +char *s; +{ + char *ptr; + char *calloc(); + int len; + if (ptr = malloc(len = strlen(s)+1)) movmem(s, ptr, len); + return(ptr); +} + +strcpy(dest, source) +char *dest; +char *source; +{ + movmem(source, dest, strlen(source)+1); + return(dest); +} + +strcmp(first, second) +char *first; +char *second; +{ + while (*first == *second) { + if (*first) { + first++; + second++; + } + else return 0; + } + return *first - *second; +} + +strcat(first, second) +char *first; +char *second; +{ + movmem(second, first + strlen(first), strlen(second)+1); + return(first); +} + +strncpy(dest, source, n) +char *dest; +char *source; +int n; +{ + int len; + movmem(source, dest, (len = strlen(source)+1) < n ? len : n); + return(dest); +} + +strncmp(first, second, n) +char *first; +char *second; +int n; +{ + while ((n--) && (*first == *second)) { + if (*first) { + if (n) { + first++; + second++; + } + } + else return 0; + } + return *first - *second; +} + +strncat(first, second, n) +char *first; +char *second; +int n; +{ + int len; + movmem(second, first + strlen(first), + ((len = strlen(second)+1) < n ? len : n)); + return(first); +} + +perror(s) +char *s; +{ + extern int errno; + extern int sys_nerr; + extern char *sys_errlist[]; + extern char *int_errlist[]; + fputs(s, stderr); putc(':', stderr); + if (errno <= sys_nerr) fputs(sys_errlist[errno], stderr); + else + if (errno > 0x80 && errno < 0x97) + fputs(int_errlist[errno - 0x81], stderr); + else + fputs("Error Cause Unknown", stderr); + putc('\n', stderr); +} + +char *gets(s) /* reads one line from stdin */ +char *s; /* into string s */ + /* newline replaced by '\0' */ +{ + int _fgets(); + char c; + if (_fgets(stdin, s, &c, '\n', 32767) > 0 || c == '\n') + return s; + else return NULL; +} + +char *fgets(s, n, fp) /* read a line from fp */ +char *s; /* into s */ +int n; /* maximum characters */ +FILE *fp; /* is n-1 */ +{ + int _fgets(); + int count; + char c; + if (n > 1) { + count = _fgets(fp, s, &c, '\n', n); + if (c == '\n') { + *(s+count++) = c; + *(s+count) = NULL; + } + else if (count < 1) return NULL; + } + else *s = NULL; + return s; +} + +char *puts(s) /* write s to stdout */ +char *s; +{ + int _write(); + int strlen(); + _write(stdout, s, strlen(s)); + if (putc('\n', stdout) != EOF) return s; + else return EOF; +} + +char *fputs(s, fp) /* write s to fp */ +char *s; +FILE *fp; +{ + int _write(); + int strlen(); + if (_write(fp, s, strlen(s)) < 0) return EOF; + else return s; +} + +STRING *stods(s) /* convert string to dynamic string */ +char *s; /* s is a normal C string */ +{ + int length; + STRING *ptr; + STRING *calloc(); + int strlen(); /* get length of string */ + length = strlen(s); + ptr = malloc(sizeof(int) + length); + if (ptr != NULL) { + ptr->length = length; + movmem(s, ptr->string, length); + } + return ptr; +} + +dstos(ptr,s) /* convert dynamic string into normal C string */ +STRING *ptr; /* pointer to dynamic string */ +char *s; /* normal string */ +{ + movmem(ptr->string, s, ptr->length); + *(s + ptr->length) = '\0'; +} + +clearerr(fp) +FILE *fp; +{ + fp->file[18] = '\0'; +} + +ferror(fp) +FILE *fp; +{ + return fp->file[18]; +} + +fflush(fp) +FILE *fp; +{ + long fseek(); + if (fseek(fp, 0L, 1) == -1) return -1; + else return 0; +} + +fileno(fp) +FILE *fp; +{ + return fp->fd; +} + + +double hypot(x, y) +double x,y; +{ + double sqrt(); + return (sqrt(x*x + y*y)); +} + +double log10(x) +double x; +{ + double log(); + return log(x) / 2.302585093; +} + + +char *realloc(oldptr, newsize) +char *oldptr; +int newsize; +{ + char *newptr, *malloc(); + if (newptr = malloc(newsize)) { + movmem(oldptr, newptr, newsize); + free(oldptr); + return newptr; + } + else return NULL; +} + +rewind(fp) +FILE *fp; +{ + return fseek(fp, 0L, 0); +} + +ungetch(c) +int c; +{ + extern char _ungetch; + return _ungetch ? EOF : (_ungetch = c); +} + +setbufsiz(size) +int size; +{ + extern char *_flagptr; + int blocks, *sizeptr; + blocks = (size/128) <= 0 ? 1 : (size/128); + *(_flagptr + 8) = blocks; + sizeptr = _flagptr + 9; + *sizeptr = blocks * 128; +} + +setbuf(fp, bufptr) +FILE *fp; +char *bufptr; +{ + char **oldbufptr, **oldbuforg; + if (fp) { + if (bufptr) { + oldbuforg = &(fp->file[21]); + oldbufptr = &(fp->file[10]); + free(*oldbuforg); + *oldbuforg = *oldbufptr = bufptr; + } + else fp->file[14] = '\100'; + } +} + +conbuf(flag) +int flag; +{ + return _setflag(flag, 42); +} + +mathtrap(flag) +int flag; +{ + return _setflag(flag, 43); +} + +exitmsg() +{ + _setflag(0, 3); +} + +_setflag(flag, offset) +int flag, offset; +{ + extern char *_flagptr; + char oldvalue; + oldvalue = *(_flagptr + offset); + *(_flagptr + offset) = flag; + return oldvalue; +} + +filetrap(flag) +int flag; +{ + return _settrap(flag, 62); +} + +heaptrap(flag) +int flag; +{ + return _settrap(flag, 63); +} + +_settrap(flag, offset) +int flag, offset; +{ + extern char *_procptr; + char oldflag; + oldflag = *(_procptr + offset); + *(_procptr + offset) = flag; + return oldflag; +} + +iofilter(linefeed, ctrlz) +int linefeed, ctrlz; +{ + extern char *_flagptr; + *(_flagptr + 4) = linefeed; + *(_flagptr + 5) = ctrlz; +} + + +/******************* MSDOS Functions ********************/ + +chdir(dirname) +char *dirname; +{ + REGS reg; + return _syscall(0x3B, dirname, ®); +} + +chmod(name, mode) +char *name; +int mode; +{ + REGS reg; + reg.word.cx = mode; + reg.byte.al = 1; + return _syscall(0x43, name, ®); +} + +getch() +{ + extern char _ungetch; + char ch; + if (ch = _ungetch) { + _ungetch = '\0'; + return ch; + } + else return bdos(0x07, 0); +} + +putch(c) +int c; +{ + bdos(0x06, c); +} + +getkey() +{ + REGS reg; + reg.byte.dl = 0xFF; + if (bdosx(0x06, ®) & 0x40) return EOF; + else return reg.byte.al; +} + +keypress() +{ + return bdos(0x0B, 0); +} + +unlink(name) +char *name; +{ + REGS reg; + return _syscall(0x41, name, ®); +} + +mkdir(dirname) +char *dirname; +{ + REGS reg; + return _syscall(0x39, dirname, ®); +} + +rmdir(dirname) +char *dirname; +{ + REGS reg; + return _syscall(0x3A, dirname, ®); +} + +rename(oldname, newname) +char *oldname, *newname; +{ + REGS reg; + reg.word.di = newname; + if (_syscall(0x56, oldname, ®)) return EOF; + else return 0; +} + +settime(s) +char *s; +{ + return _settd(0, s); +} + +setdate(s) +char *s; +{ + return _settd(1, s); +} + +_syscall(fn, s, regptr) +int fn; +char *s; +REGS *regptr; +{ + regptr->word.ds = regptr->word.es = getdseg(); + regptr->word.dx = s; + if (bdosx(fn, regptr) & 0x0001) return regptr->word.ax; + else return 0; +} + +/******************* IBM PC BIOS Functions ********************/ + +cursblk() +{ + _setcurs(0, _graphic() ? 7 : 13); +} + +curslin() +{ + int start; + _setcurs((start = _graphic() ? 7 : 13), start); +} + +_graphic() /* returns 1 if graphics display, 0 if monochrome */ +{ + REGS reg; + int display; + bios(0x11, ®); /* get display type (bits 4 and 5 of ax) */ + if ((reg.word.ax & 0x30) < 0x30) return 1; /* graphics */ + else return 0; /* monochrome */ +} + +curson() /* set bit 5 of start scan line to 0 : IBM PC */ +{ /* set bit 6 of start scan line to 0 : Tandy 2000 + use & 0x00BF */ + unsigned cursor, _getcurs(); + cursor = _getcurs(); + _setcurs((cursor >> 8) & 0x00DF, cursor & 0x00FF); +} + +cursoff() /* set bit 5 of start scan line to 1 : IBM PC */ +{ /* set bit 6 of start scan line to 1 : Tandy 2000 + use | 0x0040 */ + unsigned cursor, _getcurs(); + cursor = _getcurs(); + _setcurs((cursor >> 8) | 0x0020, cursor & 0x00FF); +} + +unsigned _getcurs() +{ + REGS reg; + reg.byte.ah = 3; + reg.byte.bh = 0; + bios(0x10, ®); + return reg.word.cx; +} + +_setcurs(start, stop) +int start, stop; +{ + REGS reg; + reg.byte.ah = 1; + reg.byte.ch = start; + reg.byte.cl = stop; + bios(0x10, ®); +} + +clrscrn() +{ + REGS reg; + reg.byte.ah = 6; + reg.byte.al = reg.byte.ch = reg.byte.cl = 0; + reg.byte.dh = 24; reg.byte.dl = 79; + reg.byte.bh = 7; + bios(0x10, ®); + poscurs(0,0); +} + +clrscrn2(attribute) +unsigned int attribute; +{ + REGS reg; + reg.byte.ah = 6; + reg.byte.al = reg.byte.ch = reg.byte.cl = 0; + reg.byte.dh = 24; reg.byte.dl = 79; + reg.byte.bh = attribute; + bios(0x10, ®); + poscurs(0,0); +} + +getmode() +{ + REGS reg; + reg.byte.ah = 15; + bios(0x10, ®); + return reg.byte.al; +} + +setmode(mode) +int mode; +{ + REGS reg; + reg.byte.ah = 0; + reg.byte.al = mode; + bios(0x10, ®); +} + +setcolor(background, palette) +int background, palette; +{ + REGS reg; + reg.byte.ah = 11; + reg.byte.bh = 0; + reg.byte.bl = background; + bios(0x10, ®); + reg.byte.bh = 1; + reg.byte.bl = palette; + bios(0x10, ®); +} + +/******************* External Variables ********************/ + +FILE *_iob[MAXFILES]; +char _ungetch = '\0'; +char *_flagptr; +char *_procptr; +int sys_nerr = 17; +long _seed; + +_dumy1_(){} + +char *sys_errlist[] = {"No Errors", + "Invalid Function Number", + "File not Found", + "Path not Found", + "No More File Handles", + "File Access Denied", + "Invalid File Handle", + "Memory Blocks Destroyed", + "Insufficient Memory", + "Invalid Block Address", + "Invalid Environment", + "Invalid Format", + "Invalid Access Code", + "Invalid Data", + "Invalid Drive", + "Can't Remove Current Directory", + "Not Same Device", + "No More Files" }; + +_dumy2_(){} + +char *int_errlist[] = {"Out of Stack", + "Out of Heap", + "Damaged Stack or Heap", + "Damaged Stack or Heap", + "Divide by Zero", + "Invalid Instruction", + "Damaged Stack or Heap", + "Damaged Stack or Heap", + "I/O Error", + "Error Cause Unknown", + "Attempt to Write Input", + "File Not Open", + "Attempt to Read Output", + "No Memory for File Buffer", + "Error Cause Unknown", + "Damaged Stack or Heap", + "Bad Digit in Number", + "Damaged Stack or Heap", + "Arithmetic Overflow", + "Error Cause Unknown", + "Arithmetic Underflow", + "Log of Negative Number", + "Sqrt of Negative Number" }; diff --git a/Mix C v251/STDLIB.H b/Mix C v251/STDLIB.H new file mode 100644 index 0000000..a10c75e --- /dev/null +++ b/Mix C v251/STDLIB.H @@ -0,0 +1,14 @@ +/*$no list*//*$no trace <<< start of stdlib.h >>> */ + +typedef char jmp_buf[32]; + +typedef union { + struct { + char al, ah, bl, bh, cl, ch, dl, dh; + } byte; + struct { + int ax, bx, cx, dx, si, di, bp, es, ds, cs; + } word; +} REGS; + +/*$list *//*$trace <<< end of stdlib.h >>> */ diff --git a/Mix C v251/TEST.C b/Mix C v251/TEST.C new file mode 100644 index 0000000..f4f2178 --- /dev/null +++ b/Mix C v251/TEST.C @@ -0,0 +1,30 @@ +/* This is a simple program that tests whether or not the Ctrace + output window is working properly. If the ANSI.SYS driver is + loaded, the output window will not work properly unless Ctrace + is informed that the driver is present. */ + +main() +{ + int i; + for (i=0; i<25; i++) puts(""); + printf(" This is a test..."); + for (i=0; i<10; i++) { + printf("\n%5d) ", i+1); + printf("Now is the time for all good programmers to learn C."); + } +} + +/* The output should look like this: + + This is a test... + 1) Now is the time for all good programmers to learn C. + 2) Now is the time for all good programmers to learn C. + 3) Now is the time for all good programmers to learn C. + 4) Now is the time for all good programmers to learn C. + 5) Now is the time for all good programmers to learn C. + 6) Now is the time for all good programmers to learn C. + 7) Now is the time for all good programmers to learn C. + 8) Now is the time for all good programmers to learn C. + 9) Now is the time for all good programmers to learn C. + 10) Now is the time for all good programmers to learn C. + */ diff --git a/Mix C v251/TTT.C b/Mix C v251/TTT.C new file mode 100644 index 0000000..e18defc --- /dev/null +++ b/Mix C v251/TTT.C @@ -0,0 +1,247 @@ +/* + This version builds with old compilers including: + Aztec C 1.06 for 8080 & Z80 on CP/M. + Microsoft C Compiler V1.04 for 8086 on DOS. (This is Lattice C) + Microsoft C Compiler V2.03 for 8086 on DOS. (Still Lattice C) + Microsoft C Compiler V3.00 for 8086 on DOS. + QuickC 1.0 + Turbo C 2.0 + The syntax is old and reminds me of 7th grade summer vacation. + Much of this code is awkward to satisfy the lowest common denominator of many compilers. + unsigned long isn't supported in many older compilers, so long is used instead. + Early DOS and CP/M require register variabes to be int, not char or other types. + The perf improvement of using register-int instead of stack-char is worth it. +*/ + +#define LINT_ARGS + +#include + +#ifdef DOSTIME +#include +#include +#endif + +#define true 1 +#define false 0 + +#define ScoreWin 6 +#define ScoreTie 5 +#define ScoreLose 4 +#define ScoreMax 9 +#define ScoreMin 2 +#define DefaultIterations 10 + +#define PieceX 1 +#define PieceO 2 +#define PieceBlank 0 + +typedef char ttype; /* 8-bit and 16-bit cpus do best with char aside from register in locals */ + +int g_Iterations = DefaultIterations; +ttype g_board[ 9 ]; + +ttype winner2( move ) ttype move; +{ + register int x; /* faster than ttype x on the stack */ + + switch( move ) /* msc v3 from 1985 generates a jump table! */ + { + case 0: + { + x = g_board[ 0 ]; + if ( ( ( x == g_board[1] ) && ( x == g_board[2] ) ) || + ( ( x == g_board[3] ) && ( x == g_board[6] ) ) || + ( ( x == g_board[4] ) && ( x == g_board[8] ) ) ) + return x; + break; + } + case 1: + { + x = g_board[ 1 ]; + if ( ( ( x == g_board[0] ) && ( x == g_board[2] ) ) || + ( ( x == g_board[4] ) && ( x == g_board[7] ) ) ) + return x; + break; + } + case 2: + { + x = g_board[ 2 ]; + if ( ( ( x == g_board[0] ) && ( x == g_board[1] ) ) || + ( ( x == g_board[5] ) && ( x == g_board[8] ) ) || + ( ( x == g_board[4] ) && ( x == g_board[6] ) ) ) + return x; + break; + } + case 3: + { + x = g_board[ 3 ]; + if ( ( ( x == g_board[4] ) && ( x == g_board[5] ) ) || + ( ( x == g_board[0] ) && ( x == g_board[6] ) ) ) + return x; + break; + } + case 4: + { + x = g_board[ 4 ]; + if ( ( ( x == g_board[0] ) && ( x == g_board[8] ) ) || + ( ( x == g_board[2] ) && ( x == g_board[6] ) ) || + ( ( x == g_board[1] ) && ( x == g_board[7] ) ) || + ( ( x == g_board[3] ) && ( x == g_board[5] ) ) ) + return x; + break; + } + case 5: + { + x = g_board[ 5 ]; + if ( ( ( x == g_board[3] ) && ( x == g_board[4] ) ) || + ( ( x == g_board[2] ) && ( x == g_board[8] ) ) ) + return x; + break; + } + case 6: + { + x = g_board[ 6 ]; + if ( ( ( x == g_board[7] ) && ( x == g_board[8] ) ) || + ( ( x == g_board[0] ) && ( x == g_board[3] ) ) || + ( ( x == g_board[4] ) && ( x == g_board[2] ) ) ) + return x; + break; + } + case 7: + { + x = g_board[ 7 ]; + if ( ( ( x == g_board[6] ) && ( x == g_board[8] ) ) || + ( ( x == g_board[1] ) && ( x == g_board[4] ) ) ) + return x; + break; + } + case 8: + { + x = g_board[ 8 ]; + if ( ( ( x == g_board[6] ) && ( x == g_board[7] ) ) || + ( ( x == g_board[2] ) && ( x == g_board[5] ) ) || + ( ( x == g_board[0] ) && ( x == g_board[4] ) ) ) + return x; + break; + } + } + + return PieceBlank; +} /*winner2*/ + +int g_IMoves = 0; + +ttype MinMax( alpha, beta, depth, move ) ttype alpha; ttype beta; ttype depth; ttype move; +{ + ttype pieceMove, score; /* better perf with char than int. out of registers so use stack */ + register int p, value; /* better perf with these as an int on Z80, 8080, and 8086 */ + + g_IMoves++; + + if ( depth >= 4 ) + { + p = winner2( move ); + + if ( PieceBlank != p ) + { + if ( PieceX == p ) + return ScoreWin; + + return ScoreLose; + } + + if ( 8 == depth ) + return ScoreTie; + } + + if ( depth & 1 ) + { + value = ScoreMin; + pieceMove = PieceX; + } + else + { + value = ScoreMax; + pieceMove = PieceO; + } + + for ( p = 0; p < 9; p++ ) + { + if ( PieceBlank == g_board[ p ] ) + { + g_board[p] = pieceMove; + score = MinMax( alpha, beta, depth + 1, p ); + g_board[p] = PieceBlank; + + if ( depth & 1 ) + { + if ( ScoreWin == score ) + return ScoreWin; + + if ( score > value ) + { + value = score; + + if ( value >= beta ) + return value; + if ( value > alpha ) + alpha = value; + } + } + else + { + if ( ScoreLose == score ) + return ScoreLose; + + if ( score < value ) + { + value = score; + + if ( value <= alpha ) + return value; + if ( value < beta ) + beta = value; + } + } + } + } + + return value; +} /*MinMax*/ + +long g_Moves = 0; + +int FindSolution( position ) ttype position; +{ + register int i; + + for ( i = 0; i < 9; i++ ) + g_board[ i ] = PieceBlank; + + g_board[ position ] = PieceX; + + for ( i = 0; i < g_Iterations; i++ ) + { + g_IMoves = 0; + MinMax( ScoreMin, ScoreMax, 0, position ); + g_Moves += g_IMoves; /* do the 4-byte long addition once per loop to save work */ + } + + return 0; +} /*FindSolution*/ + +int main( argc, argv ) int argc; char * argv[]; +{ + if ( 2 == argc ) + sscanf( argv[ 1 ], "%d", &g_Iterations ); /* no atoi in MS C 1.0 */ + + FindSolution( 0 ); + FindSolution( 1 ); + FindSolution( 4 ); + + printf( "move count: %ld\n", g_Moves ); /* 6493 * g_Iterations */ + printf( "iteration count: %d\n", g_Iterations ); + return 0; +} /*main*/ + diff --git a/Mix C v251/VAR.C b/Mix C v251/VAR.C new file mode 100644 index 0000000..4d2e475 --- /dev/null +++ b/Mix C v251/VAR.C @@ -0,0 +1,121 @@ +#define NULL 0 +struct NAME { + char first[40]; + char *middle; + char *last; +}; +struct NAMES { + char first[40]; + char *middle; + char *last; + struct NAMES *next; +}; +/******************* external variables *******************/ +int x = 0; +int *xptr = &x; +int xarray[3] = {10, 20, 30}; +struct { + int x; + int xarray[1000]; +} xstruct = {40}; + +/*************** functions and auto variables **************/ + +main() +{ + char c; + short s; + int i; + unsigned u; + long l; + float f; + double d; + for (;;) { + for(c = 'A'; c <= 'Z'; c++) { + s = c; + i = s + 1; + u = i + 1; + l = u + 1; + f = l + 1; + d = f + 1; + + } + func1(); + } +} + + +func1() +{ + int i; + char *strptr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + char string[27] = "abcdefghijklmnopqrstuvwxyz"; + char name[7][30] = { "George Armstrong Custer", + "William F. Buckley", + "Billy Bob Texas", + "Daniel T. Boone", + "Howard K. Smith", + "Tommy Lee Jones", + "Ronald MacDonald"}; + for (i = 25; i >= 0; i--) { + *(strptr+i) = '\0'; + string[i] = i + 'A'; + } + for (i = 0; i <= 25; i++) { + string[i] = i + 'a'; + *(strptr+i) = i + 'A'; + } + for (i = 0; i < 7; i++) func2(name[i]); +} + + +func2(string) +char string[]; +{ + char *ptr; + struct NAME name; + strcpy(name.first, string); + ptr = name.first; + name.middle = NULL; + name.last = NULL; + while (*ptr) { + if (*ptr == ' ') { + *ptr++ = '\0'; + if (name.middle == NULL) name.middle = ptr; + else if (name.last == NULL) name.last = ptr; + } + ptr++; + } + if (name.last == NULL) { + if (name.middle != NULL) { + name.last = name.middle; + name.middle = ptr; + } + else name.last = name.middle = ptr; + } + func3(&name); +} + +func3(name) +struct NAME *name; +{ + struct NAMES *temp; + static struct NAMES *namelist; + temp = calloc(1, sizeof(NAMES)); + if (temp != NULL) { + movmem(name->first, temp->first, 40); + temp->middle = temp->first + (name->middle - name->first); + temp->last = temp->first + (name->last - name->first); + temp->next = namelist; + namelist = temp; + x = x + 1; + } + temp = namelist; + while (temp != NULL) { + printf("%s ", temp->first); + printf("%s ", temp->middle); + puts(temp->last); + temp = temp->next; + } + puts("-----------------------------"); +} diff --git a/Mix C v251/VAR.COM b/Mix C v251/VAR.COM new file mode 100644 index 0000000..63b521b Binary files /dev/null and b/Mix C v251/VAR.COM differ diff --git a/Mix C v251/VAR.MIX b/Mix C v251/VAR.MIX new file mode 100644 index 0000000..a548b9f Binary files /dev/null and b/Mix C v251/VAR.MIX differ diff --git a/Mix C v251/VAR.SYM b/Mix C v251/VAR.SYM new file mode 100644 index 0000000..9f33a53 Binary files /dev/null and b/Mix C v251/VAR.SYM differ diff --git a/Mix C v251/VAR.TRC b/Mix C v251/VAR.TRC new file mode 100644 index 0000000..f146f63 Binary files /dev/null and b/Mix C v251/VAR.TRC differ diff --git a/Mix C v251/m.bat b/Mix C v251/m.bat new file mode 100644 index 0000000..159292f --- /dev/null +++ b/Mix C v251/m.bat @@ -0,0 +1,3 @@ +ntvdm cc %1.c +ntvdm linker %1 +