主题:救命!
楼主
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 &)
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 &)