Initial commit

This commit is contained in:
Denis Nuțiu 2016-04-11 20:00:29 +03:00
commit 24ee0f2d8a
51 changed files with 5164 additions and 0 deletions

BIN
lab1/a.out Executable file

Binary file not shown.

89
lab1/lab1.c Normal file
View file

@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isbn13(char * isbn) {
int sum = 0;
int n = 13;
for (int i = 0; i < n - 1; i++) {
if ( (i + 1) % 2 == 0 ) {
sum += 3 * (isbn[i] - '0');
} else {
sum += isbn[i] - '0';
}
}
if ( 10 - ( sum % 10) == isbn[n - 1] - '0' ) {
return 1;
}
return 0;
}
char * conv_isbn13(char * isbn) {
char * new = malloc(13);
int n = 10;
new[0] = '9';
new[1] = '7';
new[2] = '8';
for (int i = 0; i < n; i++) {
new[i+3] = isbn[i];
}
return new;
new[13] = '\0';
}
int isbn10(char * isbn) {
int sum = 0;
for (int i = 0; i < 9; i++) {
sum += (i + 1) * (isbn[i] - '0');
}
if ( ( sum % 11) == isbn[9] - '0' ) {
return 1;
}
return 0;
}
char * conv_isbn10(char * isbn) {
char * new = malloc(10);
for (int i = 0; i < 9; i++) {
new[i] = isbn[i+3];
}
new[10] = '\0';
return new;
}
void guess2(int l, int h);
void guessnum() {
printf("Think of a number between 0 and 100\n");
guess2(0, 100);
}
void guess2(int l, int h) {
int n = 0;
int fitty = (l + h) / 2 ;
int guess;
int guesst;
printf("IS your number %d?\n", fitty);
scanf("%d", &guesst);
if (guesst == 1) {
printf("Your number is %d\n", fitty);
exit(0);
}
while ( fitty != n ) {
printf("IS you number greather than %d\n", fitty);
scanf("%d", &guess);
if (guess == 1) {
guess2(fitty, h);
} else {
guess2(l, fitty);
}
}
}
int main(int argc, char const *argv[]) {
guessnum();
// printf("%s\n", conv_isbn10("9781435704572"));
// printf("%s\n", conv_isbn13("0521396549"));
return 0;
}

BIN
lab2/libstr.a Normal file

Binary file not shown.

21
lab2/main.c Normal file
View file

@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include "str.h"
int main(void) {
printf("%d\n", my_strcmp("abc", "abc"));
printf("%d\n", my_strcmp("adc", "abc"));
printf("%d\n", my_strcmp("abc", "adc"));
printf("%d\n", my_strcmp("abcd", "abc"));
printf("%d\n", my_strcmp("abc", "abcd"));
char * dest = malloc(12);
char * dest2 = malloc(6);
char * src = "Ana are mere.";
my_strcpy(dest, src);
my_strncpy(dest2, src, 5);
printf("%s\n", dest);
printf("%s\n", dest2);
return 0;
}

12
lab2/makefile Normal file
View file

@ -0,0 +1,12 @@
CC = gcc
TARGET = my_string
CFLAGS = -Wall -O3
all: $(TARGET)
$(TARGET):
$(CC) $(CFLAGS) -c str.c
ar rcs libstr.a str.o
$(CC) $(CFLAGS) main.c -o $(TARGET).x -L. -lstr
clean:
$(RM) $(TARGET).x

BIN
lab2/my_string.x Executable file

Binary file not shown.

32
lab2/str.c Normal file
View file

@ -0,0 +1,32 @@
#include <string.h>
int my_strcmp(char * one, char * two) {
if ( strlen(one) > strlen(two) ) {
return 1;
} else if ( strlen(one) < strlen(two) ) {
return -1;
}
for (int i = 0; one[i]; i++) {
if ( one[i] > two[i] ) {
return 1;
} else if ( one[i] < two[i] ) {
return -1;
}
}
return 0;
}
char * my_strcpy(char * dest, char * source) {
int index = 0;
while ( (dest[index] = source[index]) ) { index++; };
dest[index] = '\0';
return dest;
}
char * my_strncpy(char * dest, char * source, size_t len) {
int index = 0;
while ( (dest[index] = source[index]) && index < len ) { index++; };
dest[len] = '\0';
return dest;
}

10
lab2/str.h Normal file
View file

@ -0,0 +1,10 @@
#include <stddef.h>
#ifndef MY_STRING_H
#define MY_STRING_H
int my_strcmp(char * one, char * two);
char my_strcpy(char * dest, char * source);
char my_strncpy(char * dest, char * source, size_t len);
#endif

BIN
lab2/str.o Normal file

Binary file not shown.

BIN
lab3/a Executable file

Binary file not shown.

75
lab3/a.cpp Normal file
View file

@ -0,0 +1,75 @@
#include <iostream>
using namespace std;
class Person {
protected:
string name;
int age;
public:
virtual void getdata() = 0;
virtual void putdata() = 0;
};
class Professor : public Person {
static int professors;
int publications;
int id;
public:
Professor() {
Professor::professors++;
}
~Professor() {
Professor::professors--;
}
virtual void getdata() {
cin >> Professor::name;
cin >> Professor::age;
cin >> Professor::publications;
Professor::id = professors;
}
virtual void putdata() {
cout << Professor::name << " " << Professor::age << " "
<< Professor::publications << " " << Professor::id << endl;
}
};
int Professor::professors = 0;
class Student : public Person {
static int students;
int marks[6];
int id;
public:
Student() {
Student::students++;
}
~Student() {
Student::students--;
}
virtual void getdata() {
cin >> Student::name;
cin >> Student::age;
Student::id = Student::students;
int mark;
for (int i = 0; i < 6; i++) {
cin >> mark;
Student::marks[i] = mark;
}
}
virtual void putdata() {
cout << Student::name << " " << Student::age << " ";
for (int i = 0; i < 6; i++) {
cout << Student::marks[i] << " ";
}
cout << Student::id << endl;
}
};
int Student::students = 0;
int main() {
return 0;
}

BIN
lab3/a.out Executable file

Binary file not shown.

BIN
lab3/b Executable file

Binary file not shown.

BIN
lab3/c Executable file

Binary file not shown.

BIN
lab3/d Executable file

Binary file not shown.

26
lab3/i.txt Normal file
View file

@ -0,0 +1,26 @@
1
20
2
XAN 9
FIVE 5
FIVEz 5
RAD 8
Raasputiin 10
MARIONE 9
FAULT_Zero 11
THREE 3
Rick 9
Scary 9
XAN1 9
FIVE1 5
FIVEz1 5
RAD1 8
Raasputiin1 10
MARIONE1 9
FAULT1 11
THREE1 3
Rick1 9
Scary1 9
3
4
5

35
lab3/lab3_1.c Normal file
View file

@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int (*comp)(const void *, const void *);
int sorter(double * a, double * b) {
if ( *a > *b ) {
return 1;
} else if ( *a < *b ) {
return -1;
} else {
return 0;
}
}
void populate_array(double * array, size_t size) {
srand(time(0));
for (int i = 0; i < size; i++) {
array[i] = rand() % 101;
// printf("%f\n", array[i]);
}
qsort(array, size, sizeof(array[0]), (comp) sorter);
}
void parr(double * arr, size_t size) {
for (int i = 0; i < size; i++) printf("%f\n", arr[i]);
}
int main(void) {
double array[5];
populate_array(array, 5);
parr(array, 5);
return 0;
}

120
lab3/lab3_2.c Normal file
View file

@ -0,0 +1,120 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
typedef struct Student_Tag {
int grade;
char name[41];
} Student;
int n = 0;
Student ** students;
int sort_grade(const void * a, const void * b) {
Student ** aa = (Student **) a;
Student ** bb = (Student **) b;
return (*bb)->grade - (*aa)->grade;
}
int sort_alpha(const void * a, const void * b) {
Student ** aa = (Student **)a;
Student ** bb = (Student **)b;
return strcmp((*aa)->name, (*bb)->name);
}
void initialize_students(unsigned n) {
students = malloc(sizeof(Student*) * n);
}
void fenter_student() {
if (n == 0) {
printf("\tN must be bigger than 0, did you gave an N?\n");
return;
} else {
for (int i = 0; i < n; i++) {
students[i] = malloc(sizeof(Student));
printf("Enter student: FIRST NAME_LAST NAME, GRADE. MAX 40 CHARACTERS!\n");
scanf("%40s %d", students[i]->name, &students[i]->grade);
printf("Student was added successfully.\n");
printf("Name: %s\n", students[i]->name);
printf("Grade: %d\n", students[i]->grade);
}
}
}
void fgive_n() {
printf("Type your N\n");
scanf("%d", &n);
}
void display(Student ** arr, int n) {
for (int i = 0; i < n; i++) {
printf("Student %s Grade %d\n", arr[i]->name, arr[i]->grade);
}
}
int choice() {
enum { exit_program, give_n, enter_student, display_class_alpha,
display_class_grade, display_first_three };
int ch = 0; // In case of EOF, will exit. Hopefully.
scanf("%d", &ch);
switch (ch) {
case exit_program:
printf("Exiting..\n");
return 1;
break;
case give_n:
fgive_n();
initialize_students(n);
break;
case enter_student:
fenter_student();
break;
case display_class_alpha:
assert(n > 0);
printf("Display according to name\n");
qsort(students, n, sizeof(Student *), sort_alpha);
display(students, n);
break;
case display_class_grade:
assert(n > 0);
printf("Display according to grades\n");
qsort(students, n, sizeof(Student *), sort_grade);
display(students, n);
break;
case display_first_three:
assert(n > 0);
printf("Display first three according to grades\n");
qsort(students, n, sizeof(Student *), sort_grade);
display(students, 3);
break;
default:
printf("\tInvalid choice\n");
}
return 0;
}
void display_menu() {
printf("\n");
printf("0. Exit program\n");
printf("1. Give N, number of students\n");
printf("2. Enter the student\n");
printf("3. Display class in alphabetic order\n");
printf("4. Display class in creating a top based on grades\n");
printf("5. Display the first three students according to their grades\n");
printf("\n");
}
int main() {
int sgexit = 0;
while (!sgexit) {
display_menu();
sgexit = choice();
}
return 0;
}

