谭浩强C语言教材Chapter8课后题部分代码

搬运注明出处
题号均在注释,头文件部分为c++的库文件

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <random>
#include <set>
using namespace std;
#define N 100
// c8--4
// void move(int *a, int n, int m)
// {
//     int end = *(a + n - 1), *i;
//     for (i = a + n - 1; i > a; i--)
//     {
//         *i = *(i - 1);
//     }
//     *i = end;
//     m--; // 已经移动了一个,需要减1
//     if (m > 0)
//     {
//         move(a, n, m); // 递归进行,移动下一个
//     }
// }
// int main(int argc, char const *argv[])
// {
//     int a[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
//     int n = 15;
//     int m = 5; // 移动的位数
//     move(a, n, m);
//     for (int i = 0; i < n; i++)
//     {
//         printf("%d ", a[i]);
//     }
//     return 0;
// }

// c8--5报数问题
// void del(int *p, int n, int m, int m1)
// {
//     int k = 0; // 退出人数
//     int index = 0;
//     while (k < n - 1)
//     {
//         if (*(p + index) != 0)
//         {
//             m1++;
//         }
//         if (m1 == 3) // 报号到3n
//         {
//             printf("No.%d ", index + 1);
//             m1 = 0;           // 报号清零
//             *(p + index) = 0; // 位置置为0,表退出
//             k++;              // 退出人数+1
//         }
//         index++;
//         if (index == n) // 处理循环到最后一位
//         {
//             index = 0; // 回到一开始
//         }
//     }
// }
// int main(int argc, char const *argv[])
// {
//     int a[100], *p, n = 11, m = 3, m1; // n个人,报m退出, m1记录报号
//     p = a;
//     for (int i = 0; i < n; i++)
//     {
//         *(p + i) = i + 1;
//     }
//     del(p, n, m, m1);
//     for (int i = 0; i < n; i++)
//     {
//         if (*(p + i) != 0)
//         {
//             printf("\nthe rest is No.%d people.", i + 1);
//         }
//     }
//     return 0;
// }

// c8--6 字符串长度
// void len(char *str, int *len)
// {
//     char *a;
//     a = str;
//     while (*str != '\0')
//     {
//         str++;
//     }
//     *len = str - a;
// }
// int main(int argc, char const *argv[])
// {
//     char a[100] = "abcdefghij";
//     int l = 0;
//     len(a, &l);
//     printf("the length of string is %d\n", l);
//     return 0;
// }

// c8--7 复制部分字符串
// void copysubstr(char *a, char *b, int n)
// {
//     int i = 0;
//     while (i < n)
//     {
//         i++;
//         a++;
//     }
//     while (*a != '\0')
//     {
//         *b++ = *a++;
//     }
//     *b = '\0';
// }
// int main(int argc, char const *argv[])
// {
//     char a[100] = "abcdefghijxyzxyzxyz";
//     char b[100];
//     int n = 6; // f
//     copysubstr(a, b, n);
//     puts(b);// ghijxyzxyzxyz
//     return 0;
// }

// c8--9 3x3矩阵的转置
// void print(int *p, int n)
// {
//     for (int i = 0; i < n; i++)
//     {
//         for (int j = 0; j < n; j++)
//         {
//             printf("%d ", *(p + 3 * i + j));
//         }
//         printf("\n");
//     }
// }
// void move(int (*p)[3], int n)
// {
//     int t;
//     for (int i = 0; i < n; i++)
//     {
//         for (int j = i; j < n; j++)
//         {
//             t = *(*(p + i) + j);
//             *(*(p + i) + j) = *(*(p + j) + i);
//             *(*(p + j) + i) = t;
//         }
//     }
// }
// int main(int argc, char const *argv[])
// {
//     int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
//     int n = 3;
//     move(a, n);
//     print(&a[0][0], n);
//     return 0;
// }

// c8--10
// void print(int *p, int n)
// {
//     for (int i = 0; i < n; i++)
//     {
//         for (int j = 0; j < n; j++)
//         {
//             printf("%d ", *(p + n * i + j));
//         }
//         printf("\n");
//     }
// }
// void move(int (*p)[5], int n)
// {
//     int t;
//     int max = *(*p), min = *(*p);
//     for (int i = 0; i < n; i++)
//     {
//         for (int j = 0; j < n; j++)
//         {
//             if (max < *(*(p + i) + j))
//             {
//                 max = *(*(p + i) + j);
//             }
//             if (min > *(*(p + i) + j))
//             {
//                 min = *(*(p + i) + j);
//             }
//         }
//     }

//     t = *(*(p + 2) + 2);
//     *(*(p + 2) + 2) = max;
//     max = t;

