uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 0e76cabeb8799549f3816b9eb6bc3ba28f384891
parent 05e95f3b036904914288374ee4c3a603119df734
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed,  2 Jun 2021 03:35:06 +0300

finished c_os2/ex2, started java/population

Diffstat:
Mc_os2/ex2/ex1_1.c | 18++----------------
Mc_os2/ex2/ex1_2.c | 5++---
Mc_os2/ex2/ex2.c | 23++++++++---------------
Mc_os2/ex2/ex3_client.c | 16++++++----------
Mc_os2/ex2/ex3_server.c | 12++++++------
Mcpp_oop/game/Engine.cc | 13+------------
Ajava_development/CopyPaste.java | 28++++++++++++++++++++++++++++
Ajava_development/Occurences.java | 22++++++++++++++++++++++
Ajava_development/Prime.java | 36++++++++++++++++++++++++++++++++++++
Ajava_development/ReadLines.java | 32++++++++++++++++++++++++++++++++
Ajava_development/RecursiveRev.java | 14++++++++++++++
Djava_development/population/.classpath | 12------------
Djava_development/population/.project | 17-----------------
Djava_development/population/.settings/org.eclipse.jdt.core.prefs | 8--------
Djava_development/population/bin/population/ExcelParser.class | 0
Djava_development/population/bin/population/Main.class | 0
Djava_development/population/res/wpp2019_population_both_sexes.xlsx | 0
Djava_development/population/src/population/ExcelParser.java | 63---------------------------------------------------------------
Djava_development/population/src/population/Main.java | 16----------------
Ajava_development/rect/Main.java | 12++++++++++++
Ajava_development/rect/Pixel.java | 27+++++++++++++++++++++++++++
Ajava_development/rect/Point.java | 23+++++++++++++++++++++++
Ajava_development/rect/Rectangle.java | 37+++++++++++++++++++++++++++++++++++++
23 files changed, 256 insertions(+), 178 deletions(-)

