Question
The following tests are not running for my code LeftShift1 (0/5) test/test.cpp:69 Expected: node->number Which is: '0' (48, 0x30) To be equal to: str[i] Which
The following tests are not running for my code
LeftShift1 (0/5) test/test.cpp:69 Expected: node->number Which is: '0' (48, 0x30) To be equal to: str[i] Which is: '1' (49, 0x31) The return data has wrong linkedList of numbers test/test.cpp:69 Expected: node->number Which is: '0' (48, 0x30) To be equal to: str[i] Which is: '1' (49, 0x31) The return data has wrong linkedList of numbers…
LeftShift3 (0/5) test/test.cpp:69 Expected: node->number Which is: '0' (48, 0x30) To be equal to: str[i] Which is: '1' (49, 0x31) The return data has wrong linkedList of numbers test/test.cpp:69 Expected: node->number Which is: '1' (49, 0x31) To be equal to: str[i] Which is: '0' (48, 0x30) The return data has wrong linkedList of numbers…
LeftShift3_overflow (0/5) test/test.cpp:69 Expected: node->number Which is: '0' (48, 0x30) To be equal to: str[i] Which is: '1' (49, 0x31) The return data has wrong linkedList of numbers test/test.cpp:69 Expected: node->number Which is: '0' (48, 0x30) To be equal to: str[i] Which is: '1' (49, 0x31) The return data has wrong linkedList of numbers…
LeftShiftN_overflow (0/5) test/test.cpp:71 Value of: node == NULL && str[i] == '\0' Actual: false Expected: true 0 test/test.cpp:73 Expected: l Which is: 8 To be equal to: i Which is: 1 The return data of should have a linkedList with length 1
rightShift1_unsigned (0/5) test/test.cpp:71 Value of: node == NULL && str[i] == '\0' Actual: false Expected: true 1100101 test/test.cpp:73 Expected: l Which is: 7 To be equal to: i Which is: 1 The return data of should have a linkedList with length 1
rightShift5_unsigned (0/5) test/test.cpp:71 Value of: node == NULL && str[i] == '\0' Actual: false Expected: true 110
rightShift1_signed (0/5) test/test.cpp:71 Value of: node == NULL && str[i] == '\0' Actual: false Expected: true 1100101 test/test.cpp:73 Expected: l Which is: 7 To be equal to: i Which is: 1 The return data of should have a linkedList with length 1
rightShift5_signed (0/5) test/test.cpp:71 Value of: node == NULL && str[i] == '\0' Actual: false Expected: true 11111110 test/test.cpp:73 Expected: l Which is: 3 To be equal to: i Which is: 5 The return data of should have a linkedList with length 5
-----------------------------------------------------------
data.c
#include
#include
#include
#include
int convertCharToNumber(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
} else if (ch >= 'A' && ch <= 'F') {
return ch - 'A' + 10;
} else {
return -1;
}
}
char convertNumberToChar(int n) {
if (n >= 0 && n <= 9) {
return n + '0';
} else if (n >= 10 && n <= 15) {
return n - 10 + 'A';
} else {
return 0;
}
}
Data convert_to_base_n(Data src, unsigned char n) {
if (n < 2 || n > 16 || src.data == NULL) {
printf("Invalid input");
exit(1);
}
if (src.base == n) {
printf("Already in base %d. \n", n);
return src;
}
Data new_data;
new_data.base = n;
new_data.sign = src.sign;
new_data.number_bits = src.number_bits;
new_data.len = 0;
new_data.data = NULL;
int decimal_num = 0;
DataNode *node_curr = src.data;
int curr_power = src.len - 1;
while (curr_power >= 0 && node_curr != NULL) {
decimal_num = decimal_num + (pow(src.base, curr_power) * convertCharToNumber(node_curr->number));
node_curr = node_curr->next;
curr_power = curr_power - 1;
}
int num_remain;
DataNode *node_prev = NULL;
while (decimal_num > 0) {
num_remain = decimal_num % n;
decimal_num /= n;
DataNode *node_new = (DataNode*)malloc(sizeof(DataNode));
node_new->number = convertNumberToChar(num_remain);
node_new->next = node_prev;
new_data.len = new_data.len + 1;
new_data.data = node_new;
node_prev = node_new;
}
while (new_data.data != NULL && new_data.data->number == '0') {
new_data.data = new_data.data->next;
}
return new_data;
}
int convert_to_int(Data src) {
int int_num = 0;
DataNode *decimal_num = NULL;
int neg_num = 0;
if (src.sign == 1) {
Data binary_num = convert_to_base_n(src, 2);
if (binary_num.data->number == '1' && binary_num.len == binary_num.number_bits) {
DataNode *node_curr = binary_num.data;
while (node_curr != NULL) {
if (node_curr->number == '0') {
node_curr->number = '1';
}
else {
node_curr->number = '0';
}
node_curr = node_curr->next;
}
decimal_num = convert_to_base_n(binary_num, 10).data;
neg_num = 1;
}
else {
decimal_num = convert_to_base_n(binary_num, 10).data;
}
}
else {
decimal_num = convert_to_base_n(src, 10).data;
}
while (decimal_num != NULL) {
int_num = int_num * 10 + convertCharToNumber(decimal_num->number);
decimal_num = decimal_num->next;
}
if (neg_num == 1) {
int_num = -int_num;
}
return int_num;
}
Data left_shift(Data src, int n) {
if (n < 0 || n >= src.number_bits) {
printf("Invalid shift value");
exit(1);
}
if (n == 0) {
return convert_to_base_n(src, 2);
}
Data new_data = convert_to_base_n(src, 2);
DataNode* node_curr = new_data.data;
DataNode* node_prev = NULL;
while (node_curr != NULL) {
node_prev = node_curr;
node_curr = node_curr->next;
}
int shift_count = n;
while (shift_count >= 1) {
DataNode* node_temp = (DataNode*) malloc(sizeof(DataNode));
node_temp->number = '0';
node_temp->next = NULL;
node_prev->next = node_temp;
node_prev = node_temp;
shift_count = shift_count - 1;
}
node_curr = new_data.data;
for (int i = 1; i <= n; i++) {
node_prev = node_curr;
node_curr = node_curr->next;
free(node_prev);
}
new_data.data = node_curr;
return new_data;
}
Data right_shift(Data src, int n) {
if (n < 0 || n >= src.number_bits) {
printf("Invalid shift value");
exit(1);
}
if (n == 0) {
return convert_to_base_n(src, 2);
}
Data new_data = convert_to_base_n(src, 2);
if (new_data.len == 1) {
new_data.data->number = '0';
}
else {
DataNode* node_curr = new_data.data;
DataNode* node_prev = NULL;
for (int i = 1; i <= n; i++) {
node_prev = node_curr;
node_curr = node_curr->next;
}
while (node_curr != NULL) {
node_curr->number = '0';
node_curr = node_curr->next;
}
new_data.len -= n;
node_curr = node_prev->next;
node_prev->next = NULL;
while (node_curr != NULL) {
DataNode *node_temp = node_curr;
node_curr = node_curr->next;
free(node_temp);
}
}
return new_data;
}
-----------------------------------------------
data.h
#ifndef __DATA
#define __DATA
typedef struct DataNode DataNode;
typedef struct Data Data;
// Represents a node in the linked list.
struct DataNode {
unsigned char number; // '0' ~ '9' or 'A' ~ 'F'
DataNode *next;
};
// Represents data in base 2 - 16.
struct Data {
unsigned char base;
unsigned char sign;
unsigned char number_bits;
unsigned char len;
DataNode *data;
};
int convertCharToNumber(char ch);
char convertNumberToChar(int n);
Data convert_to_base_n(Data src,unsigned char n);
int convert_to_int(Data src);
Data left_shift(Data src, int shift);
Data right_shift(Data src, int shift);
#endif
-------------------------------------------
main.c
#include
#include
#include
int main() {
DataNode b0 = {'5', NULL};
DataNode b1 = {'0', &b0};
DataNode b2 = {'2', &b1};
Data d205;
d205.base = 10;
d205.sign = 0;
d205.len = 3;
d205.number_bits = 8;
d205.data = &b2;
Data b11001101 = convert_to_base_n(d205, 2);
printf("The base should be 2, and your base is %d\n", b11001101.base);
printf("The sign should not be changed, and your sign is %d\n",
b11001101.sign);
printf("The number_bits should not be changed, and your number_bits is %d\n",
b11001101.number_bits);
printf("The length should be 8, and your len is %d\n", b11001101.len);
printf("The data should be 11001101, and your data is ");
for (DataNode *node = b11001101.data; node; node = node->next) {
printf("%c", node->number);
}
printf("\n\n");
Data hCD = convert_to_base_n(d205, 16);
printf("The base should be 16, and your base is %d\n", hCD.base);
printf("The sign should not be changed, and your sign is %d\n", hCD.sign);
printf("The number_bits should not be changed, and your number_bits is %d\n",
hCD.number_bits);
printf("The length should be 2, and your len is %d\n", hCD.len);
printf("The data should be CD, and your data is ");
for (DataNode *node = hCD.data; node; node = node->next) {
printf("%c", node->number);
}
printf("\n\n");
Data b00110100 = left_shift(d205, 2);
printf("The base should be 2, and your base is %d\n", b00110100.base);
printf("The sign should not be changed, and your sign is %d\n",
b00110100.sign);
printf("The number_bits should not be changed, and your number_bits is %d\n",
b00110100.number_bits);
printf(
"The length should be 6 since it cannot exceed the number_bit and"
" the first two 0 should not be included in the list.Your len is %d\n",
b00110100.len);
printf("The data should be 110100, and your data is ");
for (DataNode *node = b00110100.data; node; node = node->next) {
printf("%c", node->number);
}
printf("\n");
printf("We expect 205, and your function returns %d\n",
convert_to_int(b11001101));
b11001101.sign = 1;
printf("We expect -51, and your function returns %d\n",
convert_to_int(b11001101));
b11001101.number_bits = 16;
printf("We expect 205, and your function returns %d\n",
convert_to_int(b11001101));
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started