本文目錄一覽:
- 1、C語言編程:請編寫一個程序,定義一個轎車銷售結構體類型,
- 2、c語言編程 汽車租賃問題
- 3、使用c語言在方法中編寫汽車減速,每次遞減10分鐘
- 4、C語言編程:定義一個汽車結構體?
- 5、C語言編程:按題目要求從鍵盤輸入乘車的起步價及行車公里數,輸出車費?
C語言編程:請編寫一個程序,定義一個轎車銷售結構體類型,
enum Color //定義顏色
{
RED,
GREEN,
COIN,
WHITE
};
typedef struct Car//定義轎車
{
char name[20];
char shape;
Color color;
int salesPrice;
}Car;
c語言編程 汽車租賃問題
親測可以運行 自己的課程設計 搞得很累 代碼如下我的課程設計 汽車租賃系統c語言
#includestdio.h
#includestdlib.h
#includestring.h
#define MaxNum 20
typedef struct A{
int No; /*車輛編號*/
char Type; /*車類型*/
int Payment;/*租費*/
int fine; /*罰金*/
struct A *next;/*指向下一個結點*/
} car;
typedef struct B{
int No; /*顧客編號*/
char Name[20]; /*顧客姓名*/
char License; /*執照類別*/
int carNo; /*租憑的車輛編號*/
int Day; /*租的天數*/
int DelayDay;/*延遲的天數*/
struct B *next;
} client;
struct C{/*這個結構體是車輛鏈表的頭結點,A,B,C每種類型有一種*/
char Type;/*車輛型號*/
int avl; /*可用數*/
car *head;/*指向車輛結點*/
} headNode[3]={{‘A’,MaxNum,NULL},{‘B’,MaxNum,NULL},{‘C’,MaxNum,NULL}} ;
client *allClien=NULL;
int pay[3]={400,300,200},fine[3]={600,500,400};
void init();/*初始化*/
int menu();/*簡單菜單界面*/
void search();/*查詢*/
void carSc(); /*查詢車輛*/
void clientSc();/*查詢顧客*/
void rent(); /*租車*/
void giveback();/*還車*/
void addCli(client *cli);/*向顧客鏈表增加顧客*/
client* delCli(int clientNo);/*從顧客鏈表刪除一個顧客*/
void addCar(char carType,int carNo);
car* delCar(char type);
void Exit();/*退出*/
void main()
{
init();
while(1)
{
switch(menu())
{
case 1:search();break;
case 2:rent();break;
case 3:giveback();break;
case 4:Exit();
default:;
}
}
}
void init()
{
int i=0;
car *ptr,*pa,*pb,*pc;
headNode[0].head=NULL,headNode[1].head=NULL,headNode[2].head=NULL;
ptr=(car *)malloc(sizeof(car));
ptr-No=100;
ptr-Type=’A’;
ptr-Payment=400;
ptr-fine=600;
headNode[0].head=ptr;
pa=ptr;
pa-next=NULL;
for( i=1;i20;i++){
ptr=(car *)malloc(sizeof(car));
ptr-No=100+i;
ptr-Type=’A’;
ptr-Payment=400;
ptr-fine=600;
pa-next=ptr;
pa=ptr;
pa-next=NULL;
}
free(ptr);
ptr=(car *)malloc(sizeof(car));
ptr-No=200;
ptr-Type=’B’;
ptr-Payment=300;
ptr-fine=500;
headNode[1].head=ptr;
pb=ptr;
pb-next=NULL;
for( i=1;i20;i++){
ptr=(car *)malloc(sizeof(car));
ptr-No=200+i;
ptr-Type=’A’;
ptr-Payment=400;
ptr-fine=600;
pb-next=ptr;
pb=ptr;
pb-next=NULL;
}
free(ptr);
ptr=(car *)malloc(sizeof(car));
ptr-No=300;
ptr-Type=’C’;
ptr-Payment=200;
ptr-fine=400;
headNode[2].head=ptr;
pc=ptr;
pc-next=NULL;
for( i=1;i20;i++){
ptr=(car *)malloc(sizeof(car));
ptr-No=300+i;
ptr-Type=’A’;
ptr-Payment=400;
ptr-fine=600;
pc-next=ptr;
pc=ptr;
pc-next=NULL;
}
free(ptr);
}
int menu()
{
int choice;
printf(“\n\n\n選擇服務:1.查詢 2.租車 3.歸還 4.退出\n”);
scanf(“%d”,choice);
while(choice!=1choice!=2choice!=3choice!=4)
{
printf(“\n輸入有誤,重新輸入:”);
scanf(“%d”,choice);
}
return choice;
}
void search()
{
int choice;
printf(“\n你想查詢:1.汽車 2.顧客 3.返回 \n”);
scanf(“%d”,choice);
while(choice!=1choice!=2choice!=3)
{
printf(“\n輸入有誤,重新輸入:”);
scanf(“%d”,choice);
}
switch(choice)
{
case 1:carSc(); break;
case 2:clientSc(); break;
case 3: ;
default:;
}
}
void carSc()
{
printf(“\n\n所有汽車信息:\n”);
printf(“\nA類汽車還剩%d輛.\nB類汽車還剩%d輛.\nC類汽車還剩%d輛.”,
headNode[0].avl,headNode[1].avl,headNode[2].avl);
}
void clientSc()
{
client *ptr=allClien;
printf(“\n\n所有顧客信息:\n”);
while(ptr!=NULL)
{ printf(“\n\n顧客編號:%d”,ptr-No);
printf(“\n顧客姓名:%s”,ptr-Name);
printf(“\n駕照類型:%c”,ptr-License);
printf(“\n租賃車號:%d”,ptr-carNo);
printf(“\n租賃天數:%d”,ptr-Day);
printf(“\n延遲天數:%d”,ptr-DelayDay);
ptr=ptr-next;
}
}
void addCli(client *cli)
{
if(allClien==NULL)
allClien=cli;
else
{
cli-next=allClien;
allClien=cli;
}
}
client* delCli(int clientNo)
{
client *ptr,*prePtr;;
ptr=allClien;
while(ptr!=NULLptr-No!=clientNo)
{ prePtr=ptr;
ptr=ptr-next;
}
if(ptr!=NULL)
{
if(ptr==allClien)
{
allClien=NULL;
}
else
{
prePtr-next=ptr-next;
}
}
return ptr;
}
void rent()
{
char name[20],type,Yes_No;
int num,day,No;
car *carPtr;
client *cli;
printf(“\n\n輸入執照類型(A/B/C):”);
scanf(“%c”,type);
while(type!=’A’type!=’B’type!=’C’)
{
printf(“輸入有誤,重新輸入:”);
scanf(“%c”,type);
}
if(type==’A’)
num=headNode[0].avl;
else if(type==’B’)
num=headNode[1].avl;
else
num=headNode[2].avl;
printf(“\n%c類汽車還剩%d輛,是否要租憑(Y/N):”,type,num);
scanf(“%c”,Yes_No);
while(Yes_No!=’Y’Yes_No!=’N’Yes_No!=’y’Yes_No!=’n’)
{
printf(“Y或N:”);
scanf(“%c”,Yes_No);
}
/*增加顧客*/
if(Yes_No==’Y’||Yes_No==’y’)
{
printf(“\n輸入你的名字:”);
scanf(“%s”,name);
printf(“\n輸入你的租賃天數:”);
scanf(“%d”,day);
}
No=rand()%60+200;
carPtr=delCar(type);
cli=(client *)malloc(sizeof(client));
cli-No=No;
strcpy(cli-Name,name);
cli-License=type;
cli-carNo=carPtr-No;
cli-Day=day;
cli-DelayDay=0;
cli-next=NULL;
addCli(cli);
/*移出一輛車*/
printf(“\n你的顧客編號是:%d”,No);
printf(“\n你所租賃的汽車是%c類車,車號是:%d”,type,carPtr-No);
printf(“\n你的租賃天數是%d天.”,day);
}
void giveback()
{
int No;
long int payment;
client *ptr;
printf(“\n\n顧客編號:”);
scanf(“%d”,No);
if((ptr=delCli(No))==NULL)
printf(“\n該顧客不存在,無法歸還!”);
else
{
switch(ptr-License)
{
case 1:payment=ptr-Day*400+ptr-DelayDay*600;break;
case 2:payment=ptr-Day*300+ptr-DelayDay*500;break;
case 3:payment=ptr-Day*200+ptr-DelayDay*400;break;
default:;
}
printf(“\n\n顧客姓名:%s”,ptr-Name);
printf(“\n駕照類型:%c”,ptr-License);
printf(“\n租賃車號:%d”,ptr-carNo);
printf(“\n租賃天數:%d”,ptr-Day);
printf(“\n延遲天數:%d”,ptr-DelayDay);
printf(“\n\n所需費用:%ld”,payment);
addCar(ptr-License,ptr-carNo);
free(ptr);
}
}
void addCar(char carType,int carNo)
{
car *ptr;
int index=carType-65;
ptr=headNode[index].head;
if(ptr==NULL)
{ptr=(car *)malloc(sizeof(car));
headNode[index].head=ptr;
headNode[index].avl++;
}
else
{while(ptr-next)
ptr=ptr-next;
ptr-next=(car *)malloc(sizeof(car));
ptr=ptr-next;
headNode[index].avl++;
}
ptr-No=carNo;
ptr-Type=carType;
ptr-Payment= pay[index];
ptr-fine=fine[index];
ptr-next=NULL;
}
car* delCar(char type)
{
car *rentcar;
car *pp;
switch(type)
{
case ‘A’:
rentcar=headNode[0].head;
headNode[0].head=rentcar-next;
headNode[0].avl–;
break;
case ‘B’:rentcar=headNode[1].head;
headNode[1].head=rentcar-next;
headNode[1].avl–;
break;
case ‘C’:rentcar=headNode[2].head;
headNode[2].head=rentcar-next;
headNode[2].avl–;
break;
default:;
}
return rentcar;
}
void Exit()
{
printf(“\n歡迎使用…..888888888886666….”);
exit(0);
}
使用c語言在方法中編寫汽車減速,每次遞減10分鐘
你好,意思是不是速度每次遞減10啊,我用循環語句,使速度每次遞減10,直至速度為0。把源程序複製一下
#includestdio.h
#includestdlib.h
#includestring.h
int main()
{
int speed;
printf(“please input the speed:\n”);
scanf(“%d”,speed);
printf(“begin slow down…\n”);
while(speed=0)
{
printf(“now the speed is:%d\n”,speed);
speed=speed-10;
}
printf(“now the speed is:0(the car stop)\n”);
return 0;
}
運行結果如下,如果有什麼不明白的還可以問我
C語言編程:定義一個汽車結構體?
按照題目要求編寫的C語言汽車結構體程序如下
#includestdio.h
struct car{
float petrol;
}c1={0};
struct car refuel(struct car c){
c.petrol=c.petrol+2;
return c;
}
int main(){
int i;
for(i=0;i5;i++){
c1=refuel(c1);
printf(“%.2f\n”,c1.petrol);
}
return 0;
}
C語言編程:按題目要求從鍵盤輸入乘車的起步價及行車公里數,輸出車費?
按照題目要求編寫的C語言程序如下
#includestdio.h
int main(){
int price,kilometre;
float payment;
printf(“請輸入起步價:”);
scanf(“%d”,price);
printf(“請輸入行車公里數:”);
scanf(“%d”,kilometre);
if(price==4){
payment=4+(kilometre-3)*1.2;
printf(“應付車資為%.2f。”,payment);
}else if(price==5){
payment=5+(kilometre-3)*1.5;
printf(“應付車資為%.2f。”,payment);
}else if(price==6){
payment=6+(kilometre-3)*1.8;
printf(“應付車資為%.2f。”,payment);
}else{
printf(“無此起步價。”);
}
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/182106.html