回 帖 发 新 帖 刷新版面

主题:救命!

楼主   

Final Project   (100 points)
    
Numeric addresses for computers on the Internet (IP addresses) comprise four integers, separated by periods, of the form aa.bb.cc.dd, where aa, bb, cc, and dd are integers, with no more than 3 digits each.  (This is actually a small lie, but let it go for this program).  Machines are also usually known by a local nickname, such as “pizza”.  Assume that machines on the same local network (sub-net) always share the same 2 first numbers (again, this isn't quite true, but it's close enough).  Create a struct ADDRESS_TYPE to hold the 4 integers and the machine name.  For the name, use a C-string (an array of characters at most 15 chars long with one extra for the trailing NUL character {‘\0’}).

Write a program to use direct file input to read a list of IP addresses and associated names.  The last entry in the file will be a machine with 0.0.0.0 as its address and the name "none".  There will be no more than 100 lines in the file, so set up an array of 100 ADDRESS_TYPE structs to hold the entries.  Use direct file output for all output.  The output is as follows (in this order):

•    a list of all groups of 2 or more machines belonging to the same sub-network (the first 2 numbers [not digits!] of their IP address are the same) by the machine name only (do not print I.P. addresses here)
•    the file contents (unsorted, or in the order entered from the file) (all file contents printing is I.P. address in dotted-decimal form, a tab, then name)
•    the file contents (sorted by IP address in ascending order)
•    the file contents (sorted by machine name in ascending order)
•    the number of groups, and the number of machines not belonging to any group, with appropriate text  (remember this from the first output group)

In the first output section, identify the machines in each group by their nicknames only.  Once a machine name is printed as part of a group, do not print it again as a part of another group.

You must use (at least) the following functions in your program, as specified here:

•    a function int ReadRecord(ADDRESS_TYPE &, ifstream &)to load a single line of data from the input file into a single machine structure (it should return a code indicating if it just read 0.0.0.0 or not)
•    a function void PrintRecord(ADDRESS_TYPE, bool, ofstream &)to print the IP address and name of a single ADDRESS_TYPE struct out to the ofstream passed in to the function.  The bool flag indicates whether to print the machine name only or to print both the I.P. address and the machine name

You will need other functions as well.  A sample data file might look like this:

111.22.3.44    platte
555.66.7.88    wabash
111.22.5.66    green
555.66.11.24    homer
123.45.67.89    loner
12.34.56.78    pizza
555.66.9.10        ulysses
0.0.0.0        none

