错误情况,如何实现呢?

时间:2017-03-21 20:42:26

标签: c sorting

所以我的代码目前做的很简单。它接收如下输入:

5 4
1 2
2 3
3 4
4 5

输入的第一行表示Ids的数量和关系的数量。以下是这些关系。程序对这些对进行排序,例如:1在2之前,2在3之前,依此类推。在这种情况下,出局将是1 2 3 4 5。

如果出现矛盾输入,例如:

4 4
1 2
2 3
3 2
3 4

返回“Incoerente”。

我目前的问题是我的输入是“非决定性的”,例如:

4 4
1 2
3 1
3 4
4 2

在这种情况下,程序并不真正知道在哪里放置4(在1之后或之前?)。我的程序现在(上面的输入)将打印“3 1 2”,并由于某种原因忽略4。当这种情况出现时我想打印“OTHERERROR”。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

void create_graph();
void add(int vertex);
int del();
int isEmpty();
int find_indegree_of_vertex(int vertex);

int total_vertices;
int adjacent_matrix[MAX][MAX];
int queue[MAX];
int front = -1;
int rear = -1;
FILE *myFile;

int main(){

    myFile = fopen("input.txt", "r");

    int i;
    int numberArray[2];

    for(i=0;i<2;i++)
        fscanf(myFile, "%1d", &numberArray[i]);

    int N = numberArray[0];
    int L = numberArray[1];

    int vertex, count, topological_sort[MAX], indegree[MAX];
    create_graph(N, L);

    for(i = 0; i < total_vertices; i++){
        indegree[i] = find_indegree_of_vertex(i);                       
        if(indegree[i] == 0){
            add(i);
        }
    }
    count = 0;
    while(!isEmpty() && count < total_vertices){
        vertex = del();
        topological_sort[++count] = vertex;
        for(i = 0; i < total_vertices; i++){
            if(adjacent_matrix[vertex][i] == 1){
                if(adjacent_matrix[i][vertex] == 1){
                    printf("INCOERENTE !!!!!!!!!!!!!!!\n");
                }
                adjacent_matrix[vertex][i] = 0;
                indegree[i] = indegree[i] - 1;
                if(indegree[i] == 0){
                    add(i);
                }
            }
        }
    }
    if(count < total_vertices){
        printf("Incoerente\n");
        exit(1);
    }
    printf("Topological Order of Vertices\n");
    for(i = 1; i <= count; i++){
        printf("%3d", topological_sort[i]+1);
    }
    printf("\n");
    return 0;
}

void add(int vertex){
    if(rear == MAX - 1){
        printf("Queue Overflow\n");
    }
    else{
        if (front == -1) {
            front = 0;
        }
        rear = rear + 1;
        queue[rear] = vertex ;
    }
}

int isEmpty(){
    if(front == -1 || front > rear ){
        return 1;
    }
    else{
        return 0;
    }
}

int del(){
    int element;
    if (front == -1 || front > rear){
        printf("Queue Underflow\n");
        exit(1);
    }
    else{
        element = queue[front];
        front = front+1;
        return element;
    }
}

int find_indegree_of_vertex(int vertex){
    int count, total_indegree = 0;
    for(count = 0; count < total_vertices; count++){
        if(adjacent_matrix[count][vertex] == 1){
            total_indegree++;
        }
    }
    return total_indegree;
}

void create_graph(int N, int L){
    int count, maximum_edges, origin_vertex, destination_vertex;
    total_vertices = N;
    maximum_edges = L;
    int integers[MAX];
    int i =0;
    int num;

    while(fscanf(myFile, "%d", &num) > 0) {
        integers[i] = num;
        i++;
    }

    i=0;
    printf("1a linha :%d %d\n", N, L);

    for(count = 1; count <= maximum_edges; count++){

        origin_vertex = integers[i]-1;

        destination_vertex = integers[i+1]-1;

        printf("pares a ser avaliados :%d %d\n", origin_vertex+1, destination_vertex+1);

        if(origin_vertex > total_vertices || destination_vertex > total_vertices || origin_vertex < 0 || destination_vertex < 0){
            printf("Edge Co-ordinates are Invalid\n");
            count--;
        }
        else
            adjacent_matrix[origin_vertex][destination_vertex] = 1;

        if(count == maximum_edges){
            break;
        }

        i+=2;
    }
}

0 个答案:

没有答案