uni

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

commit 580b8f717113e45c47d4788a5a29b574b2724426
parent 0e76cabeb8799549f3816b9eb6bc3ba28f384891
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed,  2 Jun 2021 03:37:56 +0300

i forgot how to use git

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++++++++++++-
Djava_development/CopyPaste.java | 28----------------------------
Djava_development/Occurences.java | 22----------------------
Djava_development/Prime.java | 36------------------------------------
Djava_development/ReadLines.java | 32--------------------------------
Djava_development/RecursiveRev.java | 14--------------
Ajava_development/population/.classpath | 12++++++++++++
Ajava_development/population/.project | 17+++++++++++++++++
Ajava_development/population/.settings/org.eclipse.jdt.core.prefs | 8++++++++
Ajava_development/population/bin/population/ExcelParser.class | 0
Ajava_development/population/bin/population/Main.class | 0
Ajava_development/population/res/wpp2019_population_both_sexes.xlsx | 0
Ajava_development/population/src/population/ExcelParser.java | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ajava_development/population/src/population/Main.java | 16++++++++++++++++
Djava_development/rect/Main.java | 12------------
Djava_development/rect/Pixel.java | 27---------------------------
Djava_development/rect/Point.java | 23-----------------------
Djava_development/rect/Rectangle.java | 37-------------------------------------
23 files changed, 178 insertions(+), 256 deletions(-)

diff --git a/c_os2/ex2/ex1_1.c b/c_os2/ex2/ex1_1.c @@ -6,8 +6,6 @@ #include <string.h> #include <unistd.h> -#define LEN(x) (sizeof(x) / sizeof(x[0])) - /* * Εργαστήριο ΛΣ2 (Δ6) / Εργασία 2: Άσκηση 1.1 / 2020-2021 * Ονοματεπώνυμο: Χρήστος Μαργιώλης @@ -15,6 +13,9 @@ * Τρόπος μεταγλώττισης: `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; @@ -44,12 +45,19 @@ 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"); @@ -86,6 +94,12 @@ 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,8 +5,6 @@ #include <string.h> #include <unistd.h> -#define LEN(x) (sizeof(x) / sizeof(x[0])) - /* * Εργαστήριο ΛΣ2 (Δ6) / Εργασία 2: Άσκηση 1.2 / 2020-2021 * Ονοματεπώνυμο: Χρήστος Μαργιώλης @@ -14,6 +12,8 @@ * Τρόπος μεταγλώττισης: `cc ex1_2.c -lpthread -o ex1_2` */ +#define LEN(x) (sizeof(x) / sizeof(x[0])) + struct foo { char *str; int done; @@ -87,6 +87,7 @@ 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 change its value here. + * (apart from initializing it), we need to update it here. */ f->tid++; if (pthread_mutex_unlock(&f->mtx) != 0) @@ -150,25 +150,31 @@ main(int argc, char *argv[]) { struct foo *f; pthread_t *tds; - int i, j; + int i, j, rc; argv0 = *argv; f = emalloc(sizeof(struct foo)); do { - printf("p: "); - scanf("%d", &f->ntd); + 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(); /* Cannot have less than 1 threads. */ - } while (f->ntd < 1); + } while (f->ntd < 1 || rc != 1); do { - printf("n: "); - scanf("%d", &f->n); + printf("\rn: "); + rc = scanf("%d", &f->n); + (void)getchar(); /* * 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); + } while (f->n < 0 || f->n % f->ntd != 0 || rc != 1); tds = emalloc(f->ntd * sizeof(pthread_t)); f->l_n = f->n / f->ntd; @@ -176,6 +182,7 @@ 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,6 +25,9 @@ struct pack_res { float avg; }; +static void *emalloc(size_t nb); +static void usage(void); + static char *argv0; static void * @@ -47,6 +50,7 @@ usage(void) exit(1); } +/* Code shared with `ex3_server` is explained in `ex3_server.c`. */ int main(int argc, char *argv[]) { @@ -121,7 +125,7 @@ main(int argc, char *argv[]) /* Make sure we send valid input to the server */ do { - printf("%s> n: ", argv0); + printf("\r%s> n: ", argv0); rc = scanf("%d", &n); /* Flush input buffer */ (void)getchar(); @@ -129,7 +133,7 @@ main(int argc, char *argv[]) arr = emalloc(n * sizeof(int)); for (i = 0; i < n; i++) { do { - printf("%s> arr[%d]: ", argv0, i); + printf("\r%s> arr[%d]: ", argv0, i); rc = scanf("%d", &arr[i]); (void)getchar(); } while (rc != 1); @@ -140,17 +144,17 @@ main(int argc, char *argv[]) err(1, "send"); if (recv(fd, res, sizeof(struct pack_res), 0) < 0) err(1, "recv"); - free(arr); + printf("server response: %s\tavg: %.2f\n", res->str, res->avg); - printf("%s> continue (y/n)? ", argv0); - ch = getchar(); + do + printf("\r%s> continue (y/n)? ", argv0); + while ((ch = getchar()) != 'y' && ch != 'n'); 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; - int nsucc; - int ntotal; + int cfd; /* Client file descriptor. */ + int nsucc; /* Number of successful calculations (avg > 10). */ + int ntotal; /* Total number of servings. */ }; 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, sockflags; + int iflag, uflag; char ch; argv0 = *argv; @@ -209,7 +209,7 @@ main(int argc, char *argv[]) if (sigaction(SIGTERM, &sa, NULL) < 0) err(1, "sigaction(SIGTERM)"); - /* Set up the socket for use in the Internet domain. */ + /* Set the socket up 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 up the socket for use in the UNIX domain. */ + /* Set the socket up 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,6 +125,14 @@ 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++; } @@ -137,7 +145,10 @@ Engine::load_map(const char *mapfile) w = map[0].length(); h = map.size(); - /* The map has to fit in the screen. */ + /* + * The map has to fit in the screen, obviously. The top 2 lines on + * the Y axis are reserved for the status bar. + */ 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 @@ -1,28 +0,0 @@ -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 @@ -1,22 +0,0 @@ -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 @@ -1,36 +0,0 @@ -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 @@ -1,32 +0,0 @@ -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 @@ -1,14 +0,0 @@ -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 @@ -0,0 +1,12 @@ +<?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 @@ -0,0 +1,17 @@ +<?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 @@ -0,0 +1,8 @@ +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 @@ -0,0 +1,63 @@ +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 @@ -0,0 +1,15 @@ +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 @@ -1,12 +0,0 @@ -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 @@ -1,27 +0,0 @@ -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 @@ -1,23 +0,0 @@ -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 @@ -1,37 +0,0 @@ -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(); - } -}