《数组与指针程序设计》——小组作业(21-32)

21. 输入一串字符统计其中大写字母、小写字母、数字的个数,输出结果。

#include <iostream>

using namespace std;

void BookStr (string str);

int main (void)
{
    string str;
    cout << "Please input a string: " << endl;
    cin >> str;

    BookStr (str);

    system ("pause");
    return 0;
}

void
BookStr (string str)
{
    int CntLow = 0;
    int CntUpp = 0;
    int CntNum = 0;

    const char *p = NULL;
    p = str.c_str ();

    while (*p != '\0')
    {
        if (*p >= 'a' && *p <= 'z')
        {
            CntLow ++;
        }
        else if (*p >= 'A' && *p <= 'Z')
        {
            CntUpp ++;
        }
        else if (*p >= '0' && *p <= '9')
        {
            CntNum ++;
        }

        p ++;
    }

    printf ("The string has %d numbers. \n", CntNum);
    printf ("The string has %d lowercase. \n", CntLow);
    printf ("The string has %d uppercase. \n", CntUpp);
}

22.输入两个3阶方阵,计算两个方阵之差,以数学表示方式输出。

#include <iostream>

using namespace std;

#define ROW 3
#define COL 3

int **Create (void);
void Input (int **p);
int **Subtract (int **p, int **q);
void Output (int **p);

int main (void)
{
    int **matrix1 = Create ();
    printf ("Please input numbers in first %d x %d matrix: \n", ROW, COL);
    Input (matrix1);
    printf ("\n");

    int **matrix2 = Create ();
    printf ("Please input numbers in second %d x %d matrix: \n", ROW, COL);
    Input (matrix2);
    printf ("\n");

    printf ("The difference between the two matrices is: \n");

    int **submatrix = Subtract (matrix1, matrix2);

    Output (submatrix);

    free (matrix1);
    free (matrix2);
    free (submatrix);

    system ("pause");
    return 0;
}

int
**Create (void)
{
    int **matrix = (int **)malloc (sizeof (int *) * ROW);

    for (int i = 0; i < COL; i ++)
    {
        matrix[i] = (int *)malloc (sizeof (int) * COL);
    }

    return matrix;
}

void
Input (int **p)
{
    for (int i = 0; i < ROW; i ++)
    {
        for (int j = 0; j < COL; j ++)
        {
            cin >> *(*(p + i) + j);
        }
    }
}

int
**Subtract (int **p, int **q)
{
    int **submatrix = (int **)malloc (sizeof (int *) * ROW);

    for (int i = 0; i < ROW; i ++)
    {
        submatrix[i] = (int *)malloc (sizeof (int) * COL);

        for (int j = 0; j < COL; j ++)
        {
            *(*(submatrix + i) + j) = *(*(p + i) + j) - *(*(q + i) + j);
        }
    }

    return submatrix;
}

void
Output (int **p)
{
    for (int i = 0; i < ROW; i ++)
    {
        for (int j = 0; j < COL; j ++)
        {
            printf ("%d ", *(*(p + i) + j));
        }

        printf ("\n");
    }
}

23.编程输出1000(测试用20)个数组元素的前10个最大的数。

#include <iostream>

using namespace std;

#define MAXN 1000

void MySwap (int *p1, int *p2);
void MySort (int *array, int begin, int end, int size);
void MyQuickSort (int *array, int begin, int end);
void MyPrint (int *array, int size);

int main (void)
{
    int nums[MAXN];
    int *p = NULL;

    printf ("Please input %d number: \n", MAXN);
    for (p = nums; p < nums + MAXN; p ++)
    {
        cin >> *p;
    }

    int size;
    cout << "Please enter how many numbers you need: " << endl;
    cin >> size;

    MySort (nums, 0, MAXN - 1, size);
    MyQuickSort (nums, MAXN - size, MAXN - 1);

    MyPrint (nums, size);

    system ("pause");
    return 0;
}

