I've written some code in C++ and compiled it in GNU C++11 compiler. I defined two functions using templates to sort a set of values with different data types. One is to swap values and another is to do sorting operation. And also I defined two overloaded functions of the functions I mentioned to sort C-strings. It looks I passed the required array contained C-strings entered by user correctly but I don't know why overloaded functions aren't calling.
This is my code:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<conio.h>
#define TOTAL 3
template<typename swap>
void Swap(swap *value_1, swap *value_2)
{
swap temp = *value_1;
*value_1 = *value_2;
*value_2 = temp;
}
void Swap(char *string_1, char *string_2)
{
char temp[30];
strcpy(temp, string_1);
strcpy(string_1, string_2);
strcpy(string_2, temp);
}
template<typename sort>
void Shellsort(sort *array, int Length)
{
for(short int Step = Length / 2; Step != 0; Step--)
{
for(short int Counter = 0; (Counter + Step) != Length; Counter++)
{
if(*(array + Counter) > *(array + (Counter + Step)))
Swap(*(array + Counter), *(array + (Counter + Step)));
}
}
}
void Shellsort(char **array, int Length)
{
printf("Your value is string");
system("pause");
for(short int Step = Length / 2; Step != 0; Step--)
{
for(short int Counter = 0; (Step + Counter) != Length; Counter++)
{
if(strcmp(*(array + Counter), *(array + (Step + Counter))) > 0)
Swap(*(array + Counter), *(array + (Step + Counter)));
}
}
}
int main()
{
char strings[TOTAL][30];
for(int Counter = 0; Counter < TOTAL; Counter++)
{
printf("Number %hd: Enter the string: ", Counter + 1);
fgets(*(strings + Counter), 30, stdin);
system("cls");
}
Shellsort(strings, TOTAL);
for(int Counter = 0; Counter < TOTAL; Counter++)
printf("Number %hd: %s", Counter + 1, *(strings + Counter));
getch();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire