用指针处理
1.输入三个数,从小到大排序
#include <iostream>
using namespace std;
int main()
{
void change(int* p1, int* p2);
int a, b, c;
cout << "input a b c"<<endl;
cin >> a >> b >> c;
if (a > b) change(&a, &b);
if (a > c) change(&a, &c);
if (b > c) change(&b, &c);
cout << a << " " << b << " " << c;
return 0;
}
void change(int *p1, int *p2) {
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
2.三个字符串,从小到大排序
#include <iostream>
#include <string>
using namespace std;
int main()
{
void change(string*, string*);
string str1, str2, str3;
cout << "input str1 str2 str3" << endl;
cin >> str1 >> str2 >> str3;
if (str1 > str2) change(&str1, &str2);
if (str1 > str3) change(&str1, &str3);
if (str2 > str3) change(&str2, &str3);
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
void change(string* p1, string* p2)
{
string temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
3.10个数 最小和第一个数对换,最大和最后一个数对换
#include <iostream>
using namespace std;
int main()
{
void input(int *number);
void function(int *number);
void output(int *number);
int number[10];
input(number);
function(number);
output(number);
return 0;
}
void input(int *number) {
cout << "input 10 numbers" << endl;
int i;
for (i = 0;i < 10;i++)
cin >> number[i];
}
void function(int *number) {
int *max, *min, *p, temp;
max = min = number;
for (p = number + 1;p < number + 10;p++) {
if (*p > *max) max = p;
if (*p < *min) min = p;
}
temp = *number;
*number = *min;
*min = temp;
temp = *(number+9);
*(number + 9) = *max;
*max = temp;
}
void output(int *number) {
int *p;
for (p = number;p < number + 10;p++)
cout << *p << " ";
}
4.n个整数,前面各数像后移动m,最后m个数字变成最前面m个数字
用到了递归的思想,每次循环动一位,循环m次
#include <iostream>
using namespace std;
int main()
{
void move(int* array, int n, int m);
int number[20], n, m,i;
cout << "input n:" << endl;
cin >> n;
cout << "input n numbers:" << endl;
for (i = 0;i < n;i++)
cin >> number[i];
cout << "input m:" << endl;
cin >> m;
move(number, n, m);
cout << "the new array is:" << endl;
for (i = 0;i < n;i++)
cout << number[i]<<" ";
return 0;
}
void move(int *array, int n, int m) {
int* p, end;
end = *(array + n - 1);
for (p = array + n - 1;p > array;p--) {
*p = *(p - 1);
}
*array = end;//只移动最后一位
m--;
if (m > 0) move(array, n, m);
}
5.有n个人围圈开始报数,报到3就退出,问最后留下来的人是序号
约瑟夫环问题,用指针处理
#include <iostream>
using namespace std;
int main()
{
int i, k, m, n, num[50], * p;
cout << "input the number of person n= "<<endl;
cin >> n;
p = num;
for (i = 0;i < n;i++)
*(p + i) = i + 1;
i = 0;
k = 0;//k计算123 报数量
m = 0;//m为退出人数
while (m < n - 1) {
if(*(p+i)!=0) k++;
if (k ==3) {
*(p + i) = 0;
k = 0;
m++;
}
i++;
if (i == n) i = 0;
}
while (*p == 0) p++;//找到值不为0的那个p
cout << *p;
return 0;
}
6.计算字符串的长度
#include <iostream>
using namespace std;
int main()
{
int length(char* p);
char str[20];
cout << "input strig:";
cin >> str;
int i;
i = length(str);
cout << "the length of the string is:" << i;
return 0;
}
int length(char* p)
{
int length = 0;
while (*p != '\0')
{
p++;length++;
}
return length;
}
7.字符串从第m个字符开始的字符全部复制成为另一个字符串
循环的小细节,注意!
#include <iostream>
using namespace std;
int main()
{
void copy(char* p1, char* p2, int m);
char str1[20],str2[20];
int m;
cout << "input string:";
cin >> str1;
cout << "input m:";
cin >> m;
copy(str1, str2, m);
cout << str2;
return 0;
}
void copy(char* p1, char* p2, int m) {
int i;
p1 = p1 + m - 1;
while(*p1!='\0')
{
*p2++ = *p1++;
}
*p2 = '\0';
}
8.统计一行文字中大写字母 小写字母 空格 数字 以及其他字符的个数
(真题考过)
字符串不用cin输入,用getchar()输入,可以输入空格键
while ((s[i] = getchar()) != ‘\n’)
i++;
#include <iostream>
using namespace std;
int main()
{
int upper = 0, lower = 0, digit = 0, space = 0, other = 0, i = 0;
char* p, s[20];
cout << "input string:";
while ((s[i] = getchar()) != '\n') i++;
p = s;
while (*p != '\n') {
if ((*p >= 'A') && (*p <= 'Z'))
upper++;
else if ((*p >= 'a') && (*p <= 'z'))
lower++;
else if ((*p >= '0') && (*p <= '9'))
digit++;
else if (*p == ' ')
space++;
else other++;
p++;
}
cout << "upper is " << upper<<endl;
cout << "lower is " << lower<<endl;
cout << "digit is " << digit<<endl;
cout << "space is " << space<<endl;
cout << "other is " << other<<endl;
return 0;
}
9.3x3矩阵转置
#include <iostream>
using namespace std;
int main()
{
int a[3][3],i,j,*p;
void move(int *p);
cout << "input matrix: "<<endl;
for (i = 0;i < 3;i++)
for (j = 0;j < 3;j++)
cin >> a[i][j];
p = &a[0][0];
move(p);
for (i = 0;i < 3;i++)
{
for (j = 0;j < 3;j++)
cout << a[i][j]<<" ";
cout << endl;
}
return 0;
}
void move(int *p) {
int i, j,temp;
for(i=0;i<3;i++)
for (j = i;j < 3;j++)
{
temp = *(p + 3 * i + j);
*(p + 3 * i + j) = *(p + 3 * j + i);
* (p + 3 * i + j) = temp;
}
}
10.10个字符串排序
(1)
用字符型二维数组
#include <iostream>
using namespace std;
int main()
{
void sort(char s[][6]);
int i;
char str[10][6];
cout << "input 10 strings:" << endl;
for (i = 0;i < 10;i++)
cin >> str[i];
sort(str);
cout << "now ,the sequence is" << endl;
for (i = 0;i < 10;i++)
cout << str[i] << endl;
return 0;
}
void sort(char s[][6]) {
int i, j;
char* p, temp[6];
p = temp;
for (i = 0;i < 10;i++)
for (j = 0;j < 9 - i;j++)
if (strcmp(s[j] ,s[j + 1])>0)
{
strcpy(p, s[j]);
strcpy(s[j], s[j + 1]);
strcpy(s[j + 1], p);
}
}
(2)用指向一维数组的指针`
#include <iostream>
using namespace std;
int main()
{
void sort(char (*p)[6]);
int i;
char str[10][6],(*p)[6];
cout << "input 10 strings:" << endl;
for (i = 0;i < 10;i++)
cin >> str[i];
p = str;
sort(p);
cout << "now ,the sequence is" << endl;
for (i = 0;i < 10;i++)
cout << str[i] << endl;
return 0;
}
void sort(char (*s)[6]) {
int i, j;
char * p, temp[6];
p = temp;
for (i = 0;i < 10;i++)
for (j = 0;j < 9 - i;j++)
if (strcmp(s[j] ,s[j + 1])>0)
{
strcpy(p, s[j]);
strcpy(s[j], s[j + 1]);
strcpy(s[j + 1], p);
}
}
(3)用string数组
#include <iostream>
#include <string>
using namespace std;
int main()
{
void sort(string * s);
string str[10], * p;
p = str;
int i;
cout << "input 10 strings:" << endl;
for (i = 0;i < 10;i++)
cin >> str[i];
sort(p);
cout << "now ,the sequence is" << endl;
for (i = 0;i < 10;i++)
cout << str[i] << endl;
return 0;
}
void sort(string *s) {
int i, j;
string temp;
for (i = 0;i < 10;i++)
for (j = 0;j < 9 - i;j++)
if (s[j]>s[j+1])
{
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
12,用指针数组对十个不等长的字符串排序
*p[10]
str[10][20]
p=str[i] 每一个p[i]都指向一个一维数组, *(p+i)指向一维数组的第一个元素,相当于a[i][0]
#include <iostream>
#include <string>
using namespace std;
int main()
{
void sort(char* p[10]);
char * p[10], str[10][20];
int i;
for (i = 0;i < 10;i++)
{
cin >> str[i];p[i] = str[i];
}
sort(p);
return 0;
}
void sort(char *p[10]) {
int i, j;
char *temp;
for (i = 0;i < 10;i++)
for (j = 0;j < 9 - i;j++)
if (strcmp(*(p+j),*(p+j+1))>0)
{
temp = *(p + j);// *(p+j)指向第j行的第一个元素
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
13,求定积分·
指针指向函数的运用
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float fsin(float x);
float fcos(float x);
float fexp(float x);
float integral(float (*p)(float), float a, float b, int n);
float a1, b1, a2, b2, a3, b3, c, (*p)(float);
int n = 20;
cout << "input a1,b1: ";
cin >> a1 >> b1;
cout << "input a1,b1: ";
cin >> a2 >> b2;
cout << "input a1,b1: ";
cin >> a3 >> b3;
p = fsin;
c = integral(p, a1, b1, n);
cout << "sin(x)= " << c <<endl;
p = fcos;
c = integral(p, a2, b2, n);
cout << "cos(x)= " << c << endl;
p = fexp;
c = integral(p, a3, b3, n);
cout << "exp(x)= " << c << endl;
return 0;
}
float integral(float (*p)(float), float a, float b, int n) {
int i;
float x, h, s;
h = (b - a) / n;
x = a;
s = 0;
for (i = 1;i <= n;i++) {
x = x + h;
s = s + (*p)(x)*h;
}
return s;
}
float fsin(float x)
{
return sin(x);
}
float fcos(float x)
{
return cos(x);
}
float fexp(float x)
{
return exp(x);
}
14,将n个数按输入时的顺序的逆序排列
#include <iostream>
using namespace std;
int main()
{
const int n = 6;
int a[n], i, temp;
int* p;
cout << "input"<< n<<" numbers :";
for (i = 0;i < n;i++)
cin >> a[i];
p = a;
for (i = 0;i < n / 2;i++) {
temp = *(p + i);
*(p + i) = *(p + n - 1 - i);
*(p + n - 1 - i) = temp;
}
cout << "the new sequence is: ";
for (i = 0;i < n;i++)
cout << a[i]<<" ";
return 0;
}
17,自己写strcmp函数
#include <iostream>
using namespace std;
int main()
{
int strcmp(char* p1, char* p2);
char str1[20], str2[20], * p1, * p2,i;
cout << "input two strings: ";
cin >> str1;
cin>> str2;
p1 = str1;
p2 = str2;
i = strcmp(p1, p2);
cout << i;
return 0;
}
int strcmp(char* p1, char* p2)
{
int i = 0;
while (*(p1+i) == *(p2+i))
if (*(p1+i++) == '\0') return 0;
return ( *(p1+i) - ( * (p2 + i)));
}
19
#include <iostream>
using namespace std;
int main()
{
void sort(char** p);
char** p, * pstr[5], str[5][20];
int i;
for (i = 0;i < 5;i++)
pstr[i] = str[i];
cout << "input 5 strings: " <<endl;
for (i = 0;i < 5;i++)
cin >> str[i];
p = pstr;
sort(p);
for (i = 0;i < 5;i++)
cout << str[i]<<" ";//结果是原始数组内容
for (i = 0;i < 5;i++)
cout << pstr[i]<<" ";//结果是排序后的内容
return 0;
}
void sort(char** p)
{
int i, j;
char* temp;
for(i=0;i<5;i++)
for (j = i;j < 5;j++)
{
if (strcmp(*(p + i), *(p + j )) > 0)
{
temp = *(p + i);
*(p + i) = *(p + j );
*(p + j) = temp;
}
}
}
#include <iostream>
using namespace std;
//用指向指针的方法对n个整数排序
int main()
{
void change(int** p);
const int n = 5;
int a[5],*pstr[5],**p,i;
for (i = 0;i < 5;i++)
{
cin >> a[i];
pstr[i] = &a[i];//指针数组pstr[i]指向a[i]
}
p = pstr;
change(p);
for (i = 0;i < 5;i++)
cout<< *pstr[i] << " "; //结果是顺序输出
cout << endl;
for (i = 0;i < 5;i++)
cout << a[i] << " ";//结果是原始的数组a[n]
return 0;
}
void change(int** p)
{
int i,j,*temp;
for(i=0;i<5;i++)
for (j = i; j < 5;j++)
{
if (**(p + i) > **(p + j))
{
temp = *(p + i);
*(p + i) = *(p + j);
*(p + j) = temp;
}
}
}
16输入字符串,内有数字和非数字字符,将其中连续的数字组为一个整数,依次存放到一个数组中
自己的思路:
#include <iostream>
using namespace std;
int main()
{
char a[30], * p;
int i = 0,j,k,s=0,temp[20], b[20], num;//num计算几个连续的整数,b存放整数数组,temp是临时存放数字
while ((a[i] = getchar()) != '\n') i++;
p = a;
i = 0;
num = 0;
while (*(p + i) != '\n') {
j = 0;//j是如果出现数字,开始计数
s = 0;
for (int m = 0;m < 20;m++) temp[m] = 0;
while ((*(p + i) != '\n') && (*(p + i) >= '0' && *(p + i) <= '9')) {
temp[j] = *(p + i)-48;//将数字暂存到temp中
i++;j++;//i是总体遍历,此时temp共有j个数据
}
if (j != 0) {
for (k = 0;k < j;k++)
s = s * 10 + temp[k];
b[num++] = s;
i--;
}
i++;
}
for (i = 0;i < num;i++)
cout << b[i] << " ";
return 0;
}