For this file, print out the following 2 groups of machines on the same sub-nets (don't print the notes):

platte            // these are in group 111.22
green
----------------         // Separate the groups with a line of 15-20 dashes or underscores
wabash            // these are in group 555.66
homer
ulysses
----------------        // Optionally, you my precede or follow the groups with a line of dashes
The sample test file above is on my web site as ipaddr.txt.  Test with it, as well as several of your own making.  Include a test where there are no groups, (20 or more machines with no IP addresses starting with the same values), no machines at all (the first line is 0.0.0.0 none), and some tests with large groups (a file of 50+ machines with 2 or 3 fairly big groups spread out in the file).  Include tests where the machines in a group are adjacent in the file, as well as tests where the machines in a group are scattered.  To annotate, highlight or otherwise indicate the matching lines in the file (as echo-printed in the middle set of output), showing the groups you expected to see in the output.

You MAY NOT use ANY global variables in this assignment!

Hints:
Use a second array of bools or ints (initially all false) to track whether or not this machine name has been printed yet, and set the element to true when you print the corresponding machine name.  Associate the flag with the machine record by using the same index into both arrays to mean the same machine.

Use an outer loop to pass through the list for original numbers (machines possibly not printed yet).  When you find a machine that has not been printed, use an inner loop to look for a match to the outer loop's element's address group.  If you find a match, print the separator and both names, turn on the corresponding 'printed' flags, then continue to look for more matching names in the rest of the array of machines.  On subsequent matches in the inner loop, use the 'printed' flag to avoid printing the first machine's information again.  When your inner loop stops, go on to the next element in the outer loop.  (This looks a lot like ripple sort when you finish the code).

After you print the groups, use a loop to print the elements in their original sequence.  Then use the sorting algorithm of your choice to sort the list by IP address, and print the list again.  Then use the sorting algorithm to sort the list by names, and print the list again.  Since you print the data in the array three times, you probably will want to use a function to do this job.  You may need two functions to do the sorts.  You have algorithms from class (e.g., bubble sort, ripple sort, selection sort) for sorting.  The first sort is going to be sorting on IP numbers, which are split into 4 separate values.  You can use a double, and put the address parts into the double multiplied by appropriate powers of 10 to get a single comparable key.  For example, with only 2 numbers in a sort key, 12.45 becomes (12*1000)+45 = 12045, which can be compared with another single value.  The second sort will compare the strings holding the machine names.  You can use the function int strcmp(char *s, char *s); from the C string library (#include <cstring>)  to compare 2 C-strings.

Treat 0.0.0.0 as the address of the last machine in the input file.  Sort it into the first position before printing the list sorted by IP addresses, and sort the "none" name into the list appropriately.

This is a moderately long program (for a beginning student program); however, it is not overly complex.  Expect at least several hours of effort to make it work.  Designing the algorithm for the entire problem will be critical before you write any code.  Do not try to shortcut this process!

A high level design for this program might start like this:

initialize variables (no statistics yet, empty arrays, etc.)
open files
read data from file into array of structs
find groups & print them by name only, simultaneously collect statistics for later printing
print table of machine addresses and names (unsorted)
sort table of machine names by address
print table of machine addresses and names
sort table of machine names by name
print table of machine addresses and names
print count of groups and number of machines not in any group
close files

首先,输入一堆(大概70个窝)IP 地址同距地嘅名称. 例如
IP地址         名称
111.22.3.44    platte
555.66.7.88    wabash
111.22.5.66    green
555.66.11.24    homer
123.45.67.89    loner
12.34.56.78    pizza
555.66.9.10    ulysses
0.0.0.0        none

1.将距地全部print一次出来!
2.按IP地址,由细到大排一次.print出来
3.按距地个名,由细到大排一次.print出来
4.头两组数相同你,放买一组例如555.66.11.24    homer
                             555.66.9.10    ulysses
                             555.66.7.88     wabash
如果无相同的就自己当是一组,例如 0.0.0.0        none!
计算一共有几多组,将据地的名字按照一组一组敢打print 出来!

有几条方程是一定要有嘅:
a function int ReadRecord(ADDRESS_TYPE &, ifstream &)

a function void PrintRecord(ADDRESS_TYPE, bool, ofstream &)

回复列表 (共1个回复)

沙发

能告诉我哪里出错吗?

// ParseString.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

#define MAX_NAME_LEN    50

typedef struct _ADDRESS_TYPE
{
    int a;
    int b;
    int c;
    int d;
    char name[MAX_NAME_LEN];
} ADDRESS_TYPE;


bool ParseAddressLine (ADDRESS_TYPE &add, char *addrline)
{
    char *p;
    char line[256];            // Use to parse the string
                            // since the strtok will
                            // destroy the original string
    char ipaddr[20];
    int  i;
    bool retval = true;

    if (!addrline)            // Check for null pointer
        return (false);

    // Initialize the address structure
    memset (&add, 0, sizeof(ADDRESS_TYPE));
    strcpy (line, addrline);

    // Split the string into IP address with format xxx.xxx.xxx.xxx and
name
    p = strtok (line, "\t ");
    i = 0;
    while (p)
    {
        if (i == 0)
            strcpy (ipaddr, p);
        else
            strcpy (add.name, p);
        i++;
        p = strtok (NULL, "\t ");
    }

    // Parsing the ip
    p = strtok (ipaddr, ".");
    i = 0;
    while (p)
    {
        switch (i)
        {
        case 0:
            add.a = atoi(p);
            break;
        case 1:
            add.b = atoi(p);
            break;
        case 2:
            add.c = atoi(p);
            break;
        case 3:
            add.d = atoi(p);
            break;            
        }
        i++;
        p = strtok (NULL, ".");
    }

    // validate the output.
    if (((add.a == 0) && (add.b == 0) && (add.c == 0) && (add.d == 0)) ||
(strlen (add.name) == 0))
        retval = false;

    return (retval);
}

// function compare 2 parts of the ip address
// a1 and a2 is 2 parts of the first ip address
// b1 and b2 is 2 parts of the second ip address
// return 1 if ip 1 > ip 2 (only 2 parts)
//        0    ip 1 = ip 2
//       -1    ip 1 < ip 2
int pair_comp (int a1, int a2, int b1, int b2)
{
    double a, b;

    a = a1 * 1000 + a2;
    b = b1 * 1000 + b2;

    if (a > b)
        return (1);
    else
    if (a < b)
        return (-1);

    return (0);
}

// function IP compare
// Compare all 4 parts of ip address
int IP_comppare (ADDRESS_TYPE &add1, ADDRESS_TYPE &add2)
{
    bool retval = -1;
    int  compval;

    compval = pair_comp (add1.a, add1.b, add2.a, add2.b) ;
    if (compval > 1)
        retval = 1;
    else
    if (compval < 1)
        retval = -1;
    else
    {
        // Compare the last 2 part
        retval = pair_comp (add1.c, add1.d, add2.c, add2.d);
    }
    return (retval);
}

// function IsSameGroupAddress
// Arguments: add1, add2
// Verify if 2 ip addresses are in the same group (same first 2
numbers)
// return true if they are in the same group
bool IsSameGroupAddress (ADDRESS_TYPE &ip1, ADDRESS_TYPE &ip2)
{
    return (pair_comp (ip1.a, ip1.b, ip2.a, ip2.b));
}


int _tmain(int argc, _TCHAR* argv[])
{
    ADDRESS_TYPE addTest;
    
    // Test
    ParseAddressLine (addTest, "192.1.1.20 Pizza");
    

    return 0;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册