116
lab3/lab3_2b.c Normal file
View file

@ -0,0 +1,116 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
typedef int (*comp)(const void *, const void *);
typedef struct Student_Tag {
int grade;
char name[41];
} Student;
int n = 0;
Student * students;
int sort_grade(Student * a, Student * b) {
return b->grade - a->grade;
}
int sort_alpha(Student * a, Student * b) {
return strcmp(a->name, b->name);
}
void initialize_students(unsigned n) {
students = malloc(sizeof(Student) * n);
}
void fenter_student() {
if (n == 0) {
printf("\tN must be bigger than 0, did you gave an N?\n");
return;
} else {
int index = 0;
for (int i = 0; i < n; i++) {
printf("Enter student: FIRST NAME_LAST NAME, GRADE. MAX 40 CHARACTERS!\n");
scanf("%40s %d", (students + index)->name, &(students + index)->grade);
printf("Student was added successfully.\n");
printf("Name: %s\n", (students + index)->name);
printf("Grade: %d\n", (students + index)->grade);
index += sizeof(Student);
}
}
}
void fgive_n() {
printf("Type your N\n");
scanf("%d", &n);
}
void display(Student * arr, int n) {
for (int i = 0; i < n * sizeof(Student); i += sizeof(Student)) {
printf("Student %s Grade %d\n", arr[i].name, arr[i].grade);
}
}
int choice() {
enum { exit_program, give_n, enter_student, display_class_alpha,
display_class_grade, display_first_three };
int ch = 0;
scanf("%d", &ch);
switch (ch) {
case exit_program:
printf("Exiting..\n");
return 1;
break;
case give_n:
fgive_n();
initialize_students(n);
break;
case enter_student:
fenter_student();
break;
case display_class_alpha:
assert(n > 0);
printf("Display according to name\n");
qsort(students, n, sizeof(students[0]), (comp)sort_alpha);
display(students, n);
break;
case display_class_grade:
assert(n > 0);
// printf("%lu %lu \n", sizeof(students), sizeof(students[0]) );
printf("Display according to grades\n");
qsort(students, n, sizeof(students[0]), (comp)sort_grade);
display(students, n);
break;
case display_first_three:
assert(n > 0);
printf("Display first three according to grades\n");
qsort(students, n, sizeof(students[0]), (comp)sort_grade);
display(students, 3);
break;
default:
printf("\tInvalid choice\n");
}
return 0;
}
void display_menu() {
printf("\n");
printf("0. Exit program\n");
printf("1. Give N, number of students\n");
printf("2. Enter the student\n");
printf("3. Display class in alphabetic order\n");
printf("4. Display class in creating a top based on grades\n");
printf("5. Display the first three students according to their grades\n");
printf("\n");
}
int main() {
int sgexit = 0;
while (!sgexit) {
display_menu();
sgexit = choice();
}
return 0;
}

35
lab3/lab3_3.c Normal file
View file

@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
void guess2(int, int);
void guessnum() {
printf("Think of a number between 0 and 100\n");
guess2(0, 100);
}
void guess2(int l, int h) {
int n = 0;
int fitty = (l + h) / 2 ;
int guess;
int guesst;
printf("IS your number %d?\n", fitty);
scanf("%d", &guesst);
if (guesst == 1) {
printf("Your number is %d\n", fitty);
exit(0);
}
while ( fitty != n ) {
printf("IS you number greather than %d\n", fitty);
scanf("%d", &guess);
if (guess == 1) {
guess2(fitty, h);
} else {
guess2(l, fitty);
}
}
}
int main(int argc, char const *argv[]) {
guessnum();
return 0;
}

61
lab3/lab3_4.c Normal file
View file

@ -0,0 +1,61 @@
#include <stdio.h>
#include <math.h>
int is_prime(int);
int populate_array(int * array, size_t size) {
int index = 0;
for (int i = 1; i < size; i++) {
if ( is_prime(i) ) {
array[index++] = i;
}
}
return index;
}
int is_present(int * array, size_t size, int number);
void parr(int * array, size_t size, size_t numbers) {
for (int i = 1; i < numbers; i++)
printf("%d IS PRESENT: %d\n",i, is_present(array, size, i));
}
int is_prime(int number) {
for (int i = 2; i <= number/2; i++) {
if ( number % i == 0 ) {
return 0;
}
}
return 1;
}
int _is_present(int * array, int number, int low, int high) {
int middle = ((high + low) / 2.0);
//printf("L: %d H: %d M: %d N: %d\n", low, high, middle, number);
if ( number > array[middle] ) {
return _is_present(array, number, middle, high);
} else if ( number < array[middle] ) {
return _is_present(array, number, low, middle);
} else if ( number == array[middle] ) {
return 1;
} else {
return 0;
}
}
int is_present(int * array, size_t size, int number) {
if ( is_prime(number) ) {
return _is_present(array, number, 0, size);
} else {
return 0;
}
}
int main(void) {
int primes[100];
int real_primes = populate_array(primes, 100);
//parr(primes, real_primes, 100);
printf("%d\n", is_present(primes, real_primes, 6));
printf("%d\n", is_present(primes, real_primes, 13));
return 0;
}

BIN
lab4/a.out Executable file

Binary file not shown.

11
lab4/i Normal file
View file

@ -0,0 +1,11 @@
10
1 3 3 6
2 1 1 2 2 3 3
3 1 1 2 2 3 3 4 4
1 7 5 10
3 10 15 6 9 21 25 92 89
1 1 1 2
2 0 1 0 5 5 2
3 0 0 0 5 5 0 5 5
1 3 3 7
2 5 5 6 6 7 7

235
lab4/main.c Normal file
View file

