用C语言编写软件完成以下任务
用C语言编写软件完成以下任务 每天 沈阳--植物园 有5个班次的汽车 具体数据如下 班次 发车时间 载客量 已售票数 1 06:40 50 0 2 09:30 40 0 3 13:00 40 0 4 16:00 40 0 5 20:00 50 0 请你为售票员开发一个小程序,以完成每天的 沈阳--植物园 的售票业务。 要求如下: 1 能够售票和退票。 2 某个班次已经过了开车时间,则该班次今天不再售票。 3 可以随时显示售票现状,并以文件(myfile.txt)的形式保存当天各个班次的售票情况。 请各位高手帮忙
补充:稍微改改,别从别的地方直接复制过来~
追问:有稍微简单点的做法么····这个太高深了·····
答案:/** Function:
每天"沈阳--植物园"有5个班次的汽车,具体数据如下 :
班次发车时间 载客量 已售票数
1 06:40 50 0
2 09:30 40 0
3 13:00 40 0
4 16:00 40 0
5 20:00 50 0
1. 能够售票和退票;
2. 某个班次已经过了开车时间,则该班次今天不再售票;
3. 可以随时显示售票现状,并以文件(myfile.txt)的形式保存当天各个班次的售票情况.
* Version: 1.0
* Author: Jarvis
* Date: 2010/6/15
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define TICKET struct ticket
struct ticket{
int frequency;
char departure[6];
int capacity;
int sold;
};
void menu( void ); // display the menu
void display( TICKET * ); // display theticket status
void sell( TICKET * );
void refund( TICKET * );
void save( TICKET * );
int main( void )
{
// define and initialize the structure to hold the ticket status
TICKET ticket[5] = {
{1, "06:40", 50, 0},
{2, "09:30", 40, 0},
{3, "13:00", 40, 0},
{4, "16:00", 40, 0},
{5, "20:00", 50, 0}
};
int choice;
display ( ticket );
menu();
// check the choice whether it's avalible
while ( scanf("%d",&choice) == 1 ) {
if ( choice == 1 ) sell( ticket );
else if ( choice == 2 ) refund( ticket );
else puts("\nInvalid service.");
menu();
}
save( ticket );
puts("\nThank you!");
system("pause");
return 0;
}
void display( TICKET *ticket )
{
int i;
puts("\nFrequency DepartureTime PassengerCapacity Sold\n");
for ( i = 0; i < 5; ++i )
{
printf("%-9d ",ticket[i].frequency);
printf("%-13s ",ticket[i].departure);
printf("%-17d ",ticket[i].capacity);
printf("%-4d\n",ticket[i].sold);
}
}
void menu( void )
{
puts("\nOptions:");
puts("1 - To book a ticket");
puts("2 - Ticket refund");
puts("x - Exit");
puts("Please enter your choice:");
}
void sell( TICKET *ticket )
{
int number;
struct tm *p_tm;
time_t t;
char strtime[6];
// get the current time
t= time( NULL );
p_tm = localtime( &t );
strftime( strtime, 6, "%H:%M", p_tm );
puts("\nWelcome to book a ticket!");
display( ticket );
printf("\nCurrent time: %s\n",strtime);
puts("Which frequency do you want to book?");
scanf("%d",&number);
while (1) {
// check the frequency whether it's avalible
while ( number < 1 || number > 5 ) {
puts("\nInvalid Frequency!");
puts("Please re-enter:");
scanf("%d",&number);
}
if ( number < 1 || number > 5 ) continue;
// check the time whether it's avalible
while ( strcmp(strtime,ticket[number-1].departure) > 0 ) {
puts("\nThis frequency has passed its departure time.");
puts("It's no longer selling tickets today.");
puts("Please change your ticket:");
scanf("%d",&number);
}
if ( strcmp(strtime,ticket[number-1].departure) > 0 ) continue;
// check the passenger capacity whether it's avalible
while ( ticket[number-1].capacity <= ticket[number-1].sold ) {
puts("\nThis frequency has been sold out.");
puts("Please change your ticket:");
scanf("%d",&number);
}
if ( ticket[number-1].capacity > ticket[number-1].sold ) break;
}
++ticket[number-1].sold;
display( ticket );
}
void refund( TICKET *ticket )
{
int number;
display( ticket );
for ( number = 0; number < 5; ++number ) {
if ( ticket[number].sold != 0 ) {
number = -1;
break;
}
}
if ( number != -1 ) {
puts("\nYou see, we haven't sold even one ticket.");
puts("You should book a ticket first.");
return ;
}
puts("\nWhich frequency do you want to refund?");
scanf("%d",&number);
while (1) {
while ( number < 1 || number > 5 ) {
puts("\nInval
上一个:c语言编程问题,循环+递归
下一个:求C语言运算符的优先级别