作业:1其他.c
#include
#include
#include
#include
// 素数
// 完数
// 递归
// 图形打印
void Su(int num){
for(int i = 2; i <= sqrt(num); i++){
if(num % i == 0){
printf("not a sushu.\n");
return ;
}
}
printf("is a sushu.\n");
return ;
}
void Wan(int num){
int sum = 0;
for(int i = 1; i < num; i++){
if(num % i == 0){
sum += i;
}
}
if(sum == num){
printf("is a wanshu.\n");
} else {
printf("not a wanshu.\n");
}
return ;
}
int Jie(int num){
if(num == 1){
return 1;
} else {
return num*Jie(num-1);
}
}
int Jia(int num){
if(num == 1){
return 1;
} else {
return num+Jia(num-1);
}
}
int Fei(int num){
if(num == 1 || num == 2){
return 1;
} else {
return Fei(num-1)+Fei(num-2);
}
}
void PrintStar(int num){
for(int i = 0; i < num; i++){
for(int j = 0; j <= i; j++)
putchar('*');
putchar('\n');
}
for(int i = num; i > 0; i--){
for(int j = 0; j <= i; j++)
putchar('*');
putchar('\n');
}
for(int i = 0; i < num; i++){
for(int j = 0; j < num-i; j++){
putchar(' ');
}
for(int j = 0; j <= i; j++){
putchar('*');
}
putchar('\n');
}
for(int i = num; i > 0; i--){
for(int j = 0; j < num-i; j++){
putchar(' ');
}
for(int j = 0; j <= i; j++){
putchar('*');
}
putchar('\n');
}
}
int main(int argc, char const *argv[])
{
int num = 0;
printf("Please give your num : ");
scanf("%d", &num);
#if 0
{
Su(num);
}
#endif
#if 0
{
Wan(num);
}
#endif
#if 0
{
printf("result : %d\n", Jie(num));
}
#endif
#if 0
{
printf("result : %d\n", Jia(num));
}
#endif
#if 0
{
for(int i = 1; i <= num; i++){
printf("%d ", Fei(i));
}
printf("\n");
}
#endif
#if 0
{
PrintStar(num);
}
#endif
return 0;
}
依次为Su,Wan,Jie,Jia,Fei,PrintStar
作业:2一维数组.c
#include
#include
#include
// 一维数组:
// 最值问题指针,FindMax
// 冒泡指针,SortMao
// 简单选择指针,SortXuan
// 交换数组最大最小值
void Input(int num, int *arr){
for(int i = 0; i < num; i++){
printf("arr[%d]:", i);
scanf("%d", arr+i);
}
}
void Output(int num, int *arr){
for(int i = 0; i < num; i++){
printf("arr[%d]:%d\n", i, *(arr+i));
}
}
int *FindMax(int num, int *arr);// ok
void SortMao(int num, int *arr);// ok
void SortXuan(int num, int *arr);// ok
void SwapM(int num, int *arr);
void Swap(int *num1, int *num2){
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int main(int argc, const char *argv[]){
int num = 0;
printf("Please input num : ");
scanf("%d", &num);
int arr[num];
Input(num, arr);
#if 0
{
printf("MAX : %d\n", *FindMax(num, arr));
}
#endif
#if 0
{
SortMao(num, arr);
}
#endif
#if 0
{
SortXuan(num, arr);
}
#endif
#if 0
{
SwapM(num, arr);
}
#endif
printf("After op : \n");
Output(num, arr);
return 0;
}
int *FindMax(int num, int *arr){
int *p = arr;
for(int i = 0; i < num; i++){
if(*p < *(arr+i)){
p = arr + i;
}
}
return p;
}
void SortMao(int num, int *arr){
for(int i = 0; i < num-1; i++){
int count = 0;
for(int j = 0; j < num-1-i; j++){
if(*(arr+j) < *(arr+j+1)){
Swap(arr+j, arr+j+1);
count++;
}
}
if(count == 0){
break;
}
}
}
void SortXuan(int num, int *arr){
for(int i = 0; i < num-1; i++){
int *max = arr+i;
for(int j = i+1; j < num-i; j++){
if(*max < *(arr+j)){
max = arr+j;
}
}
if(max != arr+i){
Swap(max, arr+i);
}
}
}
void SwapM(int num, int *arr){
int *max = arr;
int *min = arr;
for(int i = 0; i < num; i++){
if(*max < *(arr+i)){
max = arr+i;
}
if(*min > *(arr+i)){
min = arr+i;
}
}
Swap(max, min);
}
依次为FindMax,SortMao,SortXuan,SwapM
作业:3二维数组.c
#include
#include
#include
// 二维数组:
// 最值、最值的下标指针,
// 杨辉三角,
// 对角线求和
void Input2(int row, int column, int (*arr)[column]){
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
printf("arr[%d][%d] : ", i, j);
scanf("%d", *(arr+i)+j);
}
}
}
void Output2(int row, int column, int (*arr)[column]){
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
printf("arr[%d][%d] : %d\n", i, j, *(*(arr+i)+j));
}
}
}
int *FindM(int row, int column, int (*arr)[column], int flag);
void Yang(int line, int (*arr)[line]);
int DiaSum(int line, int (*arr)[line], int flag);
int main(int argc, char const *argv[])
{
#if 0
{
int row = 0, column = 0;
printf("Please input row and column : ");
scanf("%d %d", &row, &column);
int arr[row][column];
Input2(row, column, arr);
#if 1
{
int *max = FindM(row, column, arr, 0);
int *min = FindM(row, column, arr, 1);
printf("max = %d\n", *max);
printf("min = %d\n", *min);
}
#endif
printf("After op : \n");
Output2(row, column, arr);
}
#endif
#if 0
{
int line = 0;
printf("Please input your line : ");
scanf("%d", &line);
int arr[line][line];
Yang(line, arr);
}
#endif
#if 0
{
int line = 0;
printf("Please input your line : ");
scanf("%d", &line);
int arr[line][line];
Input2(line, line, arr);
#if 0
{
int sum1 = DiaSum(line, arr, 1);
int sum2 = DiaSum(line, arr, 0);
printf("main = %d\n", sum1);
printf("fu = %d\n", sum2);
}
#endif
printf("After op : \n");
Output2(line, line, arr);
}
#endif
return 0;
}
int *FindM(int row, int column, int (*arr)[column], int flag){
int *max = *arr;
int *min = *arr;
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++){
if(*max < *(*(arr+i)+j)){
max = *(arr+i)+j;
}
if(*min > *(*(arr+i)+j)){
min = *(arr+i)+j;
}
}
}
if(flag){
return min;
} else {
return max;
}
}
void Yang(int line, int (*arr)[line]){
for(int i = 0; i < line; i++){
for(int k = line-i; k > 0; k--){
putchar(' ');
}
for(int j = 0; j <= i; j++){
if(j == 0 || j == i){
*(*(arr+i)+j) = 1;
} else {
*(*(arr+i)+j) = *(*(arr+i-1)+j) + *(*(arr+i-1)+j-1);
}
printf("%d ", *(*(arr+i)+j));
}
printf("\n");
}
}
int DiaSum(int line, int (*arr)[line], int flag){
int sum1 = 0;
int sum2 = 0;
for(int i = 0; i < line; i++){
sum1 += *(*(arr+i)+i);
sum2 += *(*(arr+i)+(line-i-1));
}
if(flag){
return sum1;
} else {
return sum2;
}
}
依次为FindM,Yang,DiaSum
作业:4字符串.c
#include
#include
#include
// 字符串:
// 4个字符串函数【非函数实现】重点
// 字符串单词逆置 指针
// strlen
int my_strlen(char *str);// ok
// strcpy
char *my_strcpy(char *s1, char *s2);// ok
// strcat
char *my_strcat(char *s1, char *s2);// ok
// strcmp
int my_strcmp(char *s1, char *s2);// ok
// reverse
char *my_strrev(char *str);// ok
int main(int argc, char const *argv[])
{
#if 0
{
char str[50] = "";
printf("Please input your str : ");
gets(str);
#if 0
{
int len = my_strlen(str);
printf("length : %d\n", len);
}
#endif
#if 0
{
printf("str after reverse : %s\n", my_strrev(str));
}
#endif
}
#endif
#if 0
{
char str1[50] = "";
char str2[50] = "";
printf("Please input your str1 : ");
gets(str1);
printf("Please input your str2 : ");
gets(str2);
#if 0
{
puts(str1);
puts(str2);
printf("str1 after cpy : %s\n", my_strcpy(str1, str2));
}
#endif
#if 0
{
puts(str1);
puts(str2);
printf("str1 after cat : %s\n", my_strcat(str1, str2));
}
#endif
#if 0
{
puts(str1);
puts(str2);
printf("str after compare : %d\n", my_strcmp(str1, str2));
}
#endif
}
#endif
return 0;
}
int my_strlen(char *str){
int len = 0;
while(*str){
len++;
str++;
}
return len;
}
char *my_strcpy(char *s1, char *s2){
char *temp = s1;
while(*s2){
*s1 = *s2;
s1++;
s2++;
}
*s1 = *s2;
return temp;
}
char *my_strcat(char *s1, char *s2){
char *temp = s1;
while(*s1){
s1++;
}
while(*s2){
*s1 = *s2;
s1++;
s2++;
}
*s1 = *s2;
return temp;
}
int my_strcmp(char *s1, char *s2){
while(*s1 == *s2){
if(!*s1)
break;
s1++;
s2++;
}
return *s1-*s2;
}
char *my_strrev(char *str){
char *head = str;
char *tail = str;
// step1
while(*tail){
tail++;
}
tail--;
while(head < tail){
char temp = *head;
*head = *tail;
*tail = temp;
head++;
tail--;
}
// step2
head = tail = str;
char *space = str;
while(*tail){
while(*tail != ' ' && *tail){
tail++;
}
space = tail;
tail--;
while(head < tail){
char temp = *head;
*head = *tail;
*tail = temp;
head++;
tail--;
}
while(*space == ' '){
space++;
}
head = tail = space;
}
return str;
}
依次为my_strlen,my_strrev,my_strcpy,my_strcat,my_strcmp
作业:5二维字符数组.c
#include
#include
#include
// 二维字符数组:
// 二维字符数组的排序 Sort2 ascii
// 二维字符数组的查找 Search2 cmp
void Sort2(int line, char (*str)[20], int flag);
int Search(int line, char (*str)[20], char *key);
int main(int argc, char const *argv[]){
#if 0
{
int line = 0;
printf("Please set the line : ");
scanf("%d", &line);
getchar();
printf("your num of str : %d\n", line);
char str[line][20];
for(int i = 0; i < line; i++){
printf("%d : ", i);
gets(str[i]);
}
printf("OP test : \n");
#if 0
{
Sort2(line, str, 1);
// Sort2(line, str, 0);
}
#endif
#if 0
{
char key[20] = "";
printf("Please set the key : ");
gets(key);
int count = Search(line, str, key);
printf("Count : %d\n", count);
}
#endif
printf("Result : \n");
for(int i = 0; i < line; i++){
puts(str[i]);
}
}
#endif
return 0;
}
void Sort2(int line, char (*str)[20], int flag){
for(int i = 0; i < line-1; i++){
char *max = str[i], *min = str[i];
for(int j = i+1; j < line; j++){
if(strcmp(max, str[j]) < 0){
max = str[j];
}
if(strcmp(min, str[j]) > 0){
min = str[j];
}
}
if(max != str[i] && flag){// 1
char temp[20] = "";
strcpy(temp, max);
strcpy(max, str[i]);
strcpy(str[i], temp);
}
if(min != str[i] && !flag){// 0
char temp[20] = "";
strcpy(temp, min);
strcpy(min, str[i]);
strcpy(str[i], temp);
}
}
}
int Search(int line, char (*str)[20], char *key){
int count = 0;
for(int i = 0; i < line; i++){
char *p = str[i];
while(*p){
char *k = key;
char *t = p;
while(*t == *k && *k){
if(*(k+1) == '\0'){
// printf("%c\n", *t);// print for test
count++;
}
t++;
k++;
}
p++;
}
}
return count;
}
依次为Sort2,Search