`
ldb2741
  • 浏览: 31874 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

数据结构课程设计(1)

阅读更多

计算机系的同学总是很倒霉,期末考结束后都不能回家,还要留下来做课程设计。至少我们学校是这样,直到7月21号我才回家过暑假,下面就和大家分享一下本学期的课程设计题目。
题目:全国交通咨询模拟
【问题描述】处于对不同目的的旅客对交通工具有不同的要求。例如,因公出差的旅客希望在旅途中的时间尽可能短,出门旅游的游客则希望旅费尽可能省,而老年旅客则要求中转次数最少。编制一个全国城市间的交通咨询程序,为旅客提供两种或三种最优决策的交通咨询。

【基本要求】(1)提供对城市信息进行编辑(如:添加或删除)的功能。

(2)城市之间有两种交通工具:火车和飞机。提供对列车时刻表和飞机航班进行编辑(增设或删除)的功能。

(3)提供两种最优决策:最快到达或最省钱到达。全程只考虑一种交通工具。

(4)旅途中耗费的总时间应该包括中转站的等候时间。

(5)咨询以用户和计算机的对话方式进行。由用户输入起始站、终点站、最优决策原则和交通工具,输出信息:最快需要多长时间才能到达或者最少需要多少旅费才能到达,并详细说明依次于何时乘坐哪一趟列车或哪一次班机到何地

源程序
function.h
#include
using namespace std;
const int max=50;
const int lim_max=99999; //定义极限值

class time //定义时间类
{
private: //私有成员
int hour; //定义私有变量
int minute; //定义私有变量

public: //公有成员
void givevalue(int h,int m) //定义公有成员函数
{
hour=h;
minute=m;
}
void showvalue() //定义公有成员函数
{
cout< <":"<<minute<<" ?;
typedef struct
{
int weight; //定义城市之间的距离
int road[max]; //存储城市之间路过的顶点
int length; //定义最短路径的长度
time tstarttime; //定义火车出发时间
time tendtime; //定义火车到达时间
time pstarttime; //定义飞机出发时间(未用)
time pendtime; //定义飞机到达时间(未用)
}node;

typedef struct
{
char *vexs[max]; //定义城市的名称
node arcs[max][max]; //定义边之间的信息
int vexnum; //定义城市数
int arcnum; //定义边数
}Mgraph;


// 以下为函数声明
Mgraph *create(); //创建函数
void floyd(Mgraph *mg); //最短路径算法
Mgraph *addnewcity(Mgraph *mg);//添加城市信息
Mgraph *delcity(Mgraph *mg); //删除城市信息
int distime(time a,time b); //计算时间差
void view(Mgraph *mg); //查看系统信息
void list(); //菜单系统
void picture(); //图形界面
int logo(); //管理员登陆
int logoclient(); //用户登陆
void pic1(); //退出界面
void searchtime(Mgraph *mg); //查询函数
void showtime(int minute); //输出时间函数

resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by Script1.rc
//
#define IDI_ICON1 101

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
Script1.rc
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// Chinese (中国) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
#ifdef _WIN32
LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
#pragma code_page(936)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
// Icon
//
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON1 ICON DISCARDABLE "1.ico"

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END

3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END

#endif // APSTUDIO_INVOKED

#endif // Chinese (中国) resources
/////////////////////////////////////////////////////////////////////////////

#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

class.cpp
#include
#include "function.h"
using namespace std;

int distime(time a,time b) //返回b-a的时间差,用分表示
{
int ah,am;
int bh,bm;
int asum,bsum;
ah=a.outputhour();
am=a.outputminute();
bh=b.outputhour();
bm=b.outputminute();
asum=ah*60+am;
bsum=bh*60+bm;
if(asum<=bsum)
return (bsum-asum);
else
return (24*60-asum+bsum);
}

void showtime(int minute) //时间格式转换
{
int hour;
if(minute<60)
cout<<"分钟";
else
{
hour=minute/60;
minute=hour*60-minute;
cout<<"小时"<<MINUTE<<"分钟";< P>

}
}
file.cpp
#include
#include
#include
#include "function.h"
using namespace std;

Mgraph *addnewcity(Mgraph *mg)
{
int way,i;
int hour,minute;
char city1[20],city2[20]; //city1为新增的城市,文件中第一列为新增的城市,第二列为已存在的城市
char flag[20]="aaa";
ifstream infile("d:\\traffic.txt");
while(!infile.eof())
{
infile>>city1>>city2>>way>>hour>>minute;
if(strcmp(city1,flag))
{
mg->vexnum++;
mg->vexs[mg->vexnum]=new char [20];
strcpy(mg->vexs[mg->vexnum],city1);
strcpy(flag,city1);
}
for(i=1;ivexnum;i++)
{
if(strcmp(mg->vexs[i],city2)==0)
{
mg->arcs[i][mg->vexnum].weight=mg->arcs[mg->vexnum][i].weight=way;
mg->arcnum++;
mg->arcs[mg->vexnum][i].tstarttime.givevalue(hour,minute);
infile>>hour>>minute;
mg->arcs[mg->vexnum][i].tendtime.givevalue(hour,minute);
infile>>hour>>minute;
mg->arcs[i][mg->vexnum].tstarttime.givevalue(hour,minute);
infile>>hour>>minute;
mg->arcs[i][mg->vexnum].tendtime.givevalue(hour,minute);

}
else if(mg->arcs[i][mg->vexnum].weight<0||mg->arcs[i][mg->vexnum].weight>lim_max)
{
mg->arcs[i][mg->vexnum].weight=mg->arcs[mg->vexnum][i].weight=lim_max;
mg->arcs[mg->vexnum][i].tstarttime.givevalue(0,0);
mg->arcs[mg->vexnum][i].tendtime.givevalue(0,0);
mg->arcs[i][mg->vexnum].tstarttime.givevalue(0,0);
mg->arcs[i][mg->vexnum].tendtime.givevalue(0,0);
}


}
mg->arcs[mg->vexnum][mg->vexnum].weight=0;


}
mg->vexnum--; //奇怪
return mg;
}

Mgraph *delcity(Mgraph *mg)
{
char city[20];
char flag='a';
while(flag!=' ')
{
cout<<"输入要删除城市的名称:"<
cin>>city;
for(int count=1;count<=mg->vexnum;count++)
{
if(strcmp(city,mg->vexs[count])==0)
{
break;
}
}
for(int i=1;i<=mg->vexnum;i++)
{
mg->arcs[count][i].weight=mg->arcs[i][count].weight=lim_max;
mg->arcs[count][i].tstarttime.givevalue(0,0);
mg->arcs[count][i].tendtime.givevalue(0,0);
mg->arcs[i][count].tstarttime.givevalue(0,0);
mg->arcs[i][count].tendtime.givevalue(0,0);

}
mg->arcs[count][count].weight=0;
cout<<"结束请按空格键,继续请按任意键:"<
cin.ignore();
flag=cin.get();
}
return mg;
}


search.cpp
#include
#include "function.h"
using namespace std;


void view(Mgraph *mg)
{
char city1[20],city2[20];
int num1,num2;

cout<<"请输入要查找两城市的名称:"<
cin>>city1>>city2;
for(int count=1;count<=mg->vexnum;count++)
{
if(strcmp(mg->vexs[count],city1)==0)
{
num1=count;
break;
}
}

for(count=1;count<=mg->vexnum;count++)
{
if(strcmp(mg->vexs[count],city2)==0)
{
num2=count;
break;
}
}

cout<<"从"< <"到"<<city2<<"最短路径为:"<<endl;
cout<<" --> ";
for(count=1; mg->arcs[num1][num2].road[count]!=0; count++)
{
cout<vexs[mg->arcs[num1][num2].road[count]]<<" --> ";
}
cout<
cout<<"路径长度为: "<arcs[num1][num2].length<
cout<<"火车出发时间为: ";
mg->arcs[num1][num2].tstarttime.showvalue();
cout<<endl<<"火车到达时间为: ";
mg->arcs[num1][num2].tendtime.showvalue();
cout<
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics