Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <locale.h>
- #include <malloc.h>
- void showMenu(); // Напечатать меню
- void showHelp(); // Напечатать справку
- void freeMatrix(char **matrix, int row); // Освободить матрицу или массив
- char **makeDimMatrix(int *row); // Создание исходной матрицы
- char **makeFinalMatrix(char **matrix, int *validNums, int row, int validCounter); // Создание результирующей матрицы
- int isSeparator(char symToCheck); // Определение типа символа
- int *checkMatrix(char **matrix, int row, int *validNums, int *validCounter); // Проверка матрицы и сохранение номеров подходящих строк
- void printMatrix(char **mass, int row); // Вывод исходной матрицы
- int checkString(char *word); // Проверка содержания слова в массиве слов строки
- int wordLength(char *word, int type); // Подсчет длины слова или строки
- int compareWords(char **lineWords, char *word, int lineWordsLength); // Сравнение слова со словами в массиве слов строки
- int main(){
- setlocale(LC_ALL, "Russian");
- int row, *validNums=NULL, validCounter, funcAccess=0, extra=1;
- char **matrix=NULL, menu, **finalMatrix=NULL, another;
- while(1==1){
- showMenu();
- extra=1;
- while(1==1){
- another=getch();
- if(another==8&&extra==0){
- printf("%c", another);
- extra=1;
- } else if(another!=8&&extra==1){
- menu=another;
- printf("%c", menu);
- extra=0;
- }
- if(another==13){
- break;
- }
- }
- system("cls");
- switch(menu){
- case '1':
- if(matrix!=NULL){
- freeMatrix(matrix, row);
- matrix=NULL;
- }
- matrix=makeDimMatrix(&row);
- funcAccess=1;
- system("cls");
- break;
- case '2':
- if(funcAccess==0){
- printf("[ERR] Сначала нужно ввести матрицу!\n");
- system("pause");
- system("cls");
- break;
- }
- printMatrix(matrix, row);
- system("cls");
- break;
- case '3':
- if(funcAccess==0){
- printf("[ERR] Сначала нужно ввести матрицу!\n");
- system("pause");
- system("cls");
- break;
- }
- validNums=checkMatrix(matrix, row, validNums, &validCounter);
- finalMatrix=makeFinalMatrix(matrix, validNums, row, validCounter);
- printf("Результирующая матрица посчитана\n");
- system("pause");
- system("cls");
- funcAccess=2;
- break;
- case '4':
- if(funcAccess==0){
- printf("[ERR] Сначала нужно ввести матрицу!\n");
- system("pause");
- system("cls");
- break;
- } else if (funcAccess==1){
- printf("[ERR] Сначала нужно посчитать матрицу!\n");
- system("pause");
- system("cls");
- break;
- }
- printMatrix(finalMatrix, validCounter);
- system("cls");
- break;
- case '5':
- showHelp();
- system("cls");
- break;
- case '6':
- if(matrix!=NULL)freeMatrix(matrix, row);
- if(finalMatrix!=NULL)freeMatrix(finalMatrix, validCounter);
- printf("Работы программы прекращена\n");
- system("pause");
- return 0;
- default:
- printf("Такой опции не существует!\n");
- system("pause");
- system("cls");
- break;
- }
- }
- }
- void showMenu(){
- printf("%s%s%s%s%s%s%s%s",
- "Выберите один из пунктов:\n",
- "[1]-Ввести матрицу\n",
- "[2]-Вывести матрицу\n",
- "[3]-Посчитать матрицу\n",
- "[4]-Вывести результат\n",
- "[5]-Вывести справку (Обязательно, если вы не читали)\n",
- "[6]-Выйти\n",
- "Ваш выбор: ");
- }
- void showHelp(){
- printf("%s%s%s%s%s",
- "К прочтению обязательны следующие пункты:\n",
- "1) Для перехода к следующей строке нажмите Enter\n",
- "2) Пустые строки не учитываются",
- "2) Для прекращения вводе нажмите 2 раза подряд Enter\n",
- "3) Разделителями являются следующие символы:\n");
- //char p[25]={'!','@','#','$','%','^','&','*','(',')','-','_','=','+','|','/','[',']','{','}',',','.',';','"'};
- char p[30]="\n\r'!@#$%^&*()-_=+|\\/[]{},.;:\"\0";
- int i;
- for(i=0; i<80; i++)printf("=");
- for(i=0; i<30; i++){
- if(i!=29){
- if(i==0){
- printf("'enter', ");
- } else if(i==1){
- printf("'return', ");
- }else{
- printf("'%c', ", p[i]);
- }
- } else {
- printf("'%c'", p[i]);
- }
- }
- printf("\n");
- for(i=0; i<80; i++)printf("=");
- printf("\n");
- system("pause");
- printf("\n");
- }
- char **makeDimMatrix(int *row){
- char **inputMatrix=NULL, lastS='\0', *buffer=NULL;
- int counter = 0, shallStop = 0, i, j, number=15;
- *row=0;
- buffer=(char*)realloc(buffer, 81*sizeof(char));
- inputMatrix=(char**)realloc(inputMatrix, number*sizeof(char*));
- printf("Вводите строки для обработки:\n");
- while (shallStop!=2 && *row<15) {
- while (counter <79 && shallStop != 2 && *row<15 && number!=1) {
- buffer[counter] = getch();
- if(buffer[counter]!=8){
- if (buffer[counter] == 13) {
- buffer[counter]=lastS;
- counter++;
- shallStop++;
- number=1;
- printf("\n");
- } else {
- if(shallStop!=0)shallStop=0;
- printf("%c", buffer[counter]);
- counter++;
- }
- }
- }
- if(shallStop!=2){
- number=0;
- if(counter==79)printf("\n");
- inputMatrix[*row] = (char *) malloc((counter), sizeof(char));
- for (i = 0; i <counter; i++) {
- inputMatrix[*row][i] = buffer[i];
- }
- *row+=1;
- counter = 0;
- }
- }
- free(buffer);
- return inputMatrix;
- }
- void printMatrix(char **matrix, int row){
- if(row==0){
- printf("В введенном тексте нет строк с одинаковыми словами!\n");
- system("pause");
- return;
- }
- printf("Полученная матрица:\n");
- int i, j=0;
- for(i=0; i<row; i++){
- if(matrix[i]!=NULL)printf("%s", matrix[i]);
- printf("\n");
- }
- system("pause");
- }
- int *checkMatrix(char **matrix, int row, int *validNums, int *validCounter){
- int i, wordSame;
- if(validNums!=NULL){
- free(validNums);
- }
- validNums=NULL;
- validNums=(int*)realloc(validNums,15*sizeof(int));
- *validCounter=0;
- for(i=0; i<row; i++){
- if(checkString(matrix[i])){
- *(validNums+*validCounter)=i;
- *validCounter+=1;
- }
- //printf("finish line %d\n", i);
- }
- validNums=(int*)realloc(validNums,*validCounter*sizeof(int));
- //printf("finish matrix\n");
- return validNums;
- }
- int checkString(char *word){
- int cursorPosition=0, wordsAmount=0, i, j, srcLength, lineLength;
- char **words=NULL;
- words=(char**)realloc(words, 15*sizeof(char*));
- lineLength=wordLength(word,1);
- while(cursorPosition<lineLength){
- srcLength=wordLength((word+cursorPosition),0);
- if(isSeparator(word[cursorPosition])){
- cursorPosition++;
- } else {
- if(!wordsAmount){
- //words=(char**)realloc(words, sizeof(char*));
- words[0]=(char*)malloc((srcLength+1),sizeof(char));
- for(i=0; i<srcLength; i++){
- words[0][i]=word[cursorPosition+i];
- }
- words[0][i]='.';
- cursorPosition+=srcLength;
- wordsAmount++;
- /*printf("first word: ");
- for(i=0; i<srcLength; i++){
- printf("%c",words[0][i]);
- }
- printf("\n");*/
- } else {
- if(compareWords(words, (word+cursorPosition), wordsAmount)){
- //printf("exist\n");
- return 1;
- } else {
- //printf("wordsWrited=%d\n", wordsAmount);
- //words=(char**)realloc(words, sizeof(char*));
- words[wordsAmount]=(char*)malloc((srcLength+1), sizeof(char));
- for(i=0; i<srcLength; i++){
- words[wordsAmount][i]=word[cursorPosition+i];
- }
- words[wordsAmount][i]='.';
- cursorPosition+=srcLength;
- wordsAmount++;
- /*printf("current words: ");
- j=0;
- for(i=0; i<wordsAmount; i++){
- while(!isSeparator(words[i][j])){
- printf("%c",words[i][j]);
- j++;
- }
- printf(", ");
- j=0;
- }
- printf("\n");*/
- }
- }
- }
- }
- freeMatrix(words, wordsAmount);
- //printf("finish string\n");
- return 0;
- }
- int compareWords(char **words, char *word, int wordsAmount){
- int i, j=0, same=1;
- for(i=0; i<wordsAmount; i++){
- if(wordLength(word, 0)==wordLength((*(words+i)+j),0)){
- while(!isSeparator(words[i][j]) && !isSeparator(word[j])){
- if(!(tolower(words[i][j])==tolower(word[j]))){
- same=0;
- break;
- } else {
- same=1;
- }
- j++;
- }
- if(same)return 1;
- j=0;
- }
- }
- return 0;
- }
- char **makeFinalMatrix(char **matrix, int *validNums, int row, int validCounter){
- char **finalMatrix=NULL, lastS='\0';
- finalMatrix=(char**)realloc(finalMatrix, validCounter*sizeof(char*));
- int i, j, lineLength;
- for(i=0; i<validCounter; i++){
- lineLength=wordLength((*(matrix+validNums[i])), 1);
- //printf("lL=%d\n", lineLength);
- finalMatrix[i]=(char*)malloc((lineLength+1)*sizeof(char));
- for(j=0; j<lineLength; j++){
- finalMatrix[i][j]=matrix[validNums[i]][j];
- }
- finalMatrix[i][j]=lastS;
- //printf("copied\n");
- }
- return finalMatrix;
- }
- int checkForContainInArrayOfNum(int *validNums, int num, int validCounter){
- int i;
- //printf("validAm:%d\n", validCounter);
- for(i=0; i<validCounter; i++){
- //printf("%d-%d\n", *(validNums+i), num);
- if(*(validNums+i)==num)return 1;
- }
- //printf("close\n");
- return 0;
- }
- int isSeparator(char symToCheck){
- char p[30]="\n\r'!@#$%^&*()-_=+|\\/[]{},.;:\"\0";
- //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'};
- int i;
- for(i=0; i<30; i++){
- if(symToCheck==p[i])return 1;// || (int)symToCheck==32 || (int)symToCheck==13)return 1;
- }
- return 0;
- }
- int wordLength(char *word, int type){
- int i=0, length=0;
- if(type==0){
- while(!isSeparator(word[i])){
- length++;
- i++;
- }
- } else {
- while(word[i]!='\0'){
- length++;
- i++;
- }
- }
- return length;
- }
- void freeMatrix(char **matrix, int row){
- int i, j=0;
- for(i=0; i<row; i++){
- free(*(matrix+i));
- }
- free(matrix);
- }
Add Comment
Please, Sign In to add comment