//     t = *(*(p + 0) + 0);
//     *(*(p + 0) + 0) = min;
//     min = *(*(p + 0) + 0);

//     min = *(*(p + 0) + 1);
//     for (int i = 0; i < n; i++)
//     {
//         for (int j = 0; j < n; j++)
//         {
//             if (min == *(*(p + 0) + 0))
//             {
//                 min = *(*(p + i) + j);
//                 continue;
//             }
//             if (min > *(*(p + i) + j) && *(*(p + i) + j) != *(*(p + 0) + 0))
//             {
//                 min = *(*(p + i) + j);
//             }
//         }
//     }
//     cout << min << endl;

//     t = *(*(p + 0) + 4);
//     *(*(p + 0) + 4) = min;
//     min = t;

//     min = *(*(p + 0) + 2);
//     for (int i = 0; i < n; i++)
//     {
//         for (int j = 0; j < n; j++)
//         {
//             if (min == *(*(p + 0) + 0) || min == *(*(p + 0) + 4))
//             {
//                 min = *(*(p + i) + j);
//                 continue;
//             }
//             if (min > *(*(p + i) + j) && *(*(p + i) + j) != *(*(p + 0) + 0) && *(*(p + i) + j) != *(*(p + 0) + 4))
//             {
//                 min = *(*(p + i) + j);
//             }
//         }
//     }
//     cout << min << endl;

//     t = *(*(p + 4) + 0);
//     *(*(p + 4) + 0) = min;
//     min = t;

//     min = *(*(p + 0) + 3);
//     for (int i = 0; i < n; i++)
//     {
//         for (int j = 0; j < n; j++)
//         {
//             if (min == *(*(p + 0) + 0) || min == *(*(p + 0) + 4) || min == *(*(p + 4) + 0))
//             {
//                 min = *(*(p + i) + j);
//                 continue;
//             }
//             if (min > *(*(p + i) + j) && *(*(p + i) + j) != *(*(p + 0) + 0) && *(*(p + i) + j) != *(*(p + 0) + 4) && *(*(p + i) + j) != *(*(p + 4) + 0))
//             {
//                 min = *(*(p + i) + j);
//             }
//         }
//     }
//     cout << min << endl;

//     t = *(*(p + 4) + 4);
//     *(*(p + 4) + 4) = min;
//     min = t;
// }
// int main(int argc, char const *argv[])
// {
//     int a[5][5] = {{11, 1, 15, 1, 51}, {24, 1, 2, 2, 6}, {7, 1, 14, 8, 9}, {11, 14, 3, 44, 8}, {1, 4, 5, 6, 7}};
//     int n = 5;
//     printf("Before:\n");
//     print(&a[0][0], n);
//     move(a, n);
//     printf("After:\n");
//     print(&a[0][0], n);
//     return 0;
// }

// c8-1112
// void print(char (*str)[10], int n)
// {
//     for (int i = 0; i < n; i++)
//     {
//         puts(*(str + i));
//     }
// }
// 一维数组指针
//  void sort(char (*str)[10], int n)
//  {
//      char temp[10], *t;
//      t = temp;
//      for (int i = 0; i < 5; i++)
//      {
//          for (int j = i + 1; j < 5; j++)
//          {
//              if (strcmp(*(str + i), *(str + j)) > 0)
//              {
//                  strcpy(t, *(str + i));
//                  strcpy(*(str + i), *(str + j));
//                  strcpy(*(str + j), t);
//              }
//          }
//      }
//  }

// 12方法
// void sort(char *s[5], int n)
// {
//     char *t;
//     for (int i = 0; i < 5; i++)
//     {
//         for (int j = i + 1; j < 5; j++)
//         {
//             if (strcmp(*(s + i), *(s + j)) > 0)
//             {
//                 strcpy(t, *(s + i));
//                 strcpy(*(s + i), *(s + j));
//                 strcpy(*(s + j), t);
//             }
//         }
//     }
// }
// int main(int argc, char const *argv[])
// {
//     char str[5][10] = {"abcxsd", "abcwsdas", "jakka", "abbmwkk", "cscs"};
//     char *p[5];
//     for (int i = 0; i < 5; i++)
//     {
//         p[i] = str[i];
//     }
//     sort(p, 5);
//     print(str, 5);
//     return 0;
// }