@ -0,0 +1,235 @@
#include <stdio.h>
#include <stdlib.h>
enum { point, circle, triangle, rectangle }; // Holds type data. Because numbers are too mainstream.
typedef struct _point {
unsigned int type: 2;
int x;
int y;
} Point;
typedef struct _circle {
unsigned int type: 2;
Point point;
int radius;
} Circle;
typedef struct _triangle {
unsigned int type: 2;
Point points[3];
} Triangle;
typedef struct _rectangle {
unsigned int type: 2; // 11
Point points[4];
} Rectangle;
typedef union _geo {
Point point;
Triangle triangle;
Circle circle;
Rectangle rectangle;
} Shape;
// stolen from course website
typedef struct ilst *intlist_t;
intlist_t empty(void);
int isempty(intlist_t lst);
Shape head(intlist_t lst);
intlist_t tail(intlist_t lst);
intlist_t cons(Shape el, intlist_t tl);
intlist_t decons(intlist_t lst, Shape *elp);
struct ilst {
intlist_t nxt;
Shape el;
};
int isempty(intlist_t lst)
{
return lst == NULL;
}
intlist_t empty(void)
{
return NULL;
}
Shape head(intlist_t lst)
{
return lst->el;
}
intlist_t tail(intlist_t lst)
{
return lst->nxt;
}
intlist_t cons(Shape el, intlist_t tl)
{
intlist_t p = malloc(sizeof(struct ilst));
if (!p) return NULL;
p->el = el;
p->nxt = tl;
return p;
}
// end
// returns tail, assignes *elp with head
intlist_t decons(intlist_t lst, Shape *elp)
{
if (elp) *elp = lst->el;
intlist_t tl = lst->nxt;
free(lst);
return tl;
}
// end stolen from course website.
void printlist(intlist_t lst)
{
if (isempty(lst)) putchar('\n');
else {
if ( head(lst).point.type == 0 ) {
printf("This is a point.\n");
printf("x: %d y: %d\n", head(lst).point.x, head(lst).point.y );
} else if ( head(lst).point.type == circle ) {
printf("This is a Circle.\n");
printf("x: %d y: %d\n", head(lst).circle.point.x, head(lst).circle.point.y );
printf("radius: %d \n", head(lst).circle.radius );
} else if ( head(lst).point.type == triangle ) {
printf("This is a Triangle.\n");//written_by_denis
printf("x: %d y: %d\n", head(lst).triangle.points[0].x, head(lst).triangle.points[0].y );
printf("x: %d y: %d\n", head(lst).triangle.points[1].x, head(lst).triangle.points[1].y );
printf("x: %d y: %d\n", head(lst).triangle.points[2].x, head(lst).triangle.points[2].y );
} else if ( head(lst).point.type == rectangle ) {
printf("This is a Rectangle.\n");
printf("x: %d y: %d\n", head(lst).rectangle.points[0].x, head(lst).rectangle.points[0].y );
printf("x: %d y: %d\n", head(lst).rectangle.points[1].x, head(lst).rectangle.points[1].y );
printf("x: %d y: %d\n", head(lst).rectangle.points[2].x, head(lst).rectangle.points[2].y );
printf("x: %d y: %d\n", head(lst).rectangle.points[3].x, head(lst).rectangle.points[3].y );
}
printlist(tail(lst));
}
}
void print_array(Shape * shapes, size_t size) {
for ( int i = 0; i < size; i++ ) {
if ( shapes[i].point.type == point ) { // point
printf("shapes[%d] is a point.\n", i);
printf("x: %d y: %d\n", shapes[i].point.x, shapes[i].point.y );
} else if ( shapes[i].point.type == circle ) {
printf("shapes[%d] is a circle.\n", i);
printf("x: %d y: %d\n", shapes[i].circle.point.x, shapes[i].circle.point.y );
printf("radius: %d\n", shapes[i].circle.radius );
} else if ( shapes[i].point.type == triangle ) {
printf("shapes[%d] is a triangle.\n", i);
printf("x: %d y: %d\n", shapes[i].triangle.points[0].x, shapes[i].triangle.points[0].y );
printf("x: %d y: %d\n", shapes[i].triangle.points[1].x, shapes[i].triangle.points[1].y );
printf("x: %d y: %d\n", shapes[i].triangle.points[2].x, shapes[i].triangle.points[2].y );
} else if ( shapes[i].point.type == rectangle ) {
printf("shapes[%d] is a triangle.\n", i);
printf("x: %d y: %d\n", shapes[i].rectangle.points[0].x, shapes[i].rectangle.points[0].y );
printf("x: %d y: %d\n", shapes[i].rectangle.points[1].x, shapes[i].rectangle.points[1].y );
printf("x: %d y: %d\n", shapes[i].rectangle.points[2].x, shapes[i].rectangle.points[2].y );
printf("x: %d y: %d\n", shapes[i].rectangle.points[3].x, shapes[i].rectangle.points[3].y );
}
}
}
void process_input() {
int figures = -1;
intlist_t list = NULL;
printf("How many figures?\n");
scanf("%d", &figures);
// printf("figs %d\n", figures);
printf("Adding %d figures.\n", figures);
for (int i = 0; i < figures; i++) {
int type = -1;
printf("What's the type of the figure? (1 - CIRCLE, 2 - TRIANGLE, 3 - RECTANGLE)\n");
scanf("%d", &type);
Shape * shape;
switch (type) {
case circle:
shape = malloc(sizeof(Shape));
shape->circle.type = circle;
printf("What's the X and Y of the circle point?\n");
scanf("%d %d", &shape->circle.point.x, &shape->circle.point.y);
printf("What's the radius of the circle?\n");
scanf("%d", &shape->circle.radius);
printf("Circle added, moving on!\n");
list = cons(*shape, list);
free(shape);
break;
case triangle:
shape = malloc(sizeof(Shape));
shape->triangle.type = triangle;
printf("What's the X and Y of the first triangle point?\n");
scanf("%d %d", &shape->triangle.points[0].x, &shape->triangle.points[0].y);
printf("What's the X and Y of the seccond triangle point?\n");
scanf("%d %d", &shape->triangle.points[1].x, &shape->triangle.points[1].y);
printf("What's the X and Y of the third triangle point?\n");
scanf("%d %d", &shape->triangle.points[2].x, &shape->triangle.points[2].y);
printf("Triangle added, moving on!\n");
list = cons(*shape, list);
free(shape);
break;
case rectangle:
shape = malloc(sizeof(Shape));
shape->rectangle.type = rectangle;
printf("What's the X and Y of the first rectangle point?\n");
scanf("%d %d", &shape->rectangle.points[0].x, &shape->rectangle.points[0].y);
printf("What's the X and Y of the seccond rectangle point?\n");
scanf("%d %d", &shape->rectangle.points[1].x, &shape->rectangle.points[1].y);
printf("What's the X and Y of the third rectangle point?\n");
scanf("%d %d", &shape->rectangle.points[2].x, &shape->rectangle.points[2].y);
printf("What's the X and Y of the fourth rectangle point?\n");
scanf("%d %d", &shape->rectangle.points[3].x, &shape->rectangle.points[3].y);
printf("Rectangle added, moving on!\n");
list = cons(*shape, list);
free(shape);
break;
default:
fprintf(stderr, "Error, invalid type!\n");
return;
}
}
printf("\nPrinting...\n\n");
printlist(list);
}
int main() {
//METHOD !
Shape shapes[10]; // (Cast to type) (Struct initializer)
shapes[0].point = (Point) { point, 4, 6 };
shapes[1].point = (Point) { point, 1, 2};
shapes[2].point = (Point) { point, 1, 2};
shapes[3].circle = (Circle) { circle, {point, 1, 1}, 10 };
shapes[4].circle = (Circle) { circle, {point, 5, 5}, 16 };
shapes[5].triangle = (Triangle) { triangle, { {point, 1, 1}, {point, 2, 2}, {point, 3, 3} } };
shapes[6].triangle = (Triangle) { triangle, { {point, 1, 1}, {point, 2, 2}, {point, 3, 9} } };
shapes[7].circle = (Circle) { circle, {point, 52, 52}, 20 };
shapes[8].circle = (Circle) { circle, {point, 12, 64}, 36 };
shapes[9].triangle = (Triangle) { triangle, { {point, 10, 5}, {point, 15, 3}, {point, 21, 7} } };
//LIST
// intlist_t l1 = cons(shapes[0], cons(shapes[3], cons(shapes[5], cons(shapes[1], cons(shapes[2],
// cons(shapes[4], cons(shapes[6], cons(shapes[7], cons(shapes[8], cons(shapes[9], empty()))))))))));
// printlist(l1);
//ARRAY
// print_array(shapes, 10);
process_input();
return 0;
}

22
lab4/test.c Normal file
View file

@ -0,0 +1,22 @@
#include <stdio.h>
enum cardsuit {
CLUBS = 1,
DIAMONDS = 2 ,
HEARTS = 3 ,
SPADES = 4
} custom_deck [ 52 ] ;
struct {
unsigned int age : 3;
unsigned int dayWeek: 3;
unsigned int flagValid : 1;
} Info;
int main() {
unsigned int aux;
scanf ("%u", &aux);
if (aux == 0) Info.flagValid = 0; else Info.flagValid = 1;
if (aux > 1) fprintf(stderr , "Sir , You should be more careful with your input!\n");
return 0;
}

BIN
lab5/a.out Executable file

Binary file not shown.

283
lab5/tictac.c Normal file
View file

