求大神用动态数组函数和构造函数帮忙改改此程序,谢谢了!

求大神帮忙,程序和错误已经发出来了,求改正,谢谢_arduino吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:40,187贴子:
求大神帮忙,程序和错误已经发出来了,求改正,谢谢收藏
#include &mousePress.h&#include &mouseWheel.h&//左键,右键,连击,暂停的引脚const byte LEFT_BUTTON = 5;const byte RIGHT_BUTTON = 4;const byte CLICK = 6;const byte END = 10;//连击速度int clickSpeedo = 200;MousePress myMouseLeft( LEFT_BUTTON, 1 );MousePress myMouseRight( RIGHT_BUTTON, 2 );MouseWheel mywheel(9,8);/**-------------------使用ADXL345制作体感控制鼠标-----------------**/#include &Wire.h&/*------------------------ADXL345寄存器地址----------------------*/#define DEVID
//只读==291#define OFSX
0x1E//B#define OFSY
0x1F//B#define BW_RATE
//设置为B#define POWER_CTL
//设置为B#define DATA_FORMAT
//设置为B#define DATAX0
//只读#define DATAX1
//只读#define DATAY0
//只读#define DATAY1
//只读#define DATAZ0
//只读#define DATAZ1
//只读#define ADXAddressR
0xA7/*----------------------控制鼠标指针方向和加速度---------------*/boolean up,down,left,byte x=0,y=0,xVal=0,yPos=0,wheel=0;int x345,y345,x345old,y345old,
value345X = 40,value345Y = 40, //倾斜阈值,请调这里,用来控制鼠标飘,太大会比较死,太小会飘
speedo345X = 12,speedo345Y = 12;//鼠标速度,请调这里int inclineX = 0, inclineY = 0,
speedoX,speedoY,//用来控制Mouse.move()函数XY值,
speedoxVal,speedoyPos,
//incline是用ADXL345的倾斜度
speedoPWMX,speedoPWMY,
PWMGOX, PWMSTOPX,
PWMGOY, PWMSTOPY;
//加速度后来让我给放弃了,因为效果不理想
//int accelerationX = 1,accelerationY = 1,//越大,速度越快//
accelerationValue = 4;//加速度阈值//boolean valueAcceleration =
//控制acceleration产生加速度//unsigned long ctrlAccelerationTime = 0;
//用来控制acceleration的时间变量,用来给鼠标产生加速度boolean valuePWMX = true,valuePWMY =
//PWM控制鼠标速度的一个控制变量,用来切换鼠标“走”和“停”unsigned long ctrlMouseTimeX = 0,ctrlMouseTimeY = 0;
//PWM控制鼠标速度的一个控制变量unsigned long startTime = 0,
clickTime = 0;void setup() {
Wire.begin();
//初始化I2C
Serial.begin( 9600 );
Mouse.begin();
Keyboard.begin();
WireWrite( ADXAddressR, DATA_FORMAT,B );
WireWrite( ADXAddressR, POWER_CTL,B );
WireWrite( ADXAddressR, BW_RATE,B );
WireWrite( ADXAddressR, OFSX,B );//27左手 48右手
WireWrite( ADXAddressR, OFSY,B );//0左手 226右手
ctrlMouseTimeX = micros();
ctrlMouseTimeY = micros();
//ctrlAccelerationTime = millis();
clickTime = millis();
startTime = millis();
pinMode( END, INPUT_PULLUP );
pinMode( CLICK, INPUT_PULLUP );}void loop() {
if( micros() & -10000 ){//防止micros()溢出
ctrlMouseTimeX = micros();
ctrlMouseTimeY = micros();
Serial.println(&reset&);
if( !digitalRead(END) ){//END接地,airmouse失效
Mouse.end();
delay(1000);
}else{//----------读取ADXL345的XY值----------------//
y345 = -WireRead2( ADXAddressR, DATAX0,DATAX1)*speedo345X/10;
x345 = WireRead2( ADXAddressR, DATAY0,DATAY1)*speedo345Y/10; //以下的算法可以很好解决漂移严重的问题
if(abs(x345)&value345X){
if(x345&0)
x345-=value345X;
if(x345&0)
x345+=value345X;
if(abs(y345)&value345Y){
if(y345&0)
y345+=value345Y;
if(y345&0)
y345-=value345Y;
-----------------//左手和右手校准后,这里会不太一样,//有一个方向会更灵敏,所以另一个方向要*1.5,请调试这里
if(x345&0)
x345=x345*15/10;
y345=y345*12/10;
speedoX = constrain(pow(abs(x345)-value345X,2)/100,0,12700);
//我调整了算法,把阈值减去了,感觉好用多了,没有那么飘了
speedoY = constrain(pow(abs(y345)-value345Y,2)/100,0,12700);
//通过speedo函数将倾角计算成xval ypos 和PWM
************************* //------------判断上下左右---------------------//
if( x345 & 0 ){
x = -speedoxV
}else if( x345 & 0 ){
x = speedoxV
if( y345 & 0 ){
y = -speedoyP
}else if( y345 & 0 ){
y = speedoyP
//左键右键连击
myMouseLeft.press_mouse();
myMouseRight.press_mouse();
wheel = mywheel.read_wheel();
if( millis() - clickTime & clickSpeedo ){
if(!digitalRead(CLICK))
Mouse.click();
clickTime = millis();
////----------串口监视器显示ADXL345的XY值----------------//
if( millis() - startTime & 200 ){
Serial.print( &
Serial.print( x345 );
Serial.print( &
Serial.println( y345 );
Serial.print( &
speedoX = & );
Serial.print( speedoX );
Serial.print( &
speedoY = & );
Serial.println( speedoY );//
Serial.print( &
accelerationX = & );//
Serial.print( accelerationX );//
Serial.print( &
accelerationY = & );//
Serial.println( accelerationY );
startTime = millis();
}//------------PWM控制鼠标速度----------------//
if( (micros() - ctrlMouseTimeX &= PWMSTOPX*80)//“停”
&& valuePWMX == true){
ctrlMouseTimeX = micros();
valuePWMX = !valuePWMX;
}else if ( (micros() - ctrlMouseTimeX &= PWMGOX *80)//“走”
&& valuePWMX == false ){
ctrlMouseTimeX = micros();
valuePWMX = !valuePWMX;
if( (micros() - ctrlMouseTimeY &= PWMSTOPY*80) //“停”
&& valuePWMY == true){
ctrlMouseTimeY = micros();
valuePWMY = !valuePWMY;
}else if ( (micros() - ctrlMouseTimeY &= PWMGOY *80)//“走”
&& valuePWMY == false ){
ctrlMouseTimeY = micros();
valuePWMY = !valuePWMY;
}//----------最后是控制鼠标的函数---------//
Mouse.move( xVal ,yPos ,wheel );//------------计算加速度----------//
if( millis() - ctrlAccelerationTime & 10){ //
if( valueAcceleration ){//每隔10毫秒记录一次old//
x345old = x345;//
y345old = y345;//
valueAcceleration = !valueA//
}else if ( valueAcceleration == false ){//每隔10毫秒计算一次加速度//
accelerationX = abs(x345 - x345old);//
accelerationY = abs(y345 - y345old);////
if( accelerationX &2 )////
accelerationX = 2;////
if( accelerationY &2 )////
accelerationY = 2;////
if( accelerationX &5)////
accelerationX = 5;////
if( accelerationY &5)////
accelerationY = 5;//
valueAcceleration = !valueA//
ctrlAccelerationTime = millis();//
}}出现的错误:air_mouse.ino: In function 'void setup()':air_mouse.ino:55:3: error: 'Mouse' was not declared in this scopeair_mouse.ino:56:3: error: 'Keyboard' was not declared in this scopeair_mouse.ino: In function 'void loop()':air_mouse.ino:78:5: error: 'Mouse' was not declared in this scopeair_mouse.ino:132:7: error: 'Mouse' was not declared in this scopeair_mouse.ino:178:3: error: 'Mouse' was not declared in this scope
惠普暗影精灵II代游戏本---精灵绿版灵动来袭!
求助了,自顶!d=====( ̄▽ ̄*)b
看错误是mouse和keypad变量没声明啊
int mouse。。。。mouse有click事件,怎么可能是int类型。应该是mouse类型的。。
求大神帮忙啊,就差一步了
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或求c++大神,输入一个数自定义数组大小,出现错误求帮忙改写,用动态分配内存空间!!程序如下,_百度知道
求c++大神,输入一个数自定义数组大小,出现错误求帮忙改写,用动态分配内存空间!!程序如下,
p[j]=t;&i&;& } free(p);&a[N]=&&t'i&lt.h&gt:&quot#include&endl,j; int *p;&&lt,a[];endl&;&&#92,m; p=a,t;please input N;k+m;& & p=(int a[N]*)malloc(sizeof(int a[N])) for(i=0;j++)
if(p[i]&endl& } cout&&k+m;k;&&&p[i]&&p[j])
p[i]=t;&& cin&endl&&k=& cout&i++) {
for(j=k+i;&&; cin&i++) {
cin&;N,k;&&&&lt.h&& cin&
p[i]=a[j];p[i];j&&i++) {
cout&endl&lt:&&#include&& cout&m=&'i&please input a[N];N=& cout&
} } cout&lt:& '&&gt,m; cout& for(i=0;&&; for(i=k;'m;N;endl&void main(){ int N;N
提问者采纳
cin&& '&i++)
for(j=k+i;&lt,m;k,计算分配空间(int类型*个数)a[]数组没有存在必要;using namespace std,t;\endl&lt,直接用p就可以(指针指向数组)#include&
cout& cout&
cout&lt。动态分配可以用&&& t'&i++)
cout&N=&&k=&&
for(i=k; for(i=0:&你的数组在初始化地方没有加封号;&endl&&#include &N;&k+m;N;i&;p[j])
t=p[i];&gt,m;k+m;m=&
p[i]=p[j];please input N;&N;&&
cin&p[i];&;&&lt.h&
cout&lt,也可以用new排序的地方貌似赋值反了分配空间是p=(int *)malloc(sizeof(int)*N);
cout&&j++)
if(p[i]&endl&&&m:&'&#include&
p=(int *)malloc(sizeof(int)*N);endl&&lt,k;&lt.h&&iostream&j&
for(i=0,j;&;;p[i]&
cout&endl&a[N]=&i&&&'please input a[N];&
int *p;void main(){
int N:&quot
您这个最后没有交换数据。。。求帮助。。。
for(j=k;j&k+m;j++)
for(i=k;i&N-k-m;i++)
if (p[i]&p[i+1])
p[i]=p[i+1];
} 排序貌似有问题,我再想想
求解救啊啊啊啊啊!!!
for(j=k;j&k+m;j++)
for(i=k;i&k+m;i++)
if (p[i]&p[i+1])
p[i]=p[i+1];
}这个是排序算法,第K位置开始的M个数排序。是对的
提问者评价
解决啦,辛苦您了
其他类似问题
为您推荐:
动态分配内存的相关知识
其他2条回答
,不知道你的程序都要干什么,我只能告诉你正确的分配空间的方法是。。:p=(int *)malloc(N*sizeof(int));其他的地方我真的没法帮你了如果你的数组指针是p
其实主要问题就是不会动态定义数组大小
分配空间就是我上面那句话了,楼下那位也把程序给你改好了。。。。
楼下那位程序好像有点小问题。。。求帮忙!
排序的问题if(p[i]&p[j])
p[i]=p[j];
没用。。。
我给你改成这样了,你再试试吧。#include&iostream&int main(){
cout&&&please input N:&&&endl&&&N=&;
cout&&endl&&&please input a[N]:&&&
int i,k,m,j,t;
p=(int *)malloc(N*sizeof(int));
for(i=0; i&N; i++)
cout&&&a[&&&i&&&]=&;
cin&&p[i];
cout&&endl&&&please input k,m:&&&endl&&&k=&;
cout&&'\t'&&&m=&;
for(i=k;i&k+m;i++)
for(j=k+i;j&k+m;j++)
if(p[i]&p[j])
p[i]=p[j];
for(i=0;i&N;i++)
cout&&p[i]&&' ';
#include&iostream&
int main()
cout&&&please input N:&&&endl&&&N=&;
cout&&endl&&&please input a[N]:&&&
int i,k,m,j,t,*a;
p=(int *)malloc(N*sizeof(int));
for(i=0; i&N; i++)
cout&&&a[&&&i&&&]=&;
cin&&p[i];
cout&&endl&&&please input k,m:&&&endl&&&k=&;
cout&&'\t'&&&m=&;
for(i=k;i&k+m;i++)
for(j=k+i;j&k+m;j++)
if(p[i]&p[j])
p[i]=a[j];
这个好像达不到最终目的。。
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁tianya23 的BLOG
用户名:tianya23
文章数:863
评论数:63
访问量:1703322
注册日期:
阅读量:5863
阅读量:12276
阅读量:317643
阅读量:1029962
51CTO推荐博文
今天在和瑾华讨论ASM的时候,通过将反射的method转换为asm的method时遇到反射时数组的问题。当时两个人都卡住了,现在实验通了,顺手记录下来。
public&class&Person&implements&China&{&&&&&protected&String&&&&&&&public&Person(String&name)&{&&&&&&&&&this.name&=&&&&&&}&&&&&public&Person(String&name1[],String&name2)&{&&&&&}&&&&&&public&String&sayHello(String&name)&{&&&&&&&&&return&&welcome&&&+&&&&&&}&&&&&&protected&void&protect()&{&&&&&&&&&privatedMethod();&&&&&}&&&&&&private&void&privatedMethod()&{&&&&&&}&&}&
通过反射取得构造函数, 通常的方法为:
@Test&&&&&public&void&test04()&{&&&&&&&&&try&{&&&&&&&&&&&&&Class&?&&clazz&=&(Class&?&)&Class&&&&&&&&&&&&&&&&&&&&&.forName(&com.alibaba.demoeo.reflect.classes.Person&);&&&&&&&&&&&&&Constructor&?&[]&constructs&=&clazz.getConstructors();&&&&&&&&&&&&&for&(Constructor&?&&construct&:&constructs)&{&&&&&&&&&&&&&&&&&System.out.println(construct.getName());&&&&&&&&&&&&&}&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&}&&&&&}&
如果要取得特定参数的构造函数,则为:
@Test&&&&&public&void&test07()&{&&&&&&&&&try&{&&&&&&&&&&&&&Class&?&&clazz&=&(Class&?&)&Class&&&&&&&&&&&&&.forName(&com.alibaba.demoeo.reflect.classes.Person&);&&&&&&&&&&&&&Constructor&?&&construct&=&clazz.getConstructor(String.class);&&&&&&&&&&&&&System.out.println(construct);&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&}&&&&&}&
问题引出:而要取得含有数组的参数的构造函数如何办呢? 这也是我们卡住的原因,实验了clazz.getConstructor(String.class, String.class); clazz.getConstructor(Array.class, String.class);等都不行, 后来通过反射机制将参数类型输出进行查看,发现数组为:class [Ljava.lang.S
@Test&public&void&test05()&{&&&&&try&{&&&&&&&&&Class&?&&clazz&=&(Class&?&)&Class&&&&&&&&&.forName(&com.alibaba.demoeo.reflect.classes.Person&);&&&&&&&&&Constructor&?&[]&constructs&=&clazz.getConstructors();&&&&&&&&&&&&&&&&&&for&(Constructor&?&&construct&:&constructs)&{&&&&&&&&&&&&&Class&?&[]&types&=&construct.getParameterTypes();&&&&&&&&&&&&&for(Class&?&&type&:&types){&&&&&&&&&&&&&&&&&System.out.println(type);&&&&&&&&&&&&&}&&&&&&&&&&&&&System.out.println(&-------------&);&&&&&&&&&}&&&&&}&catch&(Exception&e)&{&&&&&&&&&e.printStackTrace();&&&&&}&}&
class&java.lang.String&-------------&class&[Ljava.lang.S&class&java.lang.String&-------------&
好的,现在知道是什么内容了,那就使用这种类型来构造:
@Test&&&&&public&void&test06()&{&&&&&&&&&try&{&&&&&&&&&&&&&Class&?&&clazz&=&(Class&?&)&Class&&&&&&&&&&&&&.forName(&com.alibaba.demoeo.reflect.classes.Person&);&&&&&&&&&&&&&Constructor&?&&construct&=&clazz.getConstructor(Class.forName((&[Ljava.lang.S&)),&String.class);&&&&&&&&&&&&&System.out.println(construct);&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&}&&&&&}&
测试结果正确,输出如下:
public&com.alibaba.demoeo.reflect.classes.Person(java.lang.String[],java.lang.String)&
考虑getConstructor的参数为可变数组,于是考虑通过传递数组类型给getConstructor方法,经测试后通过,具体如下:
@Test&&&&&public&void&test08()&{&&&&&&&&&try&{&&&&&&&&&&&&&Class&?&&clazz&=&(Class&?&)&Class&&&&&&&&&&&&&.forName(&com.alibaba.demoeo.reflect.classes.Person&);&&&&&&&&&&&&&Constructor&?&&construct&=&clazz.getConstructor(new&Class[]{String[].class,String.class});&&&&&&&&&&&&&System.out.println(construct);&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&e.printStackTrace();&&&&&&&&&}&&&&&}&
使用此方法测试ASM转换:
public&class&Demo&{&&&&&public&void&test(String[]&str){}&&&&&&&&&&&&public&static&void&main(String[]&args)&throws&Exception{&&&&&&&&&java.lang.reflect.Method&method&=&Demo.class.getMethod(&test&,&Class.forName(&[Ljava.lang.S&));&&&&&&&&&&&&&&&&&&&&&&&&&&&com.alibaba.mons.Method&asmMethod&=&com.alibaba.mons.Method.getMethod(method);&&&&&&&&&System.out.println(asmMethod.getDescriptor());&&&&&}&}&
public&class&Demo&{&&&&&public&void&test(String[]&str){}&&&&&&&&&&&&public&static&void&main(String[]&args)&throws&Exception{&&&&&&&&&&&&&&&&&&java.lang.reflect.Method&method&=&Demo.class.getMethod(&test&,&new&Class[]{String[].class});&&&&&&&&&&&&&&&&&&com.alibaba.mons.Method&asmMethod&=&com.alibaba.mons.Method.getMethod(method);&&&&&&&&&System.out.println(asmMethod.getDescriptor());&&&&&}&}&
测试结果:
([Ljava/lang/S)V&
1、今天讨论之后,也可以使用String[].class表达字符串数组也是可以的
了这篇文章
类别:┆阅读(0)┆评论(0)求大神指教,按电路图帮忙改一下【频率计】程序,谢谢!_c语言吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:456,756贴子:
求大神指教,按电路图帮忙改一下【频率计】程序,谢谢!收藏
#include &reg52.h&u unsigned int count1;sbit
LCD_RS=P1^0;
LCD_RW=P1^1;sbit
LCD_E=P1^2; #define LCD_DB
P0unsigned char character[10]={0};unsigned char character_1[]={&fre= Hz&};unsigned char FLAG = 0;void LCD_init(void);void LCD_write_command(unsigned char command); void LCD_write_data(unsigned char dat);
void LCD_disp_char(unsigned char x,unsigned char y,unsigned char dat);void delay_n40us(unsigned int n);
void timer_init();
void delay_n40us(unsigned int n){
for(i=n;i&0;i--)for(j=0;j&2;j++);
} void delay_1s(){unsigned int i,j;for(i = 0;i&100;i++)for(j = 0;j&1000;j++);}
void LCD_init(void) {LCD_write_command(0x38);LCD_write_command(0x38);LCD_write_command(0x38);LCD_write_command(0x0c);LCD_write_command(0x06);LCD_write_command(0x01);
delay_n40us(100);} void LCD_write_command(unsigned char dat)
{LCD_DB=LCD_RS=0; LCD_RW=0; LCD_E=1; LCD_E=0;delay_n40us(1); } void LCD_write_data(unsigned char dat){LCD_DB=LCD_RS=1; LCD_RW=0; LCD_E=1; LCD_E=0;delay_n40us(1);}void LCD_disp_char(unsigned char x,unsigned char y,unsigned char dat)//显示一个字符 {uif(y==1)
address=0x80+x; else
address=0xc0+x; LCD_write_command(address);LCD_write_data(dat);}void LCD_disp_num(unsigned char x,unsigned char y,unsigned char dat){uif(y==1)
address=0x80+x; else
address=0xc0+x;LCD_write_command(address);LCD_write_data(dat+48); void dis_num(void){unsigned char i=0,j=0,k=0;LCD_write_command(0x01);character[0] = fre/1000000;character[1] = fre/;character[2] = fre/10000%10;character[3] = fre/1000%10;character[4] = fre/100%10;character[5] = fre/10%10;character[6] = fre%10;
character[7] = 'H';character[8] = 'z';for(i = 0;i&4;i++) { LCD_disp_char(i+0,1,character_1[i]);}for(i = 0;i&10;i++) {if(character[i]!=0)} k = 10-i-2;for(j = 0;j&k;j++){LCD_disp_num(4+j,1,character[i++]);}
for(i = 5;i&7;i++){ LCD_disp_char(j+4,1,character_1[i]);j++;}}void main(){ LCD_init();timer_init();
for(i = 0;i&4;i++){ LCD_disp_char(i+0,1,character_1[i]);}while(1){dis_num();
delay_1s(); } }void timer_init(void)
{ TMOD=0x66;
TL0=0;TR0=1;
TL1=0;TR1=1;
RCAP2H=()/256; RCAP2L=()%256; TH2=RCAP2H;
TL2=RCAP2L;ET2=1;
}void timer2(void) interrupt 5 {time++;TF2=0;
if (time==16)
fre=(long)count*256+TL1;
if(fre&2000)
fre = (long)count1*256+TL0;FLAG = 1;
}} void timer0(void) interrupt 1
{count++;}void timer1(void) interrupt 3{count1++;}
有没有大神帮帮忙啊?谢谢!
不能沉啊,看到的帮帮忙
毕业设计?
这个程序没有错误吧
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或14:30 提问
C++课程设计
求大神帮忙写下构造函数和析构函数
1.网格世界类
网格中每个元素存放各种生物对象的指针或者为空。模拟过程中,我们需要移动生物,还有繁殖和饥饿(死亡),所以在网格类中,我们可以将一只生物放到网格中;可以读取网格中的某个指定位置的生物,获取它的指针,以便在每一个time step中对它进行处理(移动、繁殖和饥饿死亡)。在一个time step中,网格中每只生物都要处理一遍,先狮蚁,后蚂蚁。另外,应该有一个显示网格的成员函数。
2.有机生物类
生物要能够放到网格中,所以每一只生物都有相关的函数。可以是构造函数,即构造生物的时候,同时指明放到网格的哪一个位置上。
有Move函数,Breed函数,Starve函数(是否饿死)。这些函数在派生类中有派生类自己的实现。所以,在有机生物类中,这些函数应该是纯虚函数,有机生物类是一个抽象类。
网格世界类中,从某个网格位置读取生物并处理时,需要知道是哪一种类型的生物(是狮蚁还是蚂蚁),所以,有机生物类里应该考虑如何返回生物的类型。简单的办法是,用不同的常量来表示狮蚁和蚂蚁。例如,用整数1表示狮蚁,2表示蚂蚁。在有机生物类中定义纯虚函数GetType,返回生物的具体类型。
实现Move函数,Breed函数,Starve函数和GetType函数。
实现Move函数,Breed函数,Starve函数和GetType函数。
(二)细化:
1.网格世界类:
(1)属性。数据结构:网格世界中如何存放蚂蚁或狮蚁对象?
20*20的网格,用一个20*20的二维数组来表示,数组元素是什么类型?
(2)方法。网格世界类应该有哪些成员函数?
构造函数:初始化网格。
析构函数:释放掉所有生物。
Set函数:指定x,y位置(二维数组的下标),以及一只生物的地址(指针),将这只生物存入网格中。
Get函数:获取网格中x,y坐标上的生物的地址(指针)。
OneStep函数:在一个time step中,网格中每只生物都要处理一遍,包括移动、繁殖和饥饿。首先,把网格中的每一只生物在本轮是否移动过的标记moved先都标记为假(为什么?);其次,移动,先狮蚁,后蚂蚁,同时把该生物的moved标记为真;再次,把饥饿的狮蚁消灭掉;最后,繁殖。
Display函数:显示函数,显示网格中的具体情况。每个网格,如果是空,显示”.”;如果是蚂蚁,显示”o”;如果是狮蚁,显示”x”。
2.有机生物类
要记录每只生物的一些基本属性:属于哪个网格对象,具体在哪个位置上(x,y坐标),本轮是否移动过。另外,为了记录是否到了繁殖时间,要有一个跟踪器,即记录上次繁殖之后,又经历了多少次time step。
构造函数:
带参数的构造函数:指定网格对象和x、y位置,把构造的生物放到网格中。
析构函数:
Move函数,Breed函数,Starve函数和GetType函数为纯虚函数。
不需要再添加属性。
构造函数:
带参数的构造函数:指定网格对象和x、y位置,把构造的蚂蚁放到网格中。
析构函数:
Move函数:随机选择一个方向,看是否能移动,否则保持在原位置。
Breed函数:繁殖跟踪器+1.如果是3的倍数,就需要繁殖,同时跟踪器清零。
Starve函数:本模拟中蚂蚁不会饿死,所以仅返回false值即可。
GetType函数:返回蚂蚁的类型标记。
狮蚁会饿死,需要添加一个属性,跟踪记录狮蚁饥饿了多少次time step。
构造函数:
带参数的构造函数:指定网格对象和x、y位置,把构造的狮蚁放到网格中。
析构函数:
Move函数:若有相邻蚂蚁,移动到单元网格,吃掉蚂蚁;否则,随机选择一个方向,看是否能移动,不能移动则保持在原位置。
Breed函数:繁殖跟踪器+1,如果是8的倍数,就需要繁殖,同时跟踪器清零。
Starve函数:饥饿跟踪器+1,如果是3的倍数,则饥饿并死亡,从网格中拿掉该狮蚁,同时跟踪器清零。
GetType函数:返回狮蚁的类型标记。
(一)所有涉及的常量定义为类型常量。如:
const int ANTBREED = 3;
//蚂蚁的繁殖周期为3个time steps
const int DOODLEBREED = 8; //狮蚁的繁殖周期为8个time steps
初始化网格世界时,用的狮蚁只数和蚂蚁只数,分别为5只和100只,也定义为类型常量。如:
const int INITIALANTS = 100;
const int INITIALBUGS = 5;
按赞数排序
你是第三个在这里问这个的了。以前的问题找一找就有答案
#ifndef __SOURCE_H__
#define __SOURCE_H__
struct Step
enum AnimalType
IsDoodlebug
class Cell
Cell(int x0, int y0);
Cell *nearbyCell(int dir);
bool in(Animal *animal0);
Animal *leave();
class Animal
protected:
int maxBreedT
int breedT
int maxStarveT
int starveT
virtual ~Animal();
public://virtual
virtual Animal *breedChild() = 0;
virtual bool canEat(AnimalType animal) = 0;
virtual bool eat() = 0;
bool breed();
bool move();
void step();
class Ant :public Animal
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
class Doodlebug :public Animal
Doodlebug() ;
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
Step Steps[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
Animal *DoodleB
#endif //__SOURCE_H__
SOURCE.CPP
#include &stdio.h&
#include &stdlib.h&
#include "Source.h"
//========================================
Cell::Cell(int x0, int y0)
this-&x = x0;
this-&y = y0;
this-&animal = NULL;
Cell *Cell::nearbyCell(int dir)
int x0 = this-&x + Steps[dir].
int y0 = this-&y + Steps[dir].
if (x0 & 0 || y0 & 0 || x0 &= maxWidth || y0 &= maxHeight)
return NULL;
return Status[x0][y0];
bool Cell::in(Animal *animal0)
if (this-&animal != NULL)
animal0-&cell =
this-&animal = animal0;
Animal *Cell::leave()
Animal *theAnimal = this-&
theAnimal-&cell = NULL;
this-&animal = NULL;
return theA
//========================================
Animal::Animal()
this-&cell = NULL;
this-&breedTime = 0;
this-&starveTime = 0;
this-&prior = NULL;
this-&next = NULL;
bool Animal::breed()
if (this-&breedTime &= maxBreedTime)
for (int i = 0; i & 4; i++)
Cell *c = this-&cell-&nearbyCell(i);
if (c != NULL && c-&animal == NULL)
c-&in(this-&breedChild());
this-&breedTime = 0;
bool Animal::move()
int dir = rand() % 4;
Cell *c = this-&cell-&nearbyCell(dir);
if (c == NULL)
else if (c-&animal == NULL)
c-&in(this-&cell-&leave());
void Animal::step()
bool dosth =
this-&breedTime++;
this-&starveTime++;
dosth |= this-&breed();
dosth |= this-&eat();
if (!dosth)
this-&move();
if (this-&maxStarveTime & 0 && this-&starveTime &= this-&maxStarveTime)
this-&cell-&leave();
delete (this);
Animal::~Animal()
Animal *t = this-&
this-&prior-&next =
if (t != NULL)
t-&prior = this-&
//========================================
Ant::Ant() : Animal()
this-&type = IsA
this-&maxStarveTime = -1;
this-&maxBreedTime = 3;
if (Ants != NULL)
this-&prior = A
this-&next = Ants-&
if (Ants-&next != NULL) Ants-&next-&prior =
Ants-&next =
Animal *Ant::breedChild()
return new Ant();
bool Ant::canEat(AnimalType t)
bool Ant::eat()
//========================================
// Doodlebug
Doodlebug::Doodlebug() :Animal()
this-&type = IsD
this-&maxStarveTime = 3;
this-&maxBreedTime = 8;
if (DoodleBugs != NULL)
this-&prior = DoodleB
this-&next = DoodleBugs-&
if (DoodleBugs-&next != NULL) DoodleBugs-&next-&prior =
DoodleBugs-&next =
Doodlebug::breedChild()
return new Doodlebug();
Doodlebug::canEat(AnimalType t)
if (t == IsAnt)
Doodlebug::eat()
for (int i = 0; i & 4; i++)
Cell *c = this-&cell-&nearbyCell(i);
if (c!=NULL && c-&animal != NULL && this-&canEat(c-&animal-&type))
Animal *theAnimal = c-&leave();
delete(theAnimal);
this-&starveTime = 0;
//========================================
void randomSet(Animal *animal0)
x = rand() % maxW
y = rand() % maxH
while (Status[x][y]-&animal != NULL);
Status[x][y]-&in(animal0);
printoutHead()
printf("+");
for (int i = 0; i & maxW i++)
printf("=");
printf("+\n");
void printoutDetail(int r)
printf("|");
for (int i = 0; i & maxW i++)
if (Status[i][r]-&animal == NULL)
printf(" ");
switch (Status[i][r]-&animal-&type)
case IsAnt:
printf("O");
case IsDoodlebug:
printf("X");
printf("?");
printf("|\n");
void printout()
printoutHead();
for (int i = 0; i & maxH i++)
printoutDetail(i);
printoutHead();
void main()
int nDoodleB
printf("请输入区域宽度:");
scanf("%d", &maxWidth);
printf("请输入区域高度:");
scanf("%d", &maxHeight);
printf("请输入初始狮蚁数量:");
scanf("%d", &nDoodleBug);
printf("请输入初始蚂蚁数量:");
scanf("%d", &nAnt);
//maxWidth = 3;
//maxHeight = 4;
//nDoodleBug = 2;
//nAnt = 5;
Status = new Cell**[maxWidth];
DoodleBugs = new Doodlebug();
Ants = new Ant();
for (int i = 0; i & maxW i++)
Status[i] = new Cell*[maxHeight];
for (int j = 0; j & maxH j++)
Status[i][j] = new Cell(i, j);
for (int i = 0; i & nDoodleB i++)
randomSet(new Doodlebug());
for (int i = 0; i & nA i++)
randomSet(new Ant());
printout();
system("pause");
while (true)
Animal *a = DoodleBugs-&
Animal *a0;
for ( a0 = (a == NULL ? NULL : a-&next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a-&next))
a-&step();
a = Ants-&
for ( a0 = (a == NULL ? NULL : a-&next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a-&next))
a-&step();
printout();
system("pause");
这么多东西跟构造函数有多大的关系?你是想让人帮你写作业吧?
#ifndef SOURCE_H
#define SOURCE_H
struct Step
enum AnimalType
IsDoodlebug
class Cell
Cell(int x0, int y0);
Cell *nearbyCell(int dir);
bool in(Animal *animal0);
Animal *leave();
class Animal
protected:
int maxBreedT
int breedT
int maxStarveT
int starveT
virtual ~Animal();
public://virtual
virtual Animal *breedChild() = 0;
virtual bool canEat(AnimalType animal) = 0;
virtual bool eat() = 0;
bool breed();
bool move();
void step();
class Ant :public Animal
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
class Doodlebug :public Animal
Doodlebug() ;
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
Step Steps[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
Animal *DoodleB
#endif //__SOURCE_H__
SOURCE.CPP
#include "Source.h"
//========================================
Cell::Cell(int x0, int y0)
this-&x = x0;
this-&y = y0;
this-&animal = NULL;
Cell *Cell::nearbyCell(int dir)
int x0 = this-&x + Steps[dir].
int y0 = this-&y + Steps[dir].
if (x0 & 0 || y0 & 0 || x0 &= maxWidth || y0 &= maxHeight)
return NULL;
return Status[x0][y0];
bool Cell::in(Animal *animal0)
if (this-&animal != NULL)
animal0-&cell =
this-&animal = animal0;
Animal *Cell::leave()
Animal *theAnimal = this-&
theAnimal-&cell = NULL;
this-&animal = NULL;
return theA
//========================================
Animal::Animal()
this-&cell = NULL;
this-&breedTime = 0;
this-&starveTime = 0;
this-&prior = NULL;
this-&next = NULL;
bool Animal::breed()
if (this-&breedTime &= maxBreedTime)
for (int i = 0; i & 4; i++)
Cell *c = this-&cell-&nearbyCell(i);
if (c != NULL && c-&animal == NULL)
c-&in(this-&breedChild());
this-&breedTime = 0;
bool Animal::move()
int dir = rand() % 4;
Cell *c = this-&cell-&nearbyCell(dir);
if (c == NULL)
else if (c-&animal == NULL)
c-&in(this-&cell-&leave());
void Animal::step()
bool dosth =
this-&breedTime++;
this-&starveTime++;
dosth |= this-&breed();
dosth |= this-&eat();
if (!dosth)
this-&move();
if (this-&maxStarveTime & 0 && this-&starveTime &= this-&maxStarveTime)
this-&cell-&leave();
delete (this);
Animal::~Animal()
Animal *t = this-&
this-&prior-&next =
if (t != NULL)
t-&prior = this-&
//========================================
Ant::Ant() : Animal()
this-&type = IsA
this-&maxStarveTime = -1;
this-&maxBreedTime = 3;
if (Ants != NULL)
this-&prior = A
this-&next = Ants-&
if (Ants-&next != NULL) Ants-&next-&prior =
Ants-&next =
Animal *Ant::breedChild()
return new Ant();
bool Ant::canEat(AnimalType t)
bool Ant::eat()
//========================================
// Doodlebug
Doodlebug::Doodlebug() :Animal()
this-&type = IsD
this-&maxStarveTime = 3;
this-&maxBreedTime = 8;
if (DoodleBugs != NULL)
this-&prior = DoodleB
this-&next = DoodleBugs-&
if (DoodleBugs-&next != NULL) DoodleBugs-&next-&prior =
DoodleBugs-&next =
Doodlebug::breedChild()
return new Doodlebug();
Doodlebug::canEat(AnimalType t)
if (t == IsAnt)
Doodlebug::eat()
for (int i = 0; i & 4; i++)
Cell *c = this-&cell-&nearbyCell(i);
if (c!=NULL && c-&animal != NULL && this-&canEat(c-&animal-&type))
Animal *theAnimal = c-&leave();
delete(theAnimal);
this-&starveTime = 0;
//========================================
void randomSet(Animal *animal0)
x = rand() % maxW
y = rand() % maxH
while (Status[x][y]-&animal != NULL);
Status[x][y]-&in(animal0);
printoutHead()
printf("+");
for (int i = 0; i & maxW i++)
printf("=");
printf("+\n");
void printoutDetail(int r)
printf("|");
for (int i = 0; i & maxW i++)
if (Status[i][r]-&animal == NULL)
printf(" ");
switch (Status[i][r]-&animal-&type)
case IsAnt:
printf("O");
case IsDoodlebug:
printf("X");
printf("?");
printf("|\n");
void printout()
printoutHead();
for (int i = 0; i & maxH i++)
printoutDetail(i);
printoutHead();
void main()
int nDoodleB
printf("请输入区域宽度:");
scanf("%d", &maxWidth);
printf("请输入区域高度:");
scanf("%d", &maxHeight);
printf("请输入初始狮蚁数量:");
scanf("%d", &nDoodleBug);
printf("请输入初始蚂蚁数量:");
scanf("%d", &nAnt);
//maxWidth = 3;
//maxHeight = 4;
//nDoodleBug = 2;
//nAnt = 5;
Status = new Cell**[maxWidth];
DoodleBugs = new Doodlebug();
Ants = new Ant();
for (int i = 0; i & maxW i++)
Status[i] = new Cell*[maxHeight];
for (int j = 0; j & maxH j++)
Status[i][j] = new Cell(i, j);
for (int i = 0; i & nDoodleB i++)
randomSet(new Doodlebug());
for (int i = 0; i & nA i++)
randomSet(new Ant());
printout();
system("pause");
while (true)
Animal *a = DoodleBugs-&
Animal *a0;
for ( a0 = (a == NULL ? NULL : a-&next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a-&next))
a-&step();
a = Ants-&
for ( a0 = (a == NULL ? NULL : a-&next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a-&next))
a-&step();
printout();
system("pause");
额!好多代码,看不下去了。
#ifndef SOURCE_H
#define SOURCE_H
struct Step
enum AnimalType
IsDoodlebug
class Cell
Cell(int x0, int y0);
Cell *nearbyCell(int dir);
bool in(Animal *animal0);
Animal *leave();
class Animal
protected:
int maxBreedT
int breedT
int maxStarveT
int starveT
virtual ~Animal();
public://virtual
virtual Animal *breedChild() = 0;
virtual bool canEat(AnimalType animal) = 0;
virtual bool eat() = 0;
bool breed();
bool move();
void step();
class Ant :public Animal
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
class Doodlebug :public Animal
Doodlebug() ;
virtual Animal *breedChild();
virtual bool canEat(AnimalType t);
virtual bool eat();
Step Steps[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
Animal *DoodleB
#endif //__SOURCE_H__
SOURCE.CPP
#include "Source.h"
//========================================
Cell::Cell(int x0, int y0)
this-&x = x0;
this-&y = y0;
this-&animal = NULL;
Cell *Cell::nearbyCell(int dir)
int x0 = this-&x + Steps[dir].
int y0 = this-&y + Steps[dir].
if (x0 & 0 || y0 & 0 || x0 &= maxWidth || y0 &= maxHeight)
return NULL;
return Status[x0][y0];
bool Cell::in(Animal *animal0)
if (this-&animal != NULL)
animal0-&cell =
this-&animal = animal0;
Animal *Cell::leave()
Animal *theAnimal = this-&
theAnimal-&cell = NULL;
this-&animal = NULL;
return theA
//========================================
Animal::Animal()
this-&cell = NULL;
this-&breedTime = 0;
this-&starveTime = 0;
this-&prior = NULL;
this-&next = NULL;
bool Animal::breed()
if (this-&breedTime &= maxBreedTime)
for (int i = 0; i & 4; i++)
Cell *c = this-&cell-&nearbyCell(i);
if (c != NULL && c-&animal == NULL)
c-&in(this-&breedChild());
this-&breedTime = 0;
bool Animal::move()
int dir = rand() % 4;
Cell *c = this-&cell-&nearbyCell(dir);
if (c == NULL)
else if (c-&animal == NULL)
c-&in(this-&cell-&leave());
void Animal::step()
bool dosth =
this-&breedTime++;
this-&starveTime++;
dosth |= this-&breed();
dosth |= this-&eat();
if (!dosth)
this-&move();
if (this-&maxStarveTime & 0 && this-&starveTime &= this-&maxStarveTime)
this-&cell-&leave();
delete (this);
Animal::~Animal()
Animal *t = this-&
this-&prior-&next =
if (t != NULL)
t-&prior = this-&
//========================================
Ant::Ant() : Animal()
this-&type = IsA
this-&maxStarveTime = -1;
this-&maxBreedTime = 3;
if (Ants != NULL)
this-&prior = A
this-&next = Ants-&
if (Ants-&next != NULL) Ants-&next-&prior =
Ants-&next =
Animal *Ant::breedChild()
return new Ant();
bool Ant::canEat(AnimalType t)
bool Ant::eat()
//========================================
// Doodlebug
Doodlebug::Doodlebug() :Animal()
this-&type = IsD
this-&maxStarveTime = 3;
this-&maxBreedTime = 8;
if (DoodleBugs != NULL)
this-&prior = DoodleB
this-&next = DoodleBugs-&
if (DoodleBugs-&next != NULL) DoodleBugs-&next-&prior =
DoodleBugs-&next =
Doodlebug::breedChild()
return new Doodlebug();
Doodlebug::canEat(AnimalType t)
if (t == IsAnt)
Doodlebug::eat()
for (int i = 0; i & 4; i++)
Cell *c = this-&cell-&nearbyCell(i);
if (c!=NULL && c-&animal != NULL && this-&canEat(c-&animal-&type))
Animal *theAnimal = c-&leave();
delete(theAnimal);
this-&starveTime = 0;
//========================================
void randomSet(Animal *animal0)
x = rand() % maxW
y = rand() % maxH
while (Status[x][y]-&animal != NULL);
Status[x][y]-&in(animal0);
printoutHead()
printf("+");
for (int i = 0; i & maxW i++)
printf("=");
printf("+\n");
void printoutDetail(int r)
printf("|");
for (int i = 0; i & maxW i++)
if (Status[i][r]-&animal == NULL)
printf(" ");
switch (Status[i][r]-&animal-&type)
case IsAnt:
printf("O");
case IsDoodlebug:
printf("X");
printf("?");
printf("|\n");
void printout()
printoutHead();
for (int i = 0; i & maxH i++)
printoutDetail(i);
printoutHead();
void main()
int nDoodleB
printf("请输入区域宽度:");
scanf("%d", &maxWidth);
printf("请输入区域高度:");
scanf("%d", &maxHeight);
printf("请输入初始狮蚁数量:");
scanf("%d", &nDoodleBug);
printf("请输入初始蚂蚁数量:");
scanf("%d", &nAnt);
//maxWidth = 3;
//maxHeight = 4;
//nDoodleBug = 2;
//nAnt = 5;
Status = new Cell**[maxWidth];
DoodleBugs = new Doodlebug();
Ants = new Ant();
for (int i = 0; i & maxW i++)
Status[i] = new Cell*[maxHeight];
for (int j = 0; j & maxH j++)
Status[i][j] = new Cell(i, j);
for (int i = 0; i & nDoodleB i++)
randomSet(new Doodlebug());
for (int i = 0; i & nA i++)
randomSet(new Ant());
printout();
system("pause");
while (true)
Animal *a = DoodleBugs-&
Animal *a0;
for ( a0 = (a == NULL ? NULL : a-&next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a-&next))
a-&step();
a = Ants-&
for ( a0 = (a == NULL ? NULL : a-&next); a != NULL; a = a0, a0 = (a == NULL ? NULL : a-&next))
a-&step();
printout();
system("pause");
2162关注|975收录
其他相似问题}

我要回帖

更多关于 数组函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信