C++编“排课程序”

C++编“排课程序”,第1张

#include<iostream>

#include<cstdlib>

#include<fstream>

#include<ctime>

#define N 10

#define M 20

#define SIZE 40

using namespace std

typedef struct tcnode

{

char name[20]

int ipri

}tclist,*ptclist

typedef struct node

{

char name[20]

char num[20]

int work_time

tclist tc[M]

char cbuf[SIZE]

struct node *next

}tlist,*ptlist

typedef struct Node

{

char name[20]

char num[20]

int week_time

int all_time

int priority

bool mc//是否被选

struct Node *next

}clist,*pclist

class teacher

{

public:

teacher()

~teacher()

void initdata()

void save()

friend void print(teacher&,ptlist)

tlist* pt

}

class course

{

public:

course()

~course()

void initdata()

void save()

friend pclist convert(course&,char * s)

private:

clist* pc

}

teacher::teacher()

{

pt=NULL

}

teacher::~teacher()

{

ptlist p

while(pt)

{

p=pt

pt=pt->next

delete p

}

}

void teacher::initdata()

{

char tag

ptlist p=pt

ptlist s

int t=0

cout<<"请输入教师信息:"<<endl

cout<<"姓名\t教师号\t工作量"<<endl

do

{

if(t>=1)

{

cout<<"请输入教师信息:"<<endl

cout<<"姓名\t教师号\t工作量"<<endl

}

s=(ptlist)malloc(sizeof(tlist))

cin>>s->name>>s->num>>s->work_time

cout<<"请输入相应课程名及期望值:"<<endl

for(int i=0i<Mi++)

cin>>s->tc[i].name>>s->tc[i].ipri

fflush(stdin)

s->next=NULL

if(!pt)

{

pt=s

p=pt

}

else

{

p->next=s

p=p->next

}

t++

cout<<"是否继续输入信息:"<<endl

fflush(stdin)

cin>>tag

system("cls")

}while((tag=='y'||tag=='Y')&&t<N)

}

void teacher::save()

{

ptlist p=pt

char path[20]="D:\\teacher.txt"

char str[50]={'\0'}

cout<<"请选择保存教师信息文件的路径:"<<endl

cin>>path

ofstream outfile(path)

if(!outfile)

{

cerr<<"保存失败,请检查所选路径是否正确!"<<endl

return

}

sprintf(str,"姓名\t教师号\t工作量\n")

outfile.write((char*)str,strlen(str))

while(p)

{

sprintf(str,"%s\t%s\t%d\n",p->name,p->num,p->work_time)

outfile.write((char*)str,strlen(str))

sprintf(str,"对相应课程的期望值:\n")

outfile.write((char*)str,strlen(str))

for(int i=0i<Mi++)

{

sprintf(str,"%s\t%d\n",p->tc[i].name,p->tc[i].ipri)

outfile.write((char*)str,strlen(str))

}

sprintf(str,"\n")

outfile.write((char*)str,strlen(str))

p=p->next

}

outfile.close()

cout<<"保存成功!"<<endl

}

course::course()

{

pc=NULL

}

course::~course()

{

pclist p

while(pc)

{

p=pc

pc=pc->next

delete p

}

}

void course::initdata()

{

pclist p=pc

pclist s

char tag

int i=0

cout<<"请输入课程信息:"<<endl

cout<<"课程名\t课程号\t周学时\t总学时\t优先级"<<endl

do{

if(i>=1)

{

cout<<"请输入课程信息:"<<endl

cout<<"课程名\t课程号\t周学时\t总学时\t优先级"<<endl

}

s=(pclist)malloc(sizeof(clist))

cin>>s->name>>s->num>>s->week_time>>s->all_time>>s->priority

fflush(stdin)

s->mc=true

s->next=NULL

i++

if(!pc)

{

pc=s

p=pc

}

else

{

p->next=s

p=p->next

}

cout<<"是否继续载入课程信息? y/Y"<<endl

cin>>tag

system("cls")

}while(tag=='y'||tag=='Y')

}

void course::save()