@ -0,0 +1,283 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int take_corners(int);
// explicit instantiation
int moves[9] = {0};
int moves_made = 0;
int computer_mv = 0;
enum { human_player = 1, computer_player = 2 };
enum { top_left, top_middle, top_right, middle_left, middle, middle_right,
bottom_left, bottom_middle, bottom_right };
void display_tutorial_board() {
int move;
for (int i = 0; i < 3; i++) {
printf("|");
for (int j = 0; j < 3; j++) {
move = j + i * 3 + 1;
printf("_%d_|", move);
}
printf("\n\n");
}
}
void display_board() {
char mark;
int move;
printf("\n");
for (int i = 0; i < 3; i++) {
printf("|");
for (int j = 0; j < 3; j++) {
move = moves[j + i * 3];
if ( !move ) mark = '_';
else if (move == human_player) mark = 'X';
else mark = 'O';
printf("_%c_|", mark);
}
printf("\n\n");
}
}
int _check_win(int x) {
int player_wins = x;
int aux = 0;
for (int i = 0; i < 9; i += 3) {
aux |= moves[i] & moves[i+1] & moves[i+2]; // linii
}
for (int i = 0; i < 3; i++) {
aux |= moves[i] & moves[i+3] & moves[i+6]; // coloane
}
aux |= moves[top_left] & moves[middle] & moves[bottom_right]; // principal
aux |= moves[top_right] & moves[middle] & moves[bottom_left]; // secondary
player_wins &= aux;
return player_wins;
}
int check_win() {
int player_wins = human_player;
player_wins &= _check_win(player_wins);
int computer_wins = computer_player;
computer_wins &= _check_win(computer_wins);
return player_wins | computer_wins;
}
int block(int player) {
int move = -1;
for (int i = 0; i < 9; i += 3) { // blocheaza liniile.
if ( ((moves[i] & moves[i + 1]) == player) && !moves[i + 2] ) {
moves[i + 2] = computer_player;
move = i + 2;
return move + 1;
} else if ( ((moves[i] & moves[i + 2]) == player) && !moves[i + 1] ) {
moves[i + 1] = computer_player;
move = i + 1;
return move + 1;
} else if ( ((moves[i + 1] & moves[i + 2]) == player) && !moves[i] ) {
moves[i] = computer_player;
move = i;
return move + 1;
}
}
for (int i = 0; i < 3; i++) { //blocheaza coloanele
if ( ((moves[i] & moves[i + 3]) == player) && !moves[i + 6] ) {
moves[i+6] = computer_player;
move = i+6;
return move +1;
} else if ( ((moves[i] & moves[i + 6]) == player) && !moves[i + 3] ) {
moves[i+3] = computer_player;
move = i+3;
return move +1;
} else if ( ((moves[i + 3] & moves[i + 6]) == player) && !moves[i] ) {
moves[i] = computer_player;
move = i;
return move +1;
}
}
// principal
if ( ((moves[top_left] & moves[middle]) == player) && !moves[bottom_right] ) {
moves[bottom_right] = computer_player;
return bottom_right + 1;
} else if ( ((moves[top_left] & moves[bottom_right]) == player) && !moves[middle] ) {
moves[middle] = computer_player;
return middle + 1;
} if ( ((moves[bottom_right] & moves[middle]) == player) && !moves[top_left] ) {
moves[top_left] = computer_player;
return top_left + 1;
}
// secondary
if ( ((moves[top_right] & moves[middle]) == player) && !moves[bottom_left] ) {
moves[bottom_left] = computer_player;
return bottom_left + 1;
} else if ( ((moves[top_right] & moves[bottom_left]) == player) && !moves[middle] ) {
moves[middle] = computer_player;
return middle + 1;
} if ( ((moves[bottom_left] & moves[middle]) == player) && !moves[top_right] ) {
moves[top_right] = computer_player;
return top_right + 1;
}
return move + 1;
}
int try_to_win(int player) {
int move = -1;
if (moves[middle] == computer_player) {
for (int i = 0; i < 9; i += 3) { // tries to make a line in order to not pun random things.
if ( (moves[i] | moves[i+1] | moves[i+2]) == player ) {
if (!moves[i]) {
moves[i] = player;
move = i;
return move + 1;
} else if (!moves[i+1]) {
moves[i + 1] = player;
move = i + 1;
return move + 1;
} else if (!moves[i+2]) {
moves[i + 2] = player;
move = i + 2;
return move + 1;
}
}
}
} else {
return take_corners(computer_player);
}
return move + 1;
}
int computer_move() {
int move = rand() % 9;
if ( moves[move] == 0 ) {
moves[move] = 2;
} else {
return computer_move();
}
return move + 1;
}
int take_corners(int player) {
int move = rand() % 4;
int corners[4] = {top_left, top_right, bottom_left, bottom_right};
// prevent the human from playing tricks on us!
if ( ((moves[top_middle] & moves[middle_left]) == human_player) && !moves[top_left] ) {
moves[top_left] = computer_player;
return top_left + 1;
} else if ( ((moves[bottom_middle] & moves[middle_left]) == human_player) && !moves[bottom_left] ) {
moves[bottom_left] = computer_player;
return bottom_left + 1;
} else if ( ((moves[top_middle] & moves[middle_right]) == human_player) && !moves[top_right] ) {
moves[top_right] = computer_player;
return top_right + 1;
} else if ( ((moves[bottom_middle] & moves[middle_right]) == human_player) && !moves[bottom_right] ) {
moves[bottom_right] = computer_player;
return bottom_left + 1;
}
if (!moves[corners[move]]) { // random corner
moves[corners[move]] = player;
move = corners[move] + 1;
} else {
for (int i = 0; i < 4; i++) { // take first available corner
if (!moves[corners[i]]) {
moves[corners[i]] = computer_player;
return corners[i] + 1;
}
}
return computer_move(); // dumb move
}
return move;
}
int computer_move_i() {
int move = 0;
if ( !moves[middle] ) { // O marks the spot
moves[middle] = computer_player;
move = middle;
return move + 1;
}
if ( (move = block(computer_player)) ) {
return move;
} else if ( ( move = block(human_player) ) ) { // O is taken, take corners.
return move;
} else if ( ( move = try_to_win(computer_player) ) ) { // force block.
return move;
} else {
return take_corners(computer_player);
}
return move;
}
int game() {
int choice;
printf("Type a number between 1-9: ");
int c = scanf("%d", &choice);
if ( c == 0 ) {
printf("\nINVALID MOVE! WATCH IT KID!\n");
while( (c = getchar() != '\n') ); // clear STDIN
return game();
} else if (choice < 1 || choice > 9) {
printf("\nINVALID MOVE! WATCH IT KID!\n");
return game();
}
if ( moves[choice - 1] == 0) {
moves[choice - 1] = 1;
} else {
printf("\nWOHO, KID, WHATCHA DOING?!\n");
return game();
}
if (check_win()) {
return check_win();
}
moves_made++;
if ( moves_made < 9) computer_mv = computer_move_i(); // replace with computer_move for dumb play
moves_made++;
return check_win();
}
void game_start() {
srand( time(NULL) );
printf("Hello. Imagine every box is number from 1-9.\nType your number and hit" \
" enter.\n You are playing X.\n\n");
display_tutorial_board();
int winner;
int moves = 0;
while( !( winner = game() ) && moves++ < 4 ) {
display_board();
printf("Computer made a move at: %d\n\n", computer_mv);
}; // plays until someone wins or the board is filled.
display_board();
if ( winner == 1 ) { // Never going to happen.
printf("Congratulations! YOU'RE WINNER!\n");
} else if ( winner == 2 ) {
printf("Congratulations! YOU'RE LOSSER!\n");
} else {
printf("Congratulations! YOU'RE DRAFT!\n");
}
}
int main(int argc, char const *argv[]) {
game_start();
return 0;
}

BIN
lab6/a.out Executable file

Binary file not shown.

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.a.out</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

Binary file not shown.

10
lab6/i Normal file
View file

@ -0,0 +1,10 @@
0
1 2 3
0
4 5 6
2
2 6 0 4
2
5 8 0 6
2
7 9 0 8

21
lab6/l6_1.c Normal file
View file

@ -0,0 +1,21 @@
#include <stdio.h>
char code64[]="\x48\x31\xFF\x48\x31\xC0\xB0\x3C\x0F\x05";
/*
gcc fno stack protector -z execstack o exit exit . c ;
*/
int main (void) {
int (*func)(); /* function pointer for a function */
/* with no arguments but with a return value */
func = (int (*)()) code64; /* cast from char array to function pointer */
(int)(*func)(); /* calls exit(0), Intel 64 bit CPU */
printf("Should exit(0) gracefully before getting here!\n");
return 0;
}

139
lab6/l6_2.c Normal file
View file

@ -0,0 +1,139 @@
#include <stdio.h>
#include <math.h>
#define N 5
struct gshape {
int type ; /* 0/1/2 */
int ID;
union
{
struct
{
float radius;
float x, y;
} circle;
struct
{
float x1,y1, x2,y2, x3,y3;
} triangle;
struct {
float x1,y1;
float x4,y4;
} rectangle ;
} gshape ;
/* upper left */
/* right bottom */
/* function pointers in struct provide contained intelligence */
void (*read_gshape)(struct gshape *gs);
/* like an object having both data and code */
void (*write_gshape)(struct gshape *gs);
void (*compute_area)(struct gshape *gs);
};
void read_circle ( struct gshape *gs) {
printf("radius? ");
scanf("%f", &(gs->gshape.circle.radius));
printf("X center? ");
scanf("%f" , &(gs->gshape.circle.x));
printf("Y center? ");
scanf("%f" , &(gs->gshape.circle.x));
}
void write_circle ( struct gshape *gs) {
printf("Circle Radius: %f X: %f Y: %f\n", gs->gshape.circle.radius,
gs->gshape.circle.x,
gs->gshape.circle.y);
}
void read_triangle ( struct gshape *gs) {
printf("What is the triangle coordonates? (x1,y1) (x2,y2), (x3,y3)\n");
scanf("%f %f %f %f %f %f", &gs->gshape.rectangle.x1,
&gs->gshape.triangle.y1,
&gs->gshape.triangle.x2,
&gs->gshape.triangle.y2,
&gs->gshape.triangle.x3,
&gs->gshape.triangle.y3);
}
void write_triangle ( struct gshape *gs) {
printf("Triangle (%f,%f) (%f,%f), (%f,%f)\n", gs->gshape.triangle.x1,
gs->gshape.triangle.y1,
gs->gshape.triangle.x2,
gs->gshape.triangle.y2,
gs->gshape.triangle.x3,
gs->gshape.triangle.y3);
}
void read_rectangle(struct gshape *gs) {
printf("What's the A B C D of the Rectangle?\n" );
scanf("%f %f %f %f", &gs->gshape.rectangle.x1,
&gs->gshape.rectangle.y1,
&gs->gshape.rectangle.x4,
&gs->gshape.rectangle.y4);
}
void write_rectangle( struct gshape *gs) {
printf("Heres's the A B C D of the Rectangle\n" );
printf("%f %f %f %f\n", gs->gshape.rectangle.x1,
gs->gshape.rectangle.y1,
gs->gshape.rectangle.x4,
gs->gshape.rectangle.y4);
}
void area_rectangle(struct gshape *gs) {
double length = gs->gshape.rectangle.x4 - gs->gshape.rectangle.x1;
double width = gs->gshape.rectangle.y4 - gs->gshape.rectangle.x4;
printf("AREA OF RECTANGLE #%d IS: %g\n", gs->ID, fabs(length*width) );
}
int main() {
int i, type;
struct gshape gs[N];
for (i = 0; i < N; i++) {
do {
printf("ShapeID %d: ", i);
printf("What type of shape is it? (0=circle , 1=triangle , 2=rectangle)\n");
scanf("%d", &type);
gs[i].type = type;
gs[i].ID = i;
switch (gs[i].type) {
case 0:
gs[i].read_gshape = &read_circle;
gs[i].write_gshape = &write_circle;
gs[i].read_gshape(&gs[i]);
gs[i].write_gshape(&gs[i]);
break;
case 1:
gs[i].read_gshape = &read_triangle;
gs[i].write_gshape = &write_triangle;
gs[i].read_gshape(&gs[i]);
gs[i].write_gshape(&gs[i]);
break;
case 2:
gs[i].read_gshape = &read_rectangle;
gs[i].write_gshape = &write_rectangle;
gs[i].compute_area = &area_rectangle;
// calls
gs[i].read_gshape(&gs[i]);
gs[i].write_gshape(&gs[i]);
break;
default:
fprintf(stderr, "Err!\n");
}
} while ((gs[i].type != 0) && (gs[i].type != 1) && (gs[i].type != 2));
}
// COMPUTE AREA
printf("\n\n");
for (int i = 0; i < N; i++) {
if (gs[i].type == 2) {
gs[i].compute_area(&gs[i]);
}
}
}

