Δ.Α.Π./ΔΙΑΓΩΓΗ ΣΗΝ ΠΛΗΡΟΦΟΡΙΚΗ 3η ΓΡΑΠΣΗ ΔΡΓΑΙΑ ΑΚΑΓΗΜΑΪΚΟΤ ΔΣΟΤ 2009-2010 4 ος Σόμος ΓΛΩΔ ΠΡΟΓΡΑΜΜΑΣΙΜΟΤ ΔΝΓΔΙΚΣΙΚΑ ΥΔΓΙΑ ΛΤΔΩΝ - ΤΠΟΓΔΙΞΔΙ
ΤΠΟΔΡΓΑΙΑ 1 Α. (θέση1) y=x++; (θέση2) y=++x; Β. #include <string.h> #define N 25 typedef struct char onoma[30]; /* Ολνκαηεπώλπκν */ float imeromisthio; /* Ηκεξνκίζζην */ int imeres; /* Μέξεο εξγαζίαο */ float miktes; /* Μηθηέο απνδνρέο */ float asfal; /* Αζθαιηζηηθέο θξαηήζεηο */ float foros; /* Φόξνο */ float kathares; /* Καζαξέο απνδνρέο */ erg; erg ypal[n]; int i, flag, j; /* Αλάγλσζε δεδνκέλσλ */ for (i=0; i<n; i++) printf("\n Doste ta stoixeia toy %doy ypalliloy: ", i+1); printf("\n Onomateponymo: "); do gets(ypal[i].onoma); flag=0; for (j=0; j<i; j++) if (!strcmp(ypal[j].onoma, ypal[i].onoma)) flag=1; if (flag) printf( Prepei to onoma na mhn exei epanalhfthei.\n Parakalo ksanaprospathiste \n ); while (flag); printf("\n Hmeromisthio: "); scanf("%f", &ypal[i].imeromisthio); while (ypal[i].imeromisthio <=0) /* Ακπληηθόο πξνγξακκαηηζκόο */ printf("to hmeromisthio prepei na einai thetiko"); scanf("%f", &ypal[i].imeromisthio); printf("\n Meres ergasias: "); scanf("%d", &ypal[i].imeres); while (ypal[i].imeres <=0) /* Ακπληηθόο πξνγξακκαηηζκόο */ printf("o arithmos hmeron ergasias prepei na einai thetikos"); scanf("%d", &ypal[i].imeres); ελίδα 2 από 12
getchar(); ypal[i].miktes = ypal[i].imeromisthio * ypal[i].imeres; ypal[i].asfal = ypal[i].miktes * 0.15; ypal[i].foros = ypal[i].miktes * 0.2; ypal[i].kathares = ypal[i].miktes - ypal[i].asfal - ypal[i].foros; /* Δθηύπσζε απνηειεζκάησλ */ for (i=0; i<n; i++) printf("\n Onomateponymo: %s", ypal[i].onoma); printf("\n Miktes apodoxes: %f", ypal[i].miktes); printf("\n Asfalistikes krathseis: %f", ypal[i].asfal); printf("\n Foros: %f", ypal[i].foros); printf("\n Kathares apodoxes: %f \n", ypal[i].kathares); Δπηζεκαίλεηαη όηη ζηνλ θώδηθα πνπ δίλεηαη, ζηε δηαδηθαζία αλάγλσζεο ησλ δεδνκέλσλ εηζόδνπ, κεηά από ην ηειεπηαίν scanf(), θαινύκε ηε ζπλάξηεζε getchar(). Η ζπγθεθξηκέλε θιήζε θαλνληθά έπξεπε λα είλαη πεξηηηή, όκσο ε scanf() αθήλεη ζηνλ buffer εηζόδνπ ηνλ ραξαθηήξα \n θαη ρξεηάδεηαη λα θαζαξηζηεί ν buffer, ώζηε λα πξαγκαηνπνηήζεη ε gets() πνπ ζα θιεζεί ηε ζσζηή εηζαγσγή δεδνκέλσλ. O θαζαξηζκόο ηνπ buffer πξαγκαηνπνηείηαη κε ηελ θιήζε ηεο ζπλάξηεζεο getchar(). Γ. #define N 3 #define M 3 /* πλάξηεζε εληνπηζκνύ ηνπ κηθξόηεξνπ ζεηηθνύ αξηζκνύ αλά ζηήιε */ void find_smaller(int array[][m], int n) int i, j, min; for (j=0; j<m; j++) min=0; while (array[min][j]<=0) min++; for (i=min; i<n; i++) if ((array[i][j]>0)&&(array[i][j]<array[min][j])) min=i; /* Δύξεζε ειαρίζηνπ */ if (min < n) printf("%d\n", array[min][j]); else printf("den yparxei thetiko stoixeio sthn %d sthlh\n", j+1); /* πλάξηεζε ππνινγηζκνύ ηνπ κέζνπ όξνπ ησλ ζεηηθώλ ζηνηρείσλ θάζε γξακκήο */ void average(int array[][m], int n) int i, j, count; float average; ελίδα 3 από 12
for (i=0; i<n; i++) average=0; count=0; for (j=0; j<m; j++) if (array[i][j]>0) average+=array[i][j]; count++; /* Δύξεζε κέζνπ όξνπ */ if (count) average=average/count; printf("%f\n", average); else printf("den yparxei thetiko stoixeio sth %d grammh\n",i+1); int array[n][m]; int i, j; printf("dose ta stoixeia tou pinaka\n"); for (i=0; i<n; i++) /* Δηζαγσγή ησλ ζηνηρείσλ ηνπ πίλαθα */ for (j=0; j<m; j++) scanf("%d", &array[i][j]); find_smaller(array, N); average(array, N); ΤΠΟΔΡΓΑΙΑ 2 Α. (1.) #include <math.h> int n; double e_calc, e_theor; e_theor = 2.718281828459045; /* Γνκή επαλάιεςεο γηα ηνλ επαλαιεπηηθό ππνινγηζκό, γηα δηάθνξεο ηηκέο ηνπ n, ηνπ πξνζεγγηζηηθνύ ηύπνπ */ for (n = 1; n <=25; n++) e_calc = pow((1.0 + 1.0/(double) n), n); printf("%5d %18.15f %5.2f%%\n", n, e_calc, (e_theor-e_calc)/e_theor*100); ελίδα 4 από 12
Α. (2.) #include <math.h> int n; float desired_diff; double e_calc, e_theor; double diff; e_theor = 2.718281828459045; do printf("insert desired difference (percent)\n"); scanf("%f", &desired_diff); while (desired_diff <= 0); n = 0; /* Γνκή επαλάιεςεο γηα επαλαιεπηηθό ππνινγηζκό ηεο πξνζεγγηζηηθήο ηηκήο ηνπ e, κέρξηο όηνπ ε % δηαθνξά από ηε δνζκέλε ηηκή e ζ λα γίλεη ίζε ή κηθξόηεξε από ηελ ηηκή πνπ εηζάγεηαη από ηνλ ρξήζηε. */ do n++; e_calc = pow((1.0 + 1.0/(double) n), n); diff = (e_theor-e_calc)/e_theor*100; while (diff > desired_diff); printf(" n = %d percent difference %5.2f%%\n\n\n", n, diff); Η % δηαθνξά γίλεηαη κηθξόηεξε ή ίζε ηνπ 1% γηα n = 50 θαη κηθξόηεξε ή ίζε ηνπ 0.1% γηα n = 500. Β. #include <string.h> char x[160]=""; int i, j; printf("dwste mia protasi: "); gets(x); /* Γηαδηθαζία αθαίξεζεο θελώλ πξηλ από ζεκείν ζηίμεο */ for (i=1; i<strlen(x); i++) if (x[i]=='.' x[i]==',') while (x[i-1]==' ') for (j=i-1; j<strlen(x); j++) x[j]=x[j+1]; i--; ελίδα 5 από 12
/* Γηαδηθαζία πξνζζήθεο θελώλ κεηά από ζεκείν ζηίμεο */ for (i=0; i<strlen(x); i++) if (x[i]=='.' x[i]==',') if (x[i+1]!=' ') for (j=strlen(x); j>=i+1; j--) x[j]=x[j-1]; x[strlen(x)]='\0'; x[i+1]=' '; printf("%s\n", x); ΤΠΟΔΡΓΑΙΑ 3 #include <string.h> #include <ctype.h> #define NUMBER_SYMBOLS 36 /* Γήισζε ηεο δνκήο αλαπαξάζηαζεο ηνπ δεύγνπο αληηζηνίρηζεο ζπκβόισλ θαη θσδηθνπνίεζεο ηνπο. */ struct morse_pair char symbol; /* ύκβνιν */ char code[6]; /* Αληίζηνηρνο θώδηθαο */ ; struct morse_pair morse_code[number_symbols]; /* Αξρηθνπνίεζε ηνπ πίλαθα αληηζηνίρηζεο */ void initialize() morse_code[0].symbol='a'; strcpy(morse_code[0].code, ".-"); morse_code[1].symbol='b'; strcpy(morse_code[1].code, "-..."); morse_code[2].symbol='c'; strcpy(morse_code[2].code, "-.-."); morse_code[3].symbol='d'; strcpy(morse_code[3].code, "-.."); morse_code[4].symbol='e'; strcpy(morse_code[4].code, "."); morse_code[5].symbol='f'; strcpy(morse_code[5].code, "..-."); morse_code[6].symbol='g'; strcpy(morse_code[6].code, "--."); morse_code[7].symbol='h'; strcpy(morse_code[7].code, "..."); morse_code[8].symbol='i'; strcpy(morse_code[8].code, ".."); ελίδα 6 από 12
morse_code[9].symbol='j'; strcpy(morse_code[9].code, ".---"); morse_code[10].symbol='k'; strcpy(morse_code[10].code, "-.-"); morse_code[11].symbol='l'; strcpy(morse_code[11].code, ".-.."); morse_code[12].symbol='m'; strcpy(morse_code[12].code, "--"); morse_code[13].symbol='n'; strcpy(morse_code[13].code, "-."); morse_code[14].symbol='o'; strcpy(morse_code[14].code, "---"); morse_code[15].symbol='p'; strcpy(morse_code[15].code, ".--."); morse_code[16].symbol='q'; strcpy(morse_code[16].code, "--.-"); morse_code[17].symbol='r'; strcpy(morse_code[17].code, ".-."); morse_code[18].symbol='s'; strcpy(morse_code[18].code, "..."); morse_code[19].symbol='t'; strcpy(morse_code[19].code, "-"); morse_code[20].symbol='u'; strcpy(morse_code[20].code, "..-"); morse_code[21].symbol='v'; strcpy(morse_code[21].code, "...-"); morse_code[22].symbol='w'; strcpy(morse_code[22].code, ".--"); morse_code[23].symbol='x'; strcpy(morse_code[23].code, "-..-"); morse_code[24].symbol='y'; strcpy(morse_code[24].code, "-.--"); morse_code[25].symbol='z'; strcpy(morse_code[25].code, "--.."); morse_code[26].symbol='0'; strcpy(morse_code[26].code, "-----"); morse_code[27].symbol='1'; strcpy(morse_code[27].code, ".----"); morse_code[28].symbol='2'; strcpy(morse_code[28].code, "..---"); morse_code[29].symbol='3'; strcpy(morse_code[29].code, "...--"); morse_code[30].symbol='4'; strcpy(morse_code[30].code, "...-"); morse_code[31].symbol='5'; strcpy(morse_code[31].code, "..."); morse_code[32].symbol='6'; strcpy(morse_code[32].code, "-..."); morse_code[33].symbol='7'; strcpy(morse_code[33].code, "--..."); morse_code[34].symbol='8'; strcpy(morse_code[34].code, "---.."); morse_code[35].symbol='9'; strcpy(morse_code[35].code, "----."); ελίδα 7 από 12
/* πλάξηεζε εληνπηζκνύ ηεο ζέζεο ζηνλ πίλαθα πνπ αληηζηνηρεί ζε έλα ζύκβνιν */ int locate_symbol(char c) int i; if (isalpha(c)) c=toupper(c); /* Υεηξηζκόο κηθξώλ γξακκάησλ θαη κεηαηξνπή ηνπο ζε θεθαιαία */ for (i=0; i<number_symbols; i++) if (morse_code[i].symbol==c) return i; /* πλάξηεζε εληνπηζκνύ ηεο ζέζεο ζηνλ πίλαθα πνπ αληηζηνηρεί ζε κία ζπκβνινζεηξά */ int locate_code(char * code) int i, pos; for (i=0; i<number_symbols; i++) if (!strcmp(morse_code[i].code, code)) return i; /* Τινπνίεζε ηεο δηαδηθαζίαο θσδηθνπνίεζεο */ void encode(char *char_input, char *morse_output) int i=0; int j=0; char temp[6]; int pos; while (char_input[i]!='\0') if (char_input[i]==' ') /* Υεηξηζκόο θελνύ ραξαθηήξα */ while (char_input[i]==' ') i++; morse_output[j++]=' '; morse_output[j++]=' '; if (char_input[i]=='\0') /* Υεηξηζκόο ηέινπο ζπκβνινζεηξάο εηζόδνπ */ morse_output[j]='\0'; return; else /* Υεηξηζκόο κε θελνύ ραξαθηήξα */ morse_output[j++]=' '; pos=locate_symbol(toupper(char_input[i++])); strcpy(temp, morse_code[pos].code); int i=0; /* Σν i δειώλεηαη ηνπηθά, ζε λέν κπινθ εληνιώλ. Η εκβέιεηά ηνπ είλαη κόλν ζε απηό ην κπινθ. Απηό ην i είλαη δηαθνξεηηθό από ην i πνπ δειώλεηαη ζηελ αξρή ηεο ζπλάξηεζεο θαη πνπ έρεη εκβέιεηα όιε ηε ζπλάξηεζε πιελ ηνπ κπινθ. */ while (temp[i]!='\0') morse_output[j++]=temp[i++]; morse_output[j]='\0'; ελίδα 8 από 12
/* Τινπνίεζε ηεο δηαδηθαζίαο απνθσδηθνπνίεζεο */ void decode(char *morse_input, char * char_output) int i=0; int j=0; int k=0; int pos; char temp[6]; while (morse_input[i]!='\0') if (morse_input[i]==' ') while (morse_input[i]==' ') i++; char_output[j++]=' '; if ((morse_input[i]=='.') (morse_input[i]=='-')) k=0; while ((morse_input[i]=='.') (morse_input[i]=='-')) temp[k++]=morse_input[i++]; temp[k]='\0'; pos=locate_code(temp); /* Δύξεζε ηνπ ζπκβόινπ πνπ αληηζηνηρεί ζε έλα θώδηθα */ char_output[j++]=morse_code[pos].symbol; if (morse_input[i]==' ') i++; char_output[j]='\0'; char input[100]; char morse[200]; char output[200]; initialize(); /* Κιήζε δηαδηθαζίαο αξρηθνπνίεζεο */ printf( Dose symboloseira eisodou: ); gets(input); encode(input, morse); /* Κιήζε δηαδηθαζίαο θσδηθνπνίεζεο */ printf("\n%s", morse); decode(morse, output); /* Κιήζε δηαδηθαζίαο απνθσδηθνπνίεζεο */ printf("\n%s", output); ελίδα 9 από 12
ΤΠΟΔΡΓΑΙΑ 4 #define N 5 int DISTANCES [N][N] = 0, 7, 12, 8, 11, 3, 0, 10, 7, 13, 4, 8, 0, 9, 12, 6, 6, 9, 0, 10, 7, 7, 11, 10, 0; int PATH[N]; void find_tsp_path(int c0, int PATH[]); int path_length(int PATH[]); void display_path(); void exchange(int *a, int *b); void generate_new_path(int PATH[], int r1); int j, m; for (j=0; j<5; j++) find_tsp_path (j, PATH); m=path_length(path); printf("starting from customer %d, PATH is: ",j+1); display_path(); printf("the path has length =%d\n",m); printf("\ngenerating new paths...\n"); for (j=0; j<5; j++) generate_new_path(path, j); /* End of main */ /* Η πην θάησ ζπλάξηεζε πινπνηεί ηνλ αιγόξηζκν ηνπ "Κνληηλόηεξνπ γείηνλα" */ void find_tsp_path(int c0, int PATH[]) int i, j, nearest, c, dmin; int visited[n]; /* Κάζε ζέζε ηνπ πίλαθα αλαθέξεηαη ζε έλαλ πειάηε. Έηζη, ε ζέζε 0 αλαθέξεηαη ζηνλ πειάηε 1, ε ζέζε 1 ζηνλ πειάηε 2, θ.ν.θ. Γηα θάζε πειάηε δηαηεξείηαη ε έλδεημε αλ ήδε ηνλ έρεη επηζθεθζεί ν πσιεηήο (ηηκή 1) ή όρη (ηηκή 0). */ for (i=0; i<n; i++) visited[i]=0; /* Αξρηθά νη ηηκέο ηνπ πίλαθα είλαη όιεο κεδέλ */ c=c0; /* Αξρηθόο πειάηεο: ΠΡΟΟΥΗ: Οη πειάηεο αξηζκνύληαη από 0 σο Ν-1 */ j=-1; do j++; PATH[j]=c; visited[c]=1; /* Έλδεημε όηη ν πειάηεο c έρεη ήδε επηιεγεί ζηε δηαδξνκή */ ελίδα 10 από 12
/* Βξεο ηνλ θνληηλόηεξν γείηνλα ηνπ πειάηε c */ nearest=-1; dmin=32767; /* ηε κεηαβιεηή nearest ζα θξαηήζνπκε ηνλ αξηζκό ηνπ επόκελνπ θνληηλόηεξνπ γείηνλα. Έηζη, ζηελ αξρή μεθηλάκε κε ην -1 δει. κε έλαλ πειάηε πνπ ζηελ νπζία δελ πθίζηαηαη. ηελ dmin ζα θξαηήζνπκε ηελ απόζηαζε ηνπ πειάηε απηνύ από ηνλ πξνεγνύκελν, δει. από ηνλ c. Αξρηθά ε dmin παίξλεη κηα κεγάιε ηηκή ηέηνηα πνπ λα κελ επεξεάδεη ηηο επόκελεο δηαδνρηθέο ζπγθξίζεηο. */ for (i=0; i<n; i++) if ((i!=c) && (visited[i]==0)) if (DISTANCES[c][i]<dmin) dmin= DISTANCES[c][i]; nearest=i; c=nearest; /* Δπόκελνο πειάηεο ζηε δηαδξνκή */ while (j<n); /* Σέινο ηεο ζπλάξηεζεο find_tsp_path */ /* Τπνινγηζκόο ηνπ κήθνπο ηεο δηαδξνκήο PATH */ int path_length(int PATH[]) int j, COST=0, previous, next; for (j=0; j<n-1; j++) previous=path[j]; next=path[j+1]; COST+=DISTANCES[previous][next]; return COST; /* Δκθάληζε ηεο δηαδξνκήο */ void display_path() int j; printf("\n"); for (j=0; j<n; j++) printf("%d ", PATH[j]+1); /* Αληηκεηάζεζε (ελαιιαγή) 2 αθεξαίσλ ηηκώλ ζηηο νπνίεο δείρλνπλ 2 δείθηεο */ void exchange(int *a, int *b) int temp; temp=*a; *a=*b; *b=temp; ελίδα 11 από 12
/* Γεκηνπξγία λέαο δηαδξνκήο κε ην κεραληζκό ηεο αληηκεηάζεζεο ησλ πεξηερνκέλσλ γεηηνληθώλ ζέζεσλ */ void generate_new_path(int PATH[], int r1) int i, j, m1, m2, dev; printf("\ninitial PATH: "); display_path(); m1=path_length(path); printf("path's length =%d\n",m1); /* Παξαγσγή ηεο λέαο δηαδξνκήο */ if (r1<n-1) exchange(&path[r1], &PATH[r1+1]); /* Αληηκεηάζεζε */ else exchange(&path[r1], &PATH[0]); /* Σν r1 είλαη ε ηειεπηαία ζέζε ηνπ πίλαθα PATH */ printf("new PATH: "); display_path(); m2=path_length(path); printf("path's length =%d\n",m2); dev=m2-m1; if (dev>0) printf("the new PATH is %d km longer than the old one.", dev); else if (dev<0) printf("the new PATH is %d km shorter than the old one.", abs(dev)); else printf("the two PATHS have same length."); Γηα ιόγνπο πιεξόηεηαο επηδεηθλύεηαη ην απνηέιεζκα ηεο εθηέιεζεο ηνπ πην πάλσ πξνγξάκκαηνο: ελίδα 12 από 12