C++按要求随机生成数据并写入文件
题目要求:
(1) 用名称、人口、海拔高度、天气、年份等数据成员建立一个名为City的类。建立一个产生City对象的类。
(2)将产生的City对象(数量大于200个)填充至一个容器,容器的类型自选。
(3)对于City对象的具体属性值通过创建发生器来生成。生成规则如下:年份为2006年;名称由4-8个英文小写字符随机构成;人口在范围[100000,10000000)内随机选取;海拔高度在范围[0,4000)米内随机选取;上述三值均不可重复;天气在枚举常量表中{Rainy,Snowy,Cloudy,Sunny}随机选取(1年天气取12个值,即每月一个值)。
(4)容器填充完毕后,将其内容写入一个名为City.txt的文件。
代码实现如下:
#include<iostream> #include<vector> #include<string> #include<time.h> #include<algorithm> using namespace std; //天气在枚举常量中表示 enum Enum_weather { Rainy=0,Snowy,Cloudy,Sunny }; //创建城市类 class City { public: int year; //char *name; string name; int persons; int elevation; char *weather[12]; City() { //neme=new char[10]; } ~City(){} }; //随机生成字符串函数 char *rand_str(char *str) { int i,n; n=rand()%5; for(i=0;i<n+4;++i) { str[i]='a'+rand()%26; } str[++i]='\\0'; return str; } //枚举类型转化为char* static inline char* weather_str(enum Enum_weather w) { char *strings[] = {"Rainy", "Snowy", "Cloudy", "Sunny",}; return strings[w]; } int main() { srand((unsigned)time(NULL)); vector<City>ve(215); for (int i=0;i<215;i++) //将随机获取每个城市的数据写入vector中 { ve[i].year=2006; ve[i].persons=rand()%(10000000 - 100000 + 1) + 100000; //在某范围内随机获取数值 ve[i].elevation=rand()%(4000-0+1)+0; char name1[10]={}; rand_str(name1); //调用rand_str()函数随机生成城市名称字符串 ve[i].name=name1; for (int j=0;j<12;j++) { int n=rand()%4; Enum_weather w1=(enum Enum_weather)(n); char* nc=weather_str(w1); //调用weather_str()函数将枚举类型值转化为char* ve[i].weather[j]=nc; } } //将2006年各个城市的数据信息写入City.txt文本文件中 FILE *fp; fp=fopen("D://City.txt","w+"); fprintf(fp,"年份 城市名称 城市人口 海拔高度 1月天气 2月天气 3月天气 4月天气 5月天气 6月天气 7月天气 8月天气 9月天气 10月天气 11月天气 12月天气\n"); for (int i=0;i<215;i++) { char name2[10]={}; for (int k=0;k<ve[i].name.size();k++) //string类型转char数组 { name2[k]=ve[i].name[k]; } fprintf(fp,"%d%10s%10d%10d",ve[i].year,name2,ve[i].persons,ve[i].elevation); for (int j=0;j<12;j++) { fprintf(fp,"%11s",ve[i].weather[j]); } fprintf(fp,"\n"); } fclose(fp); }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
【Java入门提高篇】Day19 Java容器类详解(二)Map接口
上一篇里介绍了容器家族里的大族长——Collection接口,今天来看看容器家族里的二族长——Map接口。 Map也是容器家族的一个大分支,但里面的元素都是以键值对(key-value)的形式存放的,就像字典一样,用相应的key就可以拿到相应的value。 先来看看Map接口的内容,下面是阉割版的Map接口(去掉了defaultmethod),去掉的部分涉及Stream操作,属于Map的高级用法,所以暂时不做介绍。 import java.io.Serializable; import java.util.Collection; import java.util.Comparator; import java.util.Objects; import java.util.Set; public interface Map<K,V> { // 查询操作 /** * 返回键值对数量 */ int size(); /** * Map是否为空 */ boolean isEmpty(); /** * Map中是否包含指定的key */ boolean containsKey(Obje...
- 下一篇
Eclipse插件开发_异常_01_java.lang.RuntimeException: No application id has be...
一、异常现象 在运行RCP程序时,出现 java.lang.RuntimeException: No application id has been found. at org.eclipse.equinox.internal.app.EclipseAppContainer.startDefaultApp(EclipseAppContainer.java:242) at org.eclipse.equinox.internal.app.MainApplicationLauncher.run(MainApplicationLauncher.java:29) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104) at org.eclip...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合Redis,开启缓存,提高访问速度
- MySQL8.0.19开启GTID主从同步CentOS8
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS8编译安装MySQL8.0.19
- CentOS8安装Docker,最新的服务器搭配容器使用