开发者社区> 问答> 正文

C+语言 碰到输出手法 不能搞定 求解答 (注 : 不用迭代 )

#include "stdafx.h"
#include 
using namespace std;
class Teacher
{
public:
char name[20];
Teacher * tnext;
Teacher (char * name):tnext(tnext=NULL)
{
strcpy(this->name,name);
}
void Display()
{
cout<<name;
}
};
class Course
{
public:
char name[20];
Course * next;
Teacher * thead;
Course(char * name):thead(thead=NULL),next(next=NULL)
{
    strcpy(this->name,name);
}
void Display()
{
    cout<<name;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Course * head=NULL,*before;
Teacher * te=NULL,* pre;
FILE fp=fopen("E:\VC++\heminglen\组合排序test.txt","r");
do
{
char name[20];
fscanf(fp,"%s",name);
Course * temp= new Course (name);
do
{
fscanf(fp,"%s",name);
te=new Teacher (name);
if(strncmp(te->name,"end",3)==0)break;
else
{
if(NULL == temp->thead)
temp->thead=te;
else{pre->tnext=te;}
pre=te;
}
}
while (strncmp(te->name,"end",3)!=0);
if (NULL==head)
head= temp;
else 
{
before->next=temp;
}
before=temp;
}
while(!feof(fp));
/*long int a=0;
Course * k=head,*u,*v,*w;
Teacher * p, q,*r,*s;
for(p=k->thead;p!=NULL;p=p->tnext)
for(q=k->next->thead;q!=NULL;q=q->tnext)
for(r=k->next->next->thead;r!=NULL;r=r->tnext)
for(s=k->next->next->next->thead;s!=NULL;s=s->tnext)
{
a+=1;
printf ("%ld",a);
printf("\n");
}
printf("=================================\n");*/

本意对如下原始文件
数学 赵老师 钱老师 end
语文 孙老师 李老师 end
历史 何老师 郑老师 end
化学 华老师 王老师 end
输出选课老师所有组合可能 如上有81种
现在需要输出所有的可能组合 需要通用方法 用链就是为了可扩展

展开
收起
a123456678 2016-03-23 10:02:19 2157 0
1 条回答
写回答
取消 提交回答
  • 那是因为复制的时候没把 结尾
    return 0;
    }
    复制出来 在while后面/* 也忘记删除
    可以运行的;我是要改后面的输出部分 以便 改动原始文件时候可以 输出正确

    #include "stdafx.h"
    #include 
    using namespace std;
    class Teacher
    {
    public:
    char name[20];
    Teacher * tnext;
    Teacher (char * name):tnext(tnext=NULL)
    {
    strcpy(this->name,name);
    }
    void Display()
    {
    cout<<name;
    }
    };
    class Course
    {
    public:
    char name[20];
    Course * next;
    Teacher * thead;
    
    Course(char * name):thead(thead=NULL),next(next=NULL)
    {
        strcpy(this->name,name);
    }
    void Display()
    {
        cout<<name;
    }
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    Course * head=NULL,*before;
    Teacher * te=NULL,* pre;
    FILE fp=fopen("E:\VC++\heminglen\组合排序test.txt","r");
    do
    {
    char name[20];
    fscanf(fp,"%s",name);
    Course * temp= new Course (name);
    do
    {
    fscanf(fp,"%s",name);
    te=new Teacher (name);
    if(strncmp(te->name,"end",3)==0)break;
    else
    {
    if(NULL == temp->thead)
    temp->thead=te;
    else{pre->tnext=te;}
    pre=te;
    }
    }
    while (strncmp(te->name,"end",3)!=0);
    if (NULL==head)
    head= temp;
    else 
    {
    before->next=temp;
    }
    before=temp;
    }
    while(!feof(fp));
    long int a=0;
    Course * k=head,*u,*v,*w;
    Teacher * p, q,*r,*s;
    for(p=k->thead;p!=NULL;p=p->tnext)
    for(q=k->next->thead;q!=NULL;q=q->tnext)
    for(r=k->next->next->thead;r!=NULL;r=r->tnext)
    for(s=k->next->next->next->thead;s!=NULL;s=s->tnext)
    {
    a+=1;
    printf ("%ld",a);
    printf("\n");
    }
    
    printf("=================================\n");
    
    return 0;
    }
    2019-07-17 19:10:21
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
数据+算法定义新世界 立即下载
Python第五讲——关于爬虫如何做js逆向的思路 立即下载
4个迭代,从批量交...1573957773.pdf 立即下载