void
MySwap (int *p1, int *p2)
{
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

void
MySort (int *array, int begin, int end, int size)
{
    int temp = *(array + begin);

    int left = begin, right = end;
    while (left < right)
    {
        while (right > left && *(array + right) >= temp)
        {
            right --;
        }

        MySwap (array + left, array + right);

        while (right > left && *(array + left) < temp)
        {
            left ++;
        }

        MySwap (array + left, array + right);
    }


    int len = end - right + 1;

    if (len == size)
    {
        return;
    }
    else if (len > size)
    {
        MySort (array, right + 1, end, size);
    }
    else
    {
        MySort (array, begin, right - 1, size - len);
    }
}

void
MyQuickSort (int *array, int begin, int end)
{
    int i, j;

    if (begin < end)
    {
        i = begin + 1;
        j = end;

        while (i < j)
        {
            if (array[i] > array[begin])
            {
                MySwap (array + i, array + j);

                j --;
            }
            else
            {
                i ++;
            }
        }

        if (array[i] >= array[begin])
        {
            i --;
        }

        MySwap (array + begin, array + i);

        MyQuickSort (array, begin, i);
        MyQuickSort (array, j, end);
    }
}

void
MyPrint (int *array, int size)
{
    int *p = NULL;

    printf ("The numbers that you need: \n");

    for (p = array + MAXN - 1; p > array + MAXN - 1 - size; p --)
    {
        printf ("%d ", *p);
    }

    printf ("\n");
}

24.输入两个二维矩阵(阶数自定),求其乘积并输出。

#include <iostream>
#include <stdlib.h>

using namespace std;

int **Create (int row, int col);
void MyInput (int **p, int row, int col);
int **Multiply (int **p, int row1, int col1, int **q, int row2, int col2);
void Initialize (int **p, int row, int col);
void MyPrint (int **p, int row, int col);

int main (void)
{
    int row1, col1;
    cout << "Please input the first matrix row & col: " << endl;
    scanf ("%d%d", &row1, &col1);

    int **matrix1 = Create (row1, col1);
    MyInput (matrix1, row1, col1);

    int row2, col2;
    cout << "Please input the second matrix row & col: " << endl;
    cin >> row2 >> col2;

    int **matrix2 = Create (row2, col2);
    MyInput (matrix2, row2, col2);

    int **mulMatrix = Multiply (matrix1, row1, col1, matrix2, row2, col2);

    printf ("The result is: \n");
    MyPrint (mulMatrix, row1, col2);

    free (matrix1);
    free (matrix2);
    free (mulMatrix);

    system ("pause");
    return 0;
}

int **Create (int row, int col)
{
    int **matrix = (int **)malloc (sizeof (int *) * row);

    for (int i = 0; i < col; i ++)
    {
        matrix[i] = (int *)malloc (sizeof (int) * col);
    }

    return matrix;
}

void
MyInput (int **p, int row, int col)
{
    printf ("Please input numbers in %d x %d matirx: \n", row, col);

    for (int i = 0; i < row; i ++)
    {
        for (int j = 0; j < col; j ++)
        {
            scanf ("%d", (p + i) + j);
        }
    }
}

void
Initialize (int **p, int row, int col)
{
    for (int i = 0; i < row; i ++)
    {
        for (int j = 0; j < col; j ++)
        {
            *(*(p + i) + j) = 0;
        }
    }
}

int
**Multiply (int **p, int row1, int col1, int **q, int row2, int col2)
{
    int **mulMatrix = Create (row1, col2);
    Initialize (mulMatrix, row1, col2);

    for (int i = 0; i < row1; i ++)
    {
        for (int j = 0; j < col2; j ++)
        {
            for (int k  = 0; k < row2; k ++)
            {
                mulMatrix[i][j] += (p[i][k] * q[k][j]);
            }
        }
    }

    return mulMatrix;
}

void
MyPrint (int **p, int row, int col)
{
    for (int i = 0; i < row; i ++)
    {
        for (int j = 0; j < col; j ++)
        {
            printf ("%4d ", *(*(p + i) + j));
        }

        printf ("\n");
    }
}

25.输入一0、1组成的二进制字符串(不超过32位),将其转换为无符号整型,并输出10进制的值。

#include <iostream>

using namespace std;

#define MAXN 32
#define BIN 2

int MyPow (int num, int n);
int MyStrlen (char *str);
unsigned int BinToDec (char *str);

int main (void)
{
    char Bin[MAXN];

    printf("Please input Bin (< %d): ", MAXN);
    cin.getline (Bin, MAXN);

    printf ("The Bin: %s to Dec: %u \n", Bin, BinToDec (Bin));

    system ("pause");
    return 0;
}

int
MyPow (int num, int n)
{
    int iRes = 1;

    for (int i = 0; i < n; i ++)
    {
        iRes *= num;
    }

    return iRes;
}

int
MyStrlen (char *str)
{
    if (str == NULL)
    {
        return -1;
    }

    int returnSize = 0;

    while (*str != '\0')
    {
        returnSize ++;
        *str ++;
    }

    return returnSize;
}


unsigned int
BinToDec (char *str)
{
    int len = MyStrlen (str);
    unsigned int iAns = 0;

    char *p = NULL;
    p = str + len - 1;

    while (p - str >= 0)
    {
        if (*p >= '0' && *p <= '1')
        {
            iAns += (*p - '0') * MyPow (BIN, len - (p - str) - 1);
        }


        p --;
    }

    return iAns;
}

26.输入10个英文的国名,按升序排序输出。

#include <iostream>

using namespace std;

#define MAXN 10

int main (void)
{
    string contry[MAXN];

    printf ("Please input %d contry name: \n", MAXN);
    for (int i = 0; i < MAXN; i ++)
    {
        cin >> contry[i];
    }

    for (int i = 0; i < MAXN - -1; i ++)
    {
		for(int j = i + 1; j < MAXN; j ++)
		{
			if (contry[j] < contry[i])
			{
				contry[i].swap(contry[j]);
			}
		}
	}

	printf ("After sort: \n");

	for (int i = 0; i < MAXN; i ++)
    {
		cout << contry[i] << endl;
	}

	system ("pause");
    return 0;
}

27.用随机函数(自查功能和用法)生成20个数存放到数组中并按降序排序,从键盘输入一个数,完成在数组中的二分查找。

#include <iostream>
#include <ctime>

using namespace std;

void MySwap (int *p1, int *p2);
void MyQuickSort (int *array, int begin, int end);
int BinarySearch (int *p, int target, int begin, int end);
int *GetRandomArray (int numsSize);
void MyPrint (int *p, int numsSize);

int main (void)
{
    int numsSize;
    cout << "How mang randon numbers do you need? " << endl;
    cin >> numsSize;

    int *returnNums = GetRandomArray (numsSize);

    MyPrint (returnNums, numsSize);

    printf ("\n");

    int target;
    cout << "Please input teh target number: " << endl;
    cin >> target;

    printf ("\n");

    printf ("After sort: \n");
    MyQuickSort (returnNums, 0, numsSize - 1);

    MyPrint (returnNums, numsSize);

    printf ("\n");

    int iPos = BinarySearch (returnNums, target, 0, numsSize - 1);

    if (iPos == -1)
    {
        printf ("Not found! \n");
    }
    else
    {
        printf ("The target located in %d \n", iPos);
    }

    system ("pause");
    return 0;
}

void
MySwap (int *p1, int *p2)
{
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

int
*GetRandomArray (int numsSize)
{
    int *returnNums = new int [numsSize];

    for (int i = 0; i < numsSize; i ++)
    {
        *(returnNums + i) = i;
    }

    for (int j = numsSize - 1; j >= 1; j --)
    {
        MySwap (returnNums + j, returnNums + (rand() % j));
    }

    return returnNums;
}

void
MyQuickSort (int *array, int begin, int end)
{
    int i, j;

    if (begin < end)
    {
        i = begin + 1;
        j = end;

        while (i < j)
        {
            if (array[i] > array[begin])
            {
                MySwap (array + i, array + j);

                j --;
            }
            else
            {
                i ++;
            }
        }

        if (array[i] >= array[begin])
        {
            i --;
        }

        MySwap (array + begin, array + i);

        MyQuickSort (array, begin, i);
        MyQuickSort (array, j, end);
    }
}

int
BinarySearch (int *p, int target, int begin, int end)
{
    int left = begin, right = end;

    while (left <= right)
    {
        int mid = left + (right - left) / 2;

        if (*(p + mid) > target)
        {
            right = mid - 1;
        }
        else if (*(p + mid) < target)
        {
            left = mid + 1;
        }
        else
        {
            return mid;
        }
    }

    return -1;
}

void
MyPrint (int *p, int numsSize)
{
    int *q = NULL;

    for (q = p; q < p + numsSize; q ++)
    {
        printf ("%d ", *q);
    }
}

 28.定义一个20个元素的数组,给出19个数,排序。输入一个数将其插入到数组中,插入后数组依然有序.

#include <iostream>

using namespace std;

#define MAXN 20

void MySwap (int *p1, int *p2);
void Insert (int *array, int size, int target);
void MyQuickSort (int *array, int begin, int end);
void MyPrint (int *array, int numsSize);

int main (void)
{
    int array[MAXN];

    printf ("Please input %d numbers: \n", MAXN - 1);

    for (int *p = array; p < array + MAXN - 1; p ++)
    {
        cin >> *p;
    }

    printf ("\n");

    printf ("After sorting \n");
    MyQuickSort (array, 0, MAXN - 2);
    MyPrint (array, MAXN - 1);

    printf ("\n");

    int target;
    printf ("Please set target: \n");
    cin >> array[MAXN - 1];
    target = array[MAXN - 1];

    Insert (array, MAXN, target);

    MyPrint (array, MAXN);

    printf ("\n");

    system ("pause");
    return 0;
}

void
MySwap (int *p1, int *p2)
{
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

void
Insert (int *array, int size, int target)
{
    for (int i = 0; i < size - 1; i ++)
    {
        int iCur = *(array + i);
        int iNex = *(array + i + 1);

        if (iCur <= target && iNex >= target)
        {
            int *p = array + size - 1;
            int *q = p - 1;

            while (q != array + i)
            {
                MySwap (q --, p --);
            }

            return;
        }

    }
}

void
MyQuickSort (int *array, int begin, int end)
{
    int i, j;

    if (begin < end)
    {
        i = begin + 1;
        j = end;

        while (i < j)
        {
            if (array[i] > array[begin])
            {
                MySwap (array + i, array + j);

                j --;
            }
            else
            {
                i ++;
            }
        }

        if (array[i] >= array[begin])
        {
            i --;
        }

        MySwap (array + begin, array + i);

        MyQuickSort (array, begin, i);
        MyQuickSort (array, j, end);
    }
}

void
MyPrint (int *array, int numsSize)
{
    int *p = NULL;

    for (p = array; p < array + numsSize; p ++)
    {
        printf ("%d ", *p);
    }
}

29. 输入一个字符串,将其倒序存放到原数组。

#include <iostream>

using namespace std;

int MyStrlen (char *str);
void MySwap (char *p1, char *p2);
void ReverseStr (char *str);

int main (void)
{
    char str[80];
    cout << "Please input string (< 80): " << endl;
    cin.getline (str, 80);

    ReverseStr (str);

    cout << "The result is " << str << endl;

    system ("pause");
    return 0;
}

int
MyStrlen (char *str)
{
    if (str == NULL)
    {
        return -1;
    }

    int returnSize = 0;

    while (*str != '\0')
    {
        returnSize ++;
        *str ++;
    }

    return returnSize;
}

void
MySwap (char *p1, char *p2)
{
    char temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}


void
ReverseStr (char *str)
{
    int len = MyStrlen (str);

    char *p = NULL;
    char *q = NULL;

    p = str;
    q = str + len - 1;

    while (p < q)
    {
        MySwap (p ++, q --);
    }
}

30.定义20个元素的整型数组并赋初值。输入一个数,如果该数在数组中存在,则删除该数,其后的每个数前移一位,最后补0。

#include <iostream>

using namespace std;

#define MAXN 20

int main (void)
{
    int array[MAXN];

    for (int *p = array; p < array + MAXN; p ++)
    {
        *p = p - array;

        printf ("%d ", *p);
    }

    int target;
    cout << '\n' << "Input number that you want to delete: " << endl;
    cin >> target;

    for (int i = 0; i < MAXN; i ++)
    {
        if (array[i] == target)
        {
            while (i < MAXN)
            {
                if (i < MAXN - 1)
                {
                    array[i] = array[i + 1];

                    printf ("%d ", array[i]);
                }
                else
                {
                    array[i] = 0;

                    printf ("%d ", array[i]);
                }

                i ++;
            }
        }
        else
        {
            printf ("%d ", array[i]);
        }
    }

    system ("pause");
    return 0;
}

31.统计一个字符串中每个数字(‘0’~‘9’)字符的个数。

#include <iostream>

using namespace std;

int main (void)
{
    char str[80];
    int book[10] = {0};

    cout << "Please input string (< 80): " << endl;
    cin.getline (str, 80);

    char *p = NULL;
    p = str;

    while (*p != '0')
    {
        if (*p >= '0' && *p <= '9')
        {
            book[*p - '0'] ++;
        }

        p ++;
    }

    for (int j = 0; j < 10; j ++)
    {
        printf ("The number %d appear %d time(s) !\n", j, book[j]);
    }

    system ("pause");
    return 0;
}

32.在一个字符串中查找第一个子串,并输出第一个子串的下标。        

 如:This  is  a  test message! 为字符串,子串为:is          

则输出为2。        

不能用系统的字符串函数实现!)

#include <iostream>

using namespace std;

#define MAXN 80

typedef struct
{
    char date[MAXN];
    int length;
}
MyString;

int MyStrlen (char *str);
void GetNext (MyString str, int *next);
int KMP (MyString str1, MyString str2);

int main (void)
{
    MyString str1;

    printf ("Please input main-string (< %d): \n", MAXN);
    cin.getline (str1.date, MAXN);

    str1.length = MyStrlen (str1.date);

    printf ("\n");

    MyString str2;

    printf ("Please input sub-string (< %d): \n", MAXN);
    cin.getline (str2.date, MAXN);

    str2.length = MyStrlen (str2.date);

    printf ("\n");

    int iPos = KMP (str1, str2);

    if (iPos == -1)
    {
        printf ("Not found! \n");
    }
    else
    {
        printf ("Located in %d \n", iPos);
    }

    system ("pause");
    return 0;
}

int
MyStrlen (char *str)
{
    if (str == NULL)
    {
        return -1;
    }

    int returnSize = 0;

    while (*str != '\0')
    {
        returnSize ++;
        *str ++;
    }

    return returnSize;
}


void
GetNext (MyString str, int *next)
{
    int i = 0, j = -1;

    next[i] = -1;

    while (i < str.length)
    {
        if (j == -1 || str.date[i] == str.date[j])
        {
            i ++;
            j ++;

            if (str.date[i] != str.date[j])
            {
                next[i] = j;
            }
            else
            {
                next[i] = next[j];
            }
        }
        else
        {
            j = next[j];
        }
    }
}

int
KMP (MyString str1, MyString str2)
{
    int next[MAXN];
    GetNext (str2, next);

    int i = 0, j = 0;
    while (i < str1.length && j < str2.length)
    {
        if (j == -1 || str1.date[i] == str2.date[j])
        {
            i ++;
            j ++;
        }
        else
        {
            j = next[j];
        }
    }

    if (j >= str2.length)
    {
        return i - str2.length;
    }
    else
    {
        return -1;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘的企鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值