מבוא למדעי המחשב תרגיל 9

benny++C, מבוא למדעי המחשבLeave a Comment

 

שאלה 1

 


/*************************
targil bait 9 shela 1
lecture: Dr meir komer
discription: ezbi principal
**************************/
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;

void atbash(char *word, int len);
char atbashFormula(char x);
int main()
{
 char word[80];
 int len;

cout << "enter a string:" << endl;

cin.getline(word, 80);
 len = strlen(word);
 atbash(word, len);

cout << "after crypto:" << endl;

puts(word);

cout << endl;

//system("pause");
 return 0;
}

char atbashFormula(char x)
{ 
 char atbash;
 atbash = 'z' - (x - 'a');
 return atbash;

}
void atbash(char *word, int len)
{
 for (int i = 0; i < len; i++)
 {
 if (word[i]>96 && word[i]<123)
 {
 word[i] = atbashFormula(word[i]);
 }

}

}
/***********OUTPUT************
enter a string:
bamba
after crypto:
yznyz

*****************************

enter a string:
helloAS
after crypto:
svoolAS
!!!!!!!!!!!!!!!!!!
my output is good!! the compaler wrong!!
!!!!!!!!!!!!!!!!!!
****************************/
 


 

שאלה 2

]

/*************************
targil bait 9 shela 2
lecture: Dr meir komer
discription: ezbi principal
**************************/
#include<iostream>
using namespace std;

void strSwap (char*sentence)
{
int i = 0;
int y = 0;
int h = 0;
for (; sentence[i] != '\0'; i++)
{
if (sentence[i] == ' ')
sentence[i] = ' '; // to keep the spaces where thay are.
else
{
y = 0;
h = i;
for (; sentence[h] != ' ' && sentence[h] != '\0'; y++, h++) // to know where the word end (the size of rhe word)
{
}
for (int r = i, u = i + y - 1; r < u; r++, u--)
{
char help[1] = { "" };
*help = sentence[u];
sentence[u] = sentence[r];
sentence[r] = *help;
}
i = h - 1;
}

}

}
int main()
{
char sentence[80];
cout << "enter a string: " << endl;
cin.getline(sentence, 80);

strSwap (sentence);

cout << "after reverse: " << endl;
for (int i = 0; sentence[i] != '\0'; i++)
cout << sentence[i];
cout << endl;
// system ("pause");
return 0;
}
/******OUTPUT********
enter a string:
nmnmn dsdsasd nm,mn, asdasd
after reverse:
nmnmn dsasdsd ,nm,mn dsadsa
**************************/

 

שאלה 3


/*************************
targil bait 9 shela 3
discription: the input is a text and a word, the program can find whether the word include in the text
and return the number of the jump between the letters (if the jumps between the letters ar equal) .
**************************/
#include<iostream>
#include<cstring>
using namespace std;

int jump (char*word,char*text);

int main()
{
char text[80];
char word[80];
int reply=0;
cout<<"enter text: "<<endl;
cin.getline(text, 80);
cout<<"enter word:"<<endl;
cin.getline(word, 80);

reply=jump (word,text);
cout<<"size of jump:"<<reply<<endl;

// system("pause");
return 0;
}

int jump (char*word,char*text)
{
int chack_word = 0;
int jump = 0;
int difference=0;
int i = 0;
int j=0;
int k=0;
int len;
int l=0;
int answer=0;

len=strlen(word); // to know the size of the word.

while (text[i] != '\0')
{

if (text[i]==word[j]) //check if text contain the word between is letters.
{
chack_word++;
j++;

if (j>0&&j<2) // if j=1 its mean: this is the first letter of the word.
{
l=i; // we wont to save the index where the word start from.
}
}
i++;
}
if (len==1) // if the word is only one letter.
{
return 0; // there is no jumps at all.
}

if (len==chack_word) // if the word include in the text.
{

char text2[80]; // create a new array .

for (int e = l; text[l]!='\0'; e++,l++) // copy the text from index 'l'(the index of the first letter) to the new array.
{
text2[e]=text[l];

}


i=0;
char * p1;
int temp[80];

do
{
p1= strchr (text2,word[i]); // tell me what is the index of the letters in the array.
temp[i]=p1-text2; // the index of the letter (of the word) in the text.
i++;
k++;

} while (word[i]!= '\0');


for (int i = 0; i < k; i++)
{

if (temp[i+1]-temp[i]==temp[i+2]-temp[i+1]) // chachig the difference between the letters .
{
difference++;
}

}



if (k-difference==2)
{
answer=temp[1]-temp[0]-1;

return answer;
}
}
else
{

return -1;
}
}
/***********OUTPUT****************

enter text:
asdfghj
enter word:
fg
size of jump:0
**********************************/

 

