白云点缀的蓝 发表于 2021-10-25 18:15

对现实世界是如何产生的模拟

对世界如何产生的很感兴趣,所以简单写了个模拟世界产生的程序


















所有代码


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#define MAX 100//最大房子,人,车数
#define COLORNUM 7 //颜色数量
#define SEXNUM 2 //性别数量
#define NUM 20 //分配内存字节数
CRITICAL_SECTION cs;
char* WorldState[] = { "存在","消失","存在与消失中间" };//世界的三种状态
intObjectProbability[] = { 50 };//物体进入状态的概率
char *ColorData = { "红","橙","黄","绿","青","蓝","紫" };//世界颜色数据

//性别
struct Sex
{
      char *name;
}Sex;

//颜色
structColor
{
      char *name;
}Color;


//房子
struct House
{
      char *color;
      char *high;
      char *wide;
}House;


//人
structPerson
{
      char *name;
      char *sex;
}Person;


//车
struct Car
{
      char *name;
      char *color;
}Car;

//初始化性别
void initializeSex() {
      Sex.name = malloc(NUM);
      Sex.name = malloc(NUM);
      memcpy(Sex.name, "男", NUM);
      memcpy(Sex.name, "女", NUM);
}

//初始化颜色
void initializeColor() {
      int i = 0;
      while (i<7)
      {
                Color.name = malloc(NUM);
                Color.name = ColorData;
                i++;
      }

}

//初始化世界数据
int initializeData() {
      int i = 1;
      int j = 0;
      char* buf[] = { "汽车","颜色","名字","性别","高","宽" };
      char* buf1 = malloc(NUM);
      char* buf2 = malloc(NUM);
      char* buf3 = malloc(NUM);
      char* buf4 = malloc(NUM);
      char* buf5 = malloc(NUM);
      char* buf0 = malloc(NUM);
      while (i <= 100)
      {
                Car.name = malloc(NUM);
                Car.color = malloc(NUM);
                Person.name = malloc(NUM);
                Person.sex = malloc(NUM);
                House.color = malloc(NUM);
                House.high = malloc(NUM);
                House.wide = malloc(NUM);
                sprintf(buf0, "%s:%d", buf, i);//汽车
                sprintf(buf1, "%s:%s", buf, Color.name);//颜色
                sprintf(buf2, "%s:%d", buf, i);//名字
                sprintf(buf3, "%s:%s", buf, Sex.name);//性别
                sprintf(buf4, "%s:%d", buf, (rand() % 100)+5);//高
                sprintf(buf5, "%s:%d", buf, (rand() % 100)+5);//宽
                memcpy(Car.name, buf0, NUM);
                memcpy(Car.color, buf1, NUM);
                memcpy(Person.name, buf2, NUM);
                memcpy(Person.sex, buf3, NUM);
                memcpy(House.color, buf1, NUM);
                memcpy(House.high, buf4, NUM);
                memcpy(House.wide, buf5, NUM);
                i++;
      }
      /*i = 0;
      while (i<100)
      {
                printf("%s\n",Car.name);
                printf("%s\n", Car.color);
                printf("%s\n", Person.name);
                printf("%s\n", Person.sex);
                printf("%s\n", House.color);
                printf("%s\n", House.high);
                printf("%s\n", House.wide);
                i++;
      }*/
}

//初始化所有数据
void initialize() {
      srand((unsigned int)NULL);//置随机数种子
      initializeSex();//初始化性别
      initializeColor();//初始化颜色
      initializeData();//初始化世界数据
      InitializeCriticalSection(&cs);
}

//出现跑车
void WINAPI showCar() {
      int i = 0;
      while (1)
      {
                Sleep(1000);
                //printf("%d", rand() % 100);
                if ((rand() % 100) > ObjectProbability) {
                        EnterCriticalSection(&cs);
                        printf("一量跑车进入%s状态\t", WorldState);
                        int CarNum = rand() % 100;
                        printf("%s\t", Car.name);
                        printf("%s\t", Car.color);
                        i++;
                        printf("跑车离开视野,进入%s状态\t\n", WorldState);
                        LeaveCriticalSection(&cs);
                        printf("\n");

                }
         
      }
}

//出现房子
void WINAPI showHouse() {
      int i = 0;
      while (1)
      {
                Sleep(1000);
                //printf("%d", rand() % 100);
                if ((rand() % 100) > ObjectProbability) {
                        EnterCriticalSection(&cs);
                        printf("一座靓丽的房子进入%s状态\t", WorldState);
                        int HouseNum = rand() % 100;
                        printf("%s\t", House.high);
                        printf("%s\t", House.wide);
                        printf("%s\t", House.color);
                        i++;
                        printf("靓丽的房子离开视野,进入%s状态\t\n", WorldState);
                        LeaveCriticalSection(&cs);
                        printf("\n");

                }

      }
}

//出现人
void WINAPI showPerson() {
      int i = 0;
      while (1)
      {
                Sleep(1000);
                //printf("%d", rand() % 100);
                if ((rand() % 100) > ObjectProbability) {
                        EnterCriticalSection(&cs);

                        printf("一位靓仔进入%s状态\t", WorldState);
                        int PersonNum = rand() % 100;
                        printf("%s\t", Person.name);
                        printf("%s\t", Person.sex);
                        i++;
                        printf("靓仔离开视野,进入%s状态\t\n", WorldState);
                        LeaveCriticalSection(&cs);
                        printf("\n");
                }

      }
}

