dsda

Pages: 12
Your professor is an idiot.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <cstring>

int main(){
	std::cout
		<< strcmp("-1", "-6") << std::endl
		<< strcmp("1", "6") << std::endl;
	return 0;
}
Output:
-1
-1

Like I said, since "-1" < "-6", "1" < "6".
@helios: Now, I have no idea on how to solve this since honestly I'm still just a beginner and have subpar knowledge actually.
Last edited on
@helios: please help me sir
Abandon the idea of using only strcmp. You're going to need a custom comparison function. One thing you might do is try to convert the string to a number. If you can do that with one string and can't with the other, the one you can convert should be earlier in the sequence. If you can convert two strings, then compare the values you converted to. If you can convert neither string, then you can use a lexical comparison like strcmp to get your order.
@cire: That sounds like a great idea, is there such a custom comparison function in the library or do i have to make one?
A custom comparison function would be one defined to meet your specific need. If such a function were in the standard library, you wouldn't need a custom function.
C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>

// base 10, entire string is parsed, no leading or trailing spaces, value representable as long long
bool is_integer( const char* cstr )
{
    if( cstr == NULL || isspace( cstr[0] ) ) return false ;

    // try to convert to an integer http://en.cppreference.com/w/cpp/string/byte/strtol
    char* end = NULL ;
    errno = 0 ;
    strtoll( cstr, &end, 10 ) ;
    return errno == 0 && *end == 0 ;
}

long long to_integer( const char* cstr ) { return strtoll( cstr, NULL, 10 ) ; }

// ascending on value of integer
int compare_integer( long long a, long long b ) { return (b<a) - (a<b) ; }

// comparison function which returns ​a negative integer value if the first argument is less than the second,
// a positive integer value if the first argument is greater than the second and zero if the arguments are equal.
// The signature of the comparison function should be equivalent to the following: int cmp(const void *a, const void *b);
// http://en.cppreference.com/w/c/algorithm/qsort
int compare( const void* aa, const void* bb )
{
    const char* a = *(const char**)aa ;
    const char* b = *(const char**)bb ;

    if( is_integer(a) )
    {
        if( is_integer(b) ) return compare_integer( to_integer(a), to_integer(b) ) ;
        else return -1 ; // integer strings s before non-integer strings
    }
    else
    {
        if( is_integer(b) ) return +1 ; // non-integer strings after integer strings
        else return strcmp(a,b) ;
    }
}

int main()
{
    const char* a[] = { "-1", "potatoe", "-6", "zebra24", "+7", "violin", "-007", "vermilion", "7", "four", "+07" } ;
    const size_t n = sizeof(a) / sizeof( a[0] ) ;

    qsort( a, n, sizeof( a[0] ), compare ) ;
    for( size_t i = 0 ; i < n ; ++i ) printf( "'%s'\n", a[i] ) ;
}

http://coliru.stacked-crooked.com/a/058efb76cadc52fe

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

bool is_integer( const std::string& str )
{
    if( std::isspace( str[0] ) ) return false ;

    // try to convert to an integer http://en.cppreference.com/w/cpp/string/basic_string/stol
    std::size_t end = 0 ;
    try { stoll( str, &end ) ; }
    catch( const std::exception& ) { return false ; }

    return end == str.size() ;
}

// comparison function which returns ​true if the first argument is less than (ordered before) the second.
// The signature of the comparison function should be equivalent to bool cmp(const Type1 &a, const Type2 &b);
// http://en.cppreference.com/w/cpp/algorithm/sort
bool compare( const std::string& a, const std::string& b )
{
    if( is_integer(a) )
    {
        if( is_integer(b) ) return stoll(a) < stoll(b) ;
        else return true ; // integer strings s before non-integer strings
    }
    else
    {
        if( is_integer(b) ) return false ; // non-integer strings after integer strings
        else return a < b ;
    }
}

int main()
{
    std::string a[] = { "-1", "potatoe", "-6", "zebra24", "+7", "violin", "-007", "vermilion", "7", "four", "+07" } ;
    const size_t n = sizeof(a) / sizeof( a[0] ) ;

    std::sort( a, a+n, compare ) ;
    for( const auto& str : a ) std::cout << str << '\n' ; // http://www.stroustrup.com/C++11FAQ.html#for
}

http://coliru.stacked-crooked.com/a/64366dd52308060c
@JL: Thank you, sir. I truly appreciate your help but how can i translate this to Turbo C++?
1
2
3
4
5
6
7
8
9
10
11
bool is_integer( const std::string& str )
{
    if( std::isspace( str[0] ) ) return false ;

    // try to convert to an integer http://en.cppreference.com/w/cpp/string/basic_string/stol
    std::size_t end = 0 ;
    try { stoll( str, &end ) ; }
    catch( const std::exception& ) { return false ; }

    return end == str.size() ;
}
> how can i translate this to Turbo C++?

Why would you want to translate this into an obsolete, pre-standard dialect of C++?
Switch to a current C++ implementation.
@cire: so I've tried to modify my program using your advice so far I've only gotten the first index in an array to convert to int. Is this code correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(p;p>0;p--)
{
 for(c=0;c<p;c++)
  {
      n=atoi(a[c]);
	if(isdigit(n)
     if(stricmp(a[c],a[c+1])>0)
     {
      strcpy(tmp,a[c]);
      strcpy(a[c],a[c+1]);
      strcpy(a[c+1],tmp);
     }
  }
}


I don't know what to do next. Please help me.
@JLBorges: I understand sir. Thank you for all your help.
Topic archived. No new replies allowed.
Pages: 12