59
lab6/l6_3.c Normal file
View file

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
/* float is automatically promoted to double via ...
because of the stack memory requirements */
double arithmetic_mean(unsigned int n, ...) {
va_list valist;
float sum = 0.0;
unsigned int i;
/* initialize valist for n arguments */
va_start(valist , n);
/* parse n arguments of type float from valist */
for (i = 0; i < n; i++) {
sum += va_arg(valist , double);
}
/* clean valist */
va_end( valist );
return sum/n;
}
void max_and_min(int * max, int * min, int n, ...) {
va_list valist;
/* initialize valist for n arguments */
va_start(valist , n);
*max = va_arg(valist, int);
*min = va_arg(valist, int);
if ( *max < *min) {
int aux = *max;
*max = *min;
*min = aux;
}
int el;
for (int i = 0; i < n-2; i++) {
el = va_arg(valist , int);
if ( el >= *max ) {
*max = el;
}
else if ( el < *min ) {
*min = el;
}
}
/* clean valist */
va_end( valist );
}
int main() {
int max;
int min;
max_and_min(&max, &min, 10, 1, -1, 1, 3, 4, -6, 6, 8, 9, 10);
printf("Max: %d Min: %d\n", max, min);
return 0;
}

89
lab6/l6_4.c Normal file
View file

@ -0,0 +1,89 @@
#include <stdio.h>
#include <math.h>
double (*primary[7])(double);
char *secondary[] = { "sin(x)", "cos(x)", "ceil(x)", "floor(x)", "fabs(x)", "log(x)", "sqrt(x)" };
void process_choice();
void map_function_pointers(double (**arr)(double), int pos, int n) {
switch (n) {
case 0:
arr[pos] = sin;
break;
case 1:
arr[pos] = cos;
break;
case 2:
arr[pos] = ceil;
break;
case 3:
arr[pos] = floor;
break;
case 4:
arr[pos] = fabs;
break;
case 5:
arr[pos] = log;
break;
case 6:
arr[pos] = sqrt;
break;
default:
fprintf(stderr, "Error\n");
}
}
void clean_stdin() {
int c; while ( (c = getchar()) != '\n' );
}
void display_menu() {
for (int i = 0; i < 7; i++) {
printf("%d. %s\n", i, secondary[i]);
}
}
void init() {
for (int i = 0; i < 7; i++) {
map_function_pointers(primary, i, i);
}
}
int is_valid(int flag) {
if (!flag) {
fprintf(stderr, "Invalid choice!\n");
clean_stdin();
return 0;
}
return 1;
}
void process_choice() {
display_menu();
int choice = -1;
double value = 0;
printf("Choice: ");
int flag = scanf("%d", &choice);
if ( !is_valid(flag) ) { return process_choice(); }
if (choice < 0 || choice > 6) {
fprintf(stderr, "Out of range choice!\n");
return process_choice();
}
printf("Enter a value: ");
int value_flag = scanf("%lf", &value);
if ( !is_valid(value_flag) ) {
fprintf(stderr, "Invalid value!\n");
return process_choice();
}
printf("RESULT: %f\n", primary[choice](value));
}
int main() {
init();
process_choice();
return 0;
}

43
lab7/2.c Normal file
View file

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
void count_letter_frequency_from_file(char * filename) {
FILE * file;
file = fopen(filename, "r"); // filename, mode
// open
if ( !file ) {
fprintf(stderr, "Error opening file.\n");
exit(errno); // exit
}
int frequencies[128] = { 0 }; // Contains character frequencies from ascii table, is initialized to 0;
char * line = malloc( 200 ); // becauase 1 char = 1 byte, 200 chars = 200 bytes
while ( fgets(line, 200, file) ) { // max 200 chars pe line, gets the line
for ( int i = 0; line[i]; i++ ) { // process the line char by char
frequencies[ (int) line[i] ]++; // make the letter and decimal and increments it's possition in the array.
}
}
printf("Characters - Frequencies\n");
for (int i = 0; i < 128; i++) {
if ( frequencies[i] && isprint( (char) i ) ) { // different from zero and printable
printf( "\t %c - %d\n", i, frequencies[i] );
}
}
// close
if ( fclose(file) ) {
fprintf(stderr, "Error closing file\n");
exit(errno);
}
free(line);
}
int main() {
count_letter_frequency_from_file("test");
return 0;
}

51
lab7/3.c Normal file
View file

@ -0,0 +1,51 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
// won't work on windaws.
void unix_to_dos(char * filename) {
// "Use the stdin and stdout file streams." NO.
FILE * file = fopen(filename, "r");
FILE * out;
// open
if ( !file ) {
fprintf(stderr, "Error opening file.\n");
exit(errno); // exit
}
system("touch converted.txt"); // create new file.
if ( !(out = fopen("converted.txt", "w")) ) {
fprintf(stderr, "Error opening output file. Are you using windows?\n");
exit(errno); // exit
}
int c;
while ( (c = fgetc(file)) != EOF ) {
if ( c == '\n') {
fputc('\r', out); // we're placing the character in the out file.
fputc(c, out); // c is newline.
} else {
fputc(c, out); // c is whatever
}
}
// close
if ( fclose(file) || fclose(out) ) { // fclose returns zero on successful close
fprintf(stderr, "Error closing file\n");
exit(errno);
}
}
int main(int argc, char *argv[]) {
if ( argc != 2) {
fprintf(stderr, "Invalid number of arguments.\n");
fprintf(stderr, "Correct ussage: %s filename\n", argv[0]);
return 1;
}
unix_to_dos(argv[1]);
return 0;
}

66
lab7/4.c Normal file
View file

@ -0,0 +1,66 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
// won't work on windaws.
void unix_to_dos(char * filename) {
FILE * file = fopen(filename, "r");
FILE * out;
// open
if ( !file ) {
fprintf(stderr, "Error opening file.\n");
exit(errno); // exit
}
// It would have been easier if we worked on the current file,
// without creating a separated file for the converted file.
char * out_filename = malloc( 5 + strlen(filename) ); // dos_ (4) + filename (strlen) + NULL (1)
memcpy( out_filename, "dos_", 4 );
memcpy( out_filename + 4, filename, strlen(filename) + 1 ); // + 1 to copy the null
// we want to perform the following command:
// touch dos_filename.ext
// 5 + 1 + strlen(out_filename) + 1
char * command = malloc( 7 + strlen(out_filename) );
memcpy(command, "touch ", 6);
memcpy(command + 6, out_filename, strlen(out_filename) + 1); // + 1 to copy the terminating NULL
system(command); // create new file.
if ( !(out = fopen(out_filename, "w")) ) {
fprintf(stderr, "Error opening output file. Are you using windows?\n");
exit(errno); // exit
}
int c;
while ( (c = fgetc(file)) != EOF ) {
if ( c == '\n') {
fputc('\r', out); // we're placing the character in the out file.
fputc(c, out); // c is newline.
} else {
fputc(c, out); // c is whatever
}
}
// close
if ( fclose(file) || fclose(out) ) { // fclose returns zero on successful close
fprintf(stderr, "Error closing file\n");
exit(errno);
}
free(out_filename);
free(command);
}
int main(int argc, char *argv[]) {
if ( argc < 2) {
fprintf(stderr, "Invalid number of arguments.\n");
fprintf(stderr, "Correct ussage: %s filename\n", argv[0]);
return 1;
}
// *.c will expand to all the files in the current dir. eg: 1.c 2.c 3.c et.c
for (int i = 1; i < argc; i++) {
unix_to_dos( argv[i] );
}
return 0;
}

115
lab7/5.c Normal file
View file

