Skip to content

Commit

Permalink
update all
Browse files Browse the repository at this point in the history
  • Loading branch information
gioretikto committed Apr 26, 2020
1 parent 6b1d6a6 commit 78d4bdf
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
117 changes: 117 additions & 0 deletions functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include "jac.h"

void multiply (struct n *head, struct n *end)
{
double result;

if (head->next == end || head->next == NULL)
return;

else {

if ((head->next)->op == '*') {
result = (head->next)->value * head->value;
head->value = result;
del_next(head);
multiply(head, end);
}

else
multiply(head->next, end);
}
}

void divide (struct n *head, struct n *end) {

double result;

if (head->next == end || head->next == NULL)
return;

else {

if ((head->next)->op == '/') {
result = (head->next)->value / head->value;
head->value = result;
del_next(head);
divide(head, end);
}

else
divide(head->next, end);
}
}

void add (struct n *head, struct n *end) {

double result;

if (head->next == end || head->next == NULL)
return;

else {

if ((head->next)->op == '+' || (head->next)->op == '-') {

if ((head->next)->op == '+')
result = (head->next)->value +1 * head->value;

else {
result = (head->next)->value -1 * head->value;
}

head->value = result;
del_next(head);
add(head, end);
}

else
add(head->next, end);
}
}

void del_next (struct n *before_del_next) {

struct n *temp;
temp = before_del_next->next;
before_del_next->next = temp->next;
free(temp);

}

unsigned long factorial(unsigned long f) {

if ( f == 0 )
return 1;
return(f * factorial(f - 1));
}

int bin_dec(long long n) {

int dec = 0, i = 0, rem;

while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}

long long dec_bin(int n) {

long long bin = 0;
int rem, i;

rem = i = 1;

while (n != 0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}

return bin;
}
24 changes: 24 additions & 0 deletions jac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdlib.h>
#include <math.h>

#define MAX 1000

struct n{
double value;
char op;
struct n *next;
char unary;
};

void add_item(struct n **ptr, double data, char s);
void print_num(double x);
void remove_spaces(char *str);
void multiply (struct n *head, struct n *end);
void divide (struct n *head, struct n *end);
void add (struct n *head, struct n *end);
void calculate (struct n *head, struct n *end, _Bool *operation);
void del_next(struct n *head);
void unary (struct n *head, struct n *end);
unsigned long factorial(unsigned long f);
int bin_dec(long long n);
long long dec_bin(int n);

0 comments on commit 78d4bdf

Please sign in to comment.