Application of Backtracking algorithm to find minimum number of semester to graduate

南笙酒味 提交于 2019-12-08 03:11:55

问题


I'm taking input from a file , which contains
  1. No of courses and max allowed course per semester
  2. All Course name(5 alphanumeric max)
  3. couse name,Offered sem, number of prereqs, Prereq courses.
  4. -1 and -1 at the end The output will be the minimum number of semester to complete all courses.

This is my code, so that you know I've done work. My code is compiling and executing, but not showing the number of sems to complete courses. Please tell me where I'm doing wrong

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include<ctype.h>

char garbage[12][6];
char courseIdentifier[12][6],prereqIdentifier[12][5][6];
char semOffered[12];
bool done[12];

bool allDone(int);
bool donePrereq(int,int);

int main(void)
{
    FILE *fp;
    fp = fopen("graduate.in","r");
    int n,m,p;
    int i,j,k;
    for(i=0;i<12;i++)
    {
        strcpy(courseIdentifier[i],"");
    }
    for(i=0;i<12;i++)
    {
        for(j=0;j<5;j++)
        {
            strcpy(prereqIdentifier[i][j],"");
        }
    }
    fscanf(fp,"%d %d",&n,&m);// take n,m
    while(n != -1)
    {

        if( !(1<=n && n<=12) || !(2<=m && m<=6) )
        {
            printf("Wrong input");
        }
        for(i=0;i<n;i++) //the list of offered courses
        {
            fscanf(fp,"%s",garbage[i]);
        }
        for(i=0;i<n;i++)
        {
            fscanf(fp,"%s %c %d",courseIdentifier[i],&semOffered,&p);//the name of course, sem,no. of prereqs
            for(j=0;j<p;j++)
            {
                fscanf(fp,"%s",prereqIdentifier[i][j]);
            }
        }

        int sem=1;
        char semNow = 'F';
        for(i=0;i<n;i++)
        {
            done[i]=false;
        }
        while(!allDone(n))
        {
            int count=0;
            //while(count<=m)
            //{
                for(i=0;i<n;i++)
                {
                    if( (semOffered[i]==semNow || semOffered[i]=='B') && donePrereq(p,n))
                    {
                        done[i] = true;
                        count++;
                    }
                }

            //}
            sem++;
            if(semNow=='F')
            {
                semNow='S';
            }
            else if(semNow=='S')
            {
                semNow='F';
            }
        }
        printf("minimum number of semesters = %d\n",sem);
        fscanf(fp,"%d %d",&n,&m);// take n,m
    }
    return 1;
}

bool allDone(int n)
{
    bool returnBool=true;
    int i;
    for(i=0;i<n;i++)
    {
        returnBool = returnBool && done[i];
    }
    return returnBool;

}

bool donePrereq(int p,int n)
{
    bool returnBool=true;
    int i,j;
    for(i=0;i<p;i++)
    {
        for(j=0;j<n;j++)
        {
            if(strcmp(prereqIdentifier[i][j],courseIdentifier[j]) ==0 )
            {
                printf("prereq matched\n");
                returnBool = (returnBool&&done[j]);
            }
        }
    }
    return returnBool;
}

回答1:


When you are scanning your input for the information about each offered course here:

        fscanf(fp,"%s %c %d",courseIdentifier[i],&semOffered,&p);

The &semOffered parameter is incorrect. You will need to provide the pointer to a char. Since semOffered is an array of char, you probably meant to store it into the indexed position:

        fscanf(fp,"%s %c %d",courseIdentifier[i],&semOffered[i],&p);

This does not fix your program, but it does allow the program to complete.



来源:https://stackoverflow.com/questions/17575696/application-of-backtracking-algorithm-to-find-minimum-number-of-semester-to-grad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!