Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <progbase/console.h>
- #include <stdbool.h>
- #include <time.h>
- #include <progbase.h>
- #include <ctype.h>
- #include <string.h>
- #include <assert.h>
- int indexofSecSentence(char*str);
- void copyOfFirstSentense(char*str,char*buffer, int len);
- struct Sentense{
- int index;
- char Sentense[500];
- };
- struct Sentense texts[10000];
- struct SentenseArray{
- struct Sentense * text;
- int lenght;
- };
- bool isEqual(struct Sentense first,struct Sentense second);
- bool isEqualArrays(struct SentenseArray*first,struct SentenseArray*second);
- struct Sentense firstSentense(char*str);
- struct SentenseArray * getAllSentencesArrayNew(const char * str);
- void freeArray(struct SentenseArray * array);
- void getAllSentencesArray(const char * str);
- int main(void){
- int len = 400;
- char buffer[len];
- buffer[0] = '\0';
- char text[]={"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase. At 00:00, I ran away from him. But unlike Cinderella, I didn't leave a single thing behind."};
- // TESTS FOR 1 TASK;
- assert(indexofSecSentence("aasfwrh, gsfgsgd s hdhf.h") == 24);
- assert(indexofSecSentence("a.asfwrh, gsfgsgd s hdhfh") == 2);
- assert(indexofSecSentence(".aasfwrh, gsfgsgd s hdhfh") == 1);
- assert(indexofSecSentence("!.?aasfwrh, gsfgsgd s hdhfh") == 3);
- assert(indexofSecSentence("aasfwrh, gsfgsgd s hdhfh") == -1);
- assert(indexofSecSentence(NULL) == -1);
- copyOfFirstSentense("aasfwrh, gsfgsgd s hdhf.h", buffer, 50);
- assert(strcmp(buffer,"aasfwrh, gsfgsgd s hdhf")==0);
- buffer[0] = '\0';
- copyOfFirstSentense("aasfwrh, gsfgsgd s hdh.fh",buffer, 50);
- assert(strcmp(buffer,"aasfwrh, gsfgsgd s hdh")==0);
- buffer[0] = '\0';
- copyOfFirstSentense(".aasfwrh, gsfgsgd s hdhfh",buffer, 50);
- assert(strcmp(buffer,"")==0);
- buffer[0] = '\0';
- copyOfFirstSentense(NULL,buffer, len);
- assert(strcmp(buffer,"")==0);
- buffer[0] = '\0';
- copyOfFirstSentense("aasfwrh, gsfgsgd s hdhfh",buffer, 5);
- assert(strcmp(buffer,"aasfw")==0);
- buffer[0] = '\0';
- // TESTS FOR TASK 2;
- // Comparing Sentenses
- struct Sentense one = {0,"you are here"};
- struct Sentense two = {0,"you are here"};
- struct Sentense three = {1,"you are here"};
- struct Sentense four = {0,"you are here "};
- assert(isEqual(one,one)==1);
- assert(isEqual(one,two)==1);
- assert(isEqual(one,three)==0);
- assert(isEqual(one,four)==0);
- assert(isEqual(three,four)==0);
- //Comparing SentensesArrays
- struct Sentense a[3] = {one,two,three};
- struct Sentense b[3] = {one,two,three};
- struct Sentense c[3] = {one,two,four};
- struct Sentense d[4] = {one,two,three,four};
- struct Sentense e[3] = {two,one,three};
- struct SentenseArray a1 = { &a[0], 3};
- struct SentenseArray a2 = { &b[0], 3};
- struct SentenseArray a3 = { &c[0], 4};
- struct SentenseArray a4 = { &d[0], 4};
- struct SentenseArray a5 = { &e[0], 3};
- assert ( isEqualArrays ( &a1 , &a2 ) == 1);
- assert ( isEqualArrays ( &a1 , &a1 ) == 1);
- assert ( isEqualArrays ( &a2 , &a2 ) == 1);
- assert ( isEqualArrays ( &a1 , &a3 ) == 0);
- assert ( isEqualArrays ( &a1 , &a4 ) == 0);
- assert ( isEqualArrays ( &a3 , &a4 ) == 0);
- assert ( isEqualArrays ( &a1 , &a5 ) == 1);
- //testing function firstSentense
- struct Sentense test1 = {0,"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase"};
- struct Sentense test2 = {1,"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase"};
- struct Sentense test3 = {0,"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase."};
- assert (isEqual(firstSentense( text ), test1 ) == 1);
- assert (isEqual(firstSentense( text ), test2 ) == 0);
- assert (isEqual(firstSentense( text ), test3 ) == 0);
- //task3 tests
- char*p2str = text;
- int n = 0;
- int lenght = 0;
- while(p2str!='\0' && n != -1){
- lenght = lenght + 1;
- n = indexofSecSentence(p2str);
- p2str = p2str + n;
- }
- const char tex[]={"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase. At 00:00, I ran away from him. But unlike Cinderella, I didn't leave a single thing behind."};
- const char tex2[]={"At 00:00, I ran away from him. But unlike Cinderella, I didn't leave a single thing behind."};
- struct SentenseArray all = {texts,lenght};
- struct SentenseArray*p = &all;
- p = getAllSentencesArrayNew(tex);
- struct SentenseArray vll = {texts,lenght};
- struct SentenseArray*z = &vll;
- z = getAllSentencesArrayNew(tex);
- struct SentenseArray pll = {texts,lenght};
- struct SentenseArray*m = &pll;
- m = getAllSentencesArrayNew(tex2);
- struct SentenseArray ill = {texts,lenght};
- struct SentenseArray*i = &ill;
- i = getAllSentencesArrayNew(tex2);
- assert( isEqualArrays ( p , p ) == 1);
- assert( isEqualArrays ( p , z ) == 1);
- assert( isEqualArrays ( p , m ) == 0);
- assert( isEqualArrays ( p , i ) == 0);
- freeArray(z);
- freeArray(p);
- freeArray(m);
- freeArray(i);
- return 0;
- }
- int indexofSecSentence(char*str){
- char cha[]={".!?"};
- if(str!=NULL){
- int m = strlen(str);
- for(int i = 0; i < m; i++){
- if (strchr(cha,str[i])){
- while(strchr(cha,str[i+1])&&i+1<m){
- i++;
- }
- if (strchr(cha,str[i+1])==0){
- return i+1;
- }
- else {
- return -1;
- }
- }
- }
- }
- return -1;
- }
- void copyOfFirstSentense(char*str,char*buffer, int len){
- char cha[]={".!?"};
- int n = 0;
- if(str!=NULL){
- int m = strlen(str);
- for(int i = 0; i < m; i++){
- if (strchr(cha,str[i])==0&&n<len){
- buffer[n] = str[i];
- n++;
- }
- else{
- break;
- }
- }
- }
- buffer[n] = '\0';
- }
- bool isEqual(struct Sentense first,struct Sentense second){
- if(first.index == second.index && strcmp(first.Sentense,second.Sentense)==0){
- return true;
- }
- return false;
- }
- bool isEqualArrays(struct SentenseArray*first,struct SentenseArray*second){
- if(first->lenght == second->lenght){
- for(int i = 0;i<(first->lenght);i++){
- if(!isEqual(first->text[i], second->text[i])){
- return false;
- }
- }
- return true;
- }
- return false;
- }
- struct Sentense firstSentense(char*str){
- struct Sentense first;
- first.index = 0;
- copyOfFirstSentense(str,first.Sentense,500);
- return first;
- }
- void getAllSentencesArray(const char * str){
- //char b[] = "asdadsadqeqw .asfasfasffsasf.";
- char b[strlen(str)];
- strcpy(b,str);
- b[strlen(b)-1] = '\0';
- char * pstr = b;
- char*p2str = pstr;
- int lenght = 0;
- int n = 0;
- while(p2str!='\0' && n != -1){
- lenght = lenght + 1;
- n = indexofSecSentence(p2str);
- p2str = p2str + n;
- }
- struct SentenseArray heap = {texts,4};
- heap.lenght = lenght;
- int index = 0;
- heap.text[0].index = index;
- for (int i = 0; i<lenght;i++){
- copyOfFirstSentense(pstr,heap.text[i].Sentense,500);
- puts(heap.text[i].Sentense);
- heap.text[i].index = index;
- index = index + n;
- n = indexofSecSentence(pstr);
- pstr = pstr + n;
- }
- printf("%i",lenght);
- puts(" ");
- }
- struct SentenseArray * getAllSentencesArrayNew(const char * str){
- //char b[] = "asdadsadqeqw .asfasfasffsasf.";
- char b[strlen(str)];
- strcpy(b,str);
- b[strlen(b)-1] = '\0';
- char * pstr = b;
- char*p2str = pstr;
- int n = 0;
- int lenght = 0;
- while(p2str!='\0' && n != -1){
- lenght = lenght + 1;
- n = indexofSecSentence(p2str);
- p2str = p2str + n;
- }
- struct SentenseArray*heap = malloc(sizeof(struct SentenseArray));
- heap->text = malloc(lenght * sizeof(struct Sentense));
- heap->lenght = lenght;
- int index = 0;
- heap->text[0].index = index;
- for (int i = 0; i<heap->lenght;i++){
- copyOfFirstSentense(pstr,heap->text[i].Sentense,500);
- // puts(heap->text[i].Sentense);
- heap->text[i].index = index;
- index = index + n;
- n = indexofSecSentence(pstr);
- pstr = pstr + n;
- }
- // printf("%i",heap->lenght);
- //puts(" ");
- //freeArray(heap);
- return heap;
- }
- void freeArray(struct SentenseArray * array) {
- free(array->text); //
- free(array); //
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement