תרגול מס' 14

Embed Size (px)

DESCRIPTION

תרגול מס' 14. שאלות ממבחנים - ADT שאלות ממבחנים - BASH. שאלות ממבחנים - ADT. שאלה 1 (מועד ב', אביב 2006). - PowerPoint PPT Presentation

Text of תרגול מס' 14

PowerPoint Presentation

' 14 - ADT - BASH - ADT - 2341222 1 ( ', 2006) - ADT (40 ): . " (). ( ) . -ADT Image :imageCopy: imageDestroy: imagesSimilarity: 0 - 1 .

Image " ", - 2341223

1 ( ', 2006) ' (20 ): SurveillanceSystem SurSys. :CreateSurSys: SurSys.AddWorker: ( " ).AddWorkerImage: ( . , )RemoveWorker: ( )RemoveWorkerImage: (- " )GetWorkerImages: .CheckSimilarity: ( ...)DestroySurSys: -SurSys. ' 7 (Image) threshold 0 - 1. (" imagesSimilarity) . - threshold . , . , " . - 2341224 - '#ifndef SURSYS_H#define SURSYS_H#include "image.h" #include

typedef struct SurSys_t *SurSys;typedef enum {SUR_SYS_SUCCESS, SUR_SYS_FAIL, SUR_SYS_OUT_OF_MEMORY,...} SurSysResult;SurSys CreateSurSys(); // or SurSysResult CreateSurSys(SurSys *system); SurSysResult AddWorker(SurSys system, int id, const char* name);SurSysResult AddWorkerImage(SurSys system, int id, Image image, int* imageId);SurSysResult RemoveWorker(SurSys system, int id);SurSysResult RemoveWorkerImage(SurSys system, int id, int imageId);SurSysResult GetWorkerImages(SurSys system, int id, Image** images, int* imagesSize);SurSysResult CheckSimilarity(SurSys system, Image image, double threshold,bool* match, int* bestID, char** bestName);void DestroySurSys(SurSys system);#endif - 2341225 1 ( ', 2006) ' (20 ):(8 ) SurSys SurSys.c, . .

(9 ) 7 ' ' 1. . ( ), ADTs SurSys.

(3 ) 3 ( -SurSys) " ( " ", ADT(s) ).

- 2341226 - ' 1#include "SurSys.h"#include "set.h"/* the struct of the ADT */struct SurSys_t {Set workers;};/* internal struct (not ADT) */typedef struct Worker_t {int id;char* name;Set workerImages; } Worker;/* internal struct (not ADT) */typedef struct WorkerImage_t {int id;Image image;} WorkerImage; - 2341227 - ' 2SurSysResult CheckSimilarity(SurSys system, Image image, double threshold,bool* match, int* id, char** name) {if (system == NULL || match == NULL || id == NULL || name == NULL || image == NULL)return SUR_SYS_BAD_PARAM;if (threshold < 0 || threshold > 1)return SUR_SYS_INVALID_THRESHOLD;double maxSimilarity = 0.0;char* tempName = NULL;SET_FOREACH(Worker, worker, system->workers) {SET_FOREACH(WorkerImage, image, worker->workerImages) {double similarity = imagesSimilarity(image->image, image);if (similarity > maxSimilarity) {maxSimilarity = similarity;*id = worker->id;tempName = worker->name;}}} - 2341228 - ' 2 - if (tempName != NULL) {*name = malloc(strlen(tempName) + 1);if (*name == NULL)return SUR_SYS_OUT_OF_MEMORY;strcpy(*name, tempName);}*match = maxSimilarity > threshold;return SUR_SYS_SUCCESS;} - 2341229 - ' 3 :SUR_SYS_INVALID_THRESHOLDSUR_SYS_IMAGE_DOESNT_EXISTSUR_SYS_WORKER_ALREADY_EXISTS

- 23412210 2 student.h Student. C C++, ADT Student Student. student_adt.h -student_adt.c . course_adt.h set.h const correctness

- 2341221111 2#ifndef STUDENT_H_#define STUDENT_H_#include "course.h"#include

class Student {enum { MAX_COURSES = 30 };char* name;Course *courses[MAX_COURSES];int numCourses;double calculateAverageGrade();public:Student(const char*);~Student();void addCourse(const Course&);void removeCourse(const Course&);double getAverage() const ;const char* getName() const ;set getFailedCourses();bool isHonorsStudent() const ;friend bool operatornext) {if (ptr->n != n) {continue;}if (ptr->previous) {ptr->previous->next = ptr->next;}if (ptr->next) {ptr->next->previous = ptr->previous;}Node tmp = ptr;ptr = ptr->previous;free(tmp);}}Node removeDuplicates(Node head) {for(Node ptr = head; ptr; ptr = ptr->next) {removeByValue(ptr->next, ptr->n);}return head;} - 23412216: NULL ( ') . . , . if(ptr->previous) -removeByValue .16 4 () :template void bubble_sort(T* array, int size) {for(int i = 0; i < size; i++) {for(int j = 0; j < size - 1; j++) {if (array[j+1] < array[j]) {T temp = array[j];array[j] = array[j+1];array[j+1] = temp;}}}} C C++, C++- -C. -C? . .

- 23412217 4 - void bubble_sort(void** array, int size, int (*compare)(void*, void*)) {for(int i = 0; i < size; i++) {for(int j = 0; j < size - 1; j++) {if (compare(array[j+1],array[j]) > 0) {void* temp = array[j];array[j] = array[j+1];array[j+1] = temp;}}}} :array size compare ( strcmp )

- 23412218 :void bubble_sort(void** array, int size, int compare(void*, void*)) 18 4 - :struct student_t {int id;char *name;};typedef student_t* Student;

" . ? .

- 23412219 4 - " :int studentCompare(void* a, void* b) {Student student1 = a;Student student2 = b;return student1->id - student2->id;} ( ) :

bubble_sort(array, n, studentCompare);

- 23412220 - BASH - 23412221 1 docs docs . docs.

': ( -pipeline) docs "banana" ( ). . -l grep. docs .

grep "banana" -w docs/* | cut -d":" -f1 | uniq

- 23412222 -uniq grep .22 1 - ' ( ) : , . : index -docs index , "apple" -3 -docs: fruits.txt, monkies.txt -juices.txt index apple :monkies.txtfruits.txtjuices.txt

': index, "banana" '

if [[ -f index/banana ]] ; then cat index/banana; fi

- 23412223 1 - make-index index docs. docs ( )

- 23412224#!/bin/bashfunction addToIndex {while read -a line; do for word in ${line[*]}; do if [[ ! (-f index/$word ) || ( `grep $1 index/$word` == "" ) ]] ; then echo $1 >> index/$word fi donedone}mkdir indexfor f in `ls docs`; do cat docs/$f | addToIndex $fdone

make-index 2 create_makefile Makefile C (*.h, *.c) include ( )

- 23412225 2: 4 a.c, a.h, b.c, b.h

create_makefile prog Makefile .

. .: gcc MM *.c Makefile.

- 23412226a.c:#include "a.h"int main() { return 0;}a.h:#include b.c:#include "b.h"void f() {}b.h:#include "a.h"void f();CC=gccOBJS=a.o b.oprog: $(OBJS) $(CC) a.o b.o -o proga.o: a.c a.h $(CC) -c a.cb.o: b.c b.h a.h $(CC) -c b.c 2 - - 23412227#!/bin/bashfunction print_makefile {echo CC=gccobjs=`gcc -MM *.c | grep ":" | cut -f1 -d":"`echo OBJS=$objsecho $1: '$OBJS'echo ' $(CC)' $objs -o $1echowhile read a line; do echo ${line[*]} echo -n ' $(CC) c ' for f in ${line[*]}:2; do if [[ "$f" = *.c ]] ; then echo -n "$f "fi done echodone}gcc -MM *.c | print_makefile $1 > makefile

create_makefile - 23412228

28