[질문] 문자열 비교해서 정렬하는거 이상함.

winicon의 이미지

안녕하세요.

문자열 비교해서 정렬을 하려는 strcmp함수가 이상하게 안되네요.

예를 들면:

따라서 20,3,1,10,2,30 이런 값들을 strcmp()함수를 사용해서 정렬을 하게 되면 1,10,2,20,3,30 이렇게 나오네요.

단) 예제의 문자열 값들이라서 숫자로 변환해서 하면 안됩니다. 모든값들을 문자열로 처리해서 결과가 제대로 나왔으면 합니다.

이렇게요. => 1,2,3,10,20,30


그럼 즐거운 시간보내세요^^

cedar의 이미지

1,2,3,10,20,30 => 01,02,03,10,20,30

hanseok의 이미지

winicon wrote:

문자열 비교해서 정렬을 하려는 strcmp함수가 이상하게 안되네요.

예를 들면:

따라서 20,3,1,10,2,30 이런 값들을 strcmp()함수를 사용해서 정렬을 하게 되면 1,10,2,20,3,30 이렇게 나오네요.

단) 예제의 문자열 값들이라서 숫자로 변환해서 하면 안됩니다. 모든값들을 문자열로 처리해서 결과가 제대로 나왔으면 합니다.

이렇게요. => 1,2,3,10,20,30


strcmp 함수를 수정해 주셔야 겠네요 그럼..
strcmp 함수는 ==>1000, 20 이걸 비교한다고 하면..
1000
20
1,2 (첫번째것을 비교해서 ) 크기가 결정나면서 return 해 버리게 되죠.

strlen 으로 문자열의 크기가 작은것
strlen(1000) ==> 4가 되겠죠
strlen ( 20 ) ==> 2가 되겠죠.. 길이를 먼저 측정해서 크기를 판별하고
이후에.. len 가 같은 것에 대해서 strcmp를 해주면 해결할수 있을듯합니다.

글을 적으면서..~~
생각해 낸것이라.~~ 어설푸지만 될듯하네요.

잠이 들어야지만 꿈을 꿀수 있는것인가?
우리는 항상 꿈을 쫓아 가며 살아가는 사람들..

angpoo의 이미지

숫자로 변환해서 비교를 하는게 훨씬 깔끔할것 같네요.
어차피 변환해서 비교만 하고 원 값에 영향을 미치는건 아니니까요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.