ページ 11

フローチュート

Posted: 2012年2月02日(木) 20:44
by さき
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int get_cookie(char *cookiename) {
char *cookie, *tp;
int count = 0;

if((cookie = getenv("HTTP_COOKIE")) != NULL) {
tp = strtok(cookie, "= ");
while(tp != NULL) {
if(strcmp(tp, cookiename) == 0) {
tp = strtok(NULL, "; ");
break;
}
else {
tp = strtok(NULL, "; ");
}
if(tp != NULL) {
tp = strtok(NULL, "= ");
}
}
count = atoi(tp);
}
}

void set_cookie(char *cookiename, int cookielife, int count) {
time_t timer;
struct tm *date;
char expires[256];

timer = time(NULL);
timer += cookielife;
date = gmtime(&timer);

strftime(expires, 255, "%a, %d-%b-%Y %H:%M:%S GMT", date);

printf("Set-Cookie: %s=%d; expires=%s;\n", cookiename, count, expires);
}

int main(void) {
char cookiename[] = "count";
int cookielife = 50 * 60 * 24 * 30;
int count;
printf("Content-type: text/html\n");
count = get_cookie(cookiename);
count++;
set_cookie(cookiename, cookielife, count);
printf("\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>アクセス回数</title>\n");
printf("</head>\n");
printf("<body>\n");
printf("アクセス:%d回目<br>\n", count);
printf("</body>\n");
printf("</html>\n");
return 0;
}
アクセスカウンターのフローチャートが知りたいです。教えてもらえれば助かります

Re: フローチュート

Posted: 2012年2月02日(木) 20:49
by h2so5
アクセスカウンターの仕様が分からないので答えようがありません。

Re: フローチュート

Posted: 2012年2月02日(木) 21:49
by みけCAT
コードはcodeタグで囲んでいただけるとありがたいです。
フォーラムルールをお読みください。

Re: フローチュート

Posted: 2012年2月02日(木) 21:59
by softya(ソフト屋)
これは、どちらかのサイトのコードでしょうか?
そのサイトのURLも教えて下さい。

Re: フローチュート

Posted: 2012年2月03日(金) 00:29
by かずま
フローチャートです。

コード:

int get_cookie(char *cookiename) {   <------------------------------------------------+
    char *cookie, *tp;                                                                |
    int count = 0;                                                                    |
                                                                                      |
    if((cookie = getenv("HTTP_COOKIE")) != NULL) {                                    |
        tp = strtok(cookie, "= ");                                                    |
   +--> while (tp != NULL) {                                                          |
   |        if (strcmp(tp, cookiename) == 0) {                                        |
   |            tp = strtok(NULL, "; ");                                              |
   |            break;    ---------------------+                                      | 
   |        }                                  |                                      |
   |        else {                             |                                      |
   |            tp = strtok(NULL, "; ");       |                                      |
   |        }                                  |                                      |
   |        if (tp != NULL) {                  |                                      |
   |            tp = strtok(NULL, "= ");       |                                      |
   |        }                                  |                                      |
   +--- }                                      |                                      |
        count = atoi(tp);   <------------------+                                      |
    }                                                                                 |
}  ------------------------------------------------------------------------------->|  |
                                                                                   |  |
void set_cookie(char *cookiename, int cookielife, int count) { <---------------+   |  |
    time_t timer;                                                              |   |  |
    struct tm *date;                                                           |   |  |
    char expires[256];                                                         |   |  |
                                                                               |   |  |
    timer = time(NULL);                                                        |   |  |
    timer += cookielife;                                                       |   |  |
    date = gmtime(&timer);                                                     |   |  |
                                                                               |   |  |
    strftime(expires, 255, "%a, %d-%b-%Y %H:%M:%S GMT", date);                 |   |  |
                                                                               |   |  |
    printf("Set-Cookie: %s=%d; expires=%s;\n", cookiename, count, expires);    |   |  |
} ------------------------------------------------------------------>|         |   |  |
                                                                     |         |   |  |
                                                                     |         |   |  |
  ( START )                                                          |         |   |  |
      |                                                              |         |   |  |
      V                                                              |         |   |  |
int main(void) {                                                     |         |   |  |
    char cookiename[] = "count";                                     |         |   |  |
    int cookielife = 50 * 60 * 24 * 30;                              |         |   |  |
    int count;                                                       |         |   |  |
    printf("Content-type: text/html\n");                             |         |   |  |
    count = get_cookie(cookiename);  --------------------------------|-------->|   |  |
    count++;                         <-------------------------------+             |  |
    set_cookie(cookiename, cookielife, count);  -----------------------------------|->|
    printf("\n");                    <---------------------------------------------+   
    printf("<html>\n");                                                                
    printf("<head>\n");                                                                
    printf("<title>アクセス回数</title>\n");                                           
    printf("</head>\n");                                                               
    printf("<body>\n");                                                                
    printf("アクセス:%d回目<br>\n", count);                                           
    printf("</body>\n");                                                               
    printf("</html>\n");                                                               
    return 0;                                                                          
}                                                                                      
      |
      V
   ( STOP )

Re: フローチュート

Posted: 2012年2月03日(金) 00:42
by かずま
かずま さんが書きました:フローチャートです。
すみません。 get_cookie と set_cookie の呼び出しが逆でした。

フローチャートというのは、左端を揃えて書く
  「アセンブリ言語とか、昔の FORTRAN や BASIC のプログラム」
が読みにくいために存在するものです。

if や while で制御構造を表す言語では必要性の感じられないものです。

Re: フローチュート

Posted: 2012年2月03日(金) 08:12
by みけCAT
勘違い投稿でした。
削除させていただきます。

Re: フローチュート

Posted: 2012年2月03日(金) 08:17
by beatle
みけCAT さんが書きました:これが無視されていますね。
まだ質問者さんの返事が有りませんので,無視ではないと思います.