1.三数排序
代码:
#include "stdio.h"
void sort(int *arr, int count);
void print(int *arr, int count);
void input(int *arr, int count);
void init(int *arr, int count);
void delete(int *arr, int count);
void main() {
int count = 3;
int *arr = (int *) malloc(count * sizeof(int));
input(arr, count);
print(arr, count);
sort(arr, count);
print(arr, count);
delete(arr, count);
}
void sort(int *arr, int count) {
int max = 0;
for(int i = 0; i < count; i++) {
max = arr[i];
for(int j = i + 1; j < count; j++) {
if(arr[j] > max) {
max = arr[j];
arr[j] = arr[i];
arr[i] = max;
}
}
}
}
void print(int *arr, int count) {
printf("The arr is :");
for(int i = 0; i < count; i++) {
printf("%d,", arr[i]);
}
printf("\n");
}
void input(int *arr, int count) {
//int *start = arr;//形参在函数结束时,会释放掉,并且不会改写原始参数
for(int i = 0; i < count; i++) {
printf("Please input a number:");
scanf("%d", arr++);
}
//arr = start;
}
void init(int *arr, int count) {
arr = (int *) malloc(count * sizeof(int));
}
void delete(int *arr, int count) {
free(arr);
}
输出结果:
2. 比较字符串
参考第六章第14题
代码:
#include "stdio.h"
int strCompare(char *a, char *b);
void main() {
char a[100];
char b[100];
char c[100];
printf("Please input string a:");
gets(a);
printf("Please input string b:");
gets(b);
printf("Please input string c:");
gets(c);
//数组名不等价于指针,不能修改其内容
char *temp;
char *x = a;
char *y = b;
char *z = c;
int result = strCompare(x, y);
if(result > 0) {
temp = x;
x = y;
y = temp;
}
result = strCompare(x, z);
if(result > 0) {
temp = z;
z = x;
x = temp;
}
result = strCompare(y, z);
if(result > 0) {
temp = z;
z = y;
y = temp;
}
printf("sort string:%s,%s,%s", x, y, z);
}
int strCompare(char *a, char *b){
int i = 0;
while (a[i] && b[i])
{
if(a[i] != b[i]) {
return a[i] - b[i];
}
i++;
}
if(a[i] == b[i]) {
return 0;
} else if(a[i] == 0) {
return -26;
} else if(b[i] == 0) {
return 26;
}
return 27;
}
输出结果:
3.最大值和最小值
代码:
#include "stdio.h"
void max(int *arr, int count);
void min(int *arr, int count);
void print(int *arr, int count);
void input(int *arr, int count);
void delete(int *arr, int count);
void main() {
int count = 10;
int *arr = (int *) malloc(count * sizeof(int));
input(arr, count);
print(arr, count);
max(arr, count);
print(arr, count);
min(arr, count);
print(arr, count);
delete(arr, count);
}
void print(int *arr, int count) {
printf("The arr is :");
for(int i = 0; i < count; i++) {
printf("%d,", arr[i]);
}
printf("\n");
}
void input(int *arr, int count) {
//int *start = arr;//形参在函数结束时,会释放掉,并且不会改写原始参数
for(int i = 0; i < count; i++) {
printf("Please input a number:");
scanf("%d", arr++);
}
//arr = start;
}
void delete(int *arr, int count) {
free(arr);
}
void max(int *arr, int count) {
int *max = arr;
for(int i = 1; i < count; i++) {
if(arr[i] > *max) {
max = &arr[i];
}
}
int temp = 0;
temp = *max;
*max = arr[count-1];
arr[count-1] = temp;
}
void min(int *arr, int count) {
int *min = arr;
for(int i = 1; i < count; i++) {
if(arr[i] < *min) {
min = &arr[i];
}
}
int temp = 0;
temp = *min;
*min = arr[0];
arr[0] = temp;
}
输出结果:
4.数字循环右移m位
代码:
#include "stdio.h"
void reverse(int *arr, int count);
void min(int *arr, int count);
void print(int *arr, int count);
void input(int *arr, int *count, int m);
void delete(int *arr, int count);
void main() {
int n = 0;
int m = 3;
int *arr = (int *) malloc (sizeof(int));
input(arr, &n, m);
print(arr, n);
}
void input(int *arr, int *n, int m) {
int num = 0;
printf("Please input a number:");
scanf("%d", &num);
int a = 1;
for(int i = 0; i < m; i++) {
a = a*10;
}
int b = num % a;//a_b = num
a = num/a;
//printf("%d,%d", a, b);
int *p = arr;
while(b)