@ -0,0 +1,115 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define LINE_CHARS 100
// Parser
int zfind(char *str, int c) {
for (int i = 0; str[i]; i++) {
if (str[i] == c) { return 1; }
}
return 0;
}
// easy solution, no memory leak :)
char * parse_html(char *string){
char * ptr = malloc( LINE_CHARS );
int index = 0;
for (int i = 0; string[i]; i++) {
if (string[i] == '<') { // found opening bracket
if ( zfind(string + i, '>')) { // found closing bracket
while(string[i] != '>') { i++; }; // delete za tag
} else {
ptr[index++] = string[i]; // didn't find closing bracket, skip
}
} else {
ptr[index++] = string[i]; // normal character
}
}
ptr[index] = '\0';
return ptr;
}
// Time shift
void shift_time(int *h, int *m, int *s, int *scr_h, int *scr_m, int *scr_s, int shift) {
long mhs = (*h * 3600) + (*m * 60) + *s + shift;
long scr_mhs = (*scr_h * 3600) + (*scr_m * 60) + *scr_s + shift;
*h = mhs / 3600;
*m = (mhs / 60) % 60;
*s = mhs % 60;
*scr_h = scr_mhs / 3600;
*scr_m = (scr_mhs / 60) % 60;
*scr_s = scr_mhs % 60;
}
// Reader
void process_subtitles(char * filename, int time) {
FILE * file;
FILE * out;
if ( !(file = fopen(filename , "r")) ) {
fprintf(stderr, "Error, can't open file: %s\n", filename);
exit(errno);
}
system("touch working.srt");
if ( !(out = fopen("working.srt" , "w")) ) {
fprintf(stderr, "Error, can't open file: working.srt\n");
exit(errno);
}
char * line = malloc ( LINE_CHARS );
char * temp;
int line_no;
int h, m, s, ms, scr_h, scr_m, scr_s, scr_ms;
while(1) {// infinity. Executes the folowing wile for eternity.
// read numbers
while ( fscanf(file, "%d\r\n%d:%d:%d,%d --> %d:%d:%d,%d\r\n",
&line_no, &h, &m, &s, &ms, &scr_h, &scr_m, &scr_s, &scr_ms) ) {
if (feof(file)) { break; }
if(time) {
shift_time(&h, &m, &s, &scr_h, &scr_m, &scr_s, time);
}
// print formatted time.
fprintf(out, "%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
line_no, h, m, s, ms, scr_h, scr_m, scr_s, scr_ms);
while ( fgets(line, LINE_CHARS - 1, file) ) { // read sub diaglog
temp = parse_html(line);
fprintf(out, "%s", temp);
free(temp); // prevent memory leak by freeing the pointer.
if (!strcmp(line, "\r\n")) { break; }
}
}
if (feof(file)) { break; }
}
if ( fclose(file) || fclose(out) ) {
printf("Error, can't close file\n");
exit(errno);
}
free(line);
}
int main(int argc, char *argv[]) {
if ( argc < 2) {
fprintf(stderr, "Invalid number of arguments.\n");
fprintf(stderr, "Correct ussage: %s filename [secconds to shift]\n", argv[0]);
return 1;
}
int time_to_shift = 0;
if ( argc > 2) {
time_to_shift = atoi(argv[2]);
}
process_subtitles(argv[1], time_to_shift);
return 0;
}

BIN
lab7/a.out Executable file

Binary file not shown.

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.a.out</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

Binary file not shown.

284
lab7/converted.txt Normal file
View file

@ -0,0 +1,284 @@
/* tic tac toe */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int take_corners(int);
// explicit instantiation
int moves[9] = {0};
int moves_made = 0;
int computer_mv = 0;
enum { human_player = 1, computer_player = 2 };
enum { top_left, top_middle, top_right, middle_left, middle, middle_right,
bottom_left, bottom_middle, bottom_right };
void display_tutorial_board() {
int move;
for (int i = 0; i < 3; i++) {
printf("|");
for (int j = 0; j < 3; j++) {
move = j + i * 3 + 1;
printf("_%d_|", move);
}
printf("\n\n");
}
}
void display_board() {
char mark;
int move;
printf("\n");
for (int i = 0; i < 3; i++) {
printf("|");
for (int j = 0; j < 3; j++) {
move = moves[j + i * 3];
if ( !move ) mark = '_';
else if (move == human_player) mark = 'X';
else mark = 'O';
printf("_%c_|", mark);
}
printf("\n\n");
}
}
int _check_win(int x) {
int player_wins = x;
int aux = 0;
for (int i = 0; i < 9; i += 3) {
aux |= moves[i] & moves[i+1] & moves[i+2]; // linii
}
for (int i = 0; i < 3; i++) {
aux |= moves[i] & moves[i+3] & moves[i+6]; // coloane
}
aux |= moves[top_left] & moves[middle] & moves[bottom_right]; // principal
aux |= moves[top_right] & moves[middle] & moves[bottom_left]; // secondary
player_wins &= aux;
return player_wins;
}
int check_win() {
int player_wins = human_player;
player_wins &= _check_win(player_wins);
int computer_wins = computer_player;
computer_wins &= _check_win(computer_wins);
return player_wins | computer_wins;
}
int block(int player) {
int move = -1;
for (int i = 0; i < 9; i += 3) { // blocheaza liniile.
if ( ((moves[i] & moves[i + 1]) == player) && !moves[i + 2] ) {
moves[i + 2] = computer_player;
move = i + 2;
return move + 1;
} else if ( ((moves[i] & moves[i + 2]) == player) && !moves[i + 1] ) {
moves[i + 1] = computer_player;
move = i + 1;
return move + 1;
} else if ( ((moves[i + 1] & moves[i + 2]) == player) && !moves[i] ) {
moves[i] = computer_player;
move = i;
return move + 1;
}
}
for (int i = 0; i < 3; i++) { //blocheaza coloanele
if ( ((moves[i] & moves[i + 3]) == player) && !moves[i + 6] ) {
moves[i+6] = computer_player;
move = i+6;
return move +1;
} else if ( ((moves[i] & moves[i + 6]) == player) && !moves[i + 3] ) {
moves[i+3] = computer_player;
move = i+3;
return move +1;
} else if ( ((moves[i + 3] & moves[i + 6]) == player) && !moves[i] ) {
moves[i] = computer_player;
move = i;
return move +1;
}
}
// principal
if ( ((moves[top_left] & moves[middle]) == player) && !moves[bottom_right] ) {
moves[bottom_right] = computer_player;
return bottom_right + 1;
} else if ( ((moves[top_left] & moves[bottom_right]) == player) && !moves[middle] ) {
moves[middle] = computer_player;
return middle + 1;
} if ( ((moves[bottom_right] & moves[middle]) == player) && !moves[top_left] ) {
moves[top_left] = computer_player;
return top_left + 1;
}
// secondary
if ( ((moves[top_right] & moves[middle]) == player) && !moves[bottom_left] ) {
moves[bottom_left] = computer_player;
return bottom_left + 1;
} else if ( ((moves[top_right] & moves[bottom_left]) == player) && !moves[middle] ) {
moves[middle] = computer_player;
return middle + 1;
} if ( ((moves[bottom_left] & moves[middle]) == player) && !moves[top_right] ) {
moves[top_right] = computer_player;
return top_right + 1;
}
return move + 1;
}
int try_to_win(int player) {
int move = -1;
if (moves[middle] == computer_player) {
for (int i = 0; i < 9; i += 3) { // tries to make a line in order to not pun random things.
if ( (moves[i] | moves[i+1] | moves[i+2]) == player ) {
if (!moves[i]) {
moves[i] = player;
move = i;
return move + 1;
} else if (!moves[i+1]) {
moves[i + 1] = player;
move = i + 1;
return move + 1;
} else if (!moves[i+2]) {
moves[i + 2] = player;
move = i + 2;
return move + 1;
}
}
}
} else {
return take_corners(computer_player);
}
return move + 1;
}
int computer_move() {
int move = rand() % 9;
if ( moves[move] == 0 ) {
moves[move] = 2;
} else {
return computer_move();
}
return move + 1;
}
int take_corners(int player) {
int move = rand() % 4;
int corners[4] = {top_left, top_right, bottom_left, bottom_right};
// prevent the human from playing tricks on us!
if ( ((moves[top_middle] & moves[middle_left]) == human_player) && !moves[top_left] ) {
moves[top_left] = computer_player;
return top_left + 1;
} else if ( ((moves[bottom_middle] & moves[middle_left]) == human_player) && !moves[bottom_left] ) {
moves[bottom_left] = computer_player;
return bottom_left + 1;
} else if ( ((moves[top_middle] & moves[middle_right]) == human_player) && !moves[top_right] ) {
moves[top_right] = computer_player;
return top_right + 1;
} else if ( ((moves[bottom_middle] & moves[middle_right]) == human_player) && !moves[bottom_right] ) {
moves[bottom_right] = computer_player;
return bottom_left + 1;
}
if (!moves[corners[move]]) { // random corner
moves[corners[move]] = player;
move = corners[move] + 1;
} else {
for (int i = 0; i < 4; i++) { // take first available corner
if (!moves[corners[i]]) {
moves[corners[i]] = computer_player;
return corners[i] + 1;
}
}
return computer_move(); // dumb move
}
return move;
}
int computer_move_i() {
int move = 0;
if ( !moves[middle] ) { // O marks the spot
moves[middle] = computer_player;
move = middle;
return move + 1;
}
if ( (move = block(computer_player)) ) {
return move;
} else if ( ( move = block(human_player) ) ) { // O is taken, take corners.
return move;
} else if ( ( move = try_to_win(computer_player) ) ) { // force block.
return move;
} else {
return take_corners(computer_player);
}
return move;
}
int game() {
int choice;
printf("Type a number between 1-9: ");
int c = scanf("%d", &choice);
if ( c == 0 ) {
printf("\nINVALID MOVE! WATCH IT KID!\n");
while( (c = getchar() != '\n') ); // clear STDIN
return game();
} else if (choice < 1 || choice > 9) {
printf("\nINVALID MOVE! WATCH IT KID!\n");
return game();
}
if ( moves[choice - 1] == 0) {
moves[choice - 1] = 1;
} else {
printf("\nWOHO, KID, WHATCHA DOING?!\n");
return game();
}
if (check_win()) {
return check_win();
}
moves_made++;
if ( moves_made < 9) computer_mv = computer_move_i(); // replace with computer_move for dumb play
moves_made++;
return check_win();
}
void game_start() {
srand( time(NULL) );
printf("Hello. Imagine every box is number from 1-9.\nType your number and hit" \
" enter.\n You are playing X.\n\n");
display_tutorial_board();
int winner;
int moves = 0;
while( !( winner = game() ) && moves++ < 4 ) {
display_board();
printf("Computer made a move at: %d\n\n", computer_mv);
}; // plays until someone wins or the board is filled.
display_board();
if ( winner == 1 ) { // Never going to happen.
printf("Congratulations! YOU'RE WINNER!\n");
} else if ( winner == 2 ) {
printf("Congratulations! YOU'RE LOSSER!\n");
} else {
printf("Congratulations! YOU'RE DRAFT!\n");
}
}
int main(int argc, char const *argv[]) {
game_start();
return 0;
}