// c8--13
// float fsin(float x)
// {
//     return sin(x);
// }
// void integral(float (*p)(float), float a, float b, int n, float *s)
// {
//     float x, h;
//     h = (b - a) / n;
//     x = a; // 下限
//     *s = 0;
//     for (int i = 1; i <= n; i++)
//     {
//         x += h;
//         *s += (*p)(x)*h; // 求面积
//     }
// }
// int main(int argc, char const *argv[])
// {
//     int n = 200;
//     float a1, a2, sum; // 上下限,s=结果
//     printf("请输入积分的下限 上限:\n");
//     scanf("%f%f", &a1, &a2);
//     float (*p)(float), res;
//     p = fsin;
//     integral(p, a1, a2, n, &sum);
//     printf("result=%f", sum);
//     return 0;
// }

// c8-14
// void print(int *a, int n)
// {
//     for (int i = 0; i < n; i++)
//     {
//         printf("%d ", *(a + i));
//     }
// }
// void sort(int *a, int n)
// {
//     int k, l = n;
//     if (k % 2 == 0)
//     {
//         k = n / 2 - 1;
//     }
//     else
//     {
//         k = n / 2;
//     }
//     for (int i = 0; i < k; i++)
//     {
//         int t;
//         t = *(a + i);
//         *(a + i) = *(a + l - 1);
//         *(a + l - 1) = t;
//         l--;
//     }
// }
// int main(int argc, char const *argv[])
// {
//     int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, n = 10;
//     printf("Before:\n");
//     print(a, n);
//     sort(a, n);
//     printf("\nAfter:\n");
//     print(a, n);
//     return 0;
// }

// c8-16找整数
// int Digit(char *p, int index[], int ind, int length[], int len, int *a)
// {
//     int i=0, j=0, start = 0;
//     while (*(p + i) != '\0')
//     {
//         if (*(p + i) >= '0' && *(p + i) <= '9')
//         {
//             if (start == 0)
//             {
//                 index[ind++] = i; // 起点函数
//                 start = 1;        // 标志位
//             }
//             j++; // 长度
//         }
//         else
//         {
//             if (j != 0)
//             {
//                 length[len++] = j;
//             }
//             j = 0;
//             start = 0;
//         }
//         i++;
//     }
//     if (*(p + i - 1) <= '9' && *(p + i - 1) >= '0')
//     {
//         length[len] = j;
//     }
//     for (int i = 0; i < ind; i++)
//     {
//         int sum = 0;
//         int n = 0;
//         int k = length[i];
//         while (n < length[i])
//         {
//             int b = pow(10, k - 1);
//             sum += (*(p + index[i] + n) - 48) * b;
//             n++;
//             k--;
//         }
//         *(a + i) = sum;
//     }
//     return ind;
// }
// int main(int argc, char const *argv[])
// {
//     char str[100] = "A1232*19+2P--32 1431 Ynnj10TT13231", *p;
//     int a[100], index[100], ind = 0, length[100], len = 0;
//     p = str;
//     ind = Digit(p, index, ind, length, len, a);
//     for (int i = 0; i < ind; i++)
//     {
//         cout << a[i] << endl;
//     }
//     return 0;
// }

// c8-17
// int mystrcmp(char *p1, char *p2)
// {
//     while (*p1 && *p2 && *p1 == *p2)
//     {
//         p1++;
//         p2++;
//     }
//     return *p1 - *p2;
// }
// int main(int argc, char const *argv[])
// {
//     char a[100] = "CHINA", b[100] = "Chen";
//     cout << mystrcmp(a, b);
//     return 0;
// }

// c8--20
// void Sort(char **p)
// {
//     char *temp;
//     for (int i = 0; i < 5; i++)
//     {
//         for (int j = i + 1; j < 5; j++)
//         {
//             if (strcmp(*(p + i), *(p + j)) > 0)
//             {
//                 temp = *(p + i);
//                 *(p + i) = *(p + j);
//                 *(p + j) = temp;
//             }
//         }
//     }
// }
// int main(int argc, char const *argv[])
// {
//     char str[5][10] = {"China", "American", "Asia", "Europe", "English"}, **p, *pstr[5];
//     for (int i = 0; i < 5; i++)
//     {
//         pstr[i] = str[i];
//     }
//     p = pstr;
//     Sort(p);
//     for (int i = 0; i < 5; i++)
//     {
//         cout << pstr[i] << endl;
//     }
//     return 0;
// }

// c8--21
void Sort(int **p, int n)
{
    int *t;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (**(p + i) > **(p + j))
            {
                t = *(p + i);
                *(p + i) = *(p + j);
                *(p + j) = t;
            }
        }
    }
}
int main(int argc, char const *argv[])
{
    int a[10] = {21, 12, 9, 3, 18, 2, 4, 1, 19, 32}, *pa[10], **p, n = 10;
    for (int i = 0; i < n; i++)
    {
        pa[i] = &a[i];
    }
    p = pa;
    Sort(p, n);
    for (int i = 0; i < n; i++)
    {
        cout << *p[i] << " ";
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值