хжс ж яя ь вя я ь пТвв C

Programmatismì me th gl¸ssa C
Ergasthriakè ask sei gia to mˆjhma
{Plhroforik II}1
Miq. Q. Drakìpoulo
1 EPEAEK.
IIPrìgramma Anabˆjmish Proptuqiak¸n Spoud¸n
1
Plhroforik II
**Egkatastash Compiler GCC
Arqea apì:
http://www.delorie.com/djgpp/zip-picker.html
skètte s to Ergas t rio)
1. Dhmiourga
directory
(fakèllou)
C:\DJGPP
unzip)
2. Oi 4 diskètte xe-paketˆrontai (
3. Tropopohsh tou arqeou
s to
AUTOEXEC.BAT
C:\DJGPP
me 2 nèe grammè:
SET PATH=%PATH%;C:\DJGPP\BIN
SET DJGPP=C:\DJGPP\DJGPP.ENV
4.
REBOOT.
5.
SETDJGPP C:\DJGPP C:/DJGPP
**Genike Odhgie
•
Dhmiourga arqeou progrˆmmato:
EDIT myfile.c
PROSOQH: L gei se:
.c
(!!!)
• Compilation:
gcc -o myfile.exe myfile.c
An den upˆrqoun lˆjh dnei:
•
myfile.exe
Ektèlesh progrˆmmato:
myfile.exe, INPUT
apo plhktrolìgio.
myfile.exe < anyfile
,
INPUT
apo arqeo
anyfile
( 4 di-
2
Plhroforik II
**Askhsh 1
Sto prìgramma metatrop Fahrenheit-Celsius
s thn epìmenh selda, na pro-
s tejoÔn entolè ektÔpwsh, pou na parˆgoun:
Pinakas Metatrophs
==================
0F -17.8C
-----------------20F
-6.7C
-----------------.......
-----------------120F
48.9C
-----------------#include <stdio.h>
/* pinakas metatrophs Fahrenheit-Celsius */
main()
{
int fahr;
float celsius;
for (fahr = 0; fahr <= 120; fahr = fahr + 20)
celsius = (5.0/9.0)*(fahr - 32.0);
}
**Apˆnthsh
#include <stdio.h>
/* pinakas metatrophs Fahrenheit-Celsius */
main()
{
int fahr, i;
3
Plhroforik II
printf("Pinakas Metatrophs\n==================\n");
for (fahr = 0; fahr <= 120; fahr = fahr + 20) {
printf("%8dF %6.1fC\n", fahr, (5.0/9.0)*(fahr-32.0));
printf("-----------------\n");
}
}
**Askhsh 2
Domikˆ stoiqea
Na grafe prìgramma metatrop kefalawn se mikroÔ qarakt re.
• c = getchar(),
gia
• putchar(c),
c
gia
c
akèraio qarakt ra
akèraio qarakt ra
• while ((c = getchar()) != EOF )
•
Sto
ASCII
sÔnolo qarakt rwn:
’A’<’B’< . . . <’Z’
’a’<’b’< . . . <’z’
• ’a’, ’b’, ..., ’A’
klp, mikro akèraioi!!!
**Apˆnthsh
#include <stdio.h>
/* Metatroph INPUT apo kefalaia se mikra */
main()
{
int c;
while ((c = getchar()) != EOF)
if (c >= ’A’ && c <= ’Z’)
Plhroforik II
4
putchar(c - ’A’ + ’a’);
else
putchar(c);
}
**Askhsh 3
rot13. Kwdikopoie INPUT,
(= ’A’ + 13)
(= ’B’ + 13)
Programma kwdikopohsh
’A’ ←→
’B’ ←→
’M’
’a’
’b’
←→
←→
←→
’m’
←→
’N’
’O’
...
’Z’
’n’
’o’
...
’z’
metatrèponta:
(= ’M’ + 13)
(= ’a’ + 13)
(= ’b’ + 13)
(= ’m’ + 13)
**Apˆnthsh
#include <stdio.h>
/* Kwdikopoihsh me rot-13 */
main()
{
int c;
while ((c = getchar()) != EOF)
if ((c>=’a’ && c<=’m’) || (c>=’A’ && c<=’M’))
putchar(c+13);
else if ((c>=’n’ && c<=’z’) || (c>=’N’ && c<=’Z’))
putchar(c-13);
else
putchar(c);
}
**Askhsh 4
Parˆs tash me qarakt re th
sin(x).
5
Plhroforik II
*
*******
************
***************
****************
***************
************
*******
*
*******
************
***************
****************
***************
************
*******
*
Stoiqea
•
Na qwrˆei s thn ojình (dias tˆsei: 80 s t le
•
Parametrikì, me dunatìthta ektÔpwsh se
>
×
22 grammè).
22 grammè, gia akribè-
s terh apeikìnish.
•
Sunˆrthsh biblioj kh gia hmitìno:
sin(x)
me
• Compilation: gcc sine.c -o sine.exe -lm
•
Alle kainotome euprìsdekte.
**Apˆnthsh
#include <stdio.h>
#include <math.h>
#define MAXX 22
#define MAXY 39
x double.
Plhroforik II
6
#define PI 3.14159
/* SIN(X) ME XARAKTHRES */
main()
{
int i, x, y;
for (x = 0; x <= MAXX ; x++ ) {
y = MAXY*sin(x*2*PI/MAXX);
if (y < 0) {
for (i = 0; i < MAXY+y; i++)
printf(" ");
for (i = MAXY+y; i <= MAXY; i++)
printf("*");
printf("\n");
} else if(y >= 0) {
for (i = 0; i < MAXY; i++)
printf(" ");
for (i = MAXY; i <= MAXY+y; i++)
printf("*");
printf("\n");
}
}}
**Askhsh 5
Gia thn akolouja arijm¸n
Fibonacci:
f1 = 1, f2 = 1, fi = fi−1 + fi−2 ,
i∈N
Prìgramma pou na upologzei tou 10 pr¸tou ìrou. Sugkekrimmèna:
1. Sunˆrthsh
FiboIter(n)
pou upologzei ton
n-os tì
ìro qwr anadro-
m .
2. Sunˆrthsh
FiboRec(n)
gia to
n-ios tì
ìro me anadrom 7
Plhroforik II
3.
main
pou na ti kale kai na ektup¸nei tou 10 pr¸tou ìrou.
4. Oi sunart sei se diaforetikˆ arqea apì
main.
int FiboIter(int n)
{
int fo, f;
fo = 1;
f = 1;
while (n-- > 2) {
f += fo;
fo = f - fo;
}
return f;
}
int FiboRec(int n)
{
if (n < 3)
return 1;
else {
return FiboRec(n-2)+FiboRec(n-1);
--n;
}
}
#include <stdio.h>
#define N 10
int FiboRec(int);
main()
{
int i;
8
Plhroforik II
for (i = 1; i <= N; i++)
printf(" %d", FiboRec(i));
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
main()
{
int n, i;
float sum, *x, *y;
printf("Diastash N?\t");
scanf("%d", &n);
x = (float *) malloc(n*sizeof(float));
y = (float *) malloc(n*sizeof(float));
printf("Dianusma 1?\t");
for (i = 0; i < n; i++)
scanf("%f", &x[i]);
printf("Dianusma 2?\t");
for (i = 0; i < n; i++)
scanf("%f", &y[i]);
sum = 0.0;
for (i = 0; i < n; i++)
sum += x[i]*y[i];
printf("X’*Y = %f\n", sum);
}
**Askhsh
Prìgramma
cal
ektÔpwsh hmerologou m na gia èth
C> cal 2 1999
Febrouarios 1999
Ku De Tr Te Pe Pa Sa
1 2 3 4 5 6
≤ 1900.
9
Plhroforik II
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28
C> cal 13 1999
cal: epitreptes times gia mhna: 1-12
C> cal 5 99
cal: epitreptes times gia xrono: 1900-9999
C> cal
klhsh: cal mhnas xronos
#include <stdio.h>
#include <stdlib.h>
#define
#define
#define
#define
#define
#define
#define
KURIAKH
DEUTERA
TRITH
TETARTH
PEMPTH
PARASKEUH
SABBATO
0
1
2
3
4
5
6
int EinaiDisektos(int)
char *OnomaMhna(int)
int MeresStoMhna(int, int)
int PrwthMera(int, int)
void EktupwshMhna(int, int)
main(int argc, char *argv[])
{
int mhnas, xronos;
if (argc != 3) {
printf("klhsh: %s mhnas xronos\n",
argv[0]);
exit(-1);
}
mhnas = atoi(argv[1]);
Plhroforik II
xronos = atoi(argv[2]);
if (mhnas < 1 || mhnas > 12) {
printf("%s: epitreptes times gia mhna:
1-12\n", argv[0]);
exit(-1);
}
if (xronos < 1900 || xronos > 9999) {
printf("%s: epitreptes times gia xrono:
1900-9999\n", argv[0]);
exit(-1);
}
EktupwshMhna(mhnas, xronos);
}
int EinaiDisektos(int etos)
{
return ((etos%4 == 0 && etos%100 != 0)
|| etos%400 == 0);
}
int MeresStoMhna(int mhnas, int xronos)
{
switch (mhnas) {
case 2:
if (EinaiDisektos(xronos))
return 29;
return 28;
case 4: case 6: case 9: case 11:
return 30;
default:
return 31;
}
}
char *OnomaMhna(int mhnas)
10
Plhroforik II
{
switch (mhnas) {
case 1: return "Ianouarios";
case 2: return "Febrouarios";
case 3: return "Martios";
case 4: return "Aprilios";
case 5: return "Maios";
case 6: return "Iounios";
case 7: return "Ioulios";
case 8: return "Augoustos";
case 9: return "Septembrios";
case 10: return "Oktwbrios";
case 11: return "Noembrios";
case 12: return "Dekembrios";
}
}
int PrwthMera(int mhnas, int xronos)
{
int mera, i;
mera = DEUTERA;
for (i = 1900; i < xronos ; i++) {
mera = (mera + 365) % 7;
if (EinaiDisektos(i))
mera = (mera + 1) % 7;
}
for (i = 1; i < mhnas; i++)
mera = (mera + MeresStoMhna(i, xronos)) % 7;
return mera;
}
void EktupwshMhna(int mhnas, int xronos)
{
11
Plhroforik II
int i, mera, nmeres;
printf("
%s %d\n", OnomaMhna(mhnas),
xronos);
printf(" Ku De Tr Te Pe Pa Sa\n");
nmeres = MeresStoMhna(mhnas, xronos);
mera = PrwthMera(mhnas, xronos);
for (i = KURIAKH; i < mera; i++)
printf("
");
for (i = 1; i <= nmeres; i++) {
printf(" %2d", i);
if (mera == SABBATO)
printf("\n");
mera = (mera + 1) % 7;
}
if (mera != KURIAKH)
printf("\n");
}
#include <stdio.h>
main(int argc, char *argv[])
{
int ifile, nchar;
FILE *fp;
char *file;
char *prog = argv[0];
for (ifile = 1; ifile < argc; ifile++) {
file = argv[ifile];
fp = fopen(file, "r");
if (fp == NULL) {
fprintf(stderr, "%s: can’t open %s\n", prog, file);
exit(1);
} else {
nchar = 0;
while (getc(fp) != EOF)
12
Plhroforik II
++nchar;
printf("%-12s\t%8d bytes\n", file,
nchar*sizeof(char));
fclose(fp);
}
}
exit(0);
}
13