c++ - 这两个功能可以合二为一吗?

标签 c++ arrays sorting

我刚刚完成了一个迷你任务,该任务在下面代码的注释 block 中有所描述,但我一直在尝试通过组合 getRareDigitsdisplayRareDigits 来改进代码 合并到一个函数中。无论我做什么,逻辑总是以失败告终。任何人都可以向我解释这两个功能是否有可能结合使用?谢谢^_^

/*
    Written by: Stephanie Yumiko

* This program will ask the user
for a series of integers, defined by
the user.

* The program will display back to the
user the number of rare digits, digits
that only occur once in a single integer,
but not the rest. 

* The program will then sort the integers
based on the number of occurrences of
rare digits it contains, from greatest
to least.
*/

#include <iostream>
using namespace std;

bool num_contains(int, int);
void showRareDigits(int*, int);
void sortRareDigits(int*, int* , int);

bool num_contains(int digit, int n) {
    while (n) {
        if (digit == n % 10) return true;
        n /= 10;
    }
    return false;
}

void getRareDigits(int *arr, int *ordered, int len) {
    for (int index = 0; index < len; ++index) {
        int n = arr[index];
        if (n < 0)
            n *= -1;
        int d = 0;
        while (n) {
            d = n % 10;
            int i;      // keep track of loop counter outside the loop
            int stop = 0; // to break out loop
            for (i = 0; i < len; ++i) {
                if (i != index && num_contains(d, arr[i]))
                    stop = 1;
            }
            // only increment the array if the loop exited before
            // completing (implying the goto would have happened)
            if (!stop) {
                ++ordered[index];
            }
            // always execute 
            n /= 10;
        }
    }

    for (int i = 0; i<len; i++) {
        for (int j = 0; j<len - i - 1; j++) {
            if (ordered[j]<ordered[j + 1]) {
                int temp = ordered[j];
                ordered[j] = ordered[j + 1];
                ordered[j + 1] = temp;

                int temp2 = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp2;
            }
        }
    }

    cout << "\nArray after sort:\n";
    for (int i = 0; i < len; i++) {
        cout << arr[i] << endl;
    }
}

void showRareDigits(int* iAry, int size) {
    const int size2 = 10;
    int* tmpAry = new int[size];
    int totalCount[size2] = { 0 };
    int currentCount[size2] = { 0 };
    int totalUncommon = 0;
    int i, j;
    int* ordered;
    ordered = new int[size];

    for (i = 0; i < size; i++) {
        ordered[i] = 0;
        tmpAry[i] = iAry[i];
        if (tmpAry[i] < 0)
            tmpAry[i] *= -1;

        for (j = 0; j < size2; j++)
            currentCount[j] = 0;

        if (tmpAry[i] == 0) {
            currentCount[0] = 1;
        }

        while (tmpAry[i] / 10 != 0 || tmpAry[i] % 10 != 0){
            currentCount[tmpAry[i] % 10] = 1;
            tmpAry[i] /= 10;
        }

        for (j = 0; j < size2; j++) {
            totalCount[j] += currentCount[j];
        }
    }

    for (i = 0; i < size2; i++) {
        if (totalCount[i] == 1) {
            totalUncommon++;
        }
    }

    cout << "\nTotal rare digits: " << totalUncommon << endl
        << "\nThe rare digits:\n";
    if (totalUncommon == 0) {
        cout << "\nNo rare digits found.";
    }
    else {
        for (i = 0; i < size2; i++) {
            if (totalCount[i] == 1) {
                cout << i << endl;
            }
        }
    }

    getRareDigits(iAry, ordered, size);

    delete[] tmpAry;
    delete[] ordered;

    return;
}


int main() {    
    int size;
    int* arr;

    cout << "Enter # of integers: ";
    cin >> size;
    arr = new int[size];

    for (int i = 0; i < size; i++) {
        cout << "Enter the value for #" << i << " : ";
        cin >> arr[i];
    }

    cout << "Array before sorting:\n";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << endl;
    }

    showRareDigits(arr, size);  

    delete[] arr;

    return 0;
}

最佳答案

你的两个函数既大又笨重。有时这很难避免,但将它们合二为一并不是一个好主意。

相反,尝试弄清楚哪些逻辑对它们是通用的,并将其放入您可以从 get... 和 display... 函数中使用的单独函数中。

您还应该查看continuebreak 以跳出循环。尽管普遍认为 goto 是跳出多个循环级别的可行选项,并且可用于简化代码并使其更短且更易于理解。

关于c++ - 这两个功能可以合二为一吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33141336/

相关文章:

javascript - 数组洗牌,但保持相等值之间的距离

c++ - 调试时 Kubuntu 中的繁忙进程

c++ - cocos2dx v3 粒子动画

c - 如何在 C 程序中将一个长整数与不同的数字相乘?

arrays - 可以只复制数组的值吗?

php - Laravel - 不允许对图像设置为 Nullable 的请求验证并给出错误原因?

python - 在python中,按日期字段排序,字段有时可能为空

perl - 使用 Perl 按特定字母顺序对字符串进行排序

c++ - C++ 11 中的锁是否保证访问数据的新鲜度?

c++ - 提升 : copy_file fail with access denied but there are no permission problem