<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>homework &#8211; Luxing Huang</title>
	<atom:link href="https://luxing.im/tag/homework/feed/" rel="self" type="application/rss+xml" />
	<link>https://luxing.im</link>
	<description>Thoughs and things</description>
	<lastBuildDate>Sat, 12 Dec 2015 21:38:46 +0000</lastBuildDate>
	<language>en-CA</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
<site xmlns="com-wordpress:feed-additions:1">58771605</site>	<item>
		<title>Deterministic Finite Automata in C</title>
		<link>https://luxing.im/deterministic-finite-automata-in-c/</link>
					<comments>https://luxing.im/deterministic-finite-automata-in-c/#respond</comments>
		
		<dc:creator><![CDATA[Luxing Huang]]></dc:creator>
		<pubDate>Thu, 10 Oct 2013 22:07:52 +0000</pubDate>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[DFA]]></category>
		<category><![CDATA[homework]]></category>
		<guid isPermaLink="false">http://blog.luxing.im/?p=49</guid>

					<description><![CDATA[In our a5p2 question, we need to write a general template for the deterministic finite automata, with input details are listed below: &#8211; the number of states (i.e. &#124;&#124;)(the states are numbered from 0 to &#124;&#124; &#8211; 1) &#8211; the number of final states (i.e. &#124;&#124;) &#8211; the number of final symbols in (i.e. &#124;&#124;) &#8230; <p class="link-more"><a href="https://luxing.im/deterministic-finite-automata-in-c/" class="more-link">Continue reading<span class="screen-reader-text"> "Deterministic Finite Automata in C"</span></a></p>]]></description>
										<content:encoded><![CDATA[<p>In our a5p2 <a href="http://socs.acadiau.ca/~jdiamond/comp2103/assignments/a5.pdf">question</a>, we need to write a general template for the deterministic finite automata, with input details are listed below:</p>
<p><span id="more-49"></span></p>
<p>&#8211; the number of states (i.e. |<img decoding="async" src="https://s0.wp.com/latex.php?latex=Q&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="Q" class="latex" />|)(the states are numbered from 0 to |<img decoding="async" src="https://s0.wp.com/latex.php?latex=Q&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="Q" class="latex" />| &#8211; 1)<br />
&#8211; the number of final states (i.e. |<img decoding="async" src="https://s0.wp.com/latex.php?latex=F&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="F" class="latex" />|)<br />
&#8211; the number of final symbols in <img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" /> (i.e. |<img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" />|)<br />
&#8211; |<img decoding="async" src="https://s0.wp.com/latex.php?latex=F&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="F" class="latex" />| numbers, the final states<br />
&#8211; |<img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" />| characters (any white space char is significant, standing exactly for itself, so there will not be multiple spaces in this list)<br />
&#8211; the transition table, given as a <img decoding="async" src="https://s0.wp.com/latex.php?latex=%7CQ%7C%5Ctimes%7C%5CSigma%7C&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="|Q|&#92;times|&#92;Sigma|" class="latex" /> array of numbers.</p>
<p>If you have the basic concept of what DFA is, let&#8217;s then get started. Firstly, we have to understand that DFA is a model or a template for a specific string processing &#8220;machine&#8221;, thus we do not change the code to adjust the needs for a specific programme, we change the matrix (the <img decoding="async" src="https://s0.wp.com/latex.php?latex=%5Cdelta&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;delta" class="latex" />) aka transition table that allows the DFA to determine its validity.</p>
<p>So, firstly, we need to scan in the necessary data listed at the top of this article. Allow me to take Jim&#8217;s example with notation aside.</p>
<h2>Example 1</h2>
<p>Here is a visualized graph on DFA for determining odd number of 1&#8217;s in a binary input. As long as the 1&#8217;s are odd, the input is accepted.</p>
<p>Graphed like this:</p>
<p><a href="http://blog.luxing.im/wp-content/uploads/2013/10/sc3.png"><img decoding="async" class="alignnone size-medium wp-image-60" alt="sc3" src="http://blog.luxing.im/wp-content/uploads/2013/10/sc3-300x137.png" width="300" height="137" srcset="https://luxing.im/wp-content/uploads/2013/10/sc3-300x137.png 300w, https://luxing.im/wp-content/uploads/2013/10/sc3.png 320w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>&nbsp;</p>
<p>The transition table is correspondingly this:</p>
<p><a href="http://blog.luxing.im/wp-content/uploads/2013/10/sc1.png"><img decoding="async" class="alignnone size-full wp-image-67" alt="sc1" src="http://blog.luxing.im/wp-content/uploads/2013/10/sc1.png" width="131" height="90" /></a></p>
<p>2      //the number of states (<img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0%2C+q_1%2C+...%2C+q_%7Bn-1%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0, q_1, ..., q_{n-1}" class="latex" />)<br />
1      //the number of final states, we have only 1 final state here.<br />
2      //the number of final symbols in <img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" /><br />
1      //the final state is <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" />. If DFA ends here, then the input is valid.<br />
01   //the valid characters in symbols.<br />
0 1<br />
1 0  //those 2 lines are the transition table, meaning <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0+q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0 q_1" class="latex" /> and <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1+q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1 q_0" class="latex" />.</p>
<p>Let&#8217;s give out an input stream sample: <span style="color: #ff00ff;">0</span>1<span style="color: #008000;">00000<span style="color: #800080;">1</span></span><span style="color: #ffcc00;">1</span></p>
<p>The initial state is <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0" class="latex" />. 0 goes in, it looks for coordinate <img decoding="async" src="https://s0.wp.com/latex.php?latex=%280%2C+q_0%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="(0, q_0)" class="latex" />, the result is still <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0" class="latex" />.</p>
<p>Current state is <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0" class="latex" />, then 1 goes in, it looks for <img decoding="async" src="https://s0.wp.com/latex.php?latex=%281%2C+q_0%29&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="(1, q_0)" class="latex" />. State is changed to <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" />.</p>
<p>Then five <span style="color: #008000;">0<span style="color: #000000;">s</span></span> goes in, the state is locked to <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" /> based on what the transition table says.</p>
<p>Then <span style="color: #993366;">1</span> goes in, state is now <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_0&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_0" class="latex" />.</p>
<p>Finally the last <span style="color: #ffcc00;">1</span> is passed in, state is changed to <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" />.</p>
<p>EOF</p>
<p>The DFA stopped with a state at <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" />, because only <img decoding="async" src="https://s0.wp.com/latex.php?latex=n+%5Cin+%7CF%7C&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="n &#92;in |F|" class="latex" /> is valid, in this case, <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_1&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_1" class="latex" /> is valid, so the input is valid.</p>
<h2>Example 2</h2>
<p>The visual graph is like this:</p>
<p><a href="http://blog.luxing.im/wp-content/uploads/2013/10/sc2.png"><img decoding="async" class="alignnone size-medium wp-image-75" alt="sc2" src="http://blog.luxing.im/wp-content/uploads/2013/10/sc2-300x165.png" width="300" height="165" srcset="https://luxing.im/wp-content/uploads/2013/10/sc2-300x165.png 300w, https://luxing.im/wp-content/uploads/2013/10/sc2.png 532w" sizes="(max-width: 300px) 100vw, 300px" /></a> and the transition table is like this:</p>
<p><a href="http://blog.luxing.im/wp-content/uploads/2013/10/sc4.png"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-76" alt="sc4" src="http://blog.luxing.im/wp-content/uploads/2013/10/sc4.png" width="177" height="207" /></a></p>
<p>The input for building DFA will be like this:</p>
<p>6<br />
3<br />
3<br />
2 3 4<br />
abc<br />
2 1 1<br />
1 1 1<br />
5 3 4<br />
5 3 5<br />
5 5 4<br />
5 5 5</p>
<p>Similarly, there are 6 states, from 0~5. 3 final states, they are <img decoding="async" src="https://s0.wp.com/latex.php?latex=q_2+q_3+q_4&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="q_2 q_3 q_4" class="latex" />. There are 3 characters accepted, which are <img decoding="async" src="https://s0.wp.com/latex.php?latex=a%2C+b%2C+c+%5Cin+%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="a, b, c &#92;in &#92;Sigma" class="latex" />, and the matrix map.</p>
<p>How to write this programme?</p>
<p>Well firstly we need to read the data in and build a matrix. Then we need to move around the matrix table and get its state, and finally compare the state to the final states.</p>
<p>The code is relatively simple:</p>
<pre>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define N_ASCII 256
#define ACCEPT 200

int
dfa(int nstate, int nfinal, int nsymbol)
{
    int i, j, c, state = 0;
    int final[nfinal];
    int symbol[N_ASCII] = {0}, acceptable_chars[N_ASCII] = {0};
    int matrix[nstate][nsymbol];
    
    /* Buidling up the DFA */

    /* Scanning the final states */
    for (i = 0; i < nfinal; i++)
    {
        scanf("%d", &#038;final[i]);
    }
    scanf("%*[*^\n]\n");

    /*
     * Scan in the chars that are accepted by DFA, allocate its tag from 0 to
     * 1 in symbol[c].
     */
    for (i = 0; i < nsymbol; i++)
    {
        c = getchar();
        symbol[c] = i;
        acceptable_chars[c] = ACCEPT;
    }
    scanf("%*[*^\n]\n");

    /* Scan in the \delta transition table */
    for (i = 0; i < nstate; i++)
    {
        for (j = 0; j < nsymbol; j++)
        {
            scanf("%d", &#038;matrix[i][j]);
        }
    }
    scanf("%*[*^\n]\n");

    /* 
     * Test the input steam
     * We'll stop at either EOF or \n
     */
    bool empty = true;
    while ((c = getchar()) != EOF &#038;&#038; c != '\n')
    {
        if (!(acceptable_chars[c] == ACCEPT))
        {
            fprintf(stderr, "/%c/ char is not in the symbol list!\n" 
                    "Because your number of symbols is %d " 
                    "and there is a %dth.\n", c, nsymbol, nsymbol + 1);
            return -1;
        }

        /* 
         * Inside the matrix we move around based on the char's nth number,
         * until the last getchar().
         */
        state = matrix[state][symbol[c]];
        empty = false;
    }

    if (empty == true)
    {
        fprintf(stderr, "No test input detected!\n");
        return -1;
    }

    for (i = 0; i < nfinal; i++)
    {
        /* 
         * Then we check the final state, if it's a match with any of the 
         * elements inside of |F|
         */
        if(state == final[i])
        {
            return 1;
        }
    }

    return 0;
}

int
main(int argc, char *argv[])
{
    int nstate, nfinal, nsymbol, ret;
    ret = scanf("%d\n", &#038;nstate);
    if (ret != 1)
    {
        fprintf(stderr, "No. of states is invalid!\n");
        return EXIT_FAILURE;
    }
    ret = scanf("%d\n", &#038;nfinal);
    if (ret != 1)
    {
        fprintf(stderr, "No. of finals is invalid!\n");
        return EXIT_FAILURE;
    }
    ret = scanf("%d\n", &#038;nsymbol);
    if (ret != 1)
    {
        fprintf(stderr, "No. of symbols is invalid!\n");
        return EXIT_FAILURE;
    }
    ret = dfa(nstate, nfinal, nsymbol);

    if (ret == 0)
    {
        printf("Not accepted.\n");
    }
    else if (ret == -1)
    {
        fprintf(stderr, "Invalid input.\n");
        return EXIT_FAILURE;
    }
    else
        printf("Accepted.\n");

    return EXIT_SUCCESS;
}
</pre>
<p>This has to be compiled using -std=c99 flag in gcc. </p>
<p>When I wrote this program, I did not consider '\n' as a valid element of set <img decoding="async" src="https://s0.wp.com/latex.php?latex=%5CSigma&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="&#92;Sigma" class="latex" />.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://luxing.im/deterministic-finite-automata-in-c/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">49</post-id>	</item>
		<item>
		<title>A4P2</title>
		<link>https://luxing.im/a4p2/</link>
					<comments>https://luxing.im/a4p2/#respond</comments>
		
		<dc:creator><![CDATA[Luxing Huang]]></dc:creator>
		<pubDate>Tue, 08 Oct 2013 00:02:34 +0000</pubDate>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[homework]]></category>
		<guid isPermaLink="false">http://blog.luxing.im/?p=82</guid>

					<description><![CDATA[A4P2 is relatively a simple straightforward question. Here is the link to the question. #include #include #include int main(int argc, char *argv[]) { char c; short int failed = 0; int min = 0, sec = 0, total_min = 0, total_sec = 0, line = 0, wrong = 0, char_count = 0; printf("Enter a sequence &#8230; <p class="link-more"><a href="https://luxing.im/a4p2/" class="more-link">Continue reading<span class="screen-reader-text"> "A4P2"</span></a></p>]]></description>
										<content:encoded><![CDATA[<p>A4P2 is relatively a simple straightforward question. Here is the <a href="http://socs.acadiau.ca/~jdiamond/comp2103/assignments/a4.pdf" target="_blank">link</a> to the question.</p>
<p><span id="more-82"></span></p>
<pre>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int
main(int argc, char *argv[])
{
    char c;
    short int failed = 0;
    int min = 0, sec = 0, total_min = 0, total_sec = 0,
        line = 0, wrong = 0, char_count = 0;
    printf("Enter a sequence of times, end with 0:00 or EOF.\n");

    while (wrong < 2)
    {
        /* MM:SS, This part checks the MM part. */
        c = getchar();
        if (!isdigit(c) &#038;&#038; c != EOF)
        {
            printf("Invalid song time!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }
        else if (c == EOF)
        {
            break;
        }
        else
            ungetc(c, stdin);
        scanf("%d", &#038;min);

        /* The : part. */
        c = getchar();
        if (c != ':')
        {
            fprintf(stderr,"Invalid song time!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }

        /* Now the SS part. */
        c = getchar();
        if (!isdigit(c))
        {
            fprintf(stderr, "Invalid song time!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }
        else
            ungetc(c, stdin);
        scanf("%d%n", &#038;sec, &#038;char_count);
        if (char_count > 2)
        {
            fprintf(stderr, "You've input more than 2 character's" 
                    " length in seconds.\n");
            char_count = 0;
        }

        if ((c = getchar()) == ':')
        {
            fprintf(stderr, "Make sure you do not have an hour!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }
        /* Check and verify the next lines */
        for (;;)
        {
            if (c == '\n')
                break;
            c = getchar();
        }

        /* Minutes validation part */
        if (sec > 60)
        {
            fprintf(stderr, "Invalid song time!\n");
            if (failed == 1)
                wrong++;
            else
                failed = 1;
            for (;;)
            {
                if (c == '\n')
                    break;
                c = getchar();
            }
            continue;
        }

        if (sec == 0 && min == 0)
            break;


        total_sec += sec;
        total_min += min;
        line++;
        wrong = 0;
    }

    if (wrong > 1)
    {
        fprintf(stderr, "Three invalid song times in a row; I'm quitting!\n");
        return EXIT_FAILURE;
    }

    fprintf(stdout, "There were %d valid song times.\n", line);
    if (total_sec > 60)
    {
        total_min += total_sec / 60;
        total_sec = total_sec % 60;
    }
    fprintf(stdout, "The total times is %d:%.2d\n", total_min, total_sec);

    return EXIT_SUCCESS;
}</pre>
<p>Please feel free to critisize the coding if you have a better solution.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://luxing.im/a4p2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">82</post-id>	</item>
		<item>
		<title>A5P1</title>
		<link>https://luxing.im/a5p1/</link>
					<comments>https://luxing.im/a5p1/#respond</comments>
		
		<dc:creator><![CDATA[Luxing Huang]]></dc:creator>
		<pubDate>Sat, 05 Oct 2013 15:33:00 +0000</pubDate>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[assignment]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[homework]]></category>
		<guid isPermaLink="false">http://blog.luxing.im/?p=31</guid>

					<description><![CDATA[&#160; For thoses who don&#8217;t know what a5p1 means, it means Assignment 5, Problem 1. Yesterday, my professor Jim added a new assignment for us to do. I am going to talk about the first in this article. I am not a good coder and I just started learning C this term. Still, I am &#8230; <p class="link-more"><a href="https://luxing.im/a5p1/" class="more-link">Continue reading<span class="screen-reader-text"> "A5P1"</span></a></p>]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<p>For thoses who don&#8217;t know what a5p1 means, it means Assignment 5, Problem 1.</p>
<p>Yesterday, my professor Jim added a new assignment for us to do. I am going to talk about the first in this article.</p>
<p><span id="more-31"></span></p>
<p>I am not a good coder and I just started learning C this term. Still, I am trying to solve the puzzle as best I can. I know it is not a good style to put everything under main(), but since it&#8217;s a simple and straightforward problem, please accept my messy style of coding.</p>
<p>You can view the question <a href="http://file.luxing.im/dirLIST_files/download.php?file=Li9zaGFyZS9hNS5wZGY=" target="_blank">here</a>. I started writing the first problem this morning and I think I have finished this problem at the noon. (Just before the time I was writing this article.)</p>
<p>If you get a 404 error, that means this term has finished, you can get the problem sets by emailing me.</p>
<p>Things need attention:</p>
<p>1. I need to justify what format the input/output is:</p>
<pre>input:
N
Year(%d) Month(%s) Day(%d)
....

output:
Year(%d) Month(%d) Day(%s)</pre>
<p>I happened to mixed the order up during programming, but it wasn&#8217;t a big deal.</p>
<p>2. It&#8217;s important to know that the Highwings calendar count from 0 in <em>day</em> section but Lowwings calendar count start from 1 in <em>month</em> section.</p>
<p>3. Those calendar systems have different days per year. Highwings has <img decoding="async" src="https://s0.wp.com/latex.php?latex=12%5Ctimes30%2B20+%3D+380&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="12&#92;times30+20 = 380" class="latex" /> days per year and Lowwings has <img decoding="async" src="https://s0.wp.com/latex.php?latex=20%5Ctimes12%3D240&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="20&#92;times12=240" class="latex" /> days per year, we can write a macro for further use.</p>
<pre>#define TOTAL_DAYS_PERYR_HIGHWING (12 * 30 + 20)
#define TOTAL_DAYS_PERYR_LOWWING (20 * 12)</pre>
<p>The structure is simple, parse every line until <em>N</em> is reached using a for loop. Using getchar and scanf to read the values from <span style="color: #ff9900;">stdin</span>, assign it to corresponding values. Professor has already talked about this in <a href="http://pastebin.com/jTsJ1byS" target="_blank">a3p1</a>, we can extract his ideas and implement them into our code.</p>
<p>Why we don&#8217;t just parse the whole line all-together using</p>
<pre> scanf("%d %s %D\n", &h_yr, &h_m, &h_d)</pre>
<p>You&#8217;ll never know what the user is going to type in, therefore the <span style="color: #ff9900;">stdin</span> might not be what you expected. This is for error checking which I&#8217;ll talk about it later.</p>
<p>When you have all the correct values assigned to correct variables, then how to convert it to Lowwings Calendar system?</p>
<p>What I did was to find the total days, it should be the only thing in common between these 2 systems of calendar, I call the variable <em>unify_day</em>.</p>
<p>Now things are getting clearer.</p>
<p>In this case,</p>
<pre>1 charlie 17</pre>
<p>charlie is the <img decoding="async" src="https://s0.wp.com/latex.php?latex=3%5E%7Brd%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="3^{rd}" class="latex" /> month in Year 1, but the <img decoding="async" src="https://s0.wp.com/latex.php?latex=3%5E%7Brd%7D&#038;bg=ffffff&#038;fg=000&#038;s=0&#038;c=20201002" alt="3^{rd}" class="latex" /> month has not passed, so we only need to count the first 2 months into <em>unify_day</em>, then calculate the day/month/year in Lowwings system.</p>
<p>Finally you need to reset <em>unify_day</em> to 0 at the end of loop.</p>
<p>When you try to divide a number with 12, and if the remainder is 0, it doesn&#8217;t mean that it&#8217;s the start of next month, it&#8217;s the end of the previous month. So I have put zulu first in my string array.</p>
<pre>*lowwing_day_names[] = {"zulu", "november", "oscar", "papa", "quebec",
        "romeo", "sierra", "tango", "uniform", "victor", "whiskey", "xray"};</pre>
<p>The error detect part, which types of error have I considered?</p>
<pre>
(a). When N = 0.
(b). When N &lt; next few lines. (When N &gt; next few lines I just ignore the rest)
(c). Invalid data:
   i. When !isdigit(N)
   ii. !isdigit(h_day)
   iii. !isdigit(h_yr)
(d). spaces/tabs as seperator.
</pre>
<p>Programme ends whenever an error occurs.</p>
<pre>
/*
 * File:    lightplane.c
 * Author:  Luxing Huang
 * Version: 1.0
 *
 * Purpose:
 * When we have 2 different calendar systems, we need to translate the old one
 * to the new one. 
 *
 * Here we have an old system called Highwings: A year with 13 month, 30 days 
 * for the 1-12th month, and 20 days for the 13th month. We will have 30x12+20
 * = 380 days a year. Every month has its own name.
 *
 * And the second system is 240 days a year. We have 20 months with 12 days in
 * each month. Every day in each month has its own name. This system is called
 * Lowwings.
 *
 * The input format is:
 * N
 * Year(%d) Month(%s) Day(%d)
 *
 * The output format is:
 * Year(%d) Month(%d) Day(%s)
 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define CHAR_LENGTH 128
#define TOTAL_DAYS_PERYR_HIGHWING (12 * 30 + 20)
#define TOTAL_DAYS_PERYR_LOWWING (20 * 12)

int
main(int argc, char *argv[])
{
    /*
     * Structure:
     * 1. scan the input, preferrably using getchar then scanf
     * 2. Convert Highwing days to unify_day.
     * 3. Error detect:
     *  a. N < next a few lines. If > I just ignore the rest.
     *  b. Invalid data:
     *      (a) when it should be a number, it's a char.
     *      (b) after year we have more char rather than \n.
     * 4. Calculate the unify day to Lowwing system.
     */
    unsigned int i, unify_day = 0, highwing_year = 0, highwing_day = 0, 
                 lowwing_year = 0, lowwing_month = 0, lowwing_day = 0;
    int highwing_month = -1, entries = 0, c = 0;
    char highwing_month_name[CHAR_LENGTH] = "";
    char *highwing_compare[] = {"alpha", "bravo", "charlie", "delta", "echo", 
        "foxtrot", "golf", "hotel", "india", "juliet", "kilo", "lima", "mike"},
         *lowwing_day_names[] = {"zulu", "november", "oscar", "papa", "quebec", 
        "romeo", "sierra", "tango", "uniform", "victor", "whiskey", "xray"};
    
    /* Ignoring spaces if have any */
    scanf("%*[ \t]");

    /* check if is a digit */
    c = getchar();
    if (!isdigit(c))
    {
        fprintf(stderr, "You need to put a positive integer in the first " 
                "line!\n");
        return EXIT_FAILURE;
    }
    else
        ungetc(c, stdin);

    scanf("%d", &entries);
    if (entries <= 0)
    {
        fprintf(stdout, "Nothing to calculate!\n");
        return EXIT_SUCCESS;
    }
    else
    {
        /* 
         * Error detect for:
         * N more_char_here
         */
        scanf("%*[ \t]");
        c = getchar();
        if (c == EOF)
        {
            fprintf(stderr, "Where are your next lines?\n");
            return EXIT_FAILURE;
        }
        else if (c != '\n')
        {
            fprintf(stderr, "You should not input anything "
                            "behind %d!\n", entries);
            return EXIT_FAILURE;
        }

        /* We need to have a loop for entries time */
        for (i = 0; i < entries; i++)
        {
            /* We get year first, ignoring spaces */
            scanf("%*[ \t]");
            c = getchar();
            if (c == EOF)
            {
                fprintf(stderr, "Where's the %d(th) set of data?\n", i+1);
                return EXIT_FAILURE;
            }
            else if (!isdigit(c))
            {
                fprintf(stderr, "This is not a valid day!\n");
                return EXIT_FAILURE;
            }
            ungetc(c, stdin);
            scanf("%d", &#038;highwing_year);

            /* Now the name (string) for highwing month */
            int m;
            c = getchar();
            if (c != ' ' &#038;&#038; c != '\t')
                fprintf(stderr, "Your input format is wrong but we'll " 
                        "proceed anyway.\n");
            scanf("%*[ \t]");

            scanf("%s", highwing_month_name);
            for (m = 0; m <= 13; m++)
            {
                if (strcmp(highwing_month_name, highwing_compare[m]) == 0)
                {
                    /* Array of strings position start from 0 */
                    highwing_month = m;
                    break;
                }
            }
            if (highwing_month == -1)
            {
                fprintf(stderr, "Invalid month name.\n");
                return EXIT_FAILURE;
            }

            /* 
             * Let's now scan the day
             * First we need to make sure that the first character is a digit.
             */
            c = getchar();
            if (c != ' ' &#038;&#038; c != '\t')
                fprintf(stderr, "Your input format is wrong but we'll "
                        "proceed anyway.\n");

            scanf("%*[ \t]");
            c = getchar();
            if (!isdigit(c))
            {
                fprintf(stderr, "Invalid input!\n");
                return EXIT_FAILURE;
            }
            else
                ungetc(c, stdin);

            scanf("%d", &#038;highwing_day);

            if (highwing_day > 29)
            {
                fprintf(stderr, "You shouldn't input more than 30 days!\n");
                return EXIT_FAILURE;
            }
            highwing_day++;

            /* 
             * We need to make sure about the boundary values, 
             * In this case, the last day of mike is 20th
             */
            if (highwing_month == 12 && highwing_day > 20)
            {
                fprintf(stderr, "You cannot input 20 or more in day in " 
                        "Mike!\n");
                return EXIT_FAILURE;
            }

            /* ignoreing the spaces afterwards */
            scanf("%*[ \t]");

            /* Make sure that we have a nextline in the end */
            c = getchar();
            if (!(c == '\n'))
            {
                fprintf(stderr, "Nothing behind %d please!", highwing_day);
                return EXIT_FAILURE;
            }
            /* 
             * Once it is all done, we add the current inputs to unify_day.
             * Every Highwing month has 30 days except the last one.
             */
            unify_day = highwing_year * TOTAL_DAYS_PERYR_HIGHWING + highwing_month * 30
                        + highwing_day;

            /* 
             * From unify_day we need to calculate the year for Lowwing calendar
             */
            lowwing_year = unify_day / TOTAL_DAYS_PERYR_LOWWING;
            unify_day -= lowwing_year * TOTAL_DAYS_PERYR_LOWWING;

            /* We need to have lowwing_month at this point. */
            lowwing_month = unify_day / 12;
            unify_day -= lowwing_month * 12;

            if ((unify_day % 12) == 0)
                lowwing_month--;

            /* Now the day */
            lowwing_day = unify_day;

            printf("Highwings %d %s %d\t is\t Lowwings %d %d %s\n", 
                    highwing_year, highwing_month_name, highwing_day - 1,
                    lowwing_year, lowwing_month + 1, 
                    lowwing_day_names[lowwing_day]);
            
            /* Reset all the values which won't be re-assigned from stdin. */
            unify_day = 0;
        }
    }

    return EXIT_SUCCESS;
}
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://luxing.im/a5p1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">31</post-id>	</item>
	</channel>
</rss>
