实验报告

  • 题目1
  • 题目2

【实验名称】 实验六 数组、指针与字符串
【实验内容】

题目1

改造实验五中的Player类,为每个Player增加种族(门派、或其他类似属性)。在构造函数中,随机指定其种族(自行查阅C++中的随机数生成方法)及其他各项属性。在主函数中生成Player对象数组,并统计每个种族的玩家数量。

Player.h

#ifndef _Player_H_
#define _Player_H_
#include "Player.h"
#include <time.h> using namespace std; enum Sex{male,female}; enum Race{mankind,animal,angle,demon};class Player{ public:Player();Player(string name , string password , int age = 0, Sex sex = male, string e_mail = NULL);static int getPlayerNumber(){ return playerNumber; } //返回当前注册玩家数量string getTime(){ return registerTime; } //返回注册时间void displayInformation(); //显示玩家信息void changeInformation();  //修改玩家信息void play(); //开始游戏static void statistics();//统计玩家种族数量,性别数量private:void setTime();  //设置注册时间static int mankindNumber; //人族玩家数量static int animalNumber; //兽族玩家数量static int angleNumber;  //天使族玩家数量static int demonNumber; //恶魔族玩家数量static int maleNumber;  //男性玩家数量static int femaleNumber; //女性玩家数量static int playerNumber;  //所有注册玩家的数量string registerTime;  //注册时间static int IDgrowth; //ID号的增长int ID;  //唯一玩家ID号int grade;       //等级int experience;  //经验Race race;  //种族string name;   //用户名string password;  //密码int age;      //年龄Sex sex;    //性别string e_mail;    //邮箱 };#endif

Player.cpp

