Language Turbo C 2.0
Simple programs */
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main ()
{
int a, b, c;
unsigned d;
char g;
float s, t;
char j [20], k [20];
char m [5] [20];
int p [10] = {1, 23, 4, 7, 8, 0, 1, 9, 4, 7};
float *v;
int *y, *z;
a = 1996;
printf ("42.1. Now is %i year.\n", a);
printf ("50.7. ");
for (a = 10; a; a--) printf ("%i ", a);
printf ("start\n");
d = 60000;
a = d;
printf ("54.8. a = %i, d = %u.\n", a, d);
g = 'c';
printf ("59.9. %c", g);
g = '+';
printf ("%c%c\n", g, g);
a = 5;
b = 60;
a++;
++b;
printf ("68.14. a = %i, b = %i.\n", a, b);
printf ("a = %i, b = %i.\n", a++, ++b);
a = 101 <= 105;
b = 101 > 105;
printf ("70.15. true - %i, false - %i.\n", a, b);
printf ("81.20. ");
for (a = 0; a < 100; a++)
{
printf ("%i - %i ", a, a * a * a);
if (a * a * a > 10000) break;
}
printf ("\n82.21. ");
for (a = 0; a < 100; a++)
{
if (a % 7) continue;
printf ("%i ", a);
}
printf ("\n86.22. ");
strcpy (j, "Hello, ");
strcpy (k, "world.");
puts (j);
puts (k);
strcat (j, k);
puts (j);
puts (k);
strcpy (m [0], "Turbo Basic");
strcpy (m [1], "Turbo Pascal");
strcpy (m [2], "Borland C++");
strcpy (m [3], "Turbo Prolog");
strcpy (m [4], "Paradox");
printf ("89.24. Unsorted array: ");
for (a = 0; a < 10; a++) printf ("%i ", p [a]);
for (a = 0; a < 9; a++)
for (b = 0; b < 9 - a; b++)
if (p [b] > p [b + 1])
{
c = p [b];
p [b] = p [b + 1];
p [b + 1] = c;
}
printf ("\nSorted array: ");
for (a = 0; a < 10; a++) printf ("%i ", p [a]);
s = 10.1;
v = &s;
t = *v;
printf ("\n91.25. s = %g, t = %g.\n", s, t);
(*v)++;
printf ("s = %g, t = %g.\n", s, t);
t = 1 + *v * t;
printf ("s = %g, t = %g.\n", s, t);
a = 10;
y = &a;
z = y;
printf ("91.26. %p, %p; %i, %i.\n", y, z, a, *z);
s = 10.1;
y = (int *) &s;
t = *y;
printf ("92.27. s = %g, t = %g.\n", s, t);
a = 10;
y = &a;
printf ("92.28. %p, ", y);
y++;
printf ("%p.\n", y);
printf ("120.36. ");
z = y = malloc (40 * sizeof (int));
if (!y)
{
printf ("Insufficient memory.\n");
exit (1);
}
for (a = 0; a < 40; a++) *(y + a) = a;
for (a = 0; a < 40; a++) printf ("%i ", *y++);
free (z);
printf ("\n139.37. %s, %i, %s, %s.\n",
__FILE__, __LINE__, __DATE__, __TIME__);
printf ("Using version '%x' of Turbo C.\n", __TURBOC__);
getch ();
printf ("\n");
}
- - - - - - - - - - - - - - -
/* p45
Language Turbo C 2.0
Input */
#include <conio.h>
#include <stdio.h>
#include <string.h>
main ()
{
int a, b; /* input integer */
float d; /* input float */
char p; /* input character */
char s [80], t [80], u [80]; /* input string */
float g = 3.141592653589; /* number 'pi' */
int j;
float m, n;
printf ("Input 2 integer numbers: ");
scanf ("%i%i", &a, &b);
printf ("Input float number: ");
scanf ("%f", &d);
printf ("Press any key: ");
p = getche ();
printf ("\nInput string: ");
gets (s); gets (s);
printf ("Input string: ");
scanf ("%[0123456789]%s", t, u);
printf ("\n");
m = g * a * 2;
printf ("45.2. Radius %i, length %.4g.\n", a, m);
m = g * d * 2;
n = g * d * d;
printf ("46.3. Radius %g, length %.4g, area %.4g.\n", d, m, n);
printf ("60.10. ");
if (p == 'a') printf ("You have pressed key 'a'.\n");
printf ("... pressed key '%c'.\n", p);
printf ("61.11. I know your name. Your name is %s.\n", s);
printf ("65.12. %s... %s.\n", t, u);
j = b;
if (!j) j = 1;
printf ("68.13. Integer part %i, reminder of division %i.\n",
a / j, a % j);
printf ("75.16. Number %g is ", d);
if (d > 0) {j = 1; printf ("positive, j = %i.\n", j);}
if (d < 0) {j = -1; printf ("negative, j = %i.\n", j);}
if (d == 0) {j = 0; printf ("equal to zero, j = %i.\n", j);}
printf ("75.17. Number %g is ", d);
if (d > 0) {j = 1; printf ("positive.\n");}
else if (d < 0) {j = -1; printf ("negative.\n");}
else {j = 0; printf ("equal to zero.\n");}
printf ("77.18. ");
if (p >= 'А' && p <= 'Я')
switch (p)
{
case 'А': printf ("Алексеев.\n"); break;
case 'Б': printf ("Булгаков.\n"); break;
case 'В': printf ("Волошин.\n"); break;
case 'Г': printf ("Гоголь.\n"); break;
default: printf ("Достоевский, Зощенко и др.\n");
}
else printf ("You should input russian capital letter.\n");
printf ("77.19. ");
if (p >= 'А' && p <= 'Я')
switch (p)
{
case 'А': printf ("Алексеев.\n");
case 'Б': printf ("Булгаков.\n");
case 'В': printf ("Волошин.\n");
case 'Г': printf ("Гоголь.\n");
default: printf ("Достоевский, Зощенко и др.\n");
}
else printf ("You should input russian capital letter.\n");
printf ("86.23. String '%s' has length %i symbols.\n", s, strlen (s));
getch ();
printf ("\n");
}
- - - - - - - - - - - - - - -
/* p47
Language Turbo C 2.0
Extern functions */
#include <conio.h>
#include <stdio.h>
/* Declaration of functions */
a (); b (); c ();
v (); w (); x ();
d (float);
float j (float);
float k (float);
s (int *);
t (int *);
y (const char *);
float g = 3.141592653589; /* number 'pi' */
main ()
{
int m;
float p;
printf ("47.4. ");
a (); b (); c ();
m = 5;
printf ("47.5. ");
d (m);
printf ("49.6. Length of circle of radius %i equals %.4g.\n", m, j (m));
p = 5.2;
printf ("100.29. Square of number %g is equal to %g.\n", p, k (p));
m = 10;
s (&m);
m = 10;
t (&m);
v ();
printf ("102.33. ");
m = 1;
w ();
printf ("In function 'main ()' value of 'm' is %i.\n", m);
printf ("106.34. ");
for (m = 1; m < 4; m++)
{
printf ("Call number %i.\n", m);
x ();
printf ("Call number %i.\n", m);
x ();
}
printf ("118.35. ");
y ("This is test");
printf ("\n");
getch ();
printf ("\n");
}
a () {printf ("Call of first function.\n");}
b () {printf ("Call of second function.\n");}
c () {printf ("Call of third function.\n");}
d (float a)
{
printf ("Length of circle of radius %g equals %.4g.\n", a, 2 * g * a);
}
float j (float a) {return 2 * g * a;}
float k (float a) {return a * a;}
s (int *a) {return *a = *a * *a;}
t (int *a) {*a = *a * *a;}
v ()
{
int a;
for (a = 0; a < 80; a++) printf ("-");
}
w ()
{
int m;
m = 10;
printf ("In function 'w ()' value of 'm' is %i.\n", m);
}
x ()
{
int a = 1;
static b = 1;
printf ("a = %i, b = %i.\n", a++, b++);
}
y (const char *a) {while (*a) printf ("%c", *a++ + 1);}
- - - - - - - - - - - - - - -
/* p55
Language Turbo C 2.0
Input and extern functions */
#include <conio.h>
#include <math.h>
#include <stdio.h>
char a;
/* Functions declaration */
g (int);
float j (float);
float k (float);
float l (float);
float m (float (*d) (float));
float n (double (*d) (double));
main ()
{
int d;
float p, q;
float (*s) (float);
double (*t) (double);
printf ("55.42. Press any key: ");
a = getche ();
printf ("\nInput integer: ");
scanf ("%i", &d);
g (d);
p = 1.1;
s = j;
printf ("\n\n116.43. ");
q = (*s) (p);
printf ("(5) p = %.4g, q = %.4g.\n", p, q);
q = s (p);
printf ("(6) p = %.4g, q = %.4g.\n", p, q);
t = sin;
q = t (p);
printf ("(7) p = %.4g, q = %.4g.\n", p, q);
q = (*t) (2.5);
printf ("(8) p = %.4g, q = %.4g.\n", p, q);
q = m (k);
printf ("(9) p = %.4g, q = %.4g.\n\n", p, q);
q = n (cos);
printf ("(10) p = %.4g, q = %.4g.\n", p, q);
q = m (s);
printf ("(11) p = %.4g, q = %.4g.\n", p, q);
getch ();
printf ("\n");
}
g (int d)
{
int e;
if (d > 80) d = 80;
for (e = 0; e < d; e++) printf ("%c", a);
}
float j (float d)
{
puts ("(1) In j ().");
return d;
}
float k (float d)
{
puts ("(2) In k ().");
return d * d;
}
float l (float d)
{
puts ("(3) In l ().");
return d * d * d;
}
float m (float (*d) (float e))
{
printf ("(4) In m (), %.4g.\n", d (3));
return d (3);
}
float n (double (*d) (double e))
{
printf ("(12) In n (), %.4g.\n", d (3.01));
return d (3.01);
}
- - - - - - - - - - - - - - -
/* p79
Language Turbo C 2.0
Repeated input */
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main ()
{
char a;
int d, e, f;
char *p;
int *s;
int **v;
char g [80];
char *j [] = {"exe", "com", "dat", "c", "pas", "cpp"};
char m [] = "String of Letters in Different Registers.";
printf ("79.44. Press the key 'Q': ");
while (1)
{
a = getche ();
if (a == 'Q') break;
}
d = 0;
e = random (100) + 1;
printf ("\n80.45. Input number from 1 to 100: ");
do {
scanf ("%i", &f);
d++;
if (e < f) printf ("Hidden number is less. ");
if (e > f) printf ("Hidden number is big. ");
} while (e - f);
printf ("You guessed the number. The number is %i.\n", e);
printf ("You have spent for guessing %i attempts.\n", d);
printf ("85.46. Input string: ");
gets (g); gets (g);
printf ("String: '%s'.\n", g);
printf ("Input string: ");
scanf ("%s", g);
printf ("String: ");
puts (g);
printf ("95.47. Files with extension:\n");
printf ("1. exe 2. com 3. dat 4. c 5. pas 6. cpp 7. quit\n");
printf ("Your choice: ");
while (1)
{
do
a = getche ();
while (a < '1' || a > '7');
printf ("\n");
if (a == '7') break;
strcpy (g, "dir *.");
strcat (g, j [a - '1']);
system (g);
}
printf ("81.48.\n");
for (d = 0; d < 10; d++)
{
for (e = 0; e < 6; e++) printf ("%i * %i = %-4i", d, e, d * e);
printf ("\n");
}
d = 0;
printf ("94.49. String will be printed by capital letters.\n");
while (m [d]) printf ("%c", toupper (m [d++]));
p = m;
printf ("\nString will be printed by small letters.\n");
while (*p) printf ("%c", tolower (*p++));
d = 7;
s = &d;
v = &s;
printf ("\n96.50. d = %i, s = %p, v = %p.\n", d, s, v);
(*s)++;
printf ("d = %i, s = %p, v = %p.\n", d, s, v);
**v = 12;
printf ("d = %i, s = %p, v = %p.\n", d, s, v);
getch ();
printf ("\n");
}
- - - - - - - - - - - - - - -
/* p107
Language Turbo C 2.0
Extern functions */
#include <conio.h>
#include <stdio.h>
int a = 7;
/* Functions declaration */
d (); e ();
g (int, int);
j (int *, int *);
m (int a [], int);
s (int a [3] [3], int b [3] [3], int c [3] [3]);
main ()
{
int a = 10;
int b;
int p [10] = {1, 3, -5, 7, 9, 0, 22, 4, 6, 8};
int v [3] [3] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int w [3] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int x [3] [3];
printf ("107.38. (1) a = %i ", a);
a++;
{
int a = 200;
printf ("(2) a = %i ", a);
a++;
}
printf ("(3) a = %i ", a++);
d ();
printf ("(4) a = %i ", a++);
e ();
printf ("(5) a = %i ", a++);
e ();
printf ("(6) a = %i.\n", a++);
a = 5;
b = 10;
printf ("108.39. At first a = %i, b = %i.\n", a, b);
g (a, b);
printf ("Now a = %i, b = %i.\n", a, b);
printf ("Nothing changed.\n");
j (&a, &b);
printf ("Now a = %i, b = %i.\n", a, b);
printf ("Values exchanged.\n");
printf ("109.40. Before sorting: ");
for (a = 0; a < 10; a++) printf ("%i ", p [a]);
m (p, 10);
printf ("\nAfter sorting: ");
for (a = 0; a < 10; a++) printf ("%i ", p [a]);
s (v, w, x);
printf ("\n110.41. Array x:\n");
for (a = 0; a < 3; a++)
printf ("%i %i %i\n", x [a] [0], x [a] [1], x [a] [2]);
getch ();
printf ("\n");
}
d ()
{
int a = 77;
printf ("(7) a = %i ", a);
a++;
}
e ()
{
printf ("(8) a = %i ", a);
a++;
}
g (int a, int b)
{
int c;
c = a; a = b; b = c;
}
j (int *a, int *b)
{
int c;
c = *a; *a = *b; *b = c;
}
m (int a [], int d)
{
int g, h, i;
for (g = 0; g < d - 1; g++)
for (h = 0; h < d - g - 1; h++)
if (a [h] > a [h + 1])
{
i = a [h];
a [h] = a [h + 1];
a [h + 1] = i;
}
}
s (int a [3] [3], int b [3] [3], int c [3] [3])
{
int d, e, f;
for (d = 0; d < 3; d++)
for (e = 0; e < 3; e++)
{
c [d] [e] = 0;
for (f = 0; f < 3; f++)
c [d] [e] += a [d] [f] * b [f] [e];
}
}
- - - - - - - - - - - - - - -
/* p112
Language Turbo C 2.0
Arguments of function "main" */
#include <conio.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#define J 100
#define K 1
#define L 2
jmp_buf m;
p (); q (); r (); s ();
main (int a, char *d [], char *e [])
{
int g;
printf ("112.51. ");
if (a < 2)
printf ("You have forgot to type your name.\n");
else
printf ("Hello, %s.\n", d [1]);
printf ("112.52. Number of arguments of command line: %i.\n", a);
printf ("Arguments of command line: ");
for (g = 0; g < a; g++) printf ("%s ", d [g]);
printf ("\nArguments of environment state: ");
for (g = 0; e [g]; g++) printf ("%s ", e [g]);
printf ("\n135.53. ");
#if J > 99
printf ("J is greater than 99.\n");
#else
printf ("J equals %i.\n", J);
#endif
printf ("121.54. (1) We call function 'p ()'.\n");
p ();
fcloseall ();
printf ("(2) End of function 'main ()'.\n");
getch ();
printf ("\n");
}
p ()
{
int a;
printf ("(3) Function 'p ()'.\n");
if ((a = setjmp (m)) != 0)
{
switch (a)
{
case K: printf ("Out of memory.\n"); return 0;
case L: printf ("Can't open file.\n"); return 0;
}
}
printf ("Result is %i.\n", q ());
return 0;
}
q ()
{
printf ("(4) Function 'q ()'.\n");
r ();
return 0;
}
r ()
{
void *a;
printf ("(5) Function 'r ()'.\n");
if ((a = malloc (4096)) == NULL) longjmp (m, K);
s ();
free (a);
return 1;
}
s ()
{
FILE *a;
printf ("(6) Function 's ()'.\n");
if ((a = fopen ("z.txt", "r")) == NULL) longjmp (m, L);
fclose (a);
return 2;
}
- - - - - - - - - - - - - - -
/* p150
Language Turbo C 2.0
Text screen */
#include <conio.h>
main ()
{
int a, b;
char d [288];
char g [] = "This is a string";
/* p150 */
textmode (C80);
clrscr ();
for (a = BLUE; a < WHITE; a++)
{
textcolor (a);
for (b = BLACK; b <= LIGHTGRAY; b++)
{
textbackground (b);
cprintf (" тест ");
}
}
textcolor (WHITE | BLINK);
textbackground (BLACK);
cprintf (" конец теста ");
textmode (LASTMODE);
getch ();
/* p152 */
textbackground (BLACK);
window (5, 5, 20, 10);
textattr ((GREEN << 4) + RED);
clrscr ();
cputs ("Hello, world");
textattr ((GREEN << 4) + RED + BLINK);
cputs ("Press any key");
getch ();
window (1, 1, 80, 25);
gettext (4, 4, 21, 11, d);
textbackground (BLUE);
textcolor (WHITE);
for (a = 1; a < 23; a++)
{
gotoxy (1, a);
cputs (g); cputs (g); cputs (g); cputs (g);
}
getch ();
puttext (24, 5, 41, 12, d);
getch ();
for (a = 1; a < 5; a++) movetext (22, 4, 32, 8, 10 * a, 17);
normvideo ();
getch ();
/* p53 */
gotoxy (40, 13);
putch ('*');
getch ();
/* p54 */
window (1, 1, 40, 12);
textbackground (LIGHTGRAY);
clrscr ();
window (41, 1, 80, 12);
textbackground (RED);
clrscr ();
window (1, 13, 40, 25);
textbackground (RED);
clrscr ();
window (41, 13, 80, 25);
textbackground (GREEN);
clrscr ();
getch ();
/* p55 */
textbackground (LIGHTGRAY);
clrscr ();
gotoxy (1, 5);
for (a = 0; a <= 15; a++)
{
textcolor (a);
cprintf ("%i ", a);
}
getch ();
window (1, 1, 80, 25);
textbackground (BLACK);
textcolor (LIGHTGRAY);
clrscr ();
}
Присоединяйтесь — мы покажем вам много интересного
Присоединяйтесь к ОК, чтобы подписаться на группу и комментировать публикации.
Нет комментариев