diff --git a/c_os2/ex2/ex1_1.c b/c_os2/ex2/ex1_1.c @@ -6,6 +6,8 @@ #include <string.h> #include <unistd.h> +#define LEN(x) (sizeof(x) / sizeof(x[0])) + /* * Εργαστήριο ΛΣ2 (Δ6) / Εργασία 2: Άσκηση 1.1 / 2020-2021 * Ονοματεπώνυμο: Χρήστος Μαργιώλης @@ -13,9 +15,6 @@ * Τρόπος μεταγλώττισης: `cc ex1_1.c -lpthread -lrt -o ex1_1` */ -/* Calculate an array's length. */ -#define LEN(x) (sizeof(x) / sizeof(x[0])) - struct foo { char *str; int tid; @@ -45,19 +44,12 @@ thread_callback(void *foo) struct foo *f; f = (struct foo *)foo; - /* Lock the semaphore (decrement by one). */ if (sem_wait(&f->sem) < 0) err(1, "sem_wait"); - /* - * Get appropriate string. `f->tid` has the thread's ID -- we'll use - * it to give each thread a unique string. Thread 0 will get the first - * string, thread 1 the second, and so on. - */ if ((f->str = strdup(nums[f->tid++])) == NULL) err(1, "strdup"); fputs(f->str, stdout); free(f->str); - /* Unlock the semaphore (increment by one). */ if (sem_post(&f->sem) < 0) err(1, "sem_post"); @@ -94,12 +86,6 @@ main(int argc, char *argv[]) while ((ch = getopt(argc, argv, "n:")) != -1) { switch (ch) { case 'n': - /* - * Manually choose how many times the string sequence - * is going to be printed. Obviously, we cannot allow - * an N less than 1. By default `n` is 5 (see - * declaration above). - */ if ((n = atoi(optarg)) < 1) errx(1, "value must be greater than 1"); break; diff --git a/c_os2/ex2/ex1_2.c b/c_os2/ex2/ex1_2.c @@ -5,6 +5,8 @@ #include <string.h> #include <unistd.h> +#define LEN(x) (sizeof(x) / sizeof(x[0])) + /* * Εργαστήριο ΛΣ2 (Δ6) / Εργασία 2: Άσκηση 1.2 / 2020-2021 * Ονοματεπώνυμο: Χρήστος Μαργιώλης @@ -12,8 +14,6 @@ * Τρόπος μεταγλώττισης: `cc ex1_2.c -lpthread -o ex1_2` */ -#define LEN(x) (sizeof(x) / sizeof(x[0])) - struct foo { char *str; int done; @@ -87,7 +87,6 @@ usage(void) exit(1); } -/* Code shared with `ex1_1` is explained in `ex1_1.c`. */ int main(int argc, char *argv[]) { diff --git a/c_os2/ex2/ex2.c b/c_os2/ex2/ex2.c @@ -91,7 +91,7 @@ thread_callback(void *foo) /* * We need to know each thread's ID in order to calculate `start` * and `end` properly. Since we don't touch `f->tid` inside `main` - * (apart from initializing it), we need to update it here. + * (apart from initializing it), we need to change its value here. */ f->tid++; if (pthread_mutex_unlock(&f->mtx) != 0) @@ -150,31 +150,25 @@ main(int argc, char *argv[]) { struct foo *f; pthread_t *tds; - int i, j, rc; + int i, j; argv0 = *argv; f = emalloc(sizeof(struct foo)); do { - printf("\rp: "); - /* - * Save the return value of scanf(3) to make sure - * that we did read valid input. - */ - rc = scanf("%d", &f->ntd); - (void)getchar(); + printf("p: "); + scanf("%d", &f->ntd); /* Cannot have less than 1 threads. */ - } while (f->ntd < 1 || rc != 1); + } while (f->ntd < 1); do { - printf("\rn: "); - rc = scanf("%d", &f->n); - (void)getchar(); + printf("n: "); + scanf("%d", &f->n); /* * The number of elements must be greater than 0 (obviously) and * a multiple of the number of threads. */ - } while (f->n < 0 || f->n % f->ntd != 0 || rc != 1); + } while (f->n < 0 || f->n % f->ntd != 0); tds = emalloc(f->ntd * sizeof(pthread_t)); f->l_n = f->n / f->ntd; @@ -182,7 +176,6 @@ main(int argc, char *argv[]) f->l_max = emalloc(f->ntd * sizeof(int)); f->d = emalloc(f->n * sizeof(int *)); - /* The exercise says we should read from a file, but don't do it. :-) */ srand(time(NULL)); for (i = 0; i < f->n; i++) { f->g_arr[i] = emalloc(f->n * sizeof(int)); diff --git a/c_os2/ex2/ex3_client.c b/c_os2/ex2/ex3_client.c @@ -25,9 +25,6 @@ struct pack_res { float avg; }; -static void *emalloc(size_t nb); -static void usage(void); - static char *argv0; static void * @@ -50,7 +47,6 @@ usage(void) exit(1); } -/* Code shared with `ex3_server` is explained in `ex3_server.c`. */ int main(int argc, char *argv[]) { @@ -125,7 +121,7 @@ main(int argc, char *argv[]) /* Make sure we send valid input to the server */ do { - printf("\r%s> n: ", argv0); + printf("%s> n: ", argv0); rc = scanf("%d", &n); /* Flush input buffer */ (void)getchar(); @@ -133,7 +129,7 @@ main(int argc, char *argv[]) arr = emalloc(n * sizeof(int)); for (i = 0; i < n; i++) { do { - printf("\r%s> arr[%d]: ", argv0, i); + printf("%s> arr[%d]: ", argv0, i); rc = scanf("%d", &arr[i]); (void)getchar(); } while (rc != 1); @@ -144,17 +140,17 @@ main(int argc, char *argv[]) err(1, "send"); if (recv(fd, res, sizeof(struct pack_res), 0) < 0) err(1, "recv"); - free(arr); + free(arr); printf("server response: %s\tavg: %.2f\n", res->str, res->avg); - do - printf("\r%s> continue (y/n)? ", argv0); - while ((ch = getchar()) != 'y' && ch != 'n'); + printf("%s> continue (y/n)? ", argv0); + ch = getchar(); if (send(fd, &ch, 1, 0) < 0) err(1, "send"); if (ch == 'n') break; } + free(res); (void)close(fd); diff --git a/c_os2/ex2/ex3_server.c b/c_os2/ex2/ex3_server.c @@ -27,9 +27,9 @@ struct pack_res { }; struct foo { - int cfd; /* Client file descriptor. */ - int nsucc; /* Number of successful calculations (avg > 10). */ - int ntotal; /* Total number of servings. */ + int cfd; + int nsucc; + int ntotal; }; static int srv(struct foo *); @@ -148,7 +148,7 @@ main(int argc, char *argv[]) int sfd; int backlog = 10; int port = 9999; - int iflag, uflag; + int iflag, uflag, sockflags; char ch; argv0 = *argv; @@ -209,7 +209,7 @@ main(int argc, char *argv[]) if (sigaction(SIGTERM, &sa, NULL) < 0) err(1, "sigaction(SIGTERM)"); - /* Set the socket up for use in the Internet domain. */ + /* Set up the socket for use in the Internet domain. */ if (iflag) { if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) err(1, "socket(AF_INET)"); @@ -240,7 +240,7 @@ main(int argc, char *argv[]) printf("Socket: %s\nDomain: AF_INET\nIPv4: %s\n" "Port: %d\nBacklog: %d\n", sockfile, inet_ntoa(sin.sin_addr), port, backlog); - /* Set the socket up for use in the UNIX domain. */ + /* Set up the socket for use in the UNIX domain. */ } else if (uflag) { if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) err(1, "socket(AF_UNIX)"); diff --git a/cpp_oop/game/Engine.cc b/cpp_oop/game/Engine.cc @@ -125,14 +125,6 @@ Engine::load_map(const char *mapfile) if (l != str.length()) throw std::runtime_error("rows must have an equal " "length: line " + std::to_string(curline)); - - /* The map must not contain anything other than ' ' and '*'. */ - for (char& c : str) - if (c != ' ' && c != '*') - throw std::runtime_error("the map must contain " - "only spaces and asterisks: line: " + - std::to_string(curline)); - map.push_back(str); curline++; } @@ -145,10 +137,7 @@ Engine::load_map(const char *mapfile) w = map[0].length(); h = map.size(); - /* - * The map has to fit in the screen, obviously. The top 2 lines on - * the Y axis are reserved for the status bar. - */ + /* The map has to fit in the screen. */ if (w > xmax || h > ymax - 2) throw std::runtime_error("the map doesn't fit to screen"); } diff --git a/java_development/CopyPaste.java b/java_development/CopyPaste.java @@ -0,0 +1,28 @@ +import java.io.*; + +class CopyPaste { + public static void main(String args[]) { + FileInputStream fis; + FileOutputStream fos; + int c; + + try { + fis = new FileInputStream(args[0]); + fos = new FileOutputStream(args[1]); + + while ((c = fis.read()) != -1) + fos.write(c); + fis.close(); + fos.close(); + } catch (FileNotFoundException e) { + System.err.println("file not found"); + return; + } catch (IOException e) { + System.err.println("IO Exception"); + return; + } catch (ArrayIndexOutOfBoundsException e) { + System.err.println("usage: java CopyPaste src dst"); + return; + } + } +} diff --git a/java_development/Occurences.java b/java_development/Occurences.java @@ -0,0 +1,22 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class Occurences { + public static void main(String[] args) { + ArrayList<String> strs = new ArrayList<String>(); + HashMap<String, Integer> occ = new HashMap<String, Integer>(); + + strs.add("foo"); + strs.add("foo"); + strs.add("foo"); + strs.add("bar"); + strs.add("baz"); + strs.add("baz"); + + for (String s : strs) + occ.put(s, 1 + (occ.containsKey(s) ? occ.get(s) : 0)); + for (Map.Entry<String, Integer> e : occ.entrySet()) + System.out.println(e.getKey() + ": " + e.getValue()); + } +} diff --git a/java_development/Prime.java b/java_development/Prime.java @@ -0,0 +1,36 @@ +import java.io.*; + +class Prime { + public static void main(String[] args) { + try { + FileOutputStream fos = new FileOutputStream(args[0]); + BufferedOutputStream bos = new BufferedOutputStream(fos); + DataOutputStream dos = new DataOutputStream(bos); + int i = 0; + int n = 2; + + while (i < 100) { + if (is_prime(n)) { + dos.writeInt(n); + i++; + } + n++; + } + dos.close(); + fos.close(); + } catch (FileNotFoundException e) { + System.err.println("File not found"); + return; + } catch (IOException e) { + System.err.println("IO Exception"); + return; + } + } + + private static boolean is_prime(int n) { + for (int i = 2; i < n; i++) + if (n % i == 0) + return false; + return true; + } +} diff --git a/java_development/ReadLines.java b/java_development/ReadLines.java @@ -0,0 +1,32 @@ +import java.io.*; + +class ReadLines { + public static void main(String args[]) { + BufferedReader br; + File f; + String str; + int ln = 0; + + try { + f = new File(args[0]); + br = new BufferedReader(new FileReader(f)); + + while ((str = br.readLine()) != null) { + System.out.println(str); + ln++; + } + br.close(); + } catch (FileNotFoundException e) { + System.err.println("file not found"); + return; + } catch (IOException e) { + System.err.println("IO Exception"); + return; + } catch (ArrayIndexOutOfBoundsException e) { + System.err.println("usage: java ReadLines file"); + return; + } + + System.out.println("Lines read: " + ln); + } +} diff --git a/java_development/RecursiveRev.java b/java_development/RecursiveRev.java @@ -0,0 +1,14 @@ +public class RecursiveRev { + public static String strrev(String s) { + if (s.isEmpty()) + return s; + else + return strrev(s.substring(1)) + s.charAt(0); + } + + public static void main(String[] args) { + String s = "Hello world"; + + System.out.println(strrev(s)); + } +} diff --git a/java_development/population/.classpath b/java_development/population/.classpath @@ -1,12 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<classpath> - <classpathentry kind="src" path="src"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> - <attributes> - <attribute name="module" value="true"/> - </attributes> - </classpathentry> - <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX"/> - <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/POI"/> - <classpathentry kind="output" path="bin"/> -</classpath> diff --git a/java_development/population/.project b/java_development/population/.project @@ -1,17 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<projectDescription> - <name>population</name> - <comment></comment> - <projects> - </projects> - <buildSpec> - <buildCommand> - <name>org.eclipse.jdt.core.javabuilder</name> - <arguments> - </arguments> - </buildCommand> - </buildSpec> - <natures> - <nature>org.eclipse.jdt.core.javanature</nature> - </natures> -</projectDescription> diff --git a/java_development/population/.settings/org.eclipse.jdt.core.prefs b/java_development/population/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.5 diff --git a/java_development/population/bin/population/ExcelParser.class b/java_development/population/bin/population/ExcelParser.class Binary files differ. diff --git a/java_development/population/bin/population/Main.class b/java_development/population/bin/population/Main.class Binary files differ. diff --git a/java_development/population/res/wpp2019_population_both_sexes.xlsx b/java_development/population/res/wpp2019_population_both_sexes.xlsx Binary files differ. diff --git a/java_development/population/src/population/ExcelParser.java b/java_development/population/src/population/ExcelParser.java @@ -1,63 +0,0 @@ -package population; - -import java.io.*; -import java.util.Iterator; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.*; - -public class ExcelParser { - private XSSFWorkbook wb; - private XSSFSheet ss; - private XSSFRow row; - - ExcelParser(String path) throws Exception { - FileInputStream fis; - Iterator<Row> rit; - Iterator<Cell> cit; - Cell cell; - - try { - fis = new FileInputStream(new File(path)); - wb = new XSSFWorkbook(fis); - ss = wb.getSheetAt(0); - rit = ss.iterator(); - - while (rit.hasNext()) { - row = (XSSFRow)rit.next(); - cit = row.cellIterator(); - while (cit.hasNext()) { - cell = cit.next(); - switch (cell.getCellType()) { - case NUMERIC: - System.out.print( - cell.getNumericCellValue() + - "\t\t"); - break; - case STRING: - System.out.print( - cell.getStringCellValue() + - "\t\t"); - break; - case BOOLEAN: - System.out.print( - cell.getBooleanCellValue() + - "\t\t"); - break; - case BLANK: /* FALLTHROUGH */ - case ERROR: - case FORMULA: - case _NONE: - default: - break; - } - } - System.out.println(); - } - fis.close(); - } catch (FileNotFoundException e) { - throw new Exception(path + ": no such file"); - } catch (IOException e) { - throw new Exception("io error"); - } - } -} diff --git a/java_development/population/src/population/Main.java b/java_development/population/src/population/Main.java @@ -1,15 +0,0 @@ -package population; - -public class Main { - public static void main(String[] args) { - ExcelParser ep; - - try { - ep = new ExcelParser(args[0]); - } catch (IndexOutOfBoundsException e) { - System.err.println("usage: population xlsx_file"); - } catch (Exception e) { - System.err.println(e); - } - } -}- \ No newline at end of file diff --git a/java_development/rect/Main.java b/java_development/rect/Main.java @@ -0,0 +1,12 @@ +public class Main { + public static void main(String[] args) { + Point pt = new Point(50.55, 23.11); + Pixel px = new Pixel(pt, 0xffaaee); + Rectangle rect = new Rectangle(); + + System.out.println(pt.toString()); + System.out.println(px.toString()); + rect.print(); + rect.info(); + } +} diff --git a/java_development/rect/Pixel.java b/java_development/rect/Pixel.java @@ -0,0 +1,27 @@ +class Pixel extends Point { + int color; + static int n = 0; + + Pixel() { + super(0.0, 0.0f); + this.color = 0x000000; + n++; + } + + Pixel(double x, double y, int color) { + super(x, y); + this.color = color; + n++; + } + + Pixel(Point p, int color) { + super(p); + this.color = color; + n++; + } + + public String toString() { + return "x: " + x + "\ty: " + y + "\tcolor: " + + String.format("0x%06X", color); + } +} diff --git a/java_development/rect/Point.java b/java_development/rect/Point.java @@ -0,0 +1,23 @@ +class Point { + double x; + double y; + + Point() { + this.x = 0.0; + this.y = 0.0; + } + + Point(double x, double y) { + this.x = x; + this.y = y; + } + + Point(Point p) { + this.x = p.x; + this.y = p.y; + } + + public String toString() { + return "(" + x + ", " + y + ")"; + } +} diff --git a/java_development/rect/Rectangle.java b/java_development/rect/Rectangle.java @@ -0,0 +1,37 @@ +class Rectangle { + private Point pts[]; + + Rectangle() { + pts = new Point[4]; + for (int i = 0; i < pts.length; i++) + pts[i] = new Point(0.0, 0.0); + } + + Rectangle(Point[] p) { + pts = p; + } + + public Point[] getpoints() { + return pts; + } + + public double width() { + return Math.abs(pts[1].x - pts[0].x); + } + + public double height() { + return Math.abs(pts[2].y - pts[1].y); + } + + public void info() { + System.out.println("Top: " + pts[0]); + System.out.println("Width: " + width()); + System.out.println("Height: " + height()); + } + + public void print() { + for (Point p : pts) + System.out.print(p.toString() + " "); + System.out.println(); + } +}