gpt4 book ai didi

使用不同的输入(数组)调用 C++ 相同的函数,如何缩小我的语句?

转载 作者:行者123 更新时间:2023-11-30 04:06:19 27 4
gpt4 key购买 nike

我尝试使用插入排序对 7 个不同大小的数组进行排序。

在 main() 中,我知道重复调用同一个函数看起来很愚蠢,但我真的想不出一种方法来简化我的代码。

此外,我还利用 switch cases 生成了不同大小的数组。这是明智的做法吗?

希望有人能帮助我。

我在这里发布了我的代码。:

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;

vector<int> n = { 100, 500, 1000, 2000, 5000, 8000, 10000 }; //Different input size
const int N = 20000; //random scale
int n0[100], n1[500], n2[1000], n3[2000], n4[5000], n5[8000], n6[10000]; //initialize input arrays
int *myArray[] = { n0, n1, n2 };



bool isAlreadyAdded(int value, int index, int *pointer)
{
for (int i = 0; i < index; i++)
{
if (*pointer == value)
return true;
pointer++;
}
return false;
}

void generator(int size){

int input_size = n[size];
int *p, *p2; //create a pointer point to the arrays which we want to manipulate.

switch (input_size)
{
case 100:
p = n0;
p2 = n0;
//cout << "p = n0 :" << *p << endl;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;

case 500:
p = n1;
p2 = n1;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 1000:
p = n2;
p2 = n2;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 2000:
p = n3;
p2 = n3;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 5000:
p = n4;
p2 = n4;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 8000:
p = n5;
p2 = n5;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;
case 10000:
p = n6;
p2 = n6;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
break;

default:
cout << "Invalid input_size" << endl;
} //end swith cases.
}

void insertion_sort(int arr[], int length){
int j, temp;

for (int i = 0; i < length; i++){
j = i;

while (j > 0 && arr[j] < arr[j - 1]){
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
}

void writeToFile(int array[], string fileName, int length)
{
ofstream myfile;
myfile.open(fileName);
for (int i = 0; i < length; ++i)
{
myfile << array[i] << endl;
}
myfile.close();
}



int main() {

for (int i = 0; i < n.size(); ++i){
generator(i); // Parameter is the input size
}


insertion_sort(n0, sizeof(n0) / sizeof(n0[0]));
insertion_sort(n1, sizeof(n1) / sizeof(n1[0]));
insertion_sort(n2, sizeof(n2) / sizeof(n2[0]));
insertion_sort(n3, sizeof(n3) / sizeof(n3[0]));
insertion_sort(n4, sizeof(n4) / sizeof(n4[0]));
insertion_sort(n5, sizeof(n5) / sizeof(n5[0]));
insertion_sort(n6, sizeof(n6) / sizeof(n6[0]));

//writeToFile(n0, "n0", sizeof(n0) / sizeof(n0[0]));


cin.ignore();
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

最佳答案

关于:

添加索引:

#include <map>
....

vector<int> n = { 100, 500, 1000, 2000, 5000, 8000, 10000 }; //Different input size
const int N = 20000; //random scale
int n0[100], n1[500], n2[1000], n3[2000], n4[5000], n5[8000], n6[10000]; //initialize input arrays
int *myArray[] = { n0, n1, n2 };

// index by size
map<int,int*> index= { {100,n0}, {500,n1}, {1000,n2}, {2000,n3}, {5000,n4}, {8000,n5}, {10000,n6} }; //Different input size

抽象代码:

void all_the_things_I_did_in_the_case(int *nx,int size) {
int input_size = n[size];
int *p, *p2; //create a pointer point to the arrays which we want to manipulate.
p = nx;
p2 = nx;

//cout << "p = n0 :" << *p << endl;
for (int x = 0; x != input_size; ++x) //for loop to generate "input_size" number of elements.
{
int tmp = 1 + (rand() % N); //Shift right by 1.
while (x != 0 && isAlreadyAdded(tmp, x, p2)) //Check if the generated element is already existed.
tmp = 1 + (rand() % N); //Regenerate the element.
*p = tmp; // let the pointer get the value of this tmp.
//cout << *p << endl;
p++; //Increment the pointer to pointer to next element of the array.
}
}

并替换丑陋的生成器

void generator(int size){
map<int,int*>::iterator it=index.find(size);
if(it!=index.end()) {
all_the_things_I_did_in_the_case(it->second,size);
} else {
cout << "Invalid input_size" << endl;
}
}

关于使用不同的输入(数组)调用 C++ 相同的函数,如何缩小我的语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22952698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com