有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数,见图8.43。写一函数实现以上功能,在主函数中输入n个整数和输出调整后的n个数。
方法一:
#include <stdio.h>
void sort(int *s, int n, int x); //定义排序函数
int main()
{
int n[10], *p, m;
for (p=n, printf("Please enter 10 numbers: "); p<n+10; scanf("%d", p++)); //输入10个整数
printf("Please enter move number: "); //输入要移动的个数
scanf("%d", &m);
for (; m<0||m>10; printf("Error! Retry!\nPlease enter move number: "), scanf("%d", &m));
sort(n, 10, m); //调用排序函数
for (p=n, printf("Sort by: "); p<n+10; printf("%d ", *p++)); //输出排序后的10个整数
printf("\n");
return 0;
}
//排序函数
void sort(int *s, int n, int x)
{
int *i, j, *k, t[10]; //定义一个长度为10的数组t
for (i=s+n-x, j=0; i<s+n; t[j++]=*i++); //把形参数组s的后x个元素赋值到长度为10的数组t前x个元素中
for (i=s+n-x-1, k=s+n-1;k>=s+x; *k--=*i--); //把形参数组s的前n-x个元素赋值到该形参数组s的后面n-x个元素中,把控制循环条件的k>=s+x改成i>=s也行
for (j=0; j<x; *s++=t[j++]); //把数组t的前n-x个元素赋值到形参数组s的前n-x个元素中
}
在VS2019下,需将源文件的scanf改为scanf_s:
#include <stdio.h>
void sort(int* s, int n, int x); //定义排序函数
int main()
{
int n[10], * p, m;
for (p = n, printf("Please enter 10 numbers: "); p < n + 10; scanf_s("%d", p++)); //输入10个整数
printf("Please enter move number: "); //输入要移动的个数
scanf_s("%d", &m);
for (; m < 0 || m>10; printf("Error! Retry!\nPlease enter move number: "), scanf_s("%d", &m));
sort(n, 10, m); //调用排序函数
for (p = n, printf("Sort by: "); p < n + 10; printf("%d ", *p++)); //输出排序后的10个整数
printf("\n");
return 0;
}
//排序函数
void sort(int* s, int n, int x)
{
int* i, j, * k, t[10]; //定义一个长度为10的数组t
for (i = s + n - x, j = 0; i < s + n; t[j++] = *i++); //把形参数组s的后x个元素赋值到长度为10的数组t前x个元素中
for (i = s + n - x - 1, k = s + n - 1; k >= s + x; *k-- = *i--); //把形参数组s的前n-x个元素赋值到该形参数组s的后面n-x个元素中,把控制循环条件的k>=s+x改成i>=s也行
for (j = 0; j < x; *s++ = t[j++]); //把数组t的前n-x个元素赋值到形参数组s的前n-x个元素中
}
方法二:
#include <stdio.h>
#include<stdlib.h>
void sort(int* s, int n, int x); //定义排序函数
int main()
{
int n[10], * p, m;
for (p = n, printf("Please enter 10 numbers: "); p < n + 10; scanf("%d", p++)); //输入10个整数
printf("Please enter move number: "); //输入要移动的个数
scanf("%d", &m);
for (; m < 0 || m>10; printf("Error! Retry!\nPlease enter move number: "), scanf("%d", &m));
sort(n, 10, m); //调用排序函数
for (p = n, printf("Sort by: "); p < n + 10; printf("%d ", *p++)); //输出排序后的10个整数
printf("\n");
return 0;
}
//排序函数
void sort(int* s, int n, int x)
{
int* i, j, * k, * m;
int* t = (int*)calloc(n, sizeof(int));
for (i = s + n - 1, j = 0, m = s + n - x - 1, k = s + n - 1; i >= s + n - x || k >= s + x; t[j++] = *i--, *k-- = *m--);//把形参数组s中后x个元素依次赋值给数组t的同时把形参数组s的前n-x个元素再分别赋值给该形参数组s的后n-x个元素
for (j = 0, m = s + x - 1; j < x; *m-- = t[j++]);//再把数组t的n-x个元素赋值给形参数组s的前n-x个元素
}
在VS2019下,需将源文件的scanf改为scanf_s:
#include <stdio.h>
#include<stdlib.h>
void sort(int* s, int n, int x); //定义排序函数
int main()
{
int n[10], * p, m;
for (p = n, printf("Please enter 10 numbers: "); p < n + 10; scanf_s("%d", p++)); //输入10个整数
printf("Please enter move number: "); //输入要移动的个数
scanf_s("%d", &m);
for (; m < 0 || m>10; printf("Error! Retry!\nPlease enter move number: "), scanf_s("%d", &m));
sort(n, 10, m); //调用排序函数
for (p = n, printf("Sort by: "); p < n + 10; printf("%d ", *p++)); //输出排序后的10个整数
printf("\n");
return 0;
}
//排序函数
void sort(int* s, int n, int x)
{
int* i, j, * k, * m;
int* t = (int*)calloc(n, sizeof(int));
for (i = s + n - 1, j = 0, m = s + n - x - 1, k = s + n - 1; i >= s + n - x || k >= s + x; t[j++] = *i--, *k-- = *m--);//把形参数组s中后x个元素依次赋值给数组t的同时把形参数组s的前n-x个元素再分别赋值给该形参数组s的后n-x个元素
for (j = 0, m = s + x - 1; j < x; *m-- = t[j++]);//再把数组t的n-x个元素赋值给形参数组s的前n-x个元素
}
方法三:(利用动态分配内存和指针)
#include<stdio.h>
#include <stdlib.h>
void input(int *pNum, int n); //定义输入函数
void print(int *pNum, int n); //定义输出函数
void exchange(int *pNum, int mov, int n); //定义交换函数
int main()
{
int *numb, len, move_num; //定义数组,数组长度,移动个数
printf("Please enter length: "); //此两行输入数组长度
scanf("%d", &len);
numb=(int *)malloc(len*sizeof(int)); //给数组分配空间
input(numb, len); //调用输入函数
printf("Please enter move number: "); //此两行为输入移动的长度
scanf("%d", &move_num);
for (; move_num<0||move_num>10; printf("Error! Retry!\nPlease enter move number: "), scanf("%d", &move_num));
exchange(numb, move_num, len); //调用交换函数
print(numb, len); //调用输出函数
return 0;
}
//输入函数
void input(int *pNum, int n)
{
int *p;
for (p=pNum, printf("Please enter %d numbers: ", n); p<pNum+n; scanf("%d", p++));
}
//输出函数
void print(int *pNum, int n)
{
int *p;
for (p=pNum, printf("New line: "); p<pNum+n; printf("%d ", *p++));
printf("\n");
}
//交换函数
void exchange(int *pNum, int mov, int n)
{
int *p, i, j, *temp;
temp=(int *)malloc(mov*sizeof(int));
for (p=pNum+(n-mov), i=0; i<mov; *(temp+i++)=*p++); //把形参数组pNum的后mov个元素赋值到长度为mov的动态数组temp中
for (p=pNum+(n-mov-1), j=n-1; j>=mov; *(pNum+j--)=*p--); //把形参数组pNum的前n-mov个元素赋值到该性参数组PNum的后面n-move个元素中
for (p=pNum, i=0; p<pNum+mov; *p++=*(temp+i++)); //把动态数组temp的n-mov个元素赋值到形参数组pNum的前n-move个元素中
}
在VS2019下,需将源文件的scanf改为scanf_s:
#include<stdio.h>
#include <stdlib.h>
void input(int* pNum, int n); //定义输入函数
void print(int* pNum, int n); //定义输出函数
void exchange(int* pNum, int mov, int n); //定义交换函数
int main()
{
int* numb, len, move_num; //定义数组,数组长度,移动个数
printf("Please enter length: "); //此两行输入数组长度
scanf_s("%d", &len);
numb = (int*)malloc(len * sizeof(int)); //给数组分配空间
input(numb, len); //调用输入函数
printf("Please enter move number: "); //此两行为输入移动的长度
scanf_s("%d", &move_num);
for (; move_num < 0 || move_num>10; printf("Error! Retry!\nPlease enter move number: "), scanf_s("%d", &move_num));
exchange(numb, move_num, len); //调用交换函数
print(numb, len); //调用输出函数
return 0;
}
//输入函数
void input(int* pNum, int n)
{
int* p;
for (p = pNum, printf("Please enter %d numbers: ", n); p < pNum + n; scanf_s("%d", p++));
}
//输出函数
void print(int* pNum, int n)
{
int* p;
for (p = pNum, printf("New line: "); p < pNum + n; printf("%d ", *p++));
printf("\n");
}
//交换函数
void exchange(int* pNum, int mov, int n)
{
int* p, i, j, * temp;
temp = (int*)malloc(mov * sizeof(int));
for (p = pNum + (n - mov), i = 0; i < mov; *(temp + i++) = *p++); //把形参数组pNum的后mov个元素赋值到长度为mov的动态数组temp中
for (p = pNum + (n - mov - 1), j = n - 1; j >= mov; *(pNum + j--) = *p--); //把形参数组pNum的前n-mov个元素赋值到该性参数组PNum的后面n-move个元素中
for (p = pNum, i = 0; p < pNum + mov; *p++ = *(temp + i++)); //把动态数组temp的n-mov个元素赋值到形参数组pNum的前n-move个元素中
}