Commit 41092e9c by unknown

lab 9 works

parent 8c63e63e
......@@ -6,4 +6,4 @@
5 5 5 1 4
4 5 3 5 4
4 4 3 4 5
5 5 5 5 5
\ No newline at end of file
5 5 5 5 99
\ No newline at end of file
......@@ -2,7 +2,7 @@
* File: homework2.c
* Author: Tran Minh Huy Vu
*
* Created on October 4, 2018, 1:50 PM
* Created on October 29, 2018, 3:50 PM
* Description: Creating a 2D array as a score database for students and their grades
* and calculate the average and find the best student.
*/
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int toBase10(char*, int);
char* toOtherBase(int, int);
int charToInt(char);
char intToChar(int);
int main() {
int size = 1;
int location = 0;
char* input = malloc(size * sizeof(char));
int baseInput;
int baseOutput;
printf("Please input a number: ");
char c;
while ((c = getchar()) != 10) {
input[location++] = c;
if (location >= size) {
size++;
input = realloc(input, sizeof(char));
}
}
input[location] = '\0';
printf("Base of input: ");
scanf("%d", &baseInput);
printf("Desired output base: ");
scanf("%d", &baseOutput);
int resultBase10 = toBase10(input, baseInput);
if (baseOutput == 10) {
printf("Output: %d\n", resultBase10);
} else {
char* result = toOtherBase(resultBase10, baseOutput);
printf("Output: ");
for (int i = strlen(result); i >= 0; i--) {
printf("%c", result[i]);
}
printf("\n");
free(result);
}
free(input);
return 0;
}
int toBase10(char* input, int base) {
int count = strlen(input);
int result = 0;
int location = 0;
for (int i = count - 1; i >= 0; i--) {
result += charToInt(input[i]) * (int)pow(base, location++);
}
return result;
}
int charToInt(char input) {
if (input > '9') return input - 'a' + 10;
else return input - '0';
}
char* toOtherBase(int input, int base) {
int size = 1;
int location = 0;
char* result = malloc(size * sizeof(char));
int n = input;
int r;
while (n > 0) {
r = n % base;
n /= base;
result[location++] = intToChar(r);
if (location >= size) {
size++;
result = realloc(result, size * sizeof(char));
}
}
result[location] = '\0';
return result;
}
char intToChar(int input) {
if (input < 10) return input + '0';
else return input - 9 + 'a';
}
#include <stdio.h>
#include <math.h>
int main() {
float a, b;
char action;
printf("Please input action (* / + -): ");
action = getchar();
printf("Please input first number: ");
scanf("%f", &a);
printf("Please input second number: ");
scanf("%f", &b);
switch (action) {
case '+':
printf("%f\n", a + b);
break;
case '-':
printf("%f\n", a - b);
break;
case '*':
printf("%f\n", a * b);
break;
case '/':
printf("%f\n", a / b);
break;
default:
printf("Invalid action!");
break;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
float average(int*, int);
int max(int*, int);
int min(int*, int);
int main() {
int size = 1;
int location = 0;
char* array = malloc(size * sizeof(char));
printf("Please input an array of int, with space seperator:\n");
char c;
while ((c = getchar()) != 10) {
array[location++] = c;
if (location >= size) {
size++;
array = realloc(array, size * sizeof(char));
}
}
array[location] = '\0';
size = 1;
location = 0;
int* numberList = malloc(size * sizeof(int));
char* currentWord = strtok(array, " ");
while (currentWord != NULL) {
numberList[location++] = atoi(currentWord);
currentWord = strtok(NULL, " ");
if (location >= size) {
size++;
numberList = realloc(numberList, size * sizeof(int));
}
}
char input[4];
printf("Input method (max, min, avr): ");
scanf("%s", &input);
if (strncmp(input, "max", 3) == 0) {
printf("%d\n", max(numberList, location));
} else if (strncmp(input, "min", 3) == 0) {
printf("%d\n", min(numberList, location));
} else if (strncmp(input, "avr", 3) == 0) {
printf("%f\n", average(numberList, location));
} else {
printf("Invalid bullshit\n");
}
free(numberList);
free(array);
return 0;
}
float average(int* array, int length) {
float sum = 0;
for (int i = 0; i < length; i++) {
sum += array[i];
}
return (float)(sum / length);
}
int max(int* array, int length) {
int largest = array[0];
for (int i = 1; i < length; i++) {
if (largest < array[i]) largest = array[i];
}
return largest;
}
int min(int* array, int length) {
int smallest = array[0];
for (int i = 1; i < length; i++) {
if (smallest > array[i]) smallest = array[i];
}
return smallest;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int custom_isnan(char*);
int main() {
char input[1000];
printf("Please input something: ");
scanf("%s", &input);
if (custom_isnan(input)) {
puts("Not a number!");
} else {
printf("%s is a number!\n", input);
}
return 0;
}
int custom_isnan(char* input) {
int length = strlen(input);
int end = 0;
for (int i = 0; i < length && !end; i++) {
char c = input[i];
if (!(c >= '0' && c <= '9')) {
if (c != '.') {
end = 1;
}
}
}
if (end) return 1;
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int custom_isnan(char*, int);
int main() {
char input[1000];
printf("Please input something: ");
scanf("%s", &input);
int base;
printf("Please input base number: ");
scanf("%d", &base);
if (custom_isnan(input, base)) {
puts("Not a number!");
} else {
printf("%s is a number!\n", input);
}
return 0;
}
int custom_isnan(char* input, int base) {
int length = strlen(input);
int end = 0;
for (int i = 0; i < length && !end; i++) {
char c = input[i];
int number = c - '0';
if (base <= 10) {
if (!(number >= 0 && number < base)) {
end = 1;
}
} else {
if (!(number >= 0 && number < base)) {
int after10 = c - 'a';
if (!(after10 >= 0 && after10 < (base - 10))) {
end = 1;
}
}
}
}
if (end) return 1;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment