Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6.  
  7. #define N 10
  8. #define M 100
  9.  
  10. char *scan_sentences(){
  11. char *s = malloc(N*sizeof(char));
  12. int counter = 0;
  13. int size = N;
  14. char current_symbol;
  15. current_symbol = getchar();
  16. while ((current_symbol == ' ') || (current_symbol == '\t')){
  17. current_symbol = getchar();
  18. }
  19. while ((current_symbol!= '\n') && (current_symbol != '.') && (current_symbol != '!')){
  20. if(counter>=size - 5){
  21. size+=N;
  22. s = realloc(s, size);
  23. }
  24. s[counter] = current_symbol;
  25. counter++;
  26. current_symbol = getchar();
  27.  
  28. }
  29. s[counter++] = current_symbol;
  30. s[counter++] = '\n';
  31. s[counter] = '\0';
  32. return s;
  33. }
  34.  
  35. bool stcp(char* a, char* b){
  36. int lena = strlen(a);
  37. int lenb = strlen(b);
  38. char* newa = calloc(lena+1, sizeof(char));
  39. char* newb = calloc(lenb+1, sizeof(char));
  40. for(int i = 0; i<lena; i++){
  41. newa[i]=toupper(a[i]);
  42. if(i==lena-1){
  43. i++;
  44. newa[i]='\0';
  45. }
  46. }
  47. for(int i = 0; i<lenb; i++){
  48. newb[i]=toupper(b[i]);
  49. if(i==lenb-1){
  50. i++;
  51. newb[i]='\0';
  52. }
  53. }
  54. int st = strcmp(newa,newb);
  55. free(newa);
  56. free(newb);
  57. return(st==0) ? true : false;
  58.  
  59. }
  60.  
  61.  
  62. char** find_simmilar_and_kill(char** pointers, int* pointonsizeofmassive){
  63. const unsigned int sizeofmassive = *(pointonsizeofmassive);
  64. bool *boolmask1 = malloc(sizeofmassive*sizeof(bool));
  65. for(int i = 0; i<sizeofmassive; i++){
  66. boolmask1[i] = true;
  67. }
  68. for(int i = 0; i<(sizeofmassive-1); i++){
  69. for(int j=i+1; j<sizeofmassive; j++){
  70. if(stcp(pointers[i],pointers[j])){
  71. boolmask1[j]=false;
  72. }
  73. }
  74. }
  75. int suits = 0;
  76. for(int i = 0; i<sizeofmassive; i++){
  77. if(boolmask1[i]!=false){
  78. suits++;
  79. }
  80. }
  81. char** newpointers = malloc(suits*sizeof(char*));
  82. int j = 0;
  83. for(int i=0; i<sizeofmassive; i++) {
  84. if (boolmask1[i]) {
  85. newpointers[j++] = pointers[i];
  86. } else {
  87. free(pointers[i]);
  88. }
  89. }
  90. free(pointers);
  91. free(boolmask1);
  92. *pointonsizeofmassive = suits;
  93. return newpointers;
  94. }
  95. char* do_upper(char* a) {
  96. int lena = strlen(a);
  97. char *newa = malloc((lena + 1) * sizeof(char));
  98. for (int i = 0; i < lena; i++) {
  99. if(!(a[i]>=48 && a[i]<=57)){
  100. if (i == 0) {
  101. newa[i] = toupper(a[i]);
  102. } else {
  103. newa[i] = tolower(a[i]);
  104. }
  105. }else{
  106. newa[i] = a[i];
  107. }
  108. if (i == lena - 1) {
  109. i++;
  110. newa[i] = '\0';
  111. }
  112. }
  113. return newa;
  114. }
  115.  
  116. char**find_20018_and_kill(char** pointers, int *pointonsizeofmassive) {
  117. const int sizeofmassive =*(pointonsizeofmassive);
  118. char b[5] = "2018";
  119. bool *boolmask2 = malloc(sizeofmassive*sizeof(bool));
  120. for(int i = 0; i<sizeofmassive; i++){
  121. boolmask2[i] = true;
  122. }
  123. int doless = 0;
  124. for(int i = 0; i<sizeofmassive; i++){
  125. if(strstr(pointers[i],b)){
  126. doless++;
  127. boolmask2[i]=false;
  128. }
  129. }
  130. char** newpointers = malloc((sizeofmassive-doless)*sizeof(char));
  131. int j = 0;
  132. for(int i=0; i<sizeofmassive; i++) {
  133. if (boolmask2[i]) {
  134. newpointers[j++] = pointers[i];
  135. } else {
  136. free(pointers[i]);
  137. }
  138. }
  139. free(pointers);
  140. free(boolmask2);
  141. *pointonsizeofmassive = sizeofmassive - doless;
  142. return newpointers;
  143.  
  144. }
  145.  
  146. void print_all_numbers(char** pointers, int* pointonsizeofmassive){
  147. for(int i = 0; i < *(pointonsizeofmassive); i++){
  148. if(strstr(pointers[i], "0") && strstr(pointers[i], "1") && strstr(pointers[i], "2") && strstr(pointers[i], "3") && strstr(pointers[i], "4") && strstr(pointers[i], "5") && strstr(pointers[i], "6") && strstr(pointers[i], "7") && strstr(pointers[i], "8") && strstr(pointers[i], "9")){
  149. printf("%s", pointers[i]);
  150. }else{
  151. printf("There is no such a sentence.\n");
  152. }
  153. }
  154. }
  155. void justprint(char** pointers,int*pointonsizeofmassive){
  156. for (int i = 0; i < *pointonsizeofmassive; i++) {
  157. fputs(pointers[i], stdout);
  158. }
  159. }
  160.  
  161. int compare(const void* a, const void* b)
  162. {
  163. const char* str1 = *(char**)a;
  164. const char* str2 = *(char**)b;
  165.  
  166. bool isStr1HasDigit = false;
  167. bool isStr2HasDigit = false;
  168.  
  169. int sum1 = 0;
  170. int sum2 = 0;
  171.  
  172. for (int i = 0; str1[i]; i++)
  173. if(isdigit(str1[i])){
  174. sum1+=str1[i]-'0';
  175. isStr1HasDigit = true;
  176. }
  177. for (int i = 0; str2[i]; i++)
  178. if(isdigit(str2[i])){
  179. sum2+=str2[i]-'0';
  180. isStr2HasDigit = true;
  181. }
  182. if(!isStr1HasDigit)
  183. return 1;
  184. if(!isStr2HasDigit)
  185. return -1;
  186. return sum1-sum2;
  187. }
  188.  
  189. char** sort_by_sum(char** pointers, int *pointonsizeofmassive){
  190. qsort(pointers,*(pointonsizeofmassive), sizeof(char*), compare);
  191. return pointers;
  192. }
  193.  
  194. char**read_text(char**pointers, int* sizeofmassive){
  195. int counter = 0;
  196. int size = M;
  197. char *s;
  198. int sizeofmymassive = 0;
  199. do {
  200. s = scan_sentences();
  201. if (counter == size - 1) {
  202. size += M;
  203. pointers = realloc(pointers, size * sizeof(char *));
  204. }
  205. pointers[counter] = s;
  206. counter++;
  207. sizeofmymassive++;
  208. }while(strcmp(s,"\n\n")!=0);
  209. *(sizeofmassive) = sizeofmymassive -1;
  210. return pointers;
  211. }
  212.  
  213. int main(){
  214. printf("Dear user, enter your text.\n");
  215. char **pointers = malloc(M*sizeof(char*));
  216. int sizeofmassive;
  217. pointers = read_text(pointers, &sizeofmassive);
  218. int *pointonsizeofmassive = &sizeofmassive;
  219.  
  220. pointers=find_simmilar_and_kill(pointers, pointonsizeofmassive);
  221. int choise = 1;
  222. printf("Please, choose what you want to do with your text:\n");
  223. do {
  224. printf("1: convert the text so that the first word will start with a capital letter and others will be lowercase\n"
  225. "2: Delete all sentences that contain 2018\n"
  226. "3: sort sentences by increasing the sum of the digits\n"
  227. "4: print on screen sentences that contain all digits\n"
  228. "0: quit the programm\n");
  229.  
  230. scanf("%d", &choise);
  231.  
  232. if(choise!=1&&choise!=2&&choise!=3&&choise!=4&&choise!=0){
  233. printf("Invalid input!\n"
  234. "Try again.\n");
  235. scanf("%d", &choise);
  236. }
  237. switch (choise) {
  238. case 1:
  239. for (int i = 0; i < *pointonsizeofmassive; i++) {
  240. pointers[i] = do_upper(pointers[i]);
  241. }
  242. justprint(pointers, pointonsizeofmassive);
  243. printf("Choose again, please\n");
  244. break;
  245. case 2:
  246. pointers = find_20018_and_kill(pointers, pointonsizeofmassive);
  247. justprint(pointers, pointonsizeofmassive);
  248. printf("Choose again, please\n");
  249. break;
  250. case 3:
  251. pointers = sort_by_sum(pointers, pointonsizeofmassive);
  252. justprint(pointers, pointonsizeofmassive);
  253. printf("Choose again, please\n");
  254. break;
  255. case 4:
  256. print_all_numbers(pointers, pointonsizeofmassive);
  257. printf("Choose again, please\n");
  258. break;
  259. case 0:
  260. printf("See you later!");
  261.  
  262. }
  263. }while (choise);
  264. for(int i = 0; i<*(pointonsizeofmassive);i++){
  265. free(pointers[i]);
  266. }
  267. free(pointers);
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement