找到学期的最低数量回溯算法中的应用毕业 [英] Application of Backtracking algorithm to find minimum number of semester to graduate
问题描述
我正在从一个文件,其中包含输入
- 没有的课程,允许最大每学期的课程
- 所有课程名称(5个字母数字最大)
- 职高名,参团SEM,prereqs,preREQ课程的数量。
- -1和结束时-1
输出将是学期完成所有课程的最低数量。
这是我的code,让你知道我所做的工作。我的code被编译和执行,但没有显示SEMS完成课程的数量。
请告诉我哪里做错了。
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdbool.h GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;炭垃圾[12] [6];
炭courseIdentifier [12] [6],prereqIdentifier [12] [5] [6];
CHAR semOffered [12];
布尔完成[12];布尔allDone(INT);
布尔做preREQ(INT,INT);INT主要(无效)
{
FILE * FP;
FP = FOPEN(graduate.in,R);
INT N,M,P;
INT I,J,K;
对于(i = 0; I< 12;我++)
{
的strcpy(courseIdentifier [I]中,);
}
对于(i = 0; I< 12;我++)
{
为(J = 0; J&小于5; J ++)
{
的strcpy(prereqIdentifier [I] [J],);
}
}
的fscanf(FP,%D,&安培; N,放大器; M); //取N,M
而(N!= -1)
{ 如果((1 <<;!=正&放大器;&放大器; N&下; = 12)||(2';!= M&放大器;&放大器; M&下; = 6))
{
的printf(输入错误);
}
对于(i = 0; I&LT; N;我++)//所开设课程列表
{
的fscanf(FP,%S,垃圾[I]);
}
对于(i = 0; I&LT; N;我++)
{
的fscanf(FP,%s%C%D,courseIdentifier [I],和放大器; semOffered,&安培; P);当然//名称,SEM,没有。的prereqs
为(J = 0; J&LT; P; J ++)
{
的fscanf(FP,%S,prereqIdentifier [I] [J]);
}
} INT SEM = 1;
焦炭semNow ='F';
对于(i = 0; I&LT; N;我++)
{
做[I] = FALSE;
}
而(!allDone(n))的
{
诠释计数= 0;
//而(计数&LT; = M)
// {
对于(i = 0; I&LT; N;我++)
{
如果((semOffered由[i] == semNow || semOffered [Ⅰ] =='B')及与放大器;完成preREQ(P,N))
{
做[I] =真;
算上++;
}
} //}
SEM ++;
如果(semNow =='F')
{
semNow ='S';
}
否则如果(semNow =='S')
{
semNow =F;
}
}
的printf(学期的最小数量=%d个\\ N,SEM);
的fscanf(FP,%D,&安培; N,放大器; M); //取N,M
}
返回1;
}布尔allDone(INT N)
{
布尔returnBool = TRUE;
INT I;
对于(i = 0; I&LT; N;我++)
{
returnBool = returnBool&放大器;&安培;做过[I]
}
返回returnBool;}布尔做preREQ(INT P,INT N)
{
布尔returnBool = TRUE;
INT I,J;
对于(i = 0; I&LT; P;我++)
{
为(J = 0; J&LT; N; J ++)
{
如果(STRCMP(prereqIdentifier [i] [j]时,courseIdentifier [J])== 0)
{
的printf(preREQ匹配\\ n);
returnBool =(returnBool&安培;&安培;完成[J]);
}
}
}
返回returnBool;
}
当您在扫描你的输入有关每个提供的课程这里的信息:
的fscanf(FP,%s%C%D,courseIdentifier [I],和放大器; semOffered,&安培; P);
的&放大器; semOffered
参数不正确。您将需要提供指针字符
。由于 semOffered
是字符
的数组,你可能意味着其存储到索引位置:
的fscanf(FP,%s%C%D,courseIdentifier [I],和放大器; semOffered [I],和放大器; P);
这不能解决你的程序,但它允许程序来完成。
I'm taking input from a file , which contains
- No of courses and max allowed course per semester
- All Course name(5 alphanumeric max)
- couse name,Offered sem, number of prereqs, Prereq courses.
- -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;
}
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.
这篇关于找到学期的最低数量回溯算法中的应用毕业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!