Skip to content

day 1 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions day0/Kalpesh/1/ppm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <stdio.h>
#include <stdlib.h>

#define P6

typedef struct{
unsigned char r,g,b;
}Color;

typedef struct{
int x,y;
int r;
}Circle;

typedef struct{
int x,y;
int w,h;
}Rect;

int readIntFromBuffer(char * buffer, int * pos){
int a = 0;
int index = *pos;
while (buffer[index] == '\n' || buffer[index] == ' ' || buffer[index] == '\r'){
(index)++;
}
while (buffer[index] != '\n' && buffer[index] != ' ' && buffer[index] != 0 && buffer[index] != '\r'){
if (buffer[index] >= '0' && buffer[index] <= '9'){
a = 10 * a + (buffer[index] - '0');
}

(index)++;
}
*pos = index;
return(a);
}

char * loadFileToBuffer(const char * filePath, FILE * f){
fopen_s(&f, filePath, "rb");
if (!f){
printf("No file");
return(0);
}
fseek(f, 0, SEEK_END);
long long int size = ftell(f);
fseek(f, 0, SEEK_SET);
char * buffer = (char *)malloc((size + 1) * sizeof(char));

fread(buffer, 1, size, f);
buffer[size] = 0;

fclose(f);
return(buffer);
}


void main(){
FILE* f;

char * buffer = loadFileToBuffer("render.txt", f);

int i = 0;
int columns = readIntFromBuffer(buffer,&i);
int rows = readIntFromBuffer(buffer,&i);


Color * pixelData = (Color *) malloc(rows * columns * sizeof(Color));
Color current;
while (*(buffer+i) != 0){
if (*(buffer+i) == 'r'){
Rect r;
i++;
r.x = readIntFromBuffer(buffer,&i);
r.y = readIntFromBuffer(buffer,&i);
r.w = readIntFromBuffer(buffer,&i);
r.h = readIntFromBuffer(buffer,&i);
current.r = readIntFromBuffer(buffer,&i);
current.g = readIntFromBuffer(buffer,&i);
current.b = readIntFromBuffer(buffer,&i);
for (int i=r.x - 0.5 * r.w; i<= r.x + 0.5 * r.w; i++){
for (int j = r.y - 0.5 * r.h; j<= r.y + 0.5 * r.h; j++){
if (i>=0 && j>=0 && i<columns && j<rows)
pixelData[i + j * columns] = current;
}
}
}
else if (*(buffer+i) == 'c'){
Circle c;
i++;
c.x = readIntFromBuffer(buffer,&i);
c.y = readIntFromBuffer(buffer,&i);
c.r = readIntFromBuffer(buffer,&i);
current.r = readIntFromBuffer(buffer,&i);
current.g = readIntFromBuffer(buffer,&i);
current.b = readIntFromBuffer(buffer,&i);
for (int i=c.x - c.r; i<= c.x + c.r; i++){
for (int j = c.y - c.r; j<= c.y + c.r; j++){
int rel_x = i - c.x, rel_y = j-c.y;
if ((rel_x* rel_x +rel_y*rel_y -c.r*c.r) <= 0 && i>=0 && j>=0 && i<columns && j<rows){
pixelData[i + j * columns] = current;
}
}
}
}
else{
}
i++;
}

fopen_s(&f, "out.ppm", "wb");

#ifdef P3
fprintf(f, "P3 %d %d 255\n",columns, rows);
for (int i=0; i<rows*columns; i++){
fprintf(f, "%d %d %d\n", pixelData[i].r, pixelData[i].g, pixelData[i].b);
}
#endif

#ifdef P6
fprintf(f, "P6 %d %d 255\n",columns, rows);
for (int i=0; i<rows*columns; i++){
fwrite(&pixelData[i].r, sizeof(unsigned char), 1,f);
fwrite(&pixelData[i].g, sizeof(unsigned char), 1,f);
fwrite(&pixelData[i].b, sizeof(unsigned char), 1,f);
}
#endif
fclose(f);



}
158 changes: 158 additions & 0 deletions day0/Kalpesh/2/postfix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct{
double * array;
int size;
int top;
}Stack;

