在本章的学习中,我了解到了指针的原理与应用,指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它能够被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变。还学到了如何利用地址,找到它指向的存在电脑存储器中另外一个地方的值,也学习了如何把指针和函数结合使用。可是指针的使用比较复杂,在使用的过程当中容易漏写错写,好比在使用函数时容易忘记&、容易把(n)++或++n写成*n++,++优先级更高,致使不是对n所指内容加一。还有字符串的结束标志‘\0’也容易忘记加上。因此,平时对指针的练习就很重要,否则到考试的时候,真的容易忘记怎么使用,卡在题目半中间。算法
125数组
本题要求编写一个解密藏尾诗的程序。函数
定义二维数组poem[4][20],字符数组end[10]; 定义 i,t; for i=0 to i=3 do t=strlen(poem[i]); end[2*i]=poem[i][t-2]; end[2*i+1]=poem[i][t-1]; 结束循环 end[2*i]='\0'; //结束符号 输出end; end
题目:实现小学四则运算。
学习
定义整型数n,p; 定义字符x,y,op,temp; srand(time(NULL)); x=(int(rand()%10)+'0'); //生成数字字符 strcat(A,&x); A[1]='\0'; char a[]={'+','-','*','/'}; //生成运算符 n=rand()%4; op=a[n]; temp等于op; strcat(A,&op); A[2]='\0'; for p=0 to flag!=1 do y=(int(rand()%10)+'0'); if temp等于除号且y等于0,进入下一个循环; if temp等于除号且y不等于0,flag=1; if temp不等于除号 flag=1; strcat(A,&y); A[3]='='; A[4]='\0'; return A;
定义整型数n,p,q; 定义字符x,y,op; for q=0 to q<2 do for p=0 to p=1 do{ x=(int(rand()%10)+'0');//生成数字字符 strcat(B,&x); B[p+q*3+1]='\0'; } char a[]={'+','-'}; //生成运算符 n=rand()%2; op=a[n]; strcat(B,&op); B[3+q*3]='\0'; for p=0 to p=1 do //生成数字字符 { y=(int(rand()%10)+'0'); strcat(B,&y); B[7+p]='\0'; } B[8]='='; B[9]='\0'; return B;
定义整型数n,p,q; 定义字符x,y,op; for q=0 to q<2 do for p=0 to p=2 do x=(int(rand()%10)+'0');//生成数字字符 strcat(C,&x); C[p+q*4+1]='\0'; char a[]={'+','-'}; //生成运算符 n=rand()%2; op=a[n]; strcat(C,&op); C[4+q*4]='\0'; for p=0 to p=2 do //生成数字字符 y=(int(rand()%10)+'0'); strcat(C,&y); C[9+p]='\0'; C[11]='='; C[12]='\0'; return C;
int ComputeExp(char *exp) { 定义整型数 len,n=0; 定义整型数 i,flag=0; 定义字符 op; 定义浮点数 m=0,sum=0; len等于strlen(exp); for i=len-1 to i=0 do if exp[i]<='9'而且exp[i]>='0' then m=pow(10,n)*(exp[i]-'0')+m; n++; if exp[i]=='+'或者exp[i]=='-' then flag=2; if exp[i]等于加号 sum+=m; else sum-=m; n=0; m=0; if flag等于1 或 exp[i]等于除号 或 exp[i]等于乘号 then flag++; if flag等于1 then op=exp[i]; sum=m; m=0; n=0; else if op等于乘号 then sum=m*sum; if op等于除号 then sum=m/sum; return sum; if i等于0 then sum=sum+m; return sum;
1 由于当时搞不清如何把随机算式用字符串表示,因此干脆写了三个函数分别生成三个算式字符串,比较麻烦,仍是要多看看别人的代码学习一下。指针
2 在写计算正确答案的函数时花了很多时间,老是小错误不断,但最后总算也能运行正确。code
3 总的来讲,指针这块内容没学好,代码没有作到简化,应该多向他人学习。blog