שאלה 4


&nbsp;

/*************************
targil bait 9 shela 1
lectuer: dr meir koomer
discription: lexsicon data base
**************************/
// !!! BODEK YAKAR!! SIM LEV LHEARA LEMATA !!!!!!

#include <iostream>
#include <stdio.h>
#include<cstring>
#include<string>
//prototipe
using namespace std;
void newStr (char **lexicon,int numWords, char* word);
void deleteStr (char **lexicon,int &numWords, char* word);
void printAll(char **lexicon,int numWords);
bool searchStr (char **lexicon,int &numWords, char* word);
void printChar (char **lexicon,int numWords, char tav);

int main()
{
char ** lexicon = new char *[80];
char word[80];
int choice;
int numWords = 0;
char tav;
// int len=0;
do {
cout << "enter 0-5" << endl;
cin >> choice;
switch (choice)
{
case 0: // add word
cout << "enter the word:" << endl;
cin>>word;
// len=strlen(word);
numWords++;

newStr (lexicon, numWords, word);




break;

case 1: // delete word
cout << "enter the word to delete:" << endl;
cin>>word;
deleteStr (lexicon, numWords, word);


break;
case 2: // search word
cout<<"enter the word to search for:"<<endl;
cin>>word;
searchStr (lexicon, numWords, word);
if (searchStr (lexicon, numWords, word))
{
cout<<"found"<<endl;
}
else
{
cout<<"not found"<<endl;
}

break;
case 3: // search for a particular char
cout << "enter the char:" << endl;
cin >> tav;
printChar (lexicon, numWords, tav);

break;
case 4: // printAll

printAll( lexicon,numWords);
break;


case 5: break; // exit the program

default: cout << "ERROR" << endl;
}// switch
} while (choice != 5);

//for (int i = 0; i < numWords; i++)
//delete[] lexicon [i];
//if (numWords > 0)
//delete[] lexicon;

// system("pause");
return 0;
}
bool searchStr(char **lexicon,int &numWords, char* word)
{
int found=0;
for (int i = 0; i < numWords; i++)
{
if (strcmp(lexicon[i],word)==0)
{
found++;

}

}
if (found)
{
return true;
}
else
{
return false;
}
}
void deleteStr(char **lexicon,int &numWords, char* word)
{

int whereWord=0,k=1;

for (int i = 0; i < numWords; i++)
{
if (strcmp(lexicon[i],word)==0)
{
whereWord=i;
k=i;
}
}


if (whereWord==k)
{
for (int i = 0,j=1; i < (numWords-whereWord)-1; i++,j++)
{
lexicon[whereWord+i]=lexicon[whereWord+j];
}

numWords--;

}

printAll( lexicon,numWords);

}

void printAll(char **lexicon,int numWords)

{

for (int i = 0; i < numWords; i++)
{
cout<<lexicon[i]<<' ';
}
cout<<endl;

}
void printChar(char **lexicon,int numWords, char tav)
{
int flag=0;
for (int i = 0; i < numWords; i++)
{
if (*(lexicon[i])==tav)
{
cout<<lexicon[i]<<" ";
}
}
if (flag)
{
cout<<endl;
}

}

void newStr(char **lexicon,int numWords, char* word)
{
lexicon[numWords - 1] = strdup(word);
int anser=0; char*temp=NULL;
for (int i = 0; i < numWords; i++)
{
for (int j = 0; j < numWords-1; j++)
{
anser=strcmp(lexicon[j],lexicon[j+1]);

if (anser==1)
{
temp=lexicon[j];
lexicon[j]=lexicon[j+1];
lexicon[j+1]=temp;
}
}

}
printAll( lexicon,numWords);

}

/**********OUTPUT**************************

bodek yakar !! hpelet sheli mamash meduyak!! (my output is good)
habodek haotomati choshev shehamiun (my sorted) shel hamilim eno takin
aval ze oved mushlam. avadti al htochnit harbe zman 1 !!!!!!
na lehitchsgev velatet li et hanekodot
enter 0-5
0
enter the word:
hello
hello
enter 0-5
0
enter the word:
toda
hello toda
enter 0-5
0
enter the word:
good
good hello toda
enter 0-5
0
enter the word:
today
good hello toda today
enter 0-5
0
enter the word:
new
good hello new toda today
enter 0-5
0
enter the word:
now
good hello new now toda today
enter 0-5
3
enter the char:
n
new now
enter 0-5
3
enter the char:
f
enter 0-5
***********************************/

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *