本文目錄一覽:
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;
}
原創文章,作者:DDWI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/134584.html