אשמח לתשובות, תגובות
1.
;(unsigned int bitReverse(unsigned int num
הפונקציה לעיל הופכת את סדר הסיביות במספר מימין לשמאל. אנא ממש אותה מבלי להשתמש בשום
פונקצית ספריה לצורך הפתרון.
לדוגמא:
עבןר קלט של 1234 (0x000004D2): הפונקציה תחזיר את המספר 1260388352 (0x4B200000)
2.
unsigned int weakEncrypt(const char *from, const char *to, char *str)
הפונקציה לעיל מצפינה מחרוזת ע"י קוד החלפה. כל תו המופיע ב form מוחלף בתו המופיע באותו מקום ב to . כל תו אחר לא מוחלף כלל. הערך המוחזר של הפונקציה הוא 1 (אחד) אם ניתן לשחזר את המחרוזת המקורית באמצעות קריאה נוספת לפונקציה weakEncrypt, ו 0- (אפס) אם לא ניתן.
ניתן להניח שהמחרוזות חוקיות וש- from אינה מכילה אף תו יותר מפעם אחת ושהאורך של
to ושל from שווה.
אנא ממשו את הפונקציה.
לדוגמא:
הקריאה הבאה (S = “hello”; weakEncrypt(“ehlo”,”abcd",S
תחזיר 1 ו S יכיל baccd' '
הקריאה הבאה (S = “hello”; weakEncrypt(“ehlo”,”accd",S
תחזיר 0 ו S יכיל caccd'
ע"י: 1_אורח_כללי
כתבתי משהו יותר פשוט עבור השאלה ה-2:
unsigned int weakEncrypt(const char *from,const char* to,char *str)
{
int i=0;
char chCheck = {0};
if (!str)
return 0;
while (*str)
{
const char * tmpStartFrom = from;
//check if appear in from
while(*from)
{
if ((*str) == (*from ))
*str = to;
else
{
from++;i++;
}
}//of from
from = tmpStartFrom;
i=0;
str++;
}//of str
//if to has char twice we can't recover
while(*to)
{
if (chCheck == 1 )
return 0;
else
chCheck = 1;
to++;
}
return 1;
}
ע"י: 1_אורח_כללי
אוקי עכשיו אני חושב שזה בסדר
unsigned int weakEncrypt(const char *from, const char *to, char *str)
{
int i=0; int j=0; int k=0; int m=0;
int str_length = strlen(str);
char *not_in_from = new char[str_length];
strcpy(not_in_from,"");
bool in_from = false;
for (i=0;i<str_length;i++)
{
for (j=0;j<strlen(from);j++)
{
if (str[i] == from[j])
{
in_from = true;
str[i] = to[j];
break;
}
}
if (!in_from)
strcat(not_in_from,&str[i]);
in_from = false;
}
char *to_and_not_in_from = new char[str_length+strlen(to)];
strcpy(to_and_not_in_from,"");
strcat(to_and_not_in_from,to);
strcat(to_and_not_in_from,not_in_from);
for (k=0;k<strlen(to_and_not_in_from);k++)
{
for (m=k+1;m<strlen(to_and_not_in_from);m++)
{
if (to_and_not_in_from[k] == to_and_not_in_from[m])
return 0;
}
}
return 1;
}
שרון גיא
ע"י: 1_אורח_כללי
tested and it seems to work
unsigned int weakEncrypt(const char *from, const char *to, char *str)
{
int i=0; int j=0; int k=0; int m=0;
int str_length = strlen(str);
for (i=0;i<str_length;i++)
{
for (j=0;j<strlen(from);j++)
{
if (str[i] == from[j])
str[i] = to[j];
}
}
החלק השני שבו אני מחזיר 1 או 0 לא מספיק טוב , אם יהיה לי זמן אחרי שאסיים לשתוף את הבית אני אתקן.
for (k=0;k<strlen(to);k++)
{
for (m=k+1;m<strlen(to);m++)
{
if (to[k] == to[m])
return 0;
}
}
return 1;
}
ע"י: 1_אורח_כללי
tested and it seems to work
unsigned int weakEncrypt(const char *from, const char *to, char *str)
{
int i=0; int j=0; int k=0; int m=0;
int str_length = strlen(str);
for (i=0;i<str_length;i++)
{
for (j=0;j<strlen(from);j++)
{
if (str[i] == from[j])
str[i] = to[j];
}
}
for (k=0;k<strlen(to);k++)
{
for (m=k+1;m<strlen(to);m++)
{
if (to[k] == to[m])
return 0;
}
}
return 1;
}
ע"י: 1_אורח_כללי
#include<stdio.h>
unsigned int bitRev(unsigned int num);
void main()
{
unsigned int num = 0x000004D2;
unsigned int RevNum = 0;
RevNum = bitRev(num);
printf(" RevNum = 0x%x\n",RevNum);
}
unsigned int bitRev(unsigned int num)
{
int n;
unsigned int RevNum = 0;
for(n=31; n>=0; n--)
{
RevNum += (1<<n)*(num&1);
num >>=1;
}
return RevNum;
}
ע"י: 1_אורח_כללי
1.
unsigned int bitReverse(unsigned int num){
int ans = 0;
int tmp;
int acc = 2^31;
for(int i=0;i<32 && num!=0;i++)
{
tmp = num & 0x00000001;
if(tmp>0)
ans += acc;
acc /= 2;
num >> 1;
}
return ans;
}
maybe there is a better way, using bitwise operations