feat: update structure

This commit is contained in:
2024-01-22 14:27:40 +08:00
parent 7836c9185c
commit 3544a28a2e
559 changed files with 120846 additions and 4102 deletions

View File

@@ -0,0 +1,24 @@
#!/bin/bash
# Check if we have enough arguments
# Delete temporary files
# Compile the reference program
# Generate reference output files
# Now mark submissions
#
# Note: See Lab02Qn.pdf for format of output file. Marks will be deducted for missing elements.
#
# Iterate over every submission directory
# Compile C code
# Print compile error message to output file
# Generate output from C code using *.in files in ref
# Compare with reference output files and award 1 mark if they are identical
# print score for student
# print total files marked.

View File

@@ -0,0 +1,5 @@
1
3
5
3
4

View File

@@ -0,0 +1,7 @@
382
44
112
95
hello
world
12

View File

@@ -0,0 +1,4 @@
3.4
5.4
1
2.3

View File

@@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
#define MAX_STR_LEN 128
void strip(char *str) {
while(*str) {
if(*str == '\n')
*str = '\0';
str++;
}
}
int main() {
char str[MAX_STR_LEN];
int nums[128];
int count=0;
int res = 0;
while(!feof(stdin)) {
char *res = fgets(str, MAX_STR_LEN, stdin);
if(res){
strip(str);
int val = atoi(str);
if(val){
nums[count++] = val;
}
else
printf("%s is not a non-zero integer.\n", str);
}
}
res = sum(nums, count);
printf("Item count is %d\n", count);
printf("The sum is %d\n", res);
exit(res);
}

View File

@@ -0,0 +1,10 @@
#include "utils.h"
int sum(int sum_array[], int n) {
int i, res = 0;
for(i=0; i<n; i++)
res += sum_array[i];
return res;
}

View File

@@ -0,0 +1,2 @@
int sum(int [], int);

View File

@@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
#define MAX_STR_LEN 128
void strip(char *str) {
while(*str) {
if(*str == '\n')
*str = '\0';
str++;
}
}
int main() {
char str[MAX_STR_LEN];
int nums[128];
int count=0;
int res = 0;
while(!feof(stdin)) {
char *res = fgets(str, MAX_STR_LEN, stdin);
if(res){
strip(str);
int val = atoi(str);
if(val){
nums[count++] = val;
}
else
printf("%s is not a non-zero integer.\n", str);
}
}
res = sum(nums, count);
printf("Item count is %d\n", count);
printf("The sum is %d\n", res);
exit(res);
}

View File

@@ -0,0 +1,10 @@
#include "utils.h"
int sum(int sum_array[], int n) {
int i, res = 0;
for(i=0; i<n; i++)
res += sum_array[i];
return res;
}

View File

@@ -0,0 +1,2 @@
int sum(int [], int);

View File

@@ -0,0 +1,40 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
#define MAX_STR_LEN 128
void strip(char *str) {
while(*str) {
if(*str == '\n')
*str = '\0';
str++;
}
}
int main() {
char str[MAX_STR_LEN];
int nums[128];
int count=0;
int res = 0;
while(!feof(stdin)) {
char *res = fgets(str, MAX_STR_LEN, stdin);
if(res){
strip(str);
int val = atoi(str);
if(val){
nums[count++] = val;
}
}
}
res = sum(nums, count);
printf("Item count is %d\n", count);
printf("The sum is %d\n", res);
exit(res);
}

View File

@@ -0,0 +1,10 @@
#include "utils.h"
int sum(int sum_array[], int n) {
int i, res = 0;
for(i=0; i<n; i++)
res += sum_array[i];
return res;
}

View File

@@ -0,0 +1,2 @@
int sum(int [], int);

View File

@@ -0,0 +1,42 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
#define MAX_STR_LEN 128
void strip(char *str) {
while(*str) {
if(*str == '\n')
*str = '\0';
str++;
}
}
int main() {
char str[MAX_STR_LEN];
int nums[128];
int count=0;
int res = 0;
while(!feof(stdin)) {
char *res = fgets(str, MAX_STR_LEN, stdin);
if(res){
strip(str);
int val = atoi(str);
if(val){
nums[count++] = val;
}
else
printf("%s is not a non-zero integer.\n", str);
}
}
res = sum(nums, count);
printf("Item count is %d\n", count);
printf("The sum is %d\n", res);
exit(res);
}

View File

@@ -0,0 +1,10 @@
#include "utils.h"
int sum(int sum_array[], int n) {
int i, res = 0;
for(i=0; i<n; i++)
res += sum_array[i];
return res;
}

View File

@@ -0,0 +1,2 @@
double sum(int [], int);

5
cs2106/labs/lab2/part1/diff.sh Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/bash
x=15
y=20
z=$(($x-$y))
echo "$x - $y = $z"

View File

@@ -0,0 +1,2 @@
This is a text file.
CS2106 Introduction to Operating Systems is a cool module!

