本文目录一览:
c语言程序课程设计2.1【问题描述】 设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。
#include stdio.h
#include stdlib.h
#include malloc.h
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int SElemType;
typedef int Status;
typedef int CarStack;
typedef int LinkQueue;
typedef int InputData;
// definition of array based stack
#define STACK_INIT_SIZE 100 //Initial size for memory allocation
#define STACKINCREMENT 10 //incremental size
typedef struct{
SElemType *base; //base pointer
SElemType *top; //top pointer
CarStack *garage;
CarStack *Temp;
LinkQueue *road;
InputData *inputdata
int stacksize; //current size
}SqStack;
status PopStack(CarStack *garage, CarStack *Temp, LinkQueue *road, InputData *inputdata)//车出库
{
CarNode *Car = (CarNode *)malloc(sizeof(CarNode));
if (!Car)
{
cout "内存分配失败!" endl;
exit(0);
}
Car-num = inputdata-num;
Car-leave = inputdata-t;
int i = garage-top;
while (i)
{
if (garage-stack[i -1 ]-num != Car-num)
{
garage-top--;//进临时车站
Temp-stack[Temp-top] = garage-stack[garage-top];
Temp-top++;
i--;//没有找到,继续循环查找
if (i == 0)
{
cout "没有该车牌的车!" endl;
}
}
else//获得车辆信息,并回归临时车栈的车辆
{
Car-reach = garage-stack[i -1 ]-reach;
Print(Car);//打印车辆收费单
garage-top--;
while (Temp-top0)//回归临时车站的车辆
{
Temp-top--;
garage-stack[garage-top] = Temp-stack[Temp-top];
garage-top++;
}
if (road-head != road-rear)//从便道出来一辆车到停车场
{
garage-stack[garage-top] = road-head-next;
cout endl;
cout "车场有空位,便道第一辆车可以进入!" endl;
garage-top++;
road-head = road-head-next;
}
i = 0;//已经找到,退出循环
}
}
}
void InfoStack(CarStack *garage)//车库车辆信息
{
if (garage-top ==0 )
cout "车库里没有车!" endl;
else
{
cout " _________________________ " endl;
cout "| |" endl;
cout "| 车 库 |" endl;
cout "|位置 车辆号码 到达时间|" endl;
for (int i = 0; igarage-top; i++)
{
cout "| " i +1 " " garage-stack[i]-num " " garage-stack[i]-reach " |" endl;
}
cout "|_________________________|" endl;
}
}
void InfoQueue(LinkQueue *road)//便道车辆信息
{
CarNode *p;
p = road-head-next;
int i = 0;
if (road-head == road-rear)
cout "便道里没有车!" endl;
else
{
cout " __________________ " endl;
cout "| |" endl;
cout "| 便道 |" endl;
cout "| 位置 车辆号码 |" endl;
while (p != NULL)
{
cout "| " ++i " " p-num " |" endl;
p = p-next;
}
cout "|__________________|" endl;
}
free(p);
}
void Print(CarNode *Car)//打印出站车辆信息
{
int rh, lh;
float t;
rh = Car-reach;
lh = Car-leave;
t = (lh - rh) * 60 *0.01 ;
cout " ---------------------------------------------------- " endl;
cout "| 车辆的号码 进库时间 离开时间 车费(元) |" endl;
cout "| " Car-num " " rh " " lh " " t " |" endl;
cout "|____________________________________________________|" endl;
}
void Input(InputData *inputdata)
{
int maxsize;
cout "请输入停车场容量 Maxsize (最多为5) : ";
do
{
cin maxsize;
if (!(maxsize5 maxsize0))
{
cout "输入有误,请重新输入停车场容量:";
}
} while (!(maxsize5 maxsize0));
int i = 0, j =0 , k =1 ;
char c;
int n;
CarStack garage, Temp;
LinkQueue road;
CarNode car;
InitStack(garage, maxsize);
InitStack(Temp, maxsize);
InitQueue(road);
while (k)
{
i = 0;
while (!i)
{
cout " ____________________________________________________________" endl;
cout "| |" endl;
cout "| 欢迎光临停车场,本停车场每分钟收费元 |" endl;
cout "| |" endl;
cout "| 请输入车库命令,格式如下(命令,车牌号,入库时间) |" endl;
cout "| |" endl;
cout "| A(a)-入库 D(d)-离开 P(p)-查看停车场车辆 P(p)-查看过道车辆 |" endl;
cout "|____________________________________________________________|" endl;
cin (inputdata-command) (inputdata-num) (inputdata-t);
c = inputdata-command;
n = inputdata-num;
if (c != 'A'c != 'a'c != 'D'c != 'd'c != 'W'c != 'w'c != 'P'c != 'p'c != 'E'c != 'e')
{
cout "命令不正确,请重新输入!" endl;
i =0 ;
}
else
i = 1;
// if(SearchInGarage(garage,n,maxsize)SearchInRoad(road,n))
// {
// i =0 ;
// cout"命令不正确,请重新输入!"endl;
// }
}
switch (c)
{
case 'A':
case 'a':
GetCarNode(garage, road, inputdata, maxsize);//获取车辆信息
break;
case 'D':
case 'd':
PopStack(garage, Temp, road, inputdata);//车出库
break;
case 'P':
case 'p'://查询停车场的信息
InfoStack(garage);//车库车辆信息
break;
case 'W':
case 'w'://查询侯车场的信息
InfoQueue(road);//便道车辆信息
break;
case 'E':
case 'e':
k =0 ;
break;
default:
break;
}
}
cout endl;
cin.clear();
}
bool SearchInGarage(CarStack garage, int n, int maxsize)//a表示要查找的车牌号,如果在停车场里面,就返回true
{
bool tag = false;
for (int i =0 ; imaxsize || (i != garage.top); i++)
{
if (n == garage.stack[i]-num)
{
tag = true;
break;
}
}
return tag;
}
bool SearchInRoad(LinkQueue road, int n)//a表示要查找的车牌号,如果在通道里面,就返回true
{
bool tag = false;
StackNode *p;
p = (StackNode *)malloc(sizeof(StackNode));
if (road.head != road.rear)//如果队列非空
{
p = road.head;
while (p != road.rear)
{
++p;
if (p-num == n)
tag = true;
}//退出此while循环时p指向最后一个元素
}
free(p);
return tag;
}
专业课c语言程序设计有没有推荐的网课?
推荐浙江大学C语言程序设计翁恺老师,在中国大学慕课上可以观看。
学习C语言,必需注意每一个细节,书上的例子代码一定要自己亲自敲一遍,编译执行输出都跟书上说的一致才能算是学完了一个例子,如果不一致,就要仔细找原因。出了书本上有的例子,自己还要“创造”一些例子,比如学习运算符优先级的时候,可以写几个相同的表达式,在不同的位置加上括号,看看有哪些不同的行为。
比如*p++和(*p)++,又比如a = b == c、(a = b) == c和a = (b == c)等等。自己抄的书上的例子以及改造之后的例子,还有自己“创造”的例子,都应该仔细地归类保存,并且要在源代码中写上简短的注释,阐述这个例子的意图。
例子之后就是习题了,我建议初学者把所有的习题都独立做一遍,然后对照答案的代码,看看自己的代码有那些不足,再试着修改自己的代码。很多人不重视习题,这是极大的错误,因为作者通常会在习题中说明一些重要的道理,而不是单纯地检验前面的知识。
c语言程序课程设计
给你代码,你自己去研究吧
#include stdio.h
#define SIZE 10
typedef struct student
{
int num;
char name[20];
int score[3];
float average;
}student;
void indata(student *stu)
{
int i;
for (i = 0; i SIZE; i++)
{
scanf("%d %s %d %d %d%*c", stu[i].num, stu[i].name, stu[i].score[0],
stu[i].score[1], stu[i].score[2]);
}
}
void outdata(student *stu)
{
int i;
for (i = 0; i SIZE; i++)
{
printf("%d %s %d %d %d %f\n", stu[i].num, stu[i].name, stu[i].score[0],
stu[i].score[1], stu[i].score[2], stu[i].average);
}
}
int main()
{
student stu[SIZE];
int i, j;
int t;
student temp;
printf("请输入信息:\n");
indata(stu);
for (i = 0; i SIZE; i++) //算平均分
{
stu[i].average = 0;
for (j = 0; j 3; j++)
stu[i].average += stu[i].score[j];
stu[i].average /= 3;
}
for (i = 0; i SIZE - 1; i++) //按平均分进行排序
{
t = i;
for (j = i + 1; j SIZE; j++)
{
if (stu[t].average stu[j].average)
t = j;
}
if (t == i)
continue;
else
{
temp = stu[i];
stu[i] = stu[t];
stu[t] = temp;
}
}
printf("排序后信息为:\n");
outdata(stu);
return 0;
}