{

char path[20]="D:\\course.txt"

pclist p=pc

char str[100]={'\0'}

cout<<"请输入文件保存路径:"<<endl

cin>>path

ofstream outfile(path)

if(!outfile)

{

cerr<<"保存失败,请检查输入路径是否正确"

return

}

sprintf(str,"课程名\t课程号\t周学时\t总学时\t优先级\n")

outfile.write((char*)str,strlen(str))

while(p)

{

sprintf(str,"%s\t%s\t%d\t%d\t%d\n",p->name,p->num,p->week_time,p->all_time,p->priority)

outfile.write((char*)str,strlen(str))

p=p->next

}

outfile.close()

cout<<"保存成功!"<<endl

}

void sort(teacher&t,course&c)

{

ptlist q=t.pt

ptlist n=t.pt

ptlist m=n

int sum=0

int num=-1

srand(time(0))

for(int i=0i<Mi++)

{

while(m)

{

m=n->next

if(m==NULL)

break

for(int j=0j<Mj++)

if((m->tc[i].ipri==n->tc[j].ipri)&&(0==strcmp(m->tc[i].name,n->tc[j].name)))

{

num=rand()%2

switch(num)

{

case 0:

(convert(c,q->tc[num].name))->mc=false

break

case 1:

(convert(c,q->tc[num].name))->mc=false

break

default:

break

}

}

n=n->next

}

while(q)

{

for(int i=0i<Mi++)

{

if((convert(c,q->tc[i].name))->mc)

{

sum+=(convert(c,q->tc[i].name))->all_time

(convert(c,q->tc[i].name))->mc=false

if(i==0)

strcpy(q->cbuf,q->tc[i].name)

else

{

strcat(q->cbuf," ")

strcat(q->cbuf,q->tc[i].name)

}

if(sum>=q->work_time)

break

}

}

sum=0

q=q->next

}

}

cout<<"课程已排好!"<<endl

}

//test

void print(teacher&t)

{

ptlist p=t.pt

cout<<"最终排课情况:"<<endl

cout<<"教师姓名\t工作量\t教授课程"<<endl

while(p)

{

cout<<p->name<<"\t\t"<<p->work_time<<"\t"<<p->cbuf<<endl

p=p->next

}

}

pclist convert(course&c,char * s)

{

pclist p=c.pc

while(p)

{

if(0==strcmp(p->name,s))

return p

else

p=p->next

}

cout<<"应用程序出错!"<<endl

return NULL

}

teacher tea

course cou

void dis_menu()

{

bool ttag=false

bool ctag=false

char tag

int choice=0

do{

system("cls")

cout<<">>>>>>>欢迎使用教师排课系统,请根据提示完成相关 *** 作<<<<<<<<<<<<<<<<<<<<"<<endl

cout<<">>>>>>>1:载入教师信息"<<endl

cout<<">>>>>>>2:载入课程信息"<<endl

cout<<">>>>>>>3:存储教师信息"<<endl

cout<<">>>>>>>4:存储课程信息"<<endl

cout<<">>>>>>>5:教师排课"<<endl

cout<<">>>>>>>6:打印排课结果"<<endl

cout<<">>>>>>>0:退出"<<endl

fflush(stdin)

cout<<"请输入选择:"<<endl

cin>>choice

switch(choice)

{

case 1:

system("cls")

cout<<"***********教师信息载入******************"<<endl

tea.initdata()

ttag=true

break

case 2:

system("cls")

cout<<"***********课程信息载入******************"<<endl

cou.initdata()

ctag=true

break

case 3:

system("cls")

cout<<"***********教师信息保存******************"<<endl

if(!ttag)

cout<<"请先载入教师信息"<<endl

else

tea.save()

break

case 4:

system("cls")

cout<<"***********课程信息保存******************"<<endl

if(!ctag)

cout<<"请先载入课程信息"<<endl

else

cou.save()

break

case 5:

system("cls")

cout<<"***********教师排序界面*****************"<<endl

if(!ttag||!ctag)

cout<<"请先载入相关信息"<<endl

else

sort(tea,cou)

break

case 6:

system("cls")

cout<<"***********排课信息打印******************"<<endl

if(!ttag||!ctag)

cout<<"请先载入相关信息"<<endl

else

print(tea)

break

default:

system("cls")

cout<<"\t****************THANKS FOR YOUR USE***********"<<endl

cout<<"\t*****************感谢使用本系统!*************"<<endl

exit(0)

}

cout<<"系统使用中........."<<endl

cout<<"是否继续其他 *** 作? y/Y"<<endl

cin>>tag

}while(tag=='y'||tag=='Y')

}