#include<iostream>
#include<stdlib.h>
#include"Player.h"
using namespace std;int Player::playerNumber = 0; //玩家数量
int Player::IDgrowth = 10000; //ID号起始号
int Player::mankindNumber = 0; //人族玩家数量
int Player::animalNumber = 0; //兽族玩家数量
int Player::angleNumber = 0;  //天使族玩家数量
int Player::demonNumber = 0; //恶魔族玩家数量
int Player::maleNumber = 0;  //男性玩家数量
int Player::femaleNumber = 0; //女性玩家数量Player::Player(){IDgrowth = IDgrowth + rand()%11;ID = IDgrowth;int temp1;temp1 = rand()%4;switch(temp1){case 0 :race = mankind;mankindNumber++;break;case 1 :race = animal;animalNumber++;break;case 2 :race = angle;angleNumber++;break;case 3 :race = demon;demonNumber++;break;}int temp2;temp2 = rand()%2;switch(temp2){case 0 :sex = male;maleNumber++;break;case 1 :sex = female;femaleNumber++;break;}grade = 0;experience = 0;setTime();playerNumber++;}Player::Player(string name , string password , int age , Sex sex , string e_mail){this->name = name;this->password = password;this->age = age ;this->sex = sex;this->e_mail = e_mail;IDgrowth = IDgrowth + rand()%11;ID = IDgrowth;grade = 0;experience = 0;setTime();playerNumber++;}void Player::setTime()  //获取当前时间,作为注册时间{time_t timep;time (&timep);char tmp[64];strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) );registerTime = tmp;}/*显示玩家信息
*/void Player::displayInformation(){cout <<ID<< "号玩家信息如下:" <<endl;cout << "玩家姓名:" << name <<endl;cout << "唯一ID号: " << ID <<endl;cout << "注册时间: " << registerTime <<endl;cout << "玩家等级: " << grade <<endl;cout << "玩家目前的经验: " << experience <<endl;cout << "玩家年龄: " << age <<endl;cout << "玩家性别: ";switch(sex){case 0:cout<< "男生" <<endl;break;case 1:cout<< "女生" <<endl;break;}cout << "玩家的邮箱: " << e_mail <<endl;}/*修改玩家信息
*/void Player::changeInformation(){int n = 1;while(n){cout << "请输入您需要修改的信息编号:0、用户名 1、密码: 2、性别 3、邮箱" <<endl;int m;string str;cin >>m;switch(m){case 0:cout<<"请输入新的用户名:"<<endl;cin >>str;name = str;break;break;case 1:cout<<"请输入新的密码:"<<endl;cin >>str;password = str;break;case 2:cout<<"请输入性别:男生 or 女生"<<endl;cin >>str;if(str == "男生") {sex = male;cout <<"性别修改为男生"<<endl;}if(str == "女生") {sex = female;cout <<"性别修改为女生"<<endl;}break;case 3:cout<<"请输入新的邮箱:"<<endl;cin >>str;e_mail = str;cout <<"邮箱修改为:"<< e_mail <<endl;}cout <<"是否要继续修改信息: 0、退出修改  1、继续修改"<<endl;cin>>n;}}/*开始游戏
*/void Player::play(){ //cout << "尊贵的  " <<name<< "  玩家进入游戏" <<endl;}/*统计玩家种族数量,性别数量
*/
void Player::statistics(){cout<<"统计每个种族的玩家数量:"<<endl;cout<<"人族玩家数量为:"<<mankindNumber<<endl;cout<<"兽族玩家数量为:"<< animalNumber <<endl;cout<<"天使族玩家数量为:"<<angleNumber<<endl;cout<<"恶魔族玩家数量为:"<<demonNumber<<endl;cout<<"统计每个性别的玩家数量:"<<endl;cout<<"男性玩家数量为:"<<maleNumber<<endl;cout<<"女性玩家数量为:"<<femaleNumber<<endl;}

Main.cpp

#include<iostream>
#include"Player.h"
#include"Player.cpp"
using namespace std;int main(){srand( (unsigned)time( NULL ) );//srand()函数产生一个以当前时间开始的随机种子.应该放在for等循环语句前面不然要很长时间等待Player player1("大灬白","A12345678",20,male,"qq1234567@qq.com");player1.displayInformation();player1.changeInformation();player1.displayInformation();player1.play();cout<<endl;Player array[1000];cout<<"目前的注册玩家数量为:"<<Player::getPlayerNumber()<<endl;Player::statistics();return 0;
}

【实验结果】

题目2

不使用现成的string以及字符串操作函数,自行设计一个字符串类,类中的字符串使用字符指针进行描述,设计常见的字符串操作函数,在主程序中进行测试,并尽量增强其功能。

SetString.cpp

#include<iostream>
#include<stdio.h>
using namespace std;class SetString{public:SetString(){}SetString(const char *c){setStrcpy(array,c);length = 0;while(this->array[++length]!='\0');}SetString(const char *c,const int length){setStrcpy(array,c);this->length = length;}/*
strcpy(char * , char *)字符串复制函数
*/char* setStrcpy(char * strDest,const char * strSrc){if ((NULL==strDest) || (NULL==strSrc))//抛出异常throw "Invalid argument(s)";char * strDestCopy = strDest;while ((*strDest++=*strSrc++)!='\0');return strDestCopy;}/*
*字符查找函数:查找某个字符在数组中的位置
*/int setFind(const char a,const int start){int i;for(i = start;i<length;i++) {if( array[i] == a ){cout<<"在"<<array<<"中查找"<<a<<"的位置为:"<<i+1<<endl;return i;}}cout<<"所要查找的字符不存在!"<<endl;return -1;}/*字符连接函数*/void setStrcat(SetString &s){int i = 0;int j = 0;//查找字符串1的末尾while(this->array[++i]!='\0');while(s.array[j]!='\0') {this->array[i++] = s.array[j++];}array[i]='\0';cout<<"连接后字符数组中的值为:"<<array<<endl;}void output(){cout<<"输出字符数组的长度:"<<length<<endl;cout<<"输出字符数组中的值:"<<array<<endl;}/*获取子字符串*/
char* setSubstr( int offset, int length)
{int real_length = ((this->length - offset) >= length ? length : (this->length - offset));//判断srcstr的长度减去需要截取的substr开始位置之后,剩下的长度//是否大于指定的长度length,如果大于,就可以取长度为length的子串//否则就把从开始位置剩下的字符串全部返回。char* tmp = array+offset;tmp[real_length] = '\0';cout<<"从"<<array<<"数组的第"<<offset<<"个位置开始"<<"截取"<<length<<"个字符的字符数组为:"<<tmp<<endl;return tmp;
}private://输入的字符数组char array[100];char * str;//数组中字符的长度int length;
};int main(){SetString str1("ABC");//字符串复制SetString str2("a12345678",9);str2.output();//字符查找函数str2.setFind('6',0);//字符连接函数str1.setStrcat(str2);//获取子字符串str2.setSubstr(4,5);return 0;
}

【实验结果】

【小结或讨论】
本次实验是实验六 数组、指针与字符串,第一题主要是伪随机函数的使用,通过使用srand((unsigned)time(NULL))来使随机数列随当前系统时间而变化,之后统计各个种族,每种性别的人数,声明了多个对应的静态变量来记录各个种族、每种性别的人数。第二题不使用现成的string以及字符串操作函数,自行设计一个字符串类,类中的字符串使用字符指针进行描述,设计常见的字符串操作函数。主要设计了字符串复制、字符查找、字符连接、获取子字符串等函数,通过字符指针和字符数组来操纵字符串,实验过程中更加深刻的理解了指针的使用方法,以及对野指针、指针异常处理的了解,指针虽然用起来非常的方便和强大,但使用的时候也要多加小心注意。

C++实验六——数组、指针与字符串相关推荐

  1. 南京邮电大学高级语言程序设计实验五(指针与字符串实验)

    实验题目(1)[见实验教材实验六的题目2]:编程exp6_2.c,现有整型变量x,y,调用交换函数以期实现两个值的交换.下表中4种不同的定义及调用分别进行测试并填写表格. 表1 拟实现交换的四种方法 ...

  2. 【C 语言】二级指针作为输入 ( 指针数组 | 指针数组排序 | 字符串排序 | strcmp 函数 )

    文章目录 一.strcmp 函数 二.指针数组排序 ( 字符串排序 ) 二.完整代码示例 一.strcmp 函数 strcmp 是 String Compare 缩写 , 该函数用于比较两个字符串 ; ...

  3. 【C++编程题1】数组指针之字符串排序

    题目 用数组指针处理50个不等长字符串.写一个函数对字符串按降序排序,然后在主函数输入这50个字符串,调用函数后,再输出这50个已经排好序的字符串. 代码 子函数: #include <iost ...

  4. C 温故知新 之 指针:数组指针、字符串指针、函数指针

    一.数组指针  1.指向数组元素的指针 1.定义:还是那句话通俗的说指针就是地址 数组指针     :数组的起始地址 数组元素指针:数组元素的地址 2.定义一个指向数组元素的指针变量的方法,与之前介绍 ...

  5. 实验六 数组程序设计 → 张玉生《C语言程序设计实训教程》双色版 配套实验书答案 (纯手打, 仅供参考)

    实验8.1 //取出正整数中的偶数字,并用这些数字构成一个最大数 #include <stdio.h> #define N 10int main() {int i, j, k = 0, t ...

  6. 实验8.2 指针与字符串 7-2 字符串排序

    本题要求编写程序,读入5个字符串,按由小到大的顺序输出. 输入格式: 输入为由空格分隔的5个非空字符串,每个字符串不包括空格.制表符.换行符等空白字符,长度小于80. 输出格式: 按照以下格式输出排序 ...

  7. 实验内容:实验六 数组冒泡排序

    学习目标: 提示:这里可以添加学习目标 例如: 1.通过本实验使学生掌握数组的基本含义,数组申明,赋值和引用 2.通过本实验使学生掌握数组与循环结构配合程序设计 3.通过本实验使学生掌握类数组的程序设 ...

  8. 实验六 数组 (2)

    输入n个整数,存入数组a中,分别按正序和逆序输出这些数 #include<stdio.h> int main() {int i,m,max;int count[9];for(i=0;i&l ...

  9. 实验8.2 指针与字符串 6-2 删除字符

    本题要求实现一个删除字符串中的指定字符的简单函数. 函数接口定义: void delchar( char *str, char c ); 其中char *str是传入的字符串,c是待删除的字符.函数d ...

最新文章

  1. 各种 Optimizer 梯度下降优化算法回顾和总结
  2. 如何编写一份软件工程实验报告
  3. win32 masm32 汇编学习 及 远程线程实例
  4. c语言课后答案第八章,C++第一至第八章课后习题答案
  5. Apache OpenOffice 下载量突破一亿次
  6. 前端学习(3311):redux的state hook对象
  7. ios 查看同文件名_实战恢复cisco 2950交换机的IOS
  8. HTTP请求的GET与POST方式的区别
  9. ngx-echarts 图表数据动态更新
  10. 将JavaScript函数作为参数传递
  11. 驱动开发——经典图书免费试读下载及勘误讨论
  12. L3-001 凑零钱 (30 分)—团体程序设计天梯赛
  13. 诺基亚再做手机,没有机会
  14. 【nmap+masscan】各自优缺点,快速上手综合使用
  15. ionic ion-refresher 下拉刷新的使用。
  16. 公司新加了一台友宝自动售货机引发的思考-适配器模式
  17. 2022年博士招生 | 华南理工大学-鹏城实验室 联培博士 专项计划
  18. vue鼠标悬停更改图片
  19. GO语言开发高性能网络通信服务
  20. 无法从商店下载Linux系统,解决win8应用商店不能安装软件并提示“此应用无法安装”的方法...

热门文章

  1. php yii路由规则,Yii2 路由基本配置
  2. Python杀死进程操作
  3. 齐岳定制ZnCuInS/ZnSe/ZnS量子点,CuInS2-ZnS/ZnSe/ZnS核壳结构半导体量子点,CdTe/CdS/ZnS/SiO2多层核壳量子点
  4. hashmap移除元素_JAVA中HashMap如何删除元素
  5. 这操作真香!Android高级工程师每日面试题精选,面试必备
  6. IIS配置后打开网页显示空白--解决方法及原因分析
  7. HTML表格中字体居中
  8. java实现word中嵌入附件
  9. CCNA考完,总结下经验
  10. maven插件-及插件的使用