Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 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. free(a);
  114. return newa;
  115. }
  116.  
  117. char**find_20018_and_kill(char** pointers, int *pointonsizeofmassive) {
  118. const int sizeofmassive =*(pointonsizeofmassive);
  119. char b[5] = "2018";
  120. bool *boolmask2 = malloc(sizeofmassive*sizeof(bool));
  121. for(int i = 0; i<sizeofmassive; i++){
  122. boolmask2[i] = true;
  123. }
  124. int doless = 0;
  125. for(int i = 0; i<sizeofmassive; i++){
  126. if(strstr(pointers[i],b)){
  127. doless++;
  128. boolmask2[i]=false;
  129. }
  130. }
  131. char** newpointers = malloc((sizeofmassive-doless)*sizeof(char*));
  132. int j = 0;
  133. for(int i=0; i<sizeofmassive; i++) {
  134. if (boolmask2[i]) {
  135. newpointers[j++] = pointers[i];
  136. } else {
  137. free(pointers[i]);
  138. }
  139. }
  140. free(pointers);
  141. free(boolmask2);
  142. *pointonsizeofmassive = sizeofmassive - doless;
  143. return newpointers;
  144.  
  145. }
  146.  
  147. bool isHasAllNumbers(char* str){
  148. bool nums[10];
  149. for (int i = 0; i<10; i++)
  150. nums[i]=false;
  151. for (int i = 0; str[i]; i++)
  152. if(isdigit(str[i]))
  153. nums[str[i]-'0']=true;
  154. bool res = true;
  155. for (int i = 0; i<10; i++)
  156. res = res && nums[i];
  157. return res;
  158. }
  159.  
  160. void print_all_numbers(char** pointers, int* pointonsizeofmassive){
  161. int needtoprint = 0;
  162. for(int i = 0; i < *(pointonsizeofmassive); i++){
  163. if(isHasAllNumbers(pointers[i])){
  164. printf("%s", pointers[i]);
  165. needtoprint = 1;
  166. }
  167. if(!needtoprint){
  168. printf("There is no such a sentence!");
  169. }
  170. }
  171. }
  172. void justprint(char** pointers,int*pointonsizeofmassive){
  173. for (int i = 0; i < *pointonsizeofmassive; i++) {
  174. fputs(pointers[i], stdout);
  175. }
  176. }
  177.  
  178. int compare(const void* a, const void* b)
  179. {
  180. const char* str1 = *(char**)a;
  181. const char* str2 = *(char**)b;
  182.  
  183. bool isStr1HasDigit = false;
  184. bool isStr2HasDigit = false;
  185.  
  186. int sum1 = 0;
  187. int sum2 = 0;
  188.  
  189. for (int i = 0; str1[i]; i++)
  190. if(isdigit(str1[i])){
  191. sum1+=str1[i]-'0';
  192. isStr1HasDigit = true;
  193. }
  194. for (int i = 0; str2[i]; i++)
  195. if(isdigit(str2[i])){
  196. sum2+=str2[i]-'0';
  197. isStr2HasDigit = true;
  198. }
  199. if(!isStr1HasDigit)
  200. return 1;
  201. if(!isStr2HasDigit)
  202. return -1;
  203. return sum1-sum2;
  204. }
  205.  
  206. char** sort_by_sum(char** pointers, int *pointonsizeofmassive){
  207. qsort(pointers,*(pointonsizeofmassive), sizeof(char*), compare);
  208. return pointers;
  209. }
  210.  
  211. char**read_text(char**pointers, int* sizeofmassive){
  212. int counter = 0;
  213. int size = M;
  214. char *s;
  215. int sizeofmymassive = 0;
  216. do {
  217. s = scan_sentences();
  218. if (counter == size - 1) {
  219. size += M;
  220. pointers = realloc(pointers, size * sizeof(char *));
  221. }
  222. pointers[counter] = s;
  223. counter++;
  224. sizeofmymassive++;
  225. }while(strcmp(s,"\n\n")!=0);
  226. *(sizeofmassive) = sizeofmymassive -1;
  227. return pointers;
  228. }
  229.  
  230. int main(){
  231. printf("Dear user, enter your text.\n");
  232. char **pointers = malloc(M*sizeof(char*));
  233. int sizeofmassive;
  234. pointers = read_text(pointers, &sizeofmassive);
  235. int *pointonsizeofmassive = &sizeofmassive;
  236.  
  237. pointers=find_simmilar_and_kill(pointers, pointonsizeofmassive);
  238. int choise = 1;
  239. printf("Please, choose what you want to do with your text:\n");
  240. do {
  241. printf("1: convert the text so that the first word will start with a capital letter and others will be lowercase\n"
  242. "2: Delete all sentences that contain 2018\n"
  243. "3: sort sentences by increasing the sum of the digits\n"
  244. "4: print on screen sentences that contain all digits\n"
  245. "0: quit the programm\n");
  246.  
  247. scanf("%d", &choise);
  248.  
  249. if(choise!=1&&choise!=2&&choise!=3&&choise!=4&&choise!=0){
  250. printf("Invalid input!\n"
  251. "Try again.\n");
  252. scanf("%d", &choise);
  253. }
  254. switch (choise) {
  255. case 1:
  256. for (int i = 0; i < *pointonsizeofmassive; i++) {
  257. pointers[i] = do_upper(pointers[i]);
  258. }
  259. justprint(pointers, pointonsizeofmassive);
  260. printf("Choose again, please\n");
  261. break;
  262. case 2:
  263. pointers = find_20018_and_kill(pointers, pointonsizeofmassive);
  264. justprint(pointers, pointonsizeofmassive);
  265. printf("Choose again, please\n");
  266. break;
  267. case 3:
  268. pointers = sort_by_sum(pointers, pointonsizeofmassive);
  269. justprint(pointers, pointonsizeofmassive);
  270. printf("Choose again, please\n");
  271. break;
  272. case 4:
  273. print_all_numbers(pointers, pointonsizeofmassive);
  274. printf("Choose again, please\n");
  275. break;
  276. case 0:
  277. printf("See you later!");
  278.  
  279. }
  280. }while (choise);
  281. for(int i = 0; i<*(pointonsizeofmassive);i++){
  282. free(pointers[i]);
  283. }
  284. free(pointers);
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement