I am using Netbeans in C++ Numeric addresses for computers on the international
ID: 3720962 • Letter: I
Question
I am using Netbeans in C++
Numeric addresses for computers on the international network Internet are composed of four parts, separated by periods, of the form xx.yy.zz.mm where xx, yy, zz, and mm are positive integers from 0 to 255. Locally, computers are usually known by a nickname as well. You are designing a program to process a list of Internet addresses, identifying all pairs of computers from the same locality. Create a structure type called address_t with components for the four integers of an Internet address and a fifth component in which to store an associated nickname of ten characters. Your program should read a list of up to 100 addresses and nicknames terminated by a sentinel address of all zeros and a sentinel nickname.
Sample Data
111.22.3.44 platte
155.66.7.88 wabash
111.22.5.66 green
0.0.0.0 none
The program should display a list of messages identifying each pair of computers from the same locality, that is, each pair of computers with matching values in the first two components of the address. In the messages, the computers should be identified by their nicknames.
Example Message
Machines platte and green are on the same local network.
Follow the messages by a display of the full list of addresses and nicknames. Include in your program a scan_address function, a print_address function, and a local_address function. Function local_address should take two address structures as input parameters and return 1 (for true) if the addresses are on the same local network, and 0 (for false) otherwise.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct IPaddress {
int xx,yy,zz,mm;
char n[11];
} IPaddress;
void scan_address(char *nin, IPaddress *ip, int *NN)
{
FILE *fin;
int j,nn=0;
int bsize = 72;
char *buffer;
buffer = (char *) malloc(bsize * sizeof (char));
if ( (fin = fopen(nin,"r") ) == NULL ) {
printf("Cann't able to open input file: %s ", nin);exit(1);
};
while (fgets(buffer,bsize,fin) !=NULL){
j = sscanf(buffer,"%d.%d.%d.%d %10s", &ip[nn].xx, &ip[nn].yy,&ip[nn].zz, &ip[nn].mm, &ip[nn].n);
if (j == 5) nn = nn + 1;
};
fclose(fin);
*NN = nn;
printf("I have read %d line data ",*NN);
}
void print_address(IPaddress *ip, int NN)
{
int i;
for (i=0;i<NN;i++){
printf("%d %d %d %d%s ",ip[i].xx,ip[i].yy,ip[i].zz,ip[i].mm,ip[i].n);
}
}
int local_address( IPaddress ip1, IPaddress ip2)
{
if (ip1.xx == ip2.xx && ip1.yy == ip2.yy) {
printf ("%s and %s are local computer ! ",ip1.n,ip2.n);
return 1;
} else {
system("pause");
return 0;
};
}
int main(int argc, char * argv[])
{
int i,j,k;
char nin[120];
IPaddress ip[100];
int NN=0;
for (i=0;i<100;i++){
ip[i].xx=0;ip[i].yy=0;ip[i].zz=0;ip[i].mm=0;
strcpy(ip[i].n,"");
}
if (argc < 2) {
printf("Usage: %s ip_list_file_n ",argv[0]);
exit(0);
}
strcpy(nin,argv[1]);
(void) scan_address(nin, &ip[0], &NN);
(void) print_address(&ip[0], NN);
for (i=0;i<NN-2;i++)
for (j=i+1;j<NN-1;j++)
if ( local_address( ip[i], ip[j]) == 1) printf("-- true ! ");
}
Note - i have given the code in c program.. and satisfied the given condition
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.