diff --git a/console.c b/console.c index a280d2ba60..a1189bb352 100644 --- a/console.c +++ b/console.c @@ -1,7 +1,6 @@ // Console input and output. // Input is from the keyboard or serial port. // Output is written to the screen and serial port. - #include "types.h" #include "defs.h" #include "param.h" @@ -187,6 +186,9 @@ struct { } input; #define C(x) ((x)-'@') // Control-x +#define K 4 +char saved_buf[INPUT_BUF]; // saved buffer of copy-paste +int saved_buffer_size = 0; void consoleintr(int (*getc)(void)) @@ -200,6 +202,51 @@ consoleintr(int (*getc)(void)) // procdump() locks cons.lock indirectly; invoke later doprocdump = 1; break; + case 67: case 88: //copy and cut + for (int i = (input.e - input.r) - K; i < input.e - input.r ; i++) + { + saved_buf[i - (input.e - input.r - K)] = input.buf[(input.r + i ) % INPUT_BUF]; + saved_buffer_size += 1; + } + + if (c == 88) + { + while(input.e != input.w && input.buf[(input.e-1) % INPUT_BUF] != '\n'){ + input.e--; + consputc(BACKSPACE); + } + } + break; + case 86: //paste + for (int i = 0; i < saved_buffer_size; i++) + { + c = saved_buf[i]; + consputc(c); + } + break; + + case C('E'): + for (int i = 0; i < input.e - input.r ; i++) + { + int temp = input.buf[(input.r + i ) % INPUT_BUF]; + if(temp >= 48 && temp <= 53) + { + temp += 4; + } + else if(temp >= 54 && temp <= 57) + { + + } + else if(temp >= 97 && temp <= 122) + { + + } + else if(temp >= 65 && temp <= 90) + { + + } + // saved_buf[i] = + } case C('U'): // Kill line. while(input.e != input.w && input.buf[(input.e-1) % INPUT_BUF] != '\n'){ @@ -293,7 +340,5 @@ consoleinit(void) devsw[CONSOLE].write = consolewrite; devsw[CONSOLE].read = consoleread; cons.locking = 1; - ioapicenable(IRQ_KBD, 0); } - diff --git a/init.c b/init.c index 374f77cffd..01406a9f4a 100644 --- a/init.c +++ b/init.c @@ -23,10 +23,10 @@ main(void) printf(1, "init: starting sh\n"); // printing students name and id - printf(1, "Group #8 "); - printf(1, "Ali Parvizi - 810100102"); - printf(1, "Mohammad Mataee - 810199493"); - printf(1, "Mohammad Javad Afsari - 810198544"); + printf(1, "Group #8 \n"); + printf(1, "Ali Parvizi - 810100102 \n"); + printf(1, "Mohammad Mataee - 810199493 \n"); + printf(1, "Mohammad Javad Afsari - 810198544 \n"); pid = fork(); if(pid < 0){ diff --git a/sdvar.c b/sdvar.c index 8875087404..af40dbad66 100644 --- a/sdvar.c +++ b/sdvar.c @@ -1,76 +1,158 @@ +#include "types.h" +#include "stat.h" +#include "user.h" +#include "fcntl.h" + #define MAX_NUMBERS 7 -float calculateSD(double data[],int n) { - float sum = 0.0, mean, SD = 0.0; - int i; - for (i = 0; i < n; ++i) { - sum += data[i]; +int abs(int x) { + return x >= 0 ? x : -x; +} + +double sqrt(double x) { + if (x < 0) return -1; + + double epsilon = 0.1; + double guess = x; + + while (abs(guess * guess - x) >= epsilon) { + guess = (guess + x / guess) / 2.0; } - mean = sum / n; - for (i = 0; i < n; ++i) { - SD += pow(data[i] - mean, 2); + + return guess; +} + +int calculate_sum(int arr[], int size) { + int sum = 0; + for(int i = 0; i < size; i++){ + sum += arr[i]; } - return sqrt(SD / n); + return sum; } -float Variance(double a[], int n) + +double calculate_mean(int arr[], int size) { - int sum = 0; - for (int i = 0; i < n; i++) - sum += a[i]; - double mean = (double)sum / (double)n; - - double sqDiff = 0; - for (int i = 0; i < n; i++) - sqDiff += (a[i] - mean) * - (a[i] - mean); - return sqDiff / n; + return (double)calculate_sum(arr, size) / size; } -int main() { - double numbers[MAX_NUMBERS]; - int count = 0; - double sum = 0.0; - - // Read input numbers - printf("Enter up to %d numbers (separated by spaces):\n", MAX_NUMBERS); - while (count < MAX_NUMBERS && scanf("%lf", &numbers[count]) == 1) { - sum += numbers[count]; - count++; + +double calculate_variance(int arr[], int n) { + double sum_of_squared_differences = 0.0; + double mean = calculate_mean(arr, n); + + for (int i = 0; i < n; i++) { + double difference = (double)arr[i] - mean; + sum_of_squared_differences += difference * difference; } - // Calculate average - int average = sum / count; - - // Calculate standard deviation and variance - double standard_deviation_array[MAX_NUMBERS]; - double variance_array[MAX_NUMBERS]; - int sda_temp = 0; - int va_temp = 0; - for(int i=0; i 0); // Delete it + if (sign < 0) { + s[i++] = '-'; + } + s[i] = '\0'; // Null-terminate the string + reverse(s); +} + + +int main(int argc, char* argv[]) { + + if (argc > 8){ + printf(1, "Error! : size of array is not valid"); + exit(); + } + + int numbers[MAX_NUMBERS]; + int size = argc - 1; + + for (int i = 0; i < size; i++){ + numbers[i] = atoi(argv[i + 1]); + } + + int mean = (int)calculate_mean(numbers, size); + + + int smallCount = 0; + int bigCount = 0; + for (int i = 0; i < size; i++) { + if (numbers[i] <= mean) { + smallCount++; + } else { + bigCount++; } } - int standard_deviation = calculateSD(standard_deviation_array, sda_temp); - int variance = Variance(variance_array, va_temp); - // Save results to a file - FILE *file = fopen("txt.result_sdvar", "w"); - if (file != NULL) { - fprintf(file, "%d\n", average); - fprintf(file, "%d\n", standard_deviation); - fprintf(file, "%d\n", variance); - fclose(file); + + int* smallNumbers = (int*)malloc(smallCount * sizeof(int)); + int* bigNumbers = (int*)malloc(bigCount * sizeof(int)); + + + initializeBiggerAndSmallerNumbers(numbers, size, mean, smallNumbers, bigNumbers); + + int sd = (int)calculate_standard_deviation(smallNumbers, smallCount); + int variance = (int)calculate_variance(bigNumbers, bigCount); + + free(bigNumbers); + free(smallNumbers); + + + unlink("strdiff_result.txt"); + int fd = open("sdvar_result.txt", O_CREATE | O_WRONLY); + if (fd >= 0) { + + char mean_str[10]; + char sd_str[10]; + char variance_str[10]; + + itoa(mean, mean_str); + itoa(sd, sd_str); + itoa(variance, variance_str); + + write(fd, mean_str, strlen(mean_str)); + write(fd, " ", 1); + write(fd, sd_str, strlen(sd_str)); + write(fd, " ", 1); + write(fd, variance_str, strlen(variance_str)); + write(fd, "\n", 1); + + close(fd); } else { - printf("Error opening file for writing.\n"); + printf(1, "Error opening file for writing.\n"); } - return 0; + exit(); }