c语言设计一个网球场使用程序

题目:网球中心共有100个网球场,每个单位可以来申请1到100的场地,申请的场地编号必须是连续的,如果场地已经被其他单位占用,就不能再次使用,而且单位在使用完场地后必须归还。请设计一个完整的系统(c语言)。(限时5分钟)
Tennis.h

</pre><pre name="code" class="objc">struct TennisGround
{
int num;
char *agentName;
};


typedef struct TennisGround TG;


void mallocTG(TG *total);
void freeTG(TG *total);


Tennis.c

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>


#include "Tennis.h"


void mallocTG(TG *total)
{
int size, start,count = 0;
char *agentName = (char*)malloc(sizeof(char)*10);


printf("Please input your agentName:");
scanf("%s", agentName);
printf("Please input the size of the TennisGround:");
scanf("%d", &size);
printf("Please input the TennisGround number you want to start:");
scanf("%d", &start);
if((total+start)->agentName != " ")
{
printf("malloc failed!\n");
exit(-1);
}
else
{
while(count < size)
{
(total+start+count)->agentName = agentName;
count++;
}
}
}


void freeTG(TG* total)
{
char *an = (char*)malloc(sizeof(char)*10);
printf("please input agentName you want to free:");
scanf("%s", an);
int count = 0;
while(count < 100)
{
if(strcmp((total+count)->agentName, an) == 0)
(total+count)->agentName = " ";
count++;
}
}



int main()
{
int i;
int sw;
TG *total = (TG*)malloc(sizeof(TG)*100);


for(i=0; i<100; i++)
{
(total+i)->num = i; 
(total+i)->agentName = " ";
}


while(1)
{
printf("*******Tennis Ground Mallocation******************\n");
for(i=0; i<100; i++)
{
printf("%d(%s) ", (total+i)->num, (total+i)->agentName);
if(i%5 == 0)
printf("\n");
}
printf("\n");
printf("**************************************************\n");
printf("Please input your choosen:(1-malloc,2-free):");
scanf("%d", &sw);
if(sw == 1)
mallocTG(total);
else
freeTG(total);


}


return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值