车位情况弄一个随机数(车辆停放)模拟实况(变化要慢),
同时要求可以查看停放车辆的颜色,车牌号等信息
这个要怎么弄
该回答引用chatgpt:
#include <stdio.h>
#include <stdlib.h> // 包含随机数生成相关的函数
#include <time.h> // 包含时间函数
#define MAX_PLATE_NUMBER_LENGTH 10
#define MAX_COLOR_LENGTH 10
struct Car {
char plate_number[MAX_PLATE_NUMBER_LENGTH]; // 车牌号
char color[MAX_COLOR_LENGTH]; // 车辆颜色
};
struct ParkingLot {
int total_spaces; // 总停车位数
int occupied_spaces; // 已停车位数
struct Car cars[10]; // 停放的车辆数组,假设停车位最多为10个
};
void print_car_info(struct Car car) {
printf("车牌号: %s\n", car.plate_number);
printf("颜色: %s\n", car.color);
}
void park_car(struct ParkingLot* parking_lot, struct Car car) {
if (parking_lot->occupied_spaces >= parking_lot->total_spaces) {
printf("停车位已满,无法停车!\n");
return;
}
parking_lot->cars[parking_lot->occupied_spaces] = car;
parking_lot->occupied_spaces++;
printf("车辆已停放在第%d个停车位\n", parking_lot->occupied_spaces);
}
void query_car_info(struct ParkingLot parking_lot, char* plate_number) {
int i;
for (i = 0; i < parking_lot.occupied_spaces; i++) {
if (strcmp(parking_lot.cars[i].plate_number, plate_number) == 0) {
print_car_info(parking_lot.cars[i]);
return;
}
}
printf("没有找到该车辆\n");
}
int main() {
srand(time(NULL)); // 使用当前时间作为随机数种子
struct ParkingLot parking_lot;
parking_lot.total_spaces = 10;
parking_lot.occupied_spaces = 0;
int i;
for (i = 0; i < parking_lot.total_spaces; i++) {
int r = rand() % 3; // 随机生成0-2之间的整数,表示停车位状态,0-空车位,1-小汽车,2-大卡车
if (r == 1) {
struct Car car;
sprintf(car.plate_number, "京A%04d", i+1); // 生成类似于“京A0001”的车牌号
sprintf(car.color, "红色"); // 假设所有停放的车辆颜色都是红色
park_car(&parking_lot, car);
}
}
query_car_info(parking_lot, "京A0002");
return 0;
}
本人实际经验仅供参考
以下是程序的运行结果
以下为程序代码
/*
程序功能:猜数游戏,计算机自动随机生成一个数,‘人’通过输入数字来猜数,来与随机数进行匹配
只有十次输入机会,在十次机会中,猜对即可打印出数字并且显示输入次数。
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int magic;/计算机想的数/
int guess;/人猜的数/
int counter;/记录人猜数的次数的计数器变量/
srand((unsigned)time(NULL));
magic = rand() % 100 + 1;/*计算机想一个【1-100】之间的数,即为magic */
counter = 0;/*计数器初始化为0次 */
do {
printf(“please guess a magic number:\n”);
scanf_s("%d", &guess);
counter++;/计数器变量的值/
if (guess > magic)
{
printf(“wrong!too high!\n”);
}
else if (guess < magic)
{
printf(“wrong!too low!\n”);
}
else
{
printf(“right!\n”);
printf(“the right number is:%d\n”, magic);
}
}while (guess != magic && counter<10);
printf(“counter= %d\n”, counter);
system(“pause”);
return 0;
}