NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "assignment4.h"

bool test_q1a() {

stack_t* s = stack_create();
int i=0;

for (i=0;i<100;i++) stack_push(s, i);
int size1 = stack_size(s);

for (i=0;i<10;i++) stack_pop(s);
int size2 = stack_size(s);

for (i=0;i<60;i++) stack_push(s, 'a');
int size3 = stack_size(s);

stack_free(s);

if (size1==100 && size2==90 && size3==150) {
printf("Q1a okn");
return true;
}
else {
printf("Q1a ERRORn");
return false;
}
}


bool test_q1b() {

stack_t* s1 = stack_create();
stack_t* s2 = stack_create();
int i=0;

for (i=0;i<20;i++) stack_push(s1, 'a'+i);
for (i=0;i<20;i++) stack_push(s2, 'a'+i);
bool is_eq1 = stack_equal(s1, s2);

int size = stack_size(s1);

for (i=0;i<5;i++) stack_push(s1, '0'+i);
for (i=0;i<5;i++) stack_push(s2, '0'+i);

bool is_eq2 = stack_equal(s1, s2);

stack_free(s2);
stack_free(s1);

if (is_eq1 && is_eq2 && size==20) {
printf("Q1b okn");
return true;
}
else {
printf("Q1b ERRORn");
return false;
}
}



bool test_q1c() {

char str[] = "hello world";
stack_t* s1 = stack_create();
int i=0;
for (i=0;i<11;i++) stack_push(s1, str[i]);
char* ans = stack_to_string(s1);
bool check1 = ans && strcmp(ans, str)==0;
free(ans);

stack_free(s1);

if (check1) {
printf("Q1c okn");
return true;
}
else {
printf("Q1c ERRORn");
return false;
}
}



bool test_q2() {
/*** 43 = + | 45 = - | 47 = / | 42 = *
// creates the following tree
// +
// /
// * +
// / /
// 3 - 5 /
// / /
// 9 7 8 -2
****/
BTnode_t* root_plus = create_node(PLUS);
BTnode_t* mult = create_node(MULT);
BTnode_t* plus = create_node(PLUS);
BTnode_t* minus = create_node(MINUS);
BTnode_t* div = create_node(DIV);
BTnode_t* neg_two = create_node(-2);
BTnode_t* three = create_node(3);
BTnode_t* five = create_node(5);
BTnode_t* seven = create_node(7);
BTnode_t* eight = create_node(8);
BTnode_t* nine = create_node(9);

set_left_child(root_plus, mult);
set_right_child(root_plus, plus);

set_left_child(mult, three);
set_right_child(mult, minus);

set_left_child(plus, five);
set_right_child(plus, div);

set_left_child(minus, nine);
set_right_child(minus, seven);

set_left_child(div, eight);
set_right_child(div, neg_two);
char* a = get_arithmetic_expression(minus);//( 8 / -2 ) str + 3
free(a);
char* b = get_arithmetic_expression(div);//( 8 / -2 ) str + 3


/*
( ( 3 * ( 9 - 7 ) ) + ( 5 + ( 8 / -2 ) ) ) ( ( 3 * ( 9 - 7 ) )+ ( 5 + ( 8 / -2 ) ) )
*/
// char* ans8 = get_arithmetic_expression(eight);
// bool check8 = (ans8 && strcmp(ans8, "8") == 0);
// if (check8)
// printf("Q2: 8 okn");
// else
// printf("Q2: 8 ERROR: %sn", ans8);

// char* ans_neg_two = get_arithmetic_expression(neg_two);
// bool check_neg_two = (ans_neg_two && strcmp(ans_neg_two, "-2") == 0);
// if (check_neg_two)
// printf("Q2: -2 okn");
// else
// printf("Q2: -2 ERROR: %sn", ans_neg_two);

// char* ans_minus = get_arithmetic_expression(minus);
// bool check_minus = (ans_minus && strcmp(ans_minus, "9 - 7") == 0);
// if (check_minus)
// printf("Q2: 9-7 okn");
// else
// printf("Q2: 9-7 ERROR: %sn", ans_minus);

// char* ans_mult = get_arithmetic_expression(mult);
// bool check_mult = (ans_mult && strcmp(ans_mult, "3 * ( 9 - 7 )") == 0);
// if (check_mult)
// printf("Q2: 3*(9-7) okn");
// else
// printf("Q2: 3*(9-7) ERROR: %sn", ans_mult);


// char correct_ans[] = "( 3 * ( 9 - 7 ) ) + ( 5 + ( 8 / -2 ) )";
// char* ans_root = get_arithmetic_expression(root_plus);
// bool check_rook = (ans_root && strcmp(ans_root, correct_ans) == 0);
// if (check_rook)
// printf("Q2: root okn");
// else
// printf("Q2: root ERROR: %sn", ans_root);

// return true;
return false;
}

bool is_odd(int x) { return x%2==1;}

bool test_q3find() {
/***
// creates the following tree
// 1
// /
// 2 3
****/
BTnode_t* one = create_node(1);
BTnode_t* two = create_node(2);
BTnode_t* three = create_node(3);
set_left_child(one, two);
set_right_child(one, three);

BTnode_t* v = find(one, is_odd);
if (v == one) {
printf("Q3-find okn");
return true;
}
else {
printf("Q3-find ERRORn");
return false;
}
}

int square(int x) { return x*x;}

bool test_q3map() {
/***
// creates the following tree
// 1
// /
// 2 3
****/
BTnode_t* one = create_node(1);
BTnode_t* two = create_node(2);
BTnode_t* three = create_node(3);
set_left_child(one, two);
set_right_child(one, three);

map(one, square);
if (one->value == 1 && two->value == 4 && three->value == 9) {
printf("Q3-map okn");
return true;
}
else {
printf("Q3-map ERRORn");
return false;
}
}

bool test_q3copy() {
/***
// creates the following tree
// 1
// /
// 2 3
****/
BTnode_t* one = create_node(1);
BTnode_t* two = create_node(2);
BTnode_t* three = create_node(3);
set_left_child(one, two);
set_right_child(one, three);

BTnode_t* copy = copy_tree(one);
if (copy && copy->left && copy->right
&& copy->parent==NULL
&& copy->left->left==NULL && copy->left->right==NULL
&& copy->right->left==NULL && copy->right->right==NULL
&& copy != one && copy->left != two && copy->right != three ) {
printf("Q3-copy okn");
return true;
}
else {
printf("Q3-copy ERRORn");
return false;
}
}

int main() {
// test_q1a();
// test_q1b();
// test_q1c();
test_q2();
// test_q3find();
// test_q3map();
// test_q3copy();
return 0;
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.