43
lab7/dos_2.c Normal file
View file

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
void count_letter_frequency_from_file(char * filename) {
FILE * file;
file = fopen(filename, "r"); // filename, mode
// open
if ( !file ) {
fprintf(stderr, "Error opening file.\n");
exit(errno); // exit
}
int frequencies[128] = { 0 }; // Contains character frequencies from ascii table, is initialized to 0;
char * line = malloc( 200 ); // becauase 1 char = 1 byte, 200 chars = 200 bytes
while ( fgets(line, 200, file) ) { // max 200 chars pe line, gets the line
for ( int i = 0; line[i]; i++ ) { // process the line char by char
frequencies[ (int) line[i] ]++; // make the letter and decimal and increments it's possition in the array.
}
}
printf("Characters - Frequencies\n");
for (int i = 0; i < 128; i++) {
if ( frequencies[i] && isprint( (char) i ) ) { // different from zero and printable
printf( "\t %c - %d\n", i, frequencies[i] );
}
}
// close
if ( fclose(file) ) {
fprintf(stderr, "Error closing file\n");
exit(errno);
}
free(line);
}
int main() {
count_letter_frequency_from_file("test");
return 0;
}

51
lab7/dos_3.c Normal file
View file

@ -0,0 +1,51 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
// won't work on windaws.
void unix_to_dos(char * filename) {
// "Use the stdin and stdout file streams." NO.
FILE * file = fopen(filename, "r");
FILE * out;
// open
if ( !file ) {
fprintf(stderr, "Error opening file.\n");
exit(errno); // exit
}
system("touch converted.txt"); // create new file.
if ( !(out = fopen("converted.txt", "w")) ) {
fprintf(stderr, "Error opening output file. Are you using windows?\n");
exit(errno); // exit
}
int c;
while ( (c = fgetc(file)) != EOF ) {
if ( c == '\n') {
fputc('\r', out); // we're placing the character in the out file.
fputc(c, out); // c is newline.
} else {
fputc(c, out); // c is whatever
}
}
// close
if ( fclose(file) || fclose(out) ) { // fclose returns zero on successful close
fprintf(stderr, "Error closing file\n");
exit(errno);
}
}
int main(int argc, char *argv[]) {
if ( argc != 2) {
fprintf(stderr, "Invalid number of arguments.\n");
fprintf(stderr, "Correct ussage: %s filename\n", argv[0]);
return 1;
}
unix_to_dos(argv[1]);
return 0;
}

66
lab7/dos_4.c Normal file
View file

@ -0,0 +1,66 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
// won't work on windaws.
void unix_to_dos(char * filename) {
FILE * file = fopen(filename, "r");
FILE * out;
// open
if ( !file ) {
fprintf(stderr, "Error opening file.\n");
exit(errno); // exit
}
// It would have been easier if we worked on the current file,
// without creating a separated file for the converted file.
char * out_filename = malloc( 5 + strlen(filename) ); // dos_ (4) + filename (strlen) + NULL (1)
memcpy( out_filename, "dos_", 4 );
memcpy( out_filename + 4, filename, strlen(filename) + 1 ); // + 1 to copy the null
// we want to perform the following command:
// touch dos_filename.ext
// 5 + 1 + strlen(out_filename) + 1
char * command = malloc( 7 + strlen(out_filename) );
memcpy(command, "touch ", 6);
memcpy(command + 6, out_filename, strlen(out_filename) + 1); // + 1 to copy the terminating NULL
system(command); // create new file.
if ( !(out = fopen(out_filename, "w")) ) {
fprintf(stderr, "Error opening output file. Are you using windows?\n");
exit(errno); // exit
}
int c;
while ( (c = fgetc(file)) != EOF ) {
if ( c == '\n') {
fputc('\r', out); // we're placing the character in the out file.
fputc(c, out); // c is newline.
} else {
fputc(c, out); // c is whatever
}
}
// close
if ( fclose(file) || fclose(out) ) { // fclose returns zero on successful close
fprintf(stderr, "Error closing file\n");
exit(errno);
}
free(out_filename);
free(command);
}
int main(int argc, char *argv[]) {
if ( argc < 2) {
fprintf(stderr, "Invalid number of arguments.\n");
fprintf(stderr, "Correct ussage: %s filename\n", argv[0]);
return 1;
}
// *.c will expand to all the files in the current dir. eg: 1.c 2.c 3.c et.c
for (int i = 1; i < argc; i++) {
unix_to_dos( argv[i] );
}
return 0;
}

115
lab7/dos_5.c Normal file
View file

@ -0,0 +1,115 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define LINE_CHARS 100
// Parser
int zfind(char *str, int c) {
for (int i = 0; str[i]; i++) {
if (str[i] == c) { return 1; }
}
return 0;
}
// easy solution, no memory leak :)
char * parse_html(char *string){
char * ptr = malloc( LINE_CHARS );
int index = 0;
for (int i = 0; string[i]; i++) {
if (string[i] == '<') { // found opening bracket
if ( zfind(string + i, '>')) { // found closing bracket
while(string[i] != '>') { i++; }; // delete za tag
} else {
ptr[index++] = string[i]; // didn't find closing bracket, skip
}
} else {
ptr[index++] = string[i]; // normal character
}
}
ptr[index] = '\0';
return ptr;
}
// Time shift
void shift_time(int *h, int *m, int *s, int *scr_h, int *scr_m, int *scr_s, int shift) {
long mhs = (*h * 3600) + (*m * 60) + *s + shift;
long scr_mhs = (*scr_h * 3600) + (*scr_m * 60) + *scr_s + shift;
*h = mhs / 3600;
*m = (mhs / 60) % 60;
*s = mhs % 60;
*scr_h = scr_mhs / 3600;
*scr_m = (scr_mhs / 60) % 60;
*scr_s = scr_mhs % 60;
}
// Reader
void process_subtitles(char * filename, int time) {
FILE * file;
FILE * out;
if ( !(file = fopen(filename , "r")) ) {
fprintf(stderr, "Error, can't open file: %s\n", filename);
exit(errno);
}
system("touch working.srt");
if ( !(out = fopen("working.srt" , "w")) ) {
fprintf(stderr, "Error, can't open file: working.srt\n");
exit(errno);
}
char * line = malloc ( LINE_CHARS );
char * temp;
int line_no;
int h, m, s, ms, scr_h, scr_m, scr_s, scr_ms;
while(1) {// infinity. Executes the folowing wile for eternity.
// read numbers
while ( fscanf(file, "%d\r\n%d:%d:%d,%d --> %d:%d:%d,%d\r\n",
&line_no, &h, &m, &s, &ms, &scr_h, &scr_m, &scr_s, &scr_ms) ) {
if (feof(file)) { break; }
if(time) {
shift_time(&h, &m, &s, &scr_h, &scr_m, &scr_s, time);
}
// print formatted time.
fprintf(out, "%d\r\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\r\n",
line_no, h, m, s, ms, scr_h, scr_m, scr_s, scr_ms);
while ( fgets(line, LINE_CHARS - 1, file) ) { // read sub diaglog
temp = parse_html(line);
fprintf(out, "%s", temp);
free(temp); // prevent memory leak by freeing the pointer.
if (!strcmp(line, "\r\n")) { break; }
}
}
if (feof(file)) { break; }
}
if ( fclose(file) || fclose(out) ) {
printf("Error, can't close file\n");
exit(errno);
}
free(line);
}
int main(int argc, char *argv[]) {
if ( argc < 2) {
fprintf(stderr, "Invalid number of arguments.\n");
fprintf(stderr, "Correct ussage: %s filename [secconds to shift]\n", argv[0]);
return 1;
}
int time_to_shift = 0;
if ( argc > 2) {
time_to_shift = atoi(argv[2]);
}
process_subtitles(argv[1], time_to_shift);
return 0;
}

2421
lab7/g.srt Executable file

File diff suppressed because it is too large Load diff

42
lab7/sub.srt Executable file
View file