void main()

{

dis_menu()

}

#include<iostream>

#include<cstdlib>

#include<fstream>

#include<ctime>

#define N 10

#define M 20

#define SIZE 40

using namespace std

typedef struct tcnode

{

char name[20]

int ipri

}tclist,*ptclist

typedef struct node

{

char name[20]

char num[20]

int work_time

tclist tc[M]

char cbuf[SIZE]

struct node *next

}tlist,*ptlist

typedef struct Node

{

char name[20]

char num[20]

int week_time

int all_time

int priority

bool mc //是否被选

struct Node *next

}clist,*pclist

class teacher

{

public:

teacher()

~teacher()

void initdata()

void save()

friend void print(teacher&,ptlist)

tlist* pt

}

class course

{

public:

course()

~course()

void initdata()

void save()

friend pclist convert(course&,char * s)

private:

clist* pc

}

teacher::teacher()

{

pt=NULL

}

teacher::~teacher()

{

ptlist p

while(pt)

{

p=pt

pt=pt->next

delete p

}

}

void teacher::initdata()

{

char tag

ptlist p=pt

ptlist s

int t=0

cout<<"请输入教师信息:"<<endl

cout<<"姓名\t教师号\t工作量"<<endl

do

{

if(t>=1)

{

cout<<"请输入教师信息:"<<endl

cout<<"姓名\t教师号\t工作量"<<endl

}

s=(ptlist)malloc(sizeof(tlist))

cin>>s->name>>s->num>>s->work_time

cout<<"请输入相应课程名及期望值:"<<endl

for(int i=0i<Mi++)

cin>>s->tc[i].name>>s->tc[i].ipri

fflush(stdin)

s->next=NULL

if(!pt)

{

pt=s

p=pt

}

else

{

p->next=s

p=p->next

}

t++

cout<<"是否继续输入信息:"<<endl

fflush(stdin)

cin>>tag

system("cls")

}while((tag=='y'||tag=='Y')&&t<N)

}

void teacher::save()

{

ptlist p=pt

char path[20]="D:\\teacher.txt"

char str[50]={'\0'}

cout<<"请选择保存教师信息文件的路径:"<<endl

cin>>path

ofstream outfile(path)

if(!outfile)

{

cerr<<"保存失败,请检查所选路径是否正确!"<<endl

return

}

sprintf(str,"姓名\t教师号\t工作量\n")

outfile.write((char*)str,strlen(str))

while(p)

{

sprintf(str,"%s\t%s\t%d\n",p->name,p->num,p->work_time)

outfile.write((char*)str,strlen(str))

sprintf(str,"对相应课程的期望值:\n")

outfile.write((char*)str,strlen(str))

for(int i=0i<Mi++)

{

sprintf(str,"%s\t%d\n",p->tc[i].name,p->tc[i].ipri)

outfile.write((char*)str,strlen(str))

}

sprintf(str,"\n")

outfile.write((char*)str,strlen(str))

p=p->next

}

outfile.close()

cout<<"保存成功!"<<endl

}

course::course()

{

pc=NULL

}

course::~course()

{

pclist p

while(pc)

{

p=pc

pc=pc->next

delete p

}

}

void course::initdata()

{

pclist p=pc

pclist s

char tag

int i=0

cout<<"请输入课程信息:"<<endl

cout<<"课程名\t课程号\t周学时\t总学时\t优先级"<<endl

do{

if(i>=1)

{

cout<<"请输入课程信息:"<<endl

cout<<"课程名\t课程号\t周学时\t总学时\t优先级"<<endl

}

s=(pclist)malloc(sizeof(clist))

cin>>s->name>>s->num>>s->week_time>>s->all_time>>s->priority

fflush(stdin)

s->mc=true

s->next=NULL

i++

if(!pc)

{

pc=s

p=pc

}

else

{

p->next=s

p=p->next

}

cout<<"是否继续载入课程信息? y/Y"<<endl

cin>>tag

system("cls")

}while(tag=='y'||tag=='Y')

}