//开始
int begin() {
      DWORD ThreadID;
         
      CreateThread(NULL, 0, showCar, (LPVOID)"Car", 0, &ThreadID);//创建线程
      CreateThread(NULL, 0, showPerson, (LPVOID)"Person", 0, &ThreadID);//创建线程
      CreateThread(NULL, 0, showHouse, (LPVOID)"House", 0, &ThreadID);//创建线程

      while (1);
}
int main() {
      initialize();
      begin();
}

头文件的声明


#pragma once
int begin();
void initialize();
int initializeData();
void initializeColor();
void initializeSex();

初始化世界数据


//初始化世界数据
int initializeData() {
      int i = 1;
      int j = 0;
      char* buf[] = { "汽车","颜色","名字","性别","高","宽" };
      char* buf1 = malloc(NUM);
      char* buf2 = malloc(NUM);
      char* buf3 = malloc(NUM);
      char* buf4 = malloc(NUM);
      char* buf5 = malloc(NUM);
      char* buf0 = malloc(NUM);
      while (i <= 100)
      {
                Car.name = malloc(NUM);
                Car.color = malloc(NUM);
                Person.name = malloc(NUM);
                Person.sex = malloc(NUM);
                House.color = malloc(NUM);
                House.high = malloc(NUM);
                House.wide = malloc(NUM);
                sprintf(buf0, "%s:%d", buf, i);//汽车
                sprintf(buf1, "%s:%s", buf, Color.name);//颜色
                sprintf(buf2, "%s:%d", buf, i);//名字
                sprintf(buf3, "%s:%s", buf, Sex.name);//性别
                sprintf(buf4, "%s:%d", buf, (rand() % 100)+5);//高
                sprintf(buf5, "%s:%d", buf, (rand() % 100)+5);//宽
                memcpy(Car.name, buf0, NUM);
                memcpy(Car.color, buf1, NUM);
                memcpy(Person.name, buf2, NUM);
                memcpy(Person.sex, buf3, NUM);
                memcpy(House.color, buf1, NUM);
                memcpy(House.high, buf4, NUM);
                memcpy(House.wide, buf5, NUM);
                i++;
      }
      /*i = 0;
      while (i<100)
      {
                printf("%s\n",Car.name);
                printf("%s\n", Car.color);
                printf("%s\n", Person.name);
                printf("%s\n", Person.sex);
                printf("%s\n", House.color);
                printf("%s\n", House.high);
                printf("%s\n", House.wide);
                i++;
      }*/
}

初始化所有数据


//初始化所有数据
void initialize() {
      srand((unsigned int)NULL);//置随机数种子
      initializeSex();//初始化性别
      initializeColor();//初始化颜色
      initializeData();//初始化世界数据
      InitializeCriticalSection(&cs);
}

出现跑车


//出现跑车
void WINAPI showCar() {
      int i = 0;
      while (1)
      {
                Sleep(1000);
                //printf("%d", rand() % 100);
                if ((rand() % 100) > ObjectProbability) {
                        EnterCriticalSection(&cs);
                        printf("一量跑车进入%s状态\t", WorldState);
                        int CarNum = rand() % 100;
                        printf("%s\t", Car.name);
                        printf("%s\t", Car.color);
                        i++;
                        printf("跑车离开视野,进入%s状态\t\n", WorldState);
                        LeaveCriticalSection(&cs);
                        printf("\n");

                }
         
      }
}

出现房子


//出现房子
void WINAPI showHouse() {
      int i = 0;
      while (1)
      {
                Sleep(1000);
                //printf("%d", rand() % 100);
                if ((rand() % 100) > ObjectProbability) {
                        EnterCriticalSection(&cs);
                        printf("一座靓丽的房子进入%s状态\t", WorldState);
                        int HouseNum = rand() % 100;
                        printf("%s\t", House.high);
                        printf("%s\t", House.wide);
                        printf("%s\t", House.color);
                        i++;
                        printf("靓丽的房子离开视野,进入%s状态\t\n", WorldState);
                        LeaveCriticalSection(&cs);
                        printf("\n");

                }

      }
}

出现人


//出现人
void WINAPI showPerson() {
      int i = 0;
      while (1)
      {
                Sleep(1000);
                //printf("%d", rand() % 100);
                if ((rand() % 100) > ObjectProbability) {
                        EnterCriticalSection(&cs);

                        printf("一位靓仔进入%s状态\t", WorldState);
                        int PersonNum = rand() % 100;
                        printf("%s\t", Person.name);
                        printf("%s\t", Person.sex);
                        i++;
                        printf("靓仔离开视野,进入%s状态\t\n", WorldState);
                        LeaveCriticalSection(&cs);
                        printf("\n");
                }

      }
}





康帅傅 发表于 2021-10-26 10:25

看下图(不回复看不了){:5_116:}

pp2021 发表于 2021-10-27 10:20

你这个只要开着就一直在刷屏啊,那些刷屏聊天的不会也是像你这么弄的吧?{:5_117:}

甜甜向小鸭子 发表于 2022-1-22 20:09

感谢楼主

虚幻和母鸡 发表于 2022-1-22 20:12

谢谢啊,滴水之恩涌泉相报~

oYmfLF8 发表于 2022-1-29 12:49

谢了啊,啥也不说了,记心里了!

gEL1730 发表于 2022-1-29 12:49

帮顶一下

花生730498 发表于 2022-1-30 14:24

大佬无敌

yRAEnGO34 发表于 2022-1-31 19:30

不知道来晚了没有

fMb42 发表于 2022-2-2 02:43

谢谢分享
页: [1] 2 3 4 5 6 7
查看完整版本: 对现实世界是如何产生的模拟