@ -0,0 +1,42 @@
113
00:13:30,143 --> 00:13:32,670
He's a <strong>comrade</strong>.
114
00:13:32,770 --> 00:13:35,632
He is a <strong>soldier<strong>.
115
00:13:35,732 --> 00:13:40,912
He's big in business.
495
00:43:57,385 --> 00:44:03,316
I'm telling you, gents.
Informer is <b><i>this<i/></b> security guard.
496
00:44:04,600 --> 00:44:06,294
Wait >a< second,
497
00:44:06,394 --> 00:44:08,537
I hear what you <i>saying</i>.
498
00:44:08,771 --> 00:44:10,798
But this whole thing is <underline>screwed</underline>.
499
00:44:10,898 --> 00:44:15,636
The cops obviously know <u>everything</u>.
The job is a flop<.
500
00:44:15,736 --> 00:44:20,892
I'm saying we cancel the job,
finish the cop and piss off.
501
00:44:20,992 --> 00:44:23,978
Let him be killed!

284
lab7/test Normal file
View file

@ -0,0 +1,284 @@
/* tic tac toe */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int take_corners(int);
// explicit instantiation
int moves[9] = {0};
int moves_made = 0;
int computer_mv = 0;
enum { human_player = 1, computer_player = 2 };
enum { top_left, top_middle, top_right, middle_left, middle, middle_right,
bottom_left, bottom_middle, bottom_right };
void display_tutorial_board() {
int move;
for (int i = 0; i < 3; i++) {
printf("|");
for (int j = 0; j < 3; j++) {
move = j + i * 3 + 1;
printf("_%d_|", move);
}
printf("\n\n");
}
}
void display_board() {
char mark;
int move;
printf("\n");
for (int i = 0; i < 3; i++) {
printf("|");
for (int j = 0; j < 3; j++) {
move = moves[j + i * 3];
if ( !move ) mark = '_';
else if (move == human_player) mark = 'X';
else mark = 'O';
printf("_%c_|", mark);
}
printf("\n\n");
}
}
int _check_win(int x) {
int player_wins = x;
int aux = 0;
for (int i = 0; i < 9; i += 3) {
aux |= moves[i] & moves[i+1] & moves[i+2]; // linii
}
for (int i = 0; i < 3; i++) {
aux |= moves[i] & moves[i+3] & moves[i+6]; // coloane
}
aux |= moves[top_left] & moves[middle] & moves[bottom_right]; // principal
aux |= moves[top_right] & moves[middle] & moves[bottom_left]; // secondary
player_wins &= aux;
return player_wins;
}
int check_win() {
int player_wins = human_player;
player_wins &= _check_win(player_wins);
int computer_wins = computer_player;
computer_wins &= _check_win(computer_wins);
return player_wins | computer_wins;
}
int block(int player) {
int move = -1;
for (int i = 0; i < 9; i += 3) { // blocheaza liniile.
if ( ((moves[i] & moves[i + 1]) == player) && !moves[i + 2] ) {
moves[i + 2] = computer_player;
move = i + 2;
return move + 1;
} else if ( ((moves[i] & moves[i + 2]) == player) && !moves[i + 1] ) {
moves[i + 1] = computer_player;
move = i + 1;
return move + 1;
} else if ( ((moves[i + 1] & moves[i + 2]) == player) && !moves[i] ) {
moves[i] = computer_player;
move = i;
return move + 1;
}
}
for (int i = 0; i < 3; i++) { //blocheaza coloanele
if ( ((moves[i] & moves[i + 3]) == player) && !moves[i + 6] ) {
moves[i+6] = computer_player;
move = i+6;
return move +1;
} else if ( ((moves[i] & moves[i + 6]) == player) && !moves[i + 3] ) {
moves[i+3] = computer_player;
move = i+3;
return move +1;
} else if ( ((moves[i + 3] & moves[i + 6]) == player) && !moves[i] ) {
moves[i] = computer_player;
move = i;
return move +1;
}
}
// principal
if ( ((moves[top_left] & moves[middle]) == player) && !moves[bottom_right] ) {
moves[bottom_right] = computer_player;
return bottom_right + 1;
} else if ( ((moves[top_left] & moves[bottom_right]) == player) && !moves[middle] ) {
moves[middle] = computer_player;
return middle + 1;
} if ( ((moves[bottom_right] & moves[middle]) == player) && !moves[top_left] ) {
moves[top_left] = computer_player;
return top_left + 1;
}
// secondary
if ( ((moves[top_right] & moves[middle]) == player) && !moves[bottom_left] ) {
moves[bottom_left] = computer_player;
return bottom_left + 1;
} else if ( ((moves[top_right] & moves[bottom_left]) == player) && !moves[middle] ) {
moves[middle] = computer_player;
return middle + 1;
} if ( ((moves[bottom_left] & moves[middle]) == player) && !moves[top_right] ) {
moves[top_right] = computer_player;
return top_right + 1;
}
return move + 1;
}
int try_to_win(int player) {
int move = -1;
if (moves[middle] == computer_player) {
for (int i = 0; i < 9; i += 3) { // tries to make a line in order to not pun random things.
if ( (moves[i] | moves[i+1] | moves[i+2]) == player ) {
if (!moves[i]) {
moves[i] = player;
move = i;
return move + 1;
} else if (!moves[i+1]) {
moves[i + 1] = player;
move = i + 1;
return move + 1;
} else if (!moves[i+2]) {
moves[i + 2] = player;
move = i + 2;
return move + 1;
}
}
}
} else {
return take_corners(computer_player);
}
return move + 1;
}
int computer_move() {
int move = rand() % 9;
if ( moves[move] == 0 ) {
moves[move] = 2;
} else {
return computer_move();
}
return move + 1;
}
int take_corners(int player) {
int move = rand() % 4;
int corners[4] = {top_left, top_right, bottom_left, bottom_right};
// prevent the human from playing tricks on us!
if ( ((moves[top_middle] & moves[middle_left]) == human_player) && !moves[top_left] ) {
moves[top_left] = computer_player;
return top_left + 1;
} else if ( ((moves[bottom_middle] & moves[middle_left]) == human_player) && !moves[bottom_left] ) {
moves[bottom_left] = computer_player;
return bottom_left + 1;
} else if ( ((moves[top_middle] & moves[middle_right]) == human_player) && !moves[top_right] ) {
moves[top_right] = computer_player;
return top_right + 1;
} else if ( ((moves[bottom_middle] & moves[middle_right]) == human_player) && !moves[bottom_right] ) {
moves[bottom_right] = computer_player;
return bottom_left + 1;
}
if (!moves[corners[move]]) { // random corner
moves[corners[move]] = player;
move = corners[move] + 1;
} else {
for (int i = 0; i < 4; i++) { // take first available corner
if (!moves[corners[i]]) {
moves[corners[i]] = computer_player;
return corners[i] + 1;
}
}
return computer_move(); // dumb move
}
return move;
}
int computer_move_i() {
int move = 0;
if ( !moves[middle] ) { // O marks the spot
moves[middle] = computer_player;
move = middle;
return move + 1;
}
if ( (move = block(computer_player)) ) {
return move;
} else if ( ( move = block(human_player) ) ) { // O is taken, take corners.
return move;
} else if ( ( move = try_to_win(computer_player) ) ) { // force block.
return move;
} else {
return take_corners(computer_player);
}
return move;
}
int game() {
int choice;
printf("Type a number between 1-9: ");
int c = scanf("%d", &choice);
if ( c == 0 ) {
printf("\nINVALID MOVE! WATCH IT KID!\n");
while( (c = getchar() != '\n') ); // clear STDIN
return game();
} else if (choice < 1 || choice > 9) {
printf("\nINVALID MOVE! WATCH IT KID!\n");
return game();
}
if ( moves[choice - 1] == 0) {
moves[choice - 1] = 1;
} else {
printf("\nWOHO, KID, WHATCHA DOING?!\n");
return game();
}
if (check_win()) {
return check_win();
}
moves_made++;
if ( moves_made < 9) computer_mv = computer_move_i(); // replace with computer_move for dumb play
moves_made++;
return check_win();
}
void game_start() {
srand( time(NULL) );
printf("Hello. Imagine every box is number from 1-9.\nType your number and hit" \
" enter.\n You are playing X.\n\n");
display_tutorial_board();
int winner;
int moves = 0;
while( !( winner = game() ) && moves++ < 4 ) {
display_board();
printf("Computer made a move at: %d\n\n", computer_mv);
}; // plays until someone wins or the board is filled.
display_board();
if ( winner == 1 ) { // Never going to happen.
printf("Congratulations! YOU'RE WINNER!\n");
} else if ( winner == 2 ) {
printf("Congratulations! YOU'RE LOSSER!\n");
} else {
printf("Congratulations! YOU'RE DRAFT!\n");
}
}
int main(int argc, char const *argv[]) {
game_start();
return 0;
}

42
lab7/working.srt Normal file
View file

@ -0,0 +1,42 @@
113
00:13:36,143 --> 00:13:38,670
He's a comrade.
114
00:13:38,770 --> 00:13:41,632
He is a soldier.
115
00:13:41,732 --> 00:13:46,912
He's big in business.
495
00:44:03,385 --> 00:44:09,316
I'm telling you, gents.
Informer is this security guard.
496
00:44:10,600 --> 00:44:12,294
Wait >a< second,
497
00:44:12,394 --> 00:44:14,537
I hear what you saying.
498
00:44:14,771 --> 00:44:16,798
But this whole thing is screwed.
499
00:44:16,898 --> 00:44:21,636
The cops obviously know everything.
The job is a flop<.
500
00:44:21,736 --> 00:44:26,892
I'm saying we cancel the job,
finish the cop and piss off.
501
00:44:26,992 --> 00:44:29,978
Let him be killed!