Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- void quickSort(int**, int);
- void msg(int);
- int searchNode(char, char*); //функция считает степень вершины
- int searchIndex(int, int*, int); //а эта ищет индекс для вершины с известной степенью
- void main() {
- char* temp = malloc(100);
- char *input = malloc(1000), *startPos = input;
- int inputLength, searchDegree, flag = 0;
- int nodeAmount, i = 0, pos = 0, index;
- printf("Enter amount of the nodes:\n");
- scanf("%d", &nodeAmount); getchar();
- msg(0); msg(1);
- //цикл считывания структуры графа в одну строку
- while(1) {
- fgets(temp, 100, stdin);
- if (*temp == '\n') break;
- strcpy(input+pos, temp);
- pos += strlen(temp)-1;
- *(input+pos) = ' ';
- pos++; }
- *(input+pos) = '\0';
- //создаём всякие массивы для чисел, в них хранятся степени
- int *list[nodeAmount], node[nodeAmount], nodeDegree;
- inputLength = strlen(input);
- //проверка корректности ввода графа
- for (int i=0; i<inputLength-3;) {
- if (!isalpha(input[i])) msg(3);
- if (input[i+1] == ' ') i += 2;
- else if ((input[i+1] != '-') || (input[i+2] != '-')) msg(3);
- else i += 3; }
- //причёсываем массивы
- //в массиве node хранятся числа в формате 203,
- //где сотни - степень вершины, а единицы - индекс в массиве.
- for (int i=0; i < nodeAmount; i++) {
- nodeDegree = searchNode(i+'a', startPos);
- node[i] = nodeDegree*100 + i;
- list[i] = &node[i]; }
- msg(2);
- scanf("%d", &searchDegree); getchar(); //ловим искомую степень
- //проверяем массив на наличие таких степеней
- for (i=0; i<inputLength; i++) {
- if (node[input[i]-'a']/100 == searchDegree) {
- node[input[i]-'a'] *= -1; //клёвая система распознавания, у нужного числа меняется знак
- flag++; //флаг нужен для определения существования вершин с искомой степенью
- }
- }
- if (!flag) {
- printf("Вершин с такой степенью нет. Выход...\n");
- exit(0); }
- //выводим результаты
- printf("В введённом графе степень %d имеют следующие вершины:\n", searchDegree);
- printf("Node Degree\n");
- for (i=0; i<nodeAmount; i++) {
- index = searchIndex(*list[i], node, nodeAmount);
- if (node[i] < 0)
- //на -100 делим для получения положительного числа и чтобы узнать степень
- printf("%c%6d\n", 'A'+index, *list[i]/-100);
- }
- }
- int searchNode(char node, char* input) {
- int j = 0, i = 0;
- while (*(input+i) != '\0') {
- if (*(input+i) == node) {
- if (*(input+i-1) == '-') j++;
- if (*(input+i+1) == '-') j++; }
- i++; }
- return j;
- }
- int searchIndex(int degree, int* array, int length) {
- for (int i=0; i < length; i++)
- if (array[i] == degree) return i;
- return -1;
- }
- void msg(int action) {
- char* message[10] = {
- "Введите структуру графа.\nПоддерживается мнгострочный ввод, ",
- "используйте '--'.\nНапример: a--b--c\nВведите пустую строку для завершения ввода.\n",
- "Введите искомую степень:\n",
- "Неверный ввод.\n"
- };
- printf("%s", message[action]);
- if (action > 3) exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment