Mix C v2.5.1
This commit is contained in:
parent
c6b1a5602b
commit
5684a37d8f
BIN
Mix C v251/ANOTHER.CFG
Normal file
BIN
Mix C v251/ANOTHER.CFG
Normal file
Binary file not shown.
BIN
Mix C v251/CC.COM
Normal file
BIN
Mix C v251/CC.COM
Normal file
Binary file not shown.
BIN
Mix C v251/CC.EXE
Normal file
BIN
Mix C v251/CC.EXE
Normal file
Binary file not shown.
256
Mix C v251/CERRORS.DAT
Normal file
256
Mix C v251/CERRORS.DAT
Normal file
@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
BIN
Mix C v251/CLIB.MIX
Normal file
BIN
Mix C v251/CLIB.MIX
Normal file
Binary file not shown.
BIN
Mix C v251/CONVERT.COM
Normal file
BIN
Mix C v251/CONVERT.COM
Normal file
Binary file not shown.
BIN
Mix C v251/CTR.CFG
Normal file
BIN
Mix C v251/CTR.CFG
Normal file
Binary file not shown.
BIN
Mix C v251/CTR.EXE
Normal file
BIN
Mix C v251/CTR.EXE
Normal file
Binary file not shown.
30
Mix C v251/E.C
Normal file
30
Mix C v251/E.C
Normal file
@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
3
Mix C v251/EXAMPLES/EXAM11.C
Normal file
3
Mix C v251/EXAMPLES/EXAM11.C
Normal file
@ -0,0 +1,3 @@
|
||||
main()
|
||||
{
|
||||
}
|
5
Mix C v251/EXAMPLES/EXAM12.C
Normal file
5
Mix C v251/EXAMPLES/EXAM12.C
Normal file
@ -0,0 +1,5 @@
|
||||
main() /* Example 1.2 */
|
||||
{
|
||||
printf("One small step for C, one giant leap for me.");
|
||||
}
|
||||
|
12
Mix C v251/EXAMPLES/EXAM13.C
Normal file
12
Mix C v251/EXAMPLES/EXAM13.C
Normal file
@ -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. */
|
||||
}
|
||||
|
26
Mix C v251/EXAMPLES/EXAM14.C
Normal file
26
Mix C v251/EXAMPLES/EXAM14.C
Normal file
@ -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);
|
||||
|
||||
}
|
29
Mix C v251/EXAMPLES/EXAM15.C
Normal file
29
Mix C v251/EXAMPLES/EXAM15.C
Normal file
@ -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);
|
||||
}
|
||||
|
9
Mix C v251/EXAMPLES/EXAM21.C
Normal file
9
Mix C v251/EXAMPLES/EXAM21.C
Normal file
@ -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);
|
||||
}
|
40
Mix C v251/EXAMPLES/EXAM22.C
Normal file
40
Mix C v251/EXAMPLES/EXAM22.C
Normal file
@ -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);
|
||||
}
|
||||
|
16
Mix C v251/EXAMPLES/EXAM23.C
Normal file
16
Mix C v251/EXAMPLES/EXAM23.C
Normal file
@ -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);
|
||||
}
|
7
Mix C v251/EXAMPLES/EXAM24.C
Normal file
7
Mix C v251/EXAMPLES/EXAM24.C
Normal file
@ -0,0 +1,7 @@
|
||||
main() /* Example 2.4 */
|
||||
{
|
||||
int a;
|
||||
|
||||
a = 3.4 * 5;
|
||||
printf("a = %d \n",a);
|
||||
}
|
34
Mix C v251/EXAMPLES/EXAM25.C
Normal file
34
Mix C v251/EXAMPLES/EXAM25.C
Normal file
@ -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);
|
||||
}
|
19
Mix C v251/EXAMPLES/EXAM26.C
Normal file
19
Mix C v251/EXAMPLES/EXAM26.C
Normal file
@ -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);
|
||||
}
|
11
Mix C v251/EXAMPLES/EXAM31.C
Normal file
11
Mix C v251/EXAMPLES/EXAM31.C
Normal file
@ -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);
|
||||
}
|
14
Mix C v251/EXAMPLES/EXAM32.C
Normal file
14
Mix C v251/EXAMPLES/EXAM32.C
Normal file
@ -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);
|
||||
}
|
18
Mix C v251/EXAMPLES/EXAM33.C
Normal file
18
Mix C v251/EXAMPLES/EXAM33.C
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
|
24
Mix C v251/EXAMPLES/EXAM34.C
Normal file
24
Mix C v251/EXAMPLES/EXAM34.C
Normal file
@ -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);
|
||||
}
|
14
Mix C v251/EXAMPLES/EXAM35.C
Normal file
14
Mix C v251/EXAMPLES/EXAM35.C
Normal file
@ -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);
|
||||
}
|
16
Mix C v251/EXAMPLES/EXAM36.C
Normal file
16
Mix C v251/EXAMPLES/EXAM36.C
Normal file
@ -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");
|
||||
}
|
12
Mix C v251/EXAMPLES/EXAM37.C
Normal file
12
Mix C v251/EXAMPLES/EXAM37.C
Normal file
@ -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);
|
||||
}
|
22
Mix C v251/EXAMPLES/EXAM38.C
Normal file
22
Mix C v251/EXAMPLES/EXAM38.C
Normal file
@ -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);
|
||||
}
|
||||
}
|
18
Mix C v251/EXAMPLES/EXAM39.C
Normal file
18
Mix C v251/EXAMPLES/EXAM39.C
Normal file
@ -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);
|
||||
}
|
33
Mix C v251/EXAMPLES/EXAM41.C
Normal file
33
Mix C v251/EXAMPLES/EXAM41.C
Normal file
@ -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: ");
|
||||
}
|
||||
|
27
Mix C v251/EXAMPLES/EXAM42.C
Normal file
27
Mix C v251/EXAMPLES/EXAM42.C
Normal file
@ -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));
|
||||
}
|
18
Mix C v251/EXAMPLES/EXAM43.C
Normal file
18
Mix C v251/EXAMPLES/EXAM43.C
Normal file
@ -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);
|
||||
}
|
||||
|
69
Mix C v251/EXAMPLES/EXAM44.C
Normal file
69
Mix C v251/EXAMPLES/EXAM44.C
Normal file
@ -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);
|
||||
}
|
9
Mix C v251/EXAMPLES/EXAM51.C
Normal file
9
Mix C v251/EXAMPLES/EXAM51.C
Normal file
@ -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");
|
||||
}
|
15
Mix C v251/EXAMPLES/EXAM52.C
Normal file
15
Mix C v251/EXAMPLES/EXAM52.C
Normal file
@ -0,0 +1,15 @@
|
||||
/*include standard I/O header file */
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
11
Mix C v251/EXAMPLES/EXAM53.C
Normal file
11
Mix C v251/EXAMPLES/EXAM53.C
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
main () /* Example 5.3 */
|
||||
{
|
||||
/* copy a file from stdin to stdout */
|
||||
|
||||
int charin;
|
||||
|
||||
while ( (charin = getchar()) != EOF )
|
||||
putchar(charin);
|
||||
}
|
27
Mix C v251/EXAMPLES/EXAM54A.C
Normal file
27
Mix C v251/EXAMPLES/EXAM54A.C
Normal file
@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
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);
|
||||
}
|
29
Mix C v251/EXAMPLES/EXAM54B.C
Normal file
29
Mix C v251/EXAMPLES/EXAM54B.C
Normal file
@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
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);
|
||||
}
|
16
Mix C v251/EXAMPLES/EXAM55.C
Normal file
16
Mix C v251/EXAMPLES/EXAM55.C
Normal file
@ -0,0 +1,16 @@
|
||||
#define TAB 8 /* Example 5.5 */
|
||||
#include <stdio.h>
|
||||
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);
|
||||
}
|
||||
}
|
9
Mix C v251/EXAMPLES/EXAM56.C
Normal file
9
Mix C v251/EXAMPLES/EXAM56.C
Normal file
@ -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);
|
||||
}
|
36
Mix C v251/EXAMPLES/EXAM57.C
Normal file
36
Mix C v251/EXAMPLES/EXAM57.C
Normal file
@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
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);
|
||||
}
|
65
Mix C v251/EXAMPLES/EXAM58.C
Normal file
65
Mix C v251/EXAMPLES/EXAM58.C
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
27
Mix C v251/EXAMPLES/EXAM61.C
Normal file
27
Mix C v251/EXAMPLES/EXAM61.C
Normal file
@ -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);
|
||||
}
|
||||
|
22
Mix C v251/EXAMPLES/EXAM62.C
Normal file
22
Mix C v251/EXAMPLES/EXAM62.C
Normal file
@ -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 */
|
||||
}
|
29
Mix C v251/EXAMPLES/EXAM63.C
Normal file
29
Mix C v251/EXAMPLES/EXAM63.C
Normal file
@ -0,0 +1,29 @@
|
||||
#define MAX 10 /* bound of the array */
|
||||
|
||||
#include <stdio.h> /* 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);
|
||||
}
|
13
Mix C v251/EXAMPLES/EXAM64.C
Normal file
13
Mix C v251/EXAMPLES/EXAM64.C
Normal file
@ -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);
|
||||
}
|
10
Mix C v251/EXAMPLES/EXAM65.C
Normal file
10
Mix C v251/EXAMPLES/EXAM65.C
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
31
Mix C v251/EXAMPLES/EXAM66.C
Normal file
31
Mix C v251/EXAMPLES/EXAM66.C
Normal file
@ -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]);
|
||||
}
|
||||
}
|
59
Mix C v251/EXAMPLES/EXAM67.C
Normal file
59
Mix C v251/EXAMPLES/EXAM67.C
Normal file
@ -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<NO_OF_NAMES; number++)
|
||||
nameptr[number] = name[number];
|
||||
if (number == NO_OF_NAMES)
|
||||
printf("\n *** only %d names allowed ***\n",
|
||||
NO_OF_NAMES);
|
||||
printf("--- The names listed in alphabetic order ---\n");
|
||||
|
||||
/* sort the names */
|
||||
sort(nameptr, number);
|
||||
|
||||
/* print the sorted names */
|
||||
for (i=0; i<number; i++) puts(nameptr[i]);
|
||||
}
|
||||
|
||||
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<number; i++)
|
||||
if (strcmp(names[i], names[i+1]) > 0) {
|
||||
notsorted = TRUE;
|
||||
ptr = names[i]; /* swap the two pointers */
|
||||
names[i] = names[i+1];
|
||||
names[i+1] = ptr;
|
||||
}
|
||||
}
|
||||
}
|
19
Mix C v251/EXAMPLES/EXAM68.C
Normal file
19
Mix C v251/EXAMPLES/EXAM68.C
Normal file
@ -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));
|
||||
}
|
108
Mix C v251/EXAMPLES/EXAM69.C
Normal file
108
Mix C v251/EXAMPLES/EXAM69.C
Normal file
@ -0,0 +1,108 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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) <<exit program>>\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));
|
||||
}
|
31
Mix C v251/EXAMPLES/EXAM71.C
Normal file
31
Mix C v251/EXAMPLES/EXAM71.C
Normal file
@ -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;
|
||||
}
|
11
Mix C v251/EXAMPLES/EXAM72A.C
Normal file
11
Mix C v251/EXAMPLES/EXAM72A.C
Normal file
@ -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);
|
||||
}
|
11
Mix C v251/EXAMPLES/EXAM72B.C
Normal file
11
Mix C v251/EXAMPLES/EXAM72B.C
Normal file
@ -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);
|
||||
}
|
||||
|
9
Mix C v251/EXAMPLES/EXAM72C.C
Normal file
9
Mix C v251/EXAMPLES/EXAM72C.C
Normal file
@ -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;
|
||||
}
|
44
Mix C v251/EXAMPLES/EXAM73A.C
Normal file
44
Mix C v251/EXAMPLES/EXAM73A.C
Normal file
@ -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]);
|
||||
}
|
||||
|
12
Mix C v251/EXAMPLES/EXAM73B.C
Normal file
12
Mix C v251/EXAMPLES/EXAM73B.C
Normal file
@ -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 */
|
||||
}
|
22
Mix C v251/EXAMPLES/EXAM74.C
Normal file
22
Mix C v251/EXAMPLES/EXAM74.C
Normal file
@ -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);
|
||||
}
|
33
Mix C v251/EXAMPLES/EXAM75A.C
Normal file
33
Mix C v251/EXAMPLES/EXAM75A.C
Normal file
@ -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<MAX; i++)
|
||||
if (bin[i] != -1)
|
||||
printf("bin #%d = %ld\n", i, bin[i]);
|
||||
}
|
13
Mix C v251/EXAMPLES/EXAM75B.C
Normal file
13
Mix C v251/EXAMPLES/EXAM75B.C
Normal file
@ -0,0 +1,13 @@
|
||||
main() /* Example 7.5 - part B */
|
||||
{
|
||||
int allocbin(), printbin();
|
||||
long partno = 1;
|
||||
int bin_number;
|
||||
printf("-- Enter 0 to terminate data entry --\n");
|
||||
do {
|
||||
printf("Enter part number: ");
|
||||
scanf("%ld", &partno);
|
||||
if (partno) allocbin(partno);
|
||||
} while (partno);
|
||||
printbin();
|
||||
}
|
34
Mix C v251/EXAMPLES/EXAM81.C
Normal file
34
Mix C v251/EXAMPLES/EXAM81.C
Normal file
@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
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.);
|
||||
}
|
49
Mix C v251/EXAMPLES/EXAM82.C
Normal file
49
Mix C v251/EXAMPLES/EXAM82.C
Normal file
@ -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);
|
||||
}
|
29
Mix C v251/EXAMPLES/EXAM83.C
Normal file
29
Mix C v251/EXAMPLES/EXAM83.C
Normal file
@ -0,0 +1,29 @@
|
||||
#define MAX 100
|
||||
|
||||
#include <stdio.h> /* 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<number; i++) putlabel(names[i]);
|
||||
}
|
32
Mix C v251/EXAMPLES/EXAM84.C
Normal file
32
Mix C v251/EXAMPLES/EXAM84.C
Normal file
@ -0,0 +1,32 @@
|
||||
#define MAX 100
|
||||
#include <stdio.h> /* 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<number; i++) putlabel(names[i]);
|
||||
}
|
85
Mix C v251/EXAMPLES/EXAM85.C
Normal file
85
Mix C v251/EXAMPLES/EXAM85.C
Normal file
@ -0,0 +1,85 @@
|
||||
#include <stdio.h> /* 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;
|
||||
}
|
||||
}
|
22
Mix C v251/EXAMPLES/EXAM86.C
Normal file
22
Mix C v251/EXAMPLES/EXAM86.C
Normal file
@ -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");
|
||||
}
|
30
Mix C v251/EXAMPLES/EXAM91.C
Normal file
30
Mix C v251/EXAMPLES/EXAM91.C
Normal file
@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
28
Mix C v251/EXAMPLES/EXAM92.C
Normal file
28
Mix C v251/EXAMPLES/EXAM92.C
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
94
Mix C v251/EXAMPLES/EXAM93.C
Normal file
94
Mix C v251/EXAMPLES/EXAM93.C
Normal file
@ -0,0 +1,94 @@
|
||||
#define MAX 100
|
||||
#include <stdio.h> /* 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<number; i++) putlabel(out_file, names[i]);
|
||||
}
|
40
Mix C v251/EXAMPLES/EXAM94.C
Normal file
40
Mix C v251/EXAMPLES/EXAM94.C
Normal file
@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
7
Mix C v251/EXAMPLES/FILE1.C
Normal file
7
Mix C v251/EXAMPLES/FILE1.C
Normal file
@ -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
|
||||
|
5
Mix C v251/EXAMPLES/FILE2.C
Normal file
5
Mix C v251/EXAMPLES/FILE2.C
Normal file
@ -0,0 +1,5 @@
|
||||
/* common declarations from FILE2.C */
|
||||
int one,two,three;
|
||||
char a,b,c;
|
||||
float flt1,flt2;
|
||||
|
27
Mix C v251/EXAMPLES/LABELIO.C
Normal file
27
Mix C v251/EXAMPLES/LABELIO.C
Normal file
@ -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);
|
||||
}
|
||||
|
26
Mix C v251/EXAMPLES/SORT.C
Normal file
26
Mix C v251/EXAMPLES/SORT.C
Normal file
@ -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<number; i++)
|
||||
if (strcmp(names[i], names[i+1]) > 0) {
|
||||
notsorted = TRUE;
|
||||
ptr = names[i]; /* swap the two pointers */
|
||||
names[i] = names[i+1];
|
||||
names[i+1] = ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
Mix C v251/EXAMPLES/STRUCT2.H
Normal file
17
Mix C v251/EXAMPLES/STRUCT2.H
Normal file
@ -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;
|
||||
};
|
17
Mix C v251/EXAMPLES/STRUCTS.H
Normal file
17
Mix C v251/EXAMPLES/STRUCTS.H
Normal file
@ -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;
|
||||
};
|
||||
|
47
Mix C v251/JUMP.C
Normal file
47
Mix C v251/JUMP.C
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
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");
|
||||
}
|
BIN
Mix C v251/LINKER.COM
Normal file
BIN
Mix C v251/LINKER.COM
Normal file
Binary file not shown.
110
Mix C v251/MATRIX.C
Normal file
110
Mix C v251/MATRIX.C
Normal file
@ -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;n1<a;n1++) {
|
||||
for(n2=0;n2<a;n2++) {
|
||||
printf("\nx[%d][%d] is %f",n1,n2,x[n1][n2]);
|
||||
}
|
||||
}
|
||||
/* slash is at left hand end */
|
||||
for(n1=0;n1<a;n1++) {
|
||||
for(n2=0;n2<a;n2++) {
|
||||
if(n2==n1)
|
||||
y[n1][n2] = 1;
|
||||
else y[n1][n2] = 0;
|
||||
}
|
||||
}
|
||||
for(n1=0;n1<a;n1++) {
|
||||
for(n2=0;n2<a;n2++) {
|
||||
if(n2 != n1) {
|
||||
f=x[n2][n1]/x[n1][n1];
|
||||
for(n3=0;n3<a;n3++) {
|
||||
if(n3==n1)
|
||||
x[n2][n3]=0.0;
|
||||
if(n3 == 412)
|
||||
x[n2][n3] = 212.0;
|
||||
if(n3>n1)
|
||||
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;n1<a;n1++) {
|
||||
t = x[n1][n1];
|
||||
x[n1][n1] = 1.;
|
||||
for(n2=0;n2<a;n2++) {
|
||||
y[n1][n2] = y[n1][n2] / t;
|
||||
}
|
||||
}
|
||||
printf("\n\ninverse is");
|
||||
for(n1=0;n1<a;n1++) {
|
||||
for(n2=0;n2<a;n2++) {
|
||||
printf("\ny[%d][%d] is %f",n1,n2,y[n1][n2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
BIN
Mix C v251/NOSOURCE.MIX
Normal file
BIN
Mix C v251/NOSOURCE.MIX
Normal file
Binary file not shown.
262
Mix C v251/PRINTF.C
Normal file
262
Mix C v251/PRINTF.C
Normal file
@ -0,0 +1,262 @@
|
||||
#include "stdio"
|
||||
|
||||
printf(fs) /* standard routine */
|
||||
char *fs;
|
||||
{
|
||||
int putc();
|
||||
int _write();
|
||||
return _output(stdout, &fs, putc, _write);
|
||||
}
|
||||
|
||||
fprintf(fp, fs) /* standard routine */
|
||||
FILE *fp;
|
||||
char *fs;
|
||||
{
|
||||
int putc();
|
||||
int _write();
|
||||
return _output(fp, &fs, putc, _write);
|
||||
}
|
||||
|
||||
sprintf(s,fs) /* standard routine */
|
||||
char *s;
|
||||
char *fs;
|
||||
{
|
||||
int _mputc();
|
||||
int _mwrite();
|
||||
int status;
|
||||
status = _output(&s, &fs, _mputc, _mwrite);
|
||||
*s = '\0';
|
||||
return status;
|
||||
}
|
||||
|
||||
_output(fp, parms, putc, write) /* output routine for */
|
||||
/* printf, sprintf, fprintf */
|
||||
char *fp; /* pointer to destination */
|
||||
char **parms; /* pointer to addr of parameters */
|
||||
int (*putc)(); /* pointer to output function */
|
||||
int (*write)(); /* pointer to output function */
|
||||
{
|
||||
char buffer[80]; /* encode buffer */
|
||||
char *bufptr; /* pointer into buffer */
|
||||
char *format; /* format string pointer */
|
||||
char *cptr; /* pointer to character */
|
||||
char c; /* current character */
|
||||
char padchar; /* pad character */
|
||||
int ljflag; /* left justify flag */
|
||||
int length; /* length of output string */
|
||||
int precision; /* precision for reals */
|
||||
int flag; /* ftoa edit flag */
|
||||
int left; /* number digits left of decimal */
|
||||
int right; /* number digits right of decimal */
|
||||
int width; /* mimimum field width */
|
||||
int (*enc)(); /* pointer to encode function */
|
||||
int _eint(); /* encode integer function */
|
||||
int _elong(); /* encode long function */
|
||||
int _edouble(); /* encode double function */
|
||||
int _atoi(); /* convert string to integer */
|
||||
int _getformat(); /* get format flag */
|
||||
int strlen(); /* determine length of string */
|
||||
STRING *dynamic; /* dynamic string pointer */
|
||||
|
||||
format = *parms--; /* format points to format string */
|
||||
while (c = *format++) {
|
||||
if (c != '%') {
|
||||
if ((*putc)(c,fp) != EOF) continue;
|
||||
else return EOF;
|
||||
}
|
||||
precision = -1;
|
||||
bufptr = buffer;
|
||||
ljflag = 0;
|
||||
if (*format == '-') {
|
||||
++format;
|
||||
++ljflag;
|
||||
}
|
||||
padchar = (*format == '0') ? '0' : ' ';
|
||||
width = (isdigit(*format)) ? _atoi(&format) : 0;
|
||||
if ((*format) == '.') {
|
||||
++format;
|
||||
if (isdigit(*format)) precision = _atoi(&format);
|
||||
}
|
||||
enc = _eint;
|
||||
if (toupper(c = *format++) == 'L') {
|
||||
c = *format++;
|
||||
enc = _elong;
|
||||
--parms;
|
||||
}
|
||||
switch(toupper(c)) {
|
||||
|
||||
case 'D': /* signed decimal */
|
||||
(*enc)(parms, buffer, 10, 1);
|
||||
length = strlen(bufptr);
|
||||
break;
|
||||
|
||||
case 'U': /* unsigned decimal */
|
||||
(*enc)(parms, buffer, 10, 0);
|
||||
length = strlen(bufptr);
|
||||
break;
|
||||
|
||||
case 'O': /* unsigned octal */
|
||||
(*enc)(parms, buffer, 8, 0);
|
||||
length = strlen(bufptr);
|
||||
break;
|
||||
|
||||
case 'X': /* unsigned hexadecimal */
|
||||
(*enc)(parms, buffer, 16, 0);
|
||||
length = strlen(bufptr);
|
||||
break;
|
||||
|
||||
case 'S': /* string */
|
||||
bufptr = *parms;
|
||||
length = strlen(bufptr);
|
||||
break;
|
||||
|
||||
case 'C': /* character */
|
||||
cptr = parms;
|
||||
buffer[0] = *cptr;
|
||||
buffer[1] = '\0';
|
||||
length = 1;
|
||||
break;
|
||||
|
||||
case 'E': /* exponential */
|
||||
flag = 1;
|
||||
goto reals;
|
||||
|
||||
case 'F': /* fixed point */
|
||||
flag = 0;
|
||||
goto reals;
|
||||
|
||||
case 'G': /* no trailing 0's */
|
||||
/* fixed or exponential? */
|
||||
flag = _getformat(parms-3);
|
||||
|
||||
reals:
|
||||
left = 1;
|
||||
parms = parms - 3;
|
||||
if (precision == -1) right = 6;
|
||||
else right = precision;
|
||||
_edouble(parms, buffer, flag, left, right);
|
||||
precision = -1;
|
||||
length = 0;
|
||||
while (c=buffer[length]) {
|
||||
if (c == 'D') buffer[length] = 'E';
|
||||
++length;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'Y': /* dynamic string */
|
||||
dynamic = *parms;
|
||||
length = dynamic->length;
|
||||
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 */
|
||||
}
|
||||
|
BIN
Mix C v251/RUNTIME.OVY
Normal file
BIN
Mix C v251/RUNTIME.OVY
Normal file
Binary file not shown.
350
Mix C v251/SCANF.C
Normal file
350
Mix C v251/SCANF.C
Normal file
@ -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;
|
||||
}
|
||||
|
BIN
Mix C v251/SHRINK.COM
Normal file
BIN
Mix C v251/SHRINK.COM
Normal file
Binary file not shown.
35
Mix C v251/SIEVE.C
Normal file
35
Mix C v251/SIEVE.C
Normal file
@ -0,0 +1,35 @@
|
||||
/* sieve.c */
|
||||
|
||||
/* Eratosthenes Sieve Prime Number Program in C from Byte Jan 1983
|
||||
to compare the speed. */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
BIN
Mix C v251/SMALLCOM.OVY
Normal file
BIN
Mix C v251/SMALLCOM.OVY
Normal file
Binary file not shown.
BIN
Mix C v251/SPEEDUP.COM
Normal file
BIN
Mix C v251/SPEEDUP.COM
Normal file
Binary file not shown.
26
Mix C v251/STDIO.H
Normal file
26
Mix C v251/STDIO.H
Normal file
@ -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 >>> */
|
||||
|
1118
Mix C v251/STDLIB.C
Normal file
1118
Mix C v251/STDLIB.C
Normal file
File diff suppressed because it is too large
Load Diff
14
Mix C v251/STDLIB.H
Normal file
14
Mix C v251/STDLIB.H
Normal file
@ -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 >>> */
|
30
Mix C v251/TEST.C
Normal file
30
Mix C v251/TEST.C
Normal file
@ -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.
|
||||
*/
|
247
Mix C v251/TTT.C
Normal file
247
Mix C v251/TTT.C
Normal file
@ -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 <stdio.h>
|
||||
|
||||
#ifdef DOSTIME
|
||||
#include <time.h>
|
||||
#include <dos.h>
|
||||
#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*/
|
||||
|
121
Mix C v251/VAR.C
Normal file
121
Mix C v251/VAR.C
Normal file
@ -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("-----------------------------");
|
||||
}
|
BIN
Mix C v251/VAR.COM
Normal file
BIN
Mix C v251/VAR.COM
Normal file
Binary file not shown.
BIN
Mix C v251/VAR.MIX
Normal file
BIN
Mix C v251/VAR.MIX
Normal file
Binary file not shown.
BIN
Mix C v251/VAR.SYM
Normal file
BIN
Mix C v251/VAR.SYM
Normal file
Binary file not shown.
BIN
Mix C v251/VAR.TRC
Normal file
BIN
Mix C v251/VAR.TRC
Normal file
Binary file not shown.
3
Mix C v251/m.bat
Normal file
3
Mix C v251/m.bat
Normal file
@ -0,0 +1,3 @@
|
||||
ntvdm cc %1.c
|
||||
ntvdm linker %1
|
||||
|
Loading…
Reference in New Issue
Block a user