本周学习了指针,都说指针是C语言的灵魂,也是C语言区别于其余语言所独特的地方,学好指针的相关内容十分必要,但同时难度也更大,须要付出更多的时间去理解和熟练掌握。学习到指针部分以后明显感受有点吃力,虽然也和前面的知识结合紧密,可是须要对指针的概念搞得很清楚,否则就会把地址和该地址所存的内容搞混。而后指针上完以后老师又教告终构体和文件相关的内容,真的要认真学习多花时间才能彻底理解掌握。目前的本身以为仍是一直半解,蛮吃力的。 在数据传递时,若是数据块较大,这时就可使用指针传递地址而不是实际数据,即提升传输速度,又节省大量内存。 指针为动态数据结构(如二叉树、链表)提供支持
给定一句英语,要求你编写程序,将句中全部单词的顺序颠倒输出。
int main() { 定义字符数组 str[500010]; 输入gets(str); 定义长度变量len; len=strlen(str); 定义i,j,count=0; 定义flag=0; for i=len-1 to i>=0 do if str[i]!=' ' then count++;//计算字符数 end if else if str[i]==' '&&count!=0 then for j=i+1 to str[j]!=' '&&str[j]!=0 do 输出字符 end for for j=i to j>=0 do if str[j]!=' ' then break; end if if j==0 then flag=1; end if//遍历后面是否还有字符 if flag==0 then 输出空格 count=0; end if end for end for for i=0 to i<count&&str[i]!=' ' do 输出字符//因为最后一个单词末尾可能没有空格,因此单独考虑 return 0; }
在函数那章,咱们已经实现小学四则运算这份做业,如今要求你们把以前设计函数升级改造,原来的函数你们都是用全局变量实现不一样函数参数传递,此次做业要求改地址传递,减小全局变量的使用。
伪代码 int main() { 定义字符数组 exp[100]={0}; 定义整型变量 level,amount,right; 定义浮点型变量 accuracy; 定义整型变量 youranswer=0,myanswer=0; 输出menu---------------------------简单四则运算训练系统------------------------------\n 欢迎进入训练\n 等级1:1位数的一步加减乘除计算训练\n等级2:2位数的2步加减运算\n等级3:3位数的2步加减运算\n请选择等级: 输入level while level!=1&&level!=2&&level!=3 fflush(stdin);//输入非法字符时清除缓冲区数据从新输入 输出 输入错误,请从新输入\n 输入level end while 输入 请输入题目数量: while !scanf("%d",&amount) //判断是否输入非法字符 fflush(stdin); 输出 输入错误,请从新输入\n end while 定义整型变量 n; 定义 flag=0; for n=0 to n<amount&&flag==0 do CreateExp(exp,level); //建造题目 输入 youranswer myanswer=Answer(exp,level); Judge(youranswer,myanswer); 输出 输入0继续,若退出输入1: 输入 flag while flag!=0&&flag!=1 fflush(stdin); 输出 输入错误,请从新输入 输入 flag end while if flag==1 then break; end for right=Judge(youranswer,myanswer); accuracy=right*1.0/amount; 输出 正确率为%.2lf",accuracy return 0; }
伪代码 void CreateExp(char *exp,int level) { srand(time(NULL)); 定义整型变量 x,y,z; 定义整型变量 ch1,ch2; if level==1 then ch1=rand()%4; else ch1=rand()%2; ch2=rand()%2; end if//生成运算符 switch(level) { case 1: x=rand()%10;y=rand()%10; itoa(x,exp,10); 将变量x的数据类型转为字符型 switch(ch1) case 0:strcat(exp,"+");break; case 1:strcat(exp,"-");break; case 2:strcat(exp,"*");break; case 3:strcat(exp,"/");break; end switch strcat(exp,itoa(y,exp+3,10)); break;//一级 case 2: x=rand()%100;y=rand()%100;z=rand()%100; itoa(x,exp,10); switch(ch1) case 0:strcat(exp,"+");break; case 1:strcat(exp,"-");break; end switch strcat(exp,itoa(y,exp+4,10)); switch(ch2) case 0:strcat(exp,"+");break; case 1:strcat(exp,"-");break; end switch strcat(exp,itoa(z,exp+7,10)); break;//二级 case 3: x=rand()%1000;y=rand()%1000;z=rand()%1000; itoa(x,exp,10); switch(ch1) case 0:strcat(exp,"+");break; case 1:strcat(exp,"-");break; end switch strcat(exp,itoa(y,exp+5,10)); switch(ch2) case 0:strcat(exp,"+");break; case 1:strcat(exp,"-");break; end switch strcat(exp,itoa(z,exp+9,10)); break; //三级 end switch strcat(exp,"="); puts(exp); //生成exp
伪代码 int Answer(char *exp,int level) { 定义整型变量 answer; answer=*exp-'0';//将字符型转为整型 定义i; for i=0 to *(exp+i)!=0 if *(exp+i)=='+'||*(exp+i)=='-'||*(exp+i)=='*'||*(exp+i)=='/' then switch(*(exp+i)) case '+':answer=add(answer,*(exp+i+1)-'0');break; case '-':answer=sub(answer,*(exp+i+1-'0'));break; case '*':answer=mul(answer,*(exp+i+1)-'0');break; case '/':answer=divide(answer,*(exp+i+1)-'0');break; end switch end if end for return answer; int Judge(int youranswer,int myanswer) { 定义整型变量 right=0; if youranswer==myanswer then printf("答案正确!\n"); right++; else printf("答案错误,正确答案为%d\n",myanswer); end if return right; }
原来的main函数实在是太太太长了,这里就不放上来了,整体就是不简练,没有分割不一样功能的函数,而一味的都写在main函数中算法
原来只有四个简单的加减乘除函数,没有其余多余的函数分装数组
此次大做业主要就是增强了本身对于函数分装还有指针的运用,刚开始的方法虽然定义了指针可是感受并无彻底用上,后来老师指导和参考同窗的之后修改了,算是真正有用上指针了,此次的主要感觉就是要增强本身在指针运用方法的练习,以及对于你想写怎么的程序有一个大体的规划,这对于后续的函数分装和程序的拓展性怎样很是重要,而后就是,继续加油吧~