void course::save()

{

char path[20]="D:\\course.txt"

pclist p=pc

char str[100]={'\0'}

cout<<"请输入文件保存路径:"<<endl

cin>>path

ofstream outfile(path)

if(!outfile)

{

cerr<<"保存失败,请检查输入路径是否正确"

return

}

sprintf(str,"课程名\t课程号\t周学时\t总学时\t优先级\n")

outfile.write((char*)str,strlen(str))

while(p)

{

sprintf(str,"%s\t%s\t%d\t%d\t%d\n",p->name,p->num,p->week_time,p->all_time,p->priority)

outfile.write((char*)str,strlen(str))

p=p->next

}

outfile.close()

cout<<"保存成功!"<<endl

}

void sort(teacher&t,course&c)

{

ptlist q=t.pt

ptlist n=t.pt

ptlist m=n

int sum=0

int num=-1

srand(time(0))

for(int i=0i<Mi++)

{

while(m)

{

m=n->next

if(m==NULL)

break

for(int j=0j<Mj++)

if((m->tc[i].ipri==n->tc[j].ipri)&&(0==strcmp(m->tc[i].name,n->tc[j].name)))

{

num=rand()%2

switch(num)

{

case 0:

(convert(c,q->tc[num].name))->mc=false

break

case 1:

(convert(c,q->tc[num].name))->mc=false

break

default:

break

}

}

n=n->next

}

while(q)

{

for(int i=0i<Mi++)

{

if((convert(c,q->tc[i].name))->mc)

{

sum+=(convert(c,q->tc[i].name))->all_time

(convert(c,q->tc[i].name))->mc=false

if(i==0)

strcpy(q->cbuf,q->tc[i].name)

else

{

strcat(q->cbuf," ")

strcat(q->cbuf,q->tc[i].name)

}

if(sum>=q->work_time)

break

}

}

sum=0

q=q->next

}

}

cout<<"课程已排好!"<<endl

}

//test

void print(teacher&t)

{

ptlist p=t.pt

cout<<"最终排课情况:"<<endl

cout<<"教师姓名\t工作量\t教授课程"<<endl

while(p)

{

cout<<p->name<<"\t\t"<<p->work_time<<"\t"<<p->cbuf<<endl

p=p->next

}

}

pclist convert(course&c,char * s)

{

pclist p=c.pc

while(p)

{

if(0==strcmp(p->name,s))

return p

else

p=p->next

}

cout<<"应用程序出错!"<<endl

return NULL

}

teacher tea

course cou

void dis_menu()

{

bool ttag=false

bool ctag=false

char tag

int choice=0

do{

system("cls")

cout<<">>>>>>>欢迎使用教师排课系统,请根据提示完成相关 *** 作<<<<<<<<<<<<<<<<<<<<"<<endl

cout<<">>>>>>>1:载入教师信息"<<endl

cout<<">>>>>>>2:载入课程信息"<<endl

cout<<">>>>>>>3:存储教师信息"<<endl

cout<<">>>>>>>4:存储课程信息"<<endl

cout<<">>>>>>>5:教师排课"<<endl

cout<<">>>>>>>6:打印排课结果"<<endl

cout<<">>>>>>>0:退出"<<endl

fflush(stdin)

cout<<"请输入选择:"<<endl

cin>>choice

switch(choice)

{

case 1:

system("cls")

cout<<"***********教师信息载入******************"<<endl

tea.initdata()

ttag=true

break

case 2:

system("cls")

cout<<"***********课程信息载入******************"<<endl

cou.initdata()

ctag=true

break

case 3:

system("cls")

cout<<"***********教师信息保存******************"<<endl

if(!ttag)

cout<<"请先载入教师信息"<<endl

else

tea.save()

break

case 4:

system("cls")

cout<<"***********课程信息保存******************"<<endl

if(!ctag)

cout<<"请先载入课程信息"<<endl

else

cou.save()

break

case 5:

system("cls")

cout<<"***********教师排序界面*****************"<<endl

if(!ttag||!ctag)

cout<<"请先载入相关信息"<<endl

else

sort(tea,cou)

break

case 6:

system("cls")

cout<<"***********排课信息打印******************"<<endl

if(!ttag||!ctag)

cout<<"请先载入相关信息"<<endl

else

print(tea)

break

default:

system("cls")

cout<<"\t****************THANKS FOR YOUR USE***********"<<endl

cout<<"\t*****************感谢使用本系统!*************"<<endl

exit(0)

}

cout<<"系统使用中........."<<endl

cout<<"是否继续其他 *** 作? y/Y"<<endl

cin>>tag

}while(tag=='y'||tag=='Y')

}

