c++指针类练习题及答案
时间:2025-07-07
时间:2025-07-07
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、使用指针编写一个用于对整型序列进行排序的函数,排序方法使用简单选择排序法。
程序的运行结果如下所示:
输入(第一个数是序列的长度):
6
2 7 2 2 3 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、用指针编写一个对整型数组进行冒泡排序函数。冒泡排序是指将相邻的元素进行比较,如果不符合所要求的顺序,则交换这两个元素;对整个数列中所有的元素反复运用上法,直到所有的元素都排好序为止。
程序运行结果如下:
输入(第一个数是数组的长度):
5
503 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.
输出(五个数值依次为大、小写字母、数字、其他字符和总共含有的字符个数):
1
10
2
5
18
#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()
{
…… 此处隐藏:4622字,全部文档内容请下载后查看。喜欢就下载吧 ……