날짜 계산하는 C 코드 짜고 있는데 꽉 막혀서 질문드립니다..

mewestside의 이미지

코딩하고 있는 프로그램의 내용은 다음과 같습니다..

자신이 원하는 연/월/일을 입력한 뒤

입력된 일자만큼 전/후의 날짜의 연/월/일을 구하는 코드입니다..

윤년과 연도 변화까지 모두 고려한 것입니다..

일단 아래 코드와 같이 코딩을 했는데 이후에 진행이 안되고 있습니다.ㅠ

어떻게 내용을 추가하면 될까요??

코딩은 아니라도 어떤부분의 조건을 추가해줘야 하나요?

많이 진행된 상황이 아닌데 머리가 안굴러가고 있어 이렇게 올렸습니다..

#include <stdio.h>
 
int main (int argc, char **argv)
{
        int i, j, n, k, l, m;
        int gyear, gmon, gday;
        int jyear, jday;
        int lastday[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int sumday;
        int day;
        int pday, mday;
 
        sumday = 0;
        jyear = 0;
        jday = 0;
        pday = 0;
        mday = 0;
 
        scanf("%d %d %d", &gyear, &gmon, &gday);
        scanf("%d", &day);
 
        if (gyear < 0 || gmon <= 0 || gmon > 12 || gday <= 0 || gday > 31)
        {
                printf("Error -> Date error!!\n");
 
                return 0;
        }
 
        if ((gyear%4) == 0 && (gyear%100) != 0 || (gyear%400) == 0)
        {
                lastday[1] = 29;
        }
 
        else
        {
                lastday[1] = 28;
        }
 
        for (i=1; i<gmon; i++)
        {
                sumday += lastday[i-1];
        }
 
        jday = sumday + gday;
 
        pday = jday + day;
        mday = jday - day;
 
        printf ("%d\n", jday);
        printf ("%d\n", pday);
        printf ("%d\n", mday);
 
/*
        for (i=0; i<12; i++)
        {
                sumday += lastday[i];
 
                if (sumday >= jday && sumday-lastday[i] < jday)
                {
                        gmon = i+1;
                        gday = jday - sumday + lastday[i];
                }
        }
 
        printf ("%d %d %d", gyear, gmon, gday);
*/
 
        return 0;
}
chadr의 이미지

일단 알고리즘을 글로 써보세요.

글로 못쓰는 알고리즘을 코드로 짜는건 뭘 먹을지도 모르는데 가스불에 냄비를 올리는 것과 같습니다.

일단 자신이 하고 싶은게 뭔지를 글로 쓴 다음에 차근 차근 생각해보세요.

물론 무작정 짜고 디버거로 보면서 완성해 나가는 것도 방법일 수도 있지만 그다지 아름다운 방법은 아닙니다.

글로 써보시면 무엇이 중간에 빠지는지 눈에 보일겁니다.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

없음의 이미지

정말 쓸모없는 조언하시네요

없음의 이미지

정말 쓸모없는 조언하시네요

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.