void main()

{

dis_menu()

}

#include <stdio.h>

#include <stdlib.h>

#define M 100

struct Student

{

int StudentID

char name[50]

float PeacetimeScore

float TestScore

float TotalScore

}

int main()

{

int InputInformation (struct Student student[])

void TotalScoreStatistics (struct Student student[], int n)

void TotalScoreSort (struct Student student[], int n)

void ScoreRevise (struct Student student[], int n)

void display (struct Student student[], int n)

int menu ()

int n = 1, count

struct Student student[M]

while (n)

{

n = menu ()

switch (n)

{

case 1:

count = InputInformation (student)

break

case 2:

TotalScoreStatistics (student, count)

break

case 3:

TotalScoreSort (student, count)

break

case 4:

ScoreRevise (student, count)

break

case 5:

display (student, count)

break

case 0:

printf ("您选择了退出!\n")

break

default :

printf ("输入有误,重新输入!\n")

break

}

}

return 0

}

int menu ()

{

int n, i

char * menu[]={"* * * * * * * * * * * * * * *MENU* * * * * * * * * * * * * * *",

" 1.学生信息录入",

" 2.总成绩统计",

" 3.总成绩排序",

" 4.成绩更改",

" 5.显示所有学生信息",

" 0.退出",

"* * * * * * * * * * * * * * *MENU* * * * * * * * * * * * * * *"}

for (i=0i<8i++)

printf ("%s\n", menu[i])

printf ("请选择(输入序号):")

scanf ("%d", &n)

return n

}

int InputInformation (struct Student student[])

{

int i

FILE *fp

for (i=0i++)

{

printf ("输入第 %d 个学生的如下信息:\n", i+1)

printf ("学号:")

scanf ("%d", &student[i].StudentID)

if (student[i].StudentID == 0) //如果学号输入是0则结束输入

break

getchar ()

printf ("姓名:")

gets (student[i].name)

printf ("平时成绩:")

scanf ("%f", &student[i].PeacetimeScore)

printf ("考试成绩:")

scanf ("%f", &student[i].TestScore)

fp = fopen ("myfile.txt", "a")

if (fp == NULL)

{

printf ("文件打开失败!\n")

exit (-1)

}

fprintf (fp, "%d %s %.2f %.2f\n", student[i].StudentID, student[i].name,

student[i].PeacetimeScore, student[i].TestScore)

}

fclose (fp)//关闭文件

return i

}

void TotalScoreStatistics (struct Student student[], int n)

{

int i

printf ("\n 学号 姓名 总成绩\n\n")

for (i=0i<ni++)

{

student[i].TotalScore = student[i].PeacetimeScore * 0.2 + student[i].TestScore * 0.8

printf (" %d%s%.2f\n", student[i].StudentID, student[i].name, student[i].TotalScore)

}

}

void TotalScoreSort (struct Student student[], int n)

{

int i, j

float temp

for (i=0i<ni++)

for (j=i+1j<nj++)

if (student[i].TotalScore >student[j].TotalScore)

{

temp = student[i].TotalScore

student[i].TotalScore = student[j].TotalScore

student[j].TotalScore =temp

}

for (i=0i<ni++)

printf ("%.2f ", student[i].TotalScore)

printf ("\n")

}

void ScoreRevise (struct Student student[], int n)

{

int m, k, i = 0

FILE *fp

printf ("输入要修改的学生的学号:")

scanf ("%d", &k)

printf ("您是要修改平时成绩还是考试成绩呢?\n")

printf ("1.修改平时成绩\n")

printf ("2.修改考试成绩\n")

printf ("输入您的选择:")

scanf ("%d", &m)

for (i=0i<ni++)

if (student[i].StudentID == k)

if (m == 1)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8038462.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存