Shidongan

NewKursach

Dec 25th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <locale.h>
  5. #include <malloc.h>
  6. void showMenu(); // Напечатать меню
  7. void showHelp(); // Напечатать справку
  8. void freeMatrix(char **matrix, int row); // Освободить матрицу или массив
  9. char **makeDimMatrix(int *row); // Создание исходной матрицы
  10. char **makeFinalMatrix(char **matrix, int *validNums, int row, int validCounter); // Создание результирующей матрицы
  11. int isSeparator(char symToCheck); // Определение типа символа
  12. int *checkMatrix(char **matrix, int row, int *validNums, int *validCounter); // Проверка матрицы и сохранение номеров подходящих строк
  13. void printMatrix(char **mass, int row); // Вывод исходной матрицы
  14. int checkString(char *word); // Проверка содержания слова в массиве слов строки
  15. int wordLength(char *word, int type); // Подсчет длины слова или строки
  16. int compareWords(char **lineWords, char *word, int lineWordsLength); // Сравнение слова со словами в массиве слов строки
  17.  
  18. int main(){
  19.     setlocale(LC_ALL, "Russian");
  20.     int row, *validNums=NULL, validCounter, funcAccess=0, extra=1;
  21.     char  **matrix=NULL, menu, **finalMatrix=NULL, another;
  22.     while(1==1){
  23.         showMenu();
  24.         extra=1;
  25.         while(1==1){
  26.             another=getch();
  27.             if(another==8&&extra==0){
  28.                 printf("%c", another); 
  29.                 extra=1;
  30.             } else if(another!=8&&extra==1){
  31.                 menu=another;
  32.                 printf("%c", menu);
  33.                 extra=0;
  34.             }
  35.             if(another==13){
  36.                 break;
  37.             }
  38.         }      
  39.         system("cls");
  40.         switch(menu){              
  41.             case '1':
  42.                 if(matrix!=NULL){
  43.                     freeMatrix(matrix, row);
  44.                     matrix=NULL;
  45.                 }              
  46.                 matrix=makeDimMatrix(&row);
  47.                 funcAccess=1;
  48.                 system("cls");
  49.                 break;
  50.                
  51.             case '2':
  52.                 if(funcAccess==0){
  53.                     printf("[ERR] Сначала нужно ввести матрицу!\n");
  54.                     system("pause");
  55.                     system("cls");                 
  56.                     break;
  57.                 }
  58.                 printMatrix(matrix, row);
  59.                 system("cls");             
  60.                 break;
  61.                
  62.             case '3':
  63.                 if(funcAccess==0){
  64.                     printf("[ERR] Сначала нужно ввести матрицу!\n");
  65.                     system("pause");
  66.                     system("cls");                 
  67.                     break;
  68.                 }
  69.                 validNums=checkMatrix(matrix, row, validNums, &validCounter);
  70.                 finalMatrix=makeFinalMatrix(matrix, validNums, row, validCounter);
  71.                 printf("Результирующая матрица посчитана\n");
  72.                 system("pause");
  73.                 system("cls");
  74.                 funcAccess=2;
  75.                 break;
  76.                
  77.             case '4':
  78.                 if(funcAccess==0){
  79.                     printf("[ERR] Сначала нужно ввести матрицу!\n");
  80.                     system("pause");
  81.                     system("cls");                 
  82.                     break;
  83.                 } else if (funcAccess==1){
  84.                     printf("[ERR] Сначала нужно посчитать матрицу!\n");
  85.                     system("pause");
  86.                     system("cls");                 
  87.                     break;
  88.                 }
  89.                 printMatrix(finalMatrix, validCounter);
  90.                 system("cls");
  91.                 break;
  92.            
  93.             case '5':
  94.                 showHelp();
  95.                 system("cls");
  96.                 break;
  97.            
  98.             case '6':
  99.                 if(matrix!=NULL)freeMatrix(matrix, row);
  100.                 if(finalMatrix!=NULL)freeMatrix(finalMatrix, validCounter);
  101.                 printf("Работы программы прекращена\n");
  102.                 system("pause");
  103.                 return 0;
  104.                
  105.             default:
  106.                 printf("Такой опции не существует!\n");
  107.                 system("pause");
  108.                 system("cls");
  109.                 break;
  110.         }      
  111.     }
  112. }
  113.  
  114. void showMenu(){
  115.     printf("%s%s%s%s%s%s%s%s",
  116.                 "Выберите один из пунктов:\n",
  117.                 "[1]-Ввести матрицу\n",
  118.                 "[2]-Вывести матрицу\n",
  119.                 "[3]-Посчитать матрицу\n",
  120.                 "[4]-Вывести результат\n",
  121.                 "[5]-Вывести справку (Обязательно, если вы не читали)\n",
  122.                 "[6]-Выйти\n",
  123.                 "Ваш выбор: ");
  124. }
  125.  
  126. void showHelp(){
  127.     printf("%s%s%s%s%s",
  128.                 "К прочтению обязательны следующие пункты:\n",
  129.                 "1) Для перехода к следующей строке нажмите Enter\n",
  130.                 "2) Пустые строки не учитываются",
  131.                 "2) Для прекращения вводе нажмите 2 раза подряд Enter\n",
  132.                 "3) Разделителями являются следующие символы:\n");
  133.     //char p[25]={'!','@','#','$','%','^','&','*','(',')','-','_','=','+','|','/','[',']','{','}',',','.',';','"'};
  134.     char p[30]="\n\r'!@#$%^&*()-_=+|\\/[]{},.;:\"\0";
  135.     int i;
  136.     for(i=0; i<80; i++)printf("=");
  137.     for(i=0; i<30; i++){
  138.         if(i!=29){
  139.              if(i==0){
  140.                 printf("'enter', ");
  141.              } else if(i==1){
  142.                 printf("'return', ");
  143.              }else{
  144.                 printf("'%c', ", p[i]);
  145.              }
  146.         } else {
  147.             printf("'%c'", p[i]);
  148.         }
  149.     }
  150.     printf("\n");
  151.     for(i=0; i<80; i++)printf("=");
  152.     printf("\n");
  153.     system("pause");
  154.     printf("\n");
  155. }
  156.  
  157. char **makeDimMatrix(int *row){
  158.     char **inputMatrix=NULL, lastS='\0', *buffer=NULL;
  159.     int counter = 0, shallStop = 0, i, j, number=15;
  160.     *row=0;
  161.     buffer=(char*)realloc(buffer, 81*sizeof(char));
  162.     inputMatrix=(char**)realloc(inputMatrix, number*sizeof(char*));
  163.     printf("Вводите строки для обработки:\n");
  164.     while (shallStop!=2 && *row<15) {
  165.         while (counter <79 && shallStop != 2 && *row<15 && number!=1) {
  166.             buffer[counter] = getch();
  167.             if(buffer[counter]!=8){
  168.                 if (buffer[counter] == 13) {
  169.                     buffer[counter]=lastS;
  170.                     counter++;
  171.                     shallStop++;
  172.                     number=1;
  173.                     printf("\n");
  174.                 } else {
  175.                     if(shallStop!=0)shallStop=0;
  176.                     printf("%c", buffer[counter]);
  177.                     counter++;
  178.                 }
  179.             }
  180.         }
  181.         if(shallStop!=2){
  182.             number=0;
  183.             if(counter==79)printf("\n");
  184.             inputMatrix[*row] = (char *) malloc((counter), sizeof(char));
  185.             for (i = 0; i <counter; i++) {
  186.                 inputMatrix[*row][i] = buffer[i];
  187.             }
  188.             *row+=1;
  189.             counter = 0;
  190.         }
  191.     }
  192.     free(buffer);
  193.     return inputMatrix;
  194. }
  195.  
  196. void printMatrix(char **matrix, int row){
  197.     if(row==0){
  198.         printf("В введенном тексте нет строк с одинаковыми словами!\n");
  199.         system("pause");
  200.         return;
  201.     }
  202.     printf("Полученная матрица:\n");
  203.     int i, j=0;
  204.     for(i=0; i<row; i++){
  205.         if(matrix[i]!=NULL)printf("%s", matrix[i]);
  206.         printf("\n");
  207.     }  
  208.     system("pause");
  209. }
  210.  
  211. int *checkMatrix(char **matrix, int row, int *validNums, int *validCounter){
  212.     int i, wordSame;
  213.     if(validNums!=NULL){
  214.         free(validNums);   
  215.     }
  216.     validNums=NULL;
  217.     validNums=(int*)realloc(validNums,15*sizeof(int));
  218.     *validCounter=0;
  219.     for(i=0; i<row; i++){
  220.         if(checkString(matrix[i])){
  221.             *(validNums+*validCounter)=i;
  222.             *validCounter+=1;
  223.         }
  224.         //printf("finish line %d\n", i);
  225.     }
  226.     validNums=(int*)realloc(validNums,*validCounter*sizeof(int));
  227.     //printf("finish matrix\n");
  228.     return validNums;
  229. }
  230.  
  231.  
  232. int checkString(char *word){
  233.     int cursorPosition=0, wordsAmount=0, i, j, srcLength, lineLength;
  234.     char **words=NULL;
  235.     words=(char**)realloc(words, 15*sizeof(char*));
  236.     lineLength=wordLength(word,1);
  237.     while(cursorPosition<lineLength){
  238.         srcLength=wordLength((word+cursorPosition),0);
  239.         if(isSeparator(word[cursorPosition])){
  240.             cursorPosition++;
  241.         } else {
  242.             if(!wordsAmount){
  243.                 //words=(char**)realloc(words, sizeof(char*));
  244.                 words[0]=(char*)malloc((srcLength+1),sizeof(char));
  245.                 for(i=0; i<srcLength; i++){
  246.                     words[0][i]=word[cursorPosition+i];
  247.                 }
  248.                 words[0][i]='.';
  249.                 cursorPosition+=srcLength;
  250.                 wordsAmount++;
  251.                 /*printf("first word: ");
  252.                 for(i=0; i<srcLength; i++){
  253.                     printf("%c",words[0][i]);
  254.                 }
  255.                 printf("\n");*/
  256.             } else {
  257.                 if(compareWords(words, (word+cursorPosition), wordsAmount)){
  258.                     //printf("exist\n");
  259.                     return 1;
  260.                 } else {
  261.                     //printf("wordsWrited=%d\n", wordsAmount);
  262.                     //words=(char**)realloc(words, sizeof(char*));
  263.                     words[wordsAmount]=(char*)malloc((srcLength+1), sizeof(char));
  264.                     for(i=0; i<srcLength; i++){
  265.                         words[wordsAmount][i]=word[cursorPosition+i];
  266.                     }
  267.                     words[wordsAmount][i]='.';
  268.                     cursorPosition+=srcLength;
  269.                     wordsAmount++;
  270.                     /*printf("current words: ");
  271.                     j=0;
  272.                     for(i=0; i<wordsAmount; i++){                      
  273.                         while(!isSeparator(words[i][j])){
  274.                             printf("%c",words[i][j]);
  275.                             j++;
  276.                         }
  277.                         printf(", ");
  278.                         j=0;
  279.                     }
  280.                     printf("\n");*/
  281.                 }
  282.             }
  283.         }
  284.     }
  285.     freeMatrix(words, wordsAmount);
  286.     //printf("finish string\n");   
  287.     return 0;
  288. }
  289.  
  290. int compareWords(char **words, char *word, int wordsAmount){
  291.     int i, j=0, same=1;
  292.     for(i=0; i<wordsAmount; i++){
  293.         if(wordLength(word, 0)==wordLength((*(words+i)+j),0)){
  294.             while(!isSeparator(words[i][j]) && !isSeparator(word[j])){
  295.                 if(!(tolower(words[i][j])==tolower(word[j]))){
  296.                     same=0;
  297.                     break;
  298.                 } else {
  299.                     same=1;
  300.                 }
  301.                 j++;
  302.             }
  303.             if(same)return 1;
  304.             j=0;
  305.         }
  306.     }
  307.     return 0;
  308. }
  309.  
  310. char **makeFinalMatrix(char **matrix, int *validNums, int row, int validCounter){
  311.     char **finalMatrix=NULL, lastS='\0';
  312.     finalMatrix=(char**)realloc(finalMatrix, validCounter*sizeof(char*));
  313.     int i, j, lineLength;
  314.     for(i=0; i<validCounter; i++){
  315.         lineLength=wordLength((*(matrix+validNums[i])), 1);
  316.         //printf("lL=%d\n", lineLength);
  317.         finalMatrix[i]=(char*)malloc((lineLength+1)*sizeof(char));
  318.         for(j=0; j<lineLength; j++){
  319.             finalMatrix[i][j]=matrix[validNums[i]][j];
  320.         }
  321.         finalMatrix[i][j]=lastS;
  322.         //printf("copied\n");
  323.     }  
  324.     return finalMatrix;
  325. }
  326.  
  327. int checkForContainInArrayOfNum(int *validNums, int num, int validCounter){
  328.     int i;
  329.     //printf("validAm:%d\n", validCounter);
  330.     for(i=0; i<validCounter; i++){
  331.         //printf("%d-%d\n", *(validNums+i), num);
  332.         if(*(validNums+i)==num)return 1;
  333.     }
  334.     //printf("close\n");
  335.     return 0;
  336. }
  337.  
  338. int isSeparator(char symToCheck){
  339.     char p[30]="\n\r'!@#$%^&*()-_=+|\\/[]{},.;:\"\0";
  340.     //char p[28]={13, 32, (int) '!', (int)'@', (int)'#', (int)'$', (int)'%', (int)'^',(int)'&',(int)'*',(int)'(',(int)')',(int)'-',(int)'_',(int)'=',(int)'+',(int)'|',(int)'/',(int)'[',(int)']',(int)'{',(int)'}',(int)',',(int)'.',(int)';',(int)'"',(int) '\0'};
  341.     int i;
  342.     for(i=0; i<30; i++){
  343.         if(symToCheck==p[i])return 1;// || (int)symToCheck==32 || (int)symToCheck==13)return 1;
  344.     }
  345.     return 0;
  346. }
  347.  
  348. int wordLength(char *word, int type){
  349.     int i=0, length=0;
  350.     if(type==0){   
  351.         while(!isSeparator(word[i])){
  352.             length++;
  353.             i++;
  354.         }
  355.     } else {
  356.         while(word[i]!='\0'){
  357.             length++;
  358.             i++;
  359.         }
  360.     }
  361.     return length;
  362. }
  363.  
  364. void freeMatrix(char **matrix, int row){
  365.     int i, j=0;
  366.     for(i=0; i<row; i++){
  367.         free(*(matrix+i));
  368.     }
  369.     free(matrix);
  370. }
Add Comment
Please, Sign In to add comment