c++指针类练习题及答案

  • 格式:docx
  • 大小:30.58 KB
  • 文档页数:13

1、利用指针,编写用于交换两个整型变量值的函数。

程序运行结果如下:输入:5 6输出:6 5#include <iostream>using namespace std;void swap(int *xp,int *yp){int tmp;tmp=*xp;*xp=*yp;*yp=tmp;}int main(){int a,b;cin>>a>>b;swap(&a,&b);cout<<a<<" "<<b<<endl;return 0;}2、编写主程序,将输入字符串反序输出。

程序运行结果如下:输入:ABCDEFGHIJK输出:KJIHGFEDCBA#include<iostream>#include<cstring>using namespace std;int main(){char str[100];cin>>str;int len;len=strlen(str);char *p=&str[len-1];while(p>=str){cout<<*p;p--;}cout<<endl;return 0;}3、使用指针编写一个用于对整型序列进行排序的函数,排序方法使用简单选择排序法。

程序的运行结果如下所示:输入(第一个数是序列的长度):62 7 2 23 1输出:1 2 2 2 3 7#include <iostream>using namespace std;void selectsort(int *list,int count){for(int i=0;i<count-1;i++){int k=i;for(int j=i+1;j<count;j++)if(*(list+j)<*(list+k))k=j;if(k!=i){int tmp=*(list+i);*(list+i)=*(list+k);*(list+k)=tmp;}}}int main(){int n;cin>>n;int array[20];for(int j=0;j<n;j++)cin>>array[j];selectsort(array,n);for(int i=0;i<n;i++)cout<<array[i]<<" ";return 0;}4、用指针编写一个对整型数组进行冒泡排序函数。

冒泡排序是指将相邻的元素进行比较,如果不符合所要求的顺序,则交换这两个元素;对整个数列中所有的元素反复运用上法,直到所有的元素都排好序为止。

程序运行结果如下:输入(第一个数是数组的长度):5503 87 512 61 908输出:61 87 503 512 908#include <iostream>using namespace std;void bubble_up(int *ptr,int count){for(int i=0;i<count;i=i+1)for (int j=count-1;j>i;j=j-1)if(*(ptr+j-1)>*(ptr+j)){int tmp=*(ptr+j-1);*(ptr+j-1)=*(ptr+j);*(ptr+j)=tmp;}}int main(){int n;cin>>n;int list[100];for(int x=0;x<n;x++)cin>>list[x];bubble_up(list,n);for(int i=0;i<n;i++)cout<<list[i]<<" ";return 0;}5、编写程序,将某一个输入的位数不确定的正整数按照标准的三位分节格式输出,例如,当用户输入82668634时,程序应该输出82,668,634。

程序运行结果如下:输入:82668634输出:82,668,634#include<iostream>using namespace std;int main(){int n,i=1;char a[20],*ptr;ptr=a;cin>>n;do{if(i%4){*ptr=n%10+'0';n=n/10;}else *ptr=',';ptr++;i++;}while(n);do{ptr--;i--;cout<<*ptr;}while(i>1);cout<<endl;return 0;}6、编写程序,把10个整数赋予某个int型数组,然后用int型指针输出该数组元素的值。

输入参数是待输出元素的个数。

输出:1 2 3 4 5 6 7 8 9 10注:数组元素之间空一个空格。

#include <iostream>using namespace std;int main( ){int a[]={1,2,3,4,5,6,7,8,9,10},*ptr;ptr=a;for(int i =0;i<9;i++){cout<<*ptr<<" ";ptr++;}cout<<endl;return 0;}7、用指针编写一个程序,当输入一个字符串后,要求不仅能够统计其中字符的个数,还能分别指出其中大、小写字母、数字以及其他字符的个数。

程序运行结果如下:输入:I am 21 years old.输出(五个数值依次为大、小写字母、数字、其他字符和总共含有的字符个数):1102518#include<iostream>using namespace std;int main(){char str[100];char *ptr=str;int total,cap,sma,num,oth;total=cap=sma=num=oth=0;cin.get(ptr,100);while(*ptr!=0){total++;if(*ptr>='A'&&*ptr<='Z')cap++;else if(*ptr>='a'&&*ptr<='z')sma++;else if(*ptr>='0'&&*ptr<='9')num++;else oth++;ptr++;}cout<<cap<<endl;cout<<sma<<endl;cout<<num<<endl;cout<<oth<<endl;cout<<total<<endl;return 0;}8、编写一个函数, 用于将一个字符串转换为整型数值。

其原型为:int myatoi(char *string);其中参数string为待转换的字符串(其中包括正、负号和数字),返回值为转换结果。

程序运行结果如下:输入:-529输出:-529#include<iostream>using namespace std;int atoi(char*string){int num=0;int s=1;if(*string=='-'){s=-1;string++;}if(*string=='+'){s=1;string++;}while(*string!=0&&*string>='0'&&*string<='9'){num=num*10+*string-'0';string++;}return s*num;}int main(){char str[50];cin>>str;cout<<atoi(str)<<endl;return 0;}9、改善模拟梵塔问题的递归程序,打印移动过程的同时,再打印每一移动后个针上的金箔的数量。

初始金箔的数量由用户输入。

提示:其中关于步数和状态可用静态变量。

程序的运行结果如下所示:输入:3输出:Step 0A:3 B:0 C:0)Step 1:From A to C,(A:2 B:0 C:1)Step 2:From A to B,(A:1 B:1 C:1)Step 3:From C to B,(A:1 B:2 C:0)Step 4:From A to C,(A:0 B:2 C:1)Step 5:From B to A,(A:1 B:1 C:1)Step 6:From B to C,(A:1 B:0 C:2)Step 7:From A to C,(A:0 B:0 C:3)注意:冒号、逗号为英文符号,冒号、逗号后无空格。

#include <iostream>using namespace std;int step,x,y,z;void move(char from,char to){if (from=='A') x=x-1;if (from=='B') y=y-1;if (from=='C') z=z-1;if (to=='A') x=x+1;if (to=='B') y=y+1;if(to=='C') z=z+1;cout<<"Step "<<step<<":From "<<from<<" to "<<to<<",(A:"<<x<<" B:"<<y<<" C:"<<z<<")"<<endl;}void hannoi(int n,char a,char b,char c){if (n==1){move(a,c);step++;}else{hannoi(n-1,a,c,b);move(a,c);step++;hannoi(n-1,b,a,c);}}int main(){int n;cin>>n;step=1;x=n;y=0;z=0;cout<<"Step 0:(A:"<<n<<" B:0 C:0)"<<endl;hannoi(n,'A','B','C');return 0;}10、重载求数组中最大元素的函数(两个函数,一个求整数的最大值,另一个求双精度的最大值),函数名为:max_element。