Skip to content

Commit

Permalink
Added program ascii, bubblesort, and help
Browse files Browse the repository at this point in the history
bubblesort.c check integer input

Reformat

ASCII print from 32 to 126, bubblesort.c assume 0 when enter illegal values, random.c print out the random number, modified tests

Fix help.c 0<=n<=100
  • Loading branch information
hachihao792001 authored and leduythuccs committed Oct 13, 2021
1 parent d8bd212 commit 0072ce5
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 1 deletion.
13 changes: 13 additions & 0 deletions build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ echo "255 "$(head -c 255 < /dev/zero | tr '\0' 'a') | \
../build.linux/nachos -x str_io | \
grep -q $(head -c 255 < /dev/zero | tr '\0' 'a')"Machine halting!"

../build.linux/nachos -x ascii | grep -q "33 !"
../build.linux/nachos -x ascii | grep -q "126 ~"
echo "5 3 4 1 5 2 1" | ../build.linux/nachos -x bubblesort | grep -q '1 2 3 4 5 Machine halting!'
echo "5 3 4 1 5 2 2" | ../build.linux/nachos -x bubblesort | grep -q '5 4 3 2 1 Machine halting!'
echo "5 -3 4 1 -5 2 1" | ../build.linux/nachos -x bubblesort | grep -q '\-5 \-3 1 2 4 Machine halting!'
echo "a 5 3 4 1 5 2 1" | ../build.linux/nachos -x bubblesort | grep -q 'Sorted array: Machine halting!'
echo "-5 5 3 4 1 5 2 1" | ../build.linux/nachos -x bubblesort | grep -q 'n has to be an integer between 1 and 100 (inclusive), please try again'
echo "101 5 3 4 1 5 2 1" | ../build.linux/nachos -x bubblesort | grep -q 'n has to be an integer between 1 and 100 (inclusive), please try again'
echo "5 3 4 1 5 2 3 1" | ../build.linux/nachos -x bubblesort | grep -q 'Wrong input, please try again'
echo "5 a 4 1 5 2 1" | ../build.linux/nachos -x bubblesort | grep -q '0 1 2 4 5 Machine halting!'

../build.linux/nachos -x help | grep -q "\- The program will print out the sorted array and then exit"

echo "Success!"
20 changes: 19 additions & 1 deletion code/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ ifeq ($(hosttype),unknown)
PROGRAMS = unknownhost
else
# change this if you create a new test program!
PROGRAMS = add halt shell matmult sort segments test_syscall num_io char_io random str_io
PROGRAMS = add halt shell matmult sort segments test_syscall num_io char_io random str_io ascii bubblesort help
endif

all: $(PROGRAMS)
Expand Down Expand Up @@ -186,6 +186,24 @@ str_io.o: str_io.c
str_io: str_io.o start.o
$(LD) $(LDFLAGS) start.o str_io.o -o str_io.coff
$(COFF2NOFF) str_io.coff str_io

ascii.o: ascii.c
$(CC) $(CFLAGS) -c ascii.c
ascii: ascii.o start.o
$(LD) $(LDFLAGS) start.o ascii.o -o ascii.coff
$(COFF2NOFF) ascii.coff ascii

bubblesort.o: bubblesort.c
$(CC) $(CFLAGS) -c bubblesort.c
bubblesort: bubblesort.o start.o
$(LD) $(LDFLAGS) start.o bubblesort.o -o bubblesort.coff
$(COFF2NOFF) bubblesort.coff bubblesort

help.o: help.c
$(CC) $(CFLAGS) -c help.c
help: help.o start.o
$(LD) $(LDFLAGS) start.o help.o -o help.coff
$(COFF2NOFF) help.coff help

clean:
$(RM) -f *.o *.ii
Expand Down
11 changes: 11 additions & 0 deletions code/test/ascii.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "syscall.h"

int main() {
int i;
for (i = 32; i <= 126; i++) {
PrintNum(i);
PrintChar(' ');
PrintChar((char)i);
PrintChar('\n');
}
}
52 changes: 52 additions & 0 deletions code/test/bubblesort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "syscall.h"
#define SIZE (100)

int main() {
int n, a[SIZE + 1], order, temp, i, j;
do {
PrintString("Enter n (0 <= n <= 100): ");
n = ReadNum();
if (n < 0 || n > 100)
PrintString(
"n has to be an integer between 1 and 100 (inclusive), please "
"try again\n");
} while (n < 0 || n > 100);

for (i = 0; i < n; i++) {
PrintString("Enter a[");
PrintNum(i);
PrintString("]: ");
a[i] = ReadNum();
}

do {
PrintString("Enter sort order (1: increasing, 2: decreasing): ");
order = ReadNum();
if (order != 1 && order != 2)
PrintString("Wrong input, please try again\n");
} while (order != 1 && order != 2);

for (i = 0; i < n; i++) {
for (j = 0; j < n - 1; j++) {
if (order == 1) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
} else if (order == 2) {
if (a[j] < a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}

PrintString("Sorted array: ");
for (i = 0; i < n; i++) {
PrintNum(a[i]);
PrintChar(' ');
}
}
21 changes: 21 additions & 0 deletions code/test/help.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "syscall.h"

int main() {
PrintString("Our team:\n");
PrintString("19120037 Le Duy Thuc\n");
PrintString("19120395 Tran Duy Tien\n");
PrintString("19120219 Ha Chi Hao\n\n");
PrintString(
"ASCII: at directory code, run build.linux/nachos -x test/ascii to "
"print the ASCII table\n");
PrintString(
"Sort: at directory code, run build.linux/nachos -x test/bubblesort to "
"start the sort program\n");
PrintString("\t- Enter n (the length of the array, 0 <= n <= 100)\n");
PrintString("\t- Enter n elements of the array\n");
PrintString(
"\t- Enter the order you want to sort the array with (1: increasing, "
"2: decreasing)\n");
PrintString(
"\t- The program will print out the sorted array and then exit\n");
}
1 change: 1 addition & 0 deletions code/test/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
int main() {
int result;
result = RandomNum();
PrintNum(result);
}

0 comments on commit 0072ce5

Please sign in to comment.