View File

@@ -0,0 +1,3 @@
#!/bin/bash
echo "Hello $(whoami), today is $(date)"

View File

@@ -0,0 +1,2 @@
#!/bin/bash
echo "Hello World"

View File

@@ -0,0 +1,29 @@
#include <stdio.h>
// For the sleep functiopn
#include <unistd.h>
// For the atoi function that converts
// strings to integers
#include <stdlib.h>
int main(int ac, char **av) {
if(ac != 2) {
printf("\nCounts from specified integer n to n + 5\n");
printf("Usage: %s <integer>\n\n", av[0]);
exit(-1);
}
int n = atoi(av[1]);
int i;
for(i=n; i<=n+5; i++) {
printf("%d\n", i);
sleep(1);
}
printf("\nFinal value of i is %d\n\n", i);
exit(i);
}

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#define BUF_SIZE 128
int main() {
char input[128];
char *ret;
while(!feof(stdin)) {
ret = fgets(input, BUF_SIZE - 1, stdin);
if(ret != NULL)
printf("This is what I read: %s\n", input);
}
}

View File

@@ -0,0 +1,2 @@
This is a text file.
CS2106 Introduction to Operating Systems is a cool module!

View File

@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int slow(char *name, int time, int n) {
for(int i=n; i<=n+4; i++) {
printf("%s: i = %d\n", name, i);
sleep(time);
}
}
sdf
asdf
as
dfas
f
int main() {
int id;
if((id = fork()) != 0) {
int stat;
int my_id = getpid();
int parent_id = getppid();
printf("\nI am the parent.\n");
printf("My ID is %d\n", my_id);
printf("My parent's ID is %d\n", parent_id);
printf("My child's ID is %d\n\n", id);
slow("Parent", 1, 5);
printf("\nWaiting for child to exit.\n");
wait(&stat);
printf("CHILD HAS EXITED WITH STATUS %d\n", WEXITSTATUS(stat));
}
else
{
id = getpid();
int parent_id = getppid();
printf("\nI am the child.\n");
printf("My ID is %d\n", id);
printf("My parent's ID is %d\n\n", parent_id);
slow("Child", 2, 10);
exit(25);
}
}

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
int main(int ac, char **av, char **vp) {
printf("ac = %d\n", ac);
printf("Arguments:\n");
int i;
for(i=0; i<ac; i++)
printf("Arg %d is %s\n", i, av[i]);
i=0;
while(vp[i] != NULL) {
printf("Env %d is %s\n", i, vp[i]);
i++;
}
}

View File

@@ -0,0 +1,12 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
int main() {
if(fork() == 0) {
execlp("cat", "cat", "file.txt", NULL);
}
else
wait(NULL);
}

View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
int fp_in = open("./file.txt", O_RDONLY);
int fp_out = open("./talk.out", O_CREAT | O_WRONLY);
if(fork() == 0) {
dup2(fp_in, STDIN_FILENO);
dup2(fp_out, STDOUT_FILENO);
execlp("./talk", "talk", (char *) 0);
close(fp_in);
close(fp_out);
}
else
wait(NULL);
}

View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
int main() {
int p[2];
char str[] = "Hello this is the parent.";
// This creates a pipe. p[0] is the reading end,
// p[1] is the writing end.
if(pipe(p) < 0)
perror("lab2p2e: ");
// We will send a message from father to child
if(fork() != 0) {
close(p[0]); // The the end we are not using.
write(p[1], str, strlen(str));
close(p[1]);
wait(NULL);
}
else
{
char buffer[128];
close(p[1]); // Close the writing end
read(p[0], buffer, 127);
printf("Child got the message \"%s\"\n", buffer);
close(p[0]);
}
}

View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
int main() {
printf("Be patient, the program will take around 7 seconds to run.\n");
printf("At the end you can do \"cat results.out\" to see the result.\n");
//
// Add code here to pipe from ./slow 5 to ./talk and redirect
// output of ./talk to results.out
// I.e. your program should do the equivalent of ./slow 5 | talk > results.out
// WITHOUT using | and > from the shell.
//
}

View File

@@ -0,0 +1,29 @@
#include <stdio.h>
// For the sleep functiopn
#include <unistd.h>
// For the atoi function that converts
// strings to integers
#include <stdlib.h>
int main(int ac, char **av) {
if(ac != 2) {
printf("\nCounts from specified integer n to n + 5\n");
printf("Usage: %s <integer>\n\n", av[0]);
exit(-1);
}
int n = atoi(av[1]);
int i;
for(i=n; i<=n+5; i++) {
printf("%d\n", i);
sleep(1);
}
printf("\nFinal value of i is %d\n\n", i);
exit(i);
}

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#define BUF_SIZE 128
int main() {
char input[128];
char *ret;
while(!feof(stdin)) {
ret = fgets(input, BUF_SIZE - 1, stdin);
if(ret != NULL)
printf("This is what I read: %s\n", input);
}
}