void createStack(Stack * stack, int size){
stack->size = size;
stack->array = (double *)malloc(size*sizeof(double));
stack->top = -1;
}


void StackDump(Stack * stack){
for (int i=0; i<=stack->top; i++){
printf("%.2f\t", stack->array[i]);
}
printf("\n");
}

void push(Stack * stack, double data){
stack->top++;
if (stack->top == stack->size){

int newSize = stack->size * 1.7 + 1;
// printf("[Realloc increase] Old size: %d New size: %d\n", stack->size, newSize);
double *newArray = (double *)malloc(newSize * sizeof(double));
for (int i=0; i< stack->top; i++){
newArray[i] = stack->array[i];
}
free(stack->array);
stack->size = newSize;
stack->array = newArray;
}
stack->array[stack->top] = data;
}

double pop(Stack * stack){
if (stack->top == -1){
return(0.0f);
}
double top = stack->array[stack->top];
stack->top--;
if ((stack->top) < (stack->size)*0.6){
int newSize = stack->size * 0.8 + 1;
// printf("[Realloc decrease] Old size: %d New size: %d\n", stack->size, newSize);
double *newArray = (double *)malloc(newSize * sizeof(double));
for (int i=0; i<= stack->top; i++){
newArray[i] = stack->array[i];
}
free(stack->array);
stack->size = newSize;
stack->array = newArray;
}
return(top);

}

char * loadFileToBuffer(const char * filePath, FILE * f){
fopen_s(&f, filePath, "rb");
if (!f){
printf("No file");
return(0);
}
fseek(f, 0, SEEK_END);
long long int size = ftell(f);
fseek(f, 0, SEEK_SET);
char * buffer = (char *)malloc((size + 1) * sizeof(char));

fread(buffer, 1, size, f);
buffer[size] = 0;

fclose(f);
return(buffer);
}

int isNumber(char c){
return((c >= '0')&& (c<= '9'));
}

int isOperator(char c){
return((c == '+')||(c == '-')||(c == '*')||(c == '/'));
}

int readIntFromBuffer(char * buffer, int * pos){
int a = 0;
int index = *pos;
while (buffer[index] == '\n' || buffer[index] == ' ' || buffer[index] == '\r'){
(index)++;
}
while (buffer[index] != '\n' && buffer[index] != ' ' && buffer[index] != 0 && buffer[index] != '\r' && !isOperator(buffer[index])){
if (buffer[index] >= '0' && buffer[index] <= '9'){
a = 10 * a + (buffer[index] - '0');
}

(index)++;
}
*pos = index;
return(a);
}


int main (){
Stack stack;
createStack(&stack, 8);

FILE * f = NULL;
char * buffer = loadFileToBuffer("expressions.txt", f);

int i = 0;
fopen_s(&f, "results.txt", "w");
while (buffer[i]){
if (isNumber(buffer[i])){
push(&stack, (double)readIntFromBuffer(buffer, &i));
}
if (isOperator(buffer[i])){
if (stack.top < 1){
printf("Invalid expression\n");
return(-1);
}
double a = pop(&stack);
double b = pop(&stack);
// printf("[EVALUATE]: ");

switch (buffer[i]){
case '+':
push(&stack, (double)b+a);
break;
case '-':
push(&stack, (double)b-a);
break;
case '*':
push(&stack, (double)b*a);
break;
case '/':
push(&stack, (double)b/a);
break;
default:
break;
}
i++;
}
if (buffer[i] == '\n' || buffer[i] == 0){
double result = pop(&stack);
if (stack.top == -1){
fprintf(f, "%0.6f\n", result);
}
}
i++;
}
fclose(f);
free(buffer);

return(0);
}
Loading