Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in C. For this assignment we will write a simple database server. We wil

ID: 3821664 • Letter: T

Question

This is in C.

For this assignment we will write a simple database server.

We will be creating a simple database of student records, so let’s describe these first. The format of a student record is as follows:

typedef struct student {

    char lname[ 10 ], initial, fname[ 10 ];

    unsigned long SID;

    float GPA;

} SREC;

Part One – the Server

We will create a database server. The job of the server is to accept a connection on a socket and perform one of the following actions:

A “get” command asking for the data to be returned in order, either alphabetical order by last name, alphabetical order by first name, in order by student ID, or in order by grade point average. In the case of GPA, the highest should come first (4.0 down to 0.0, not 0.0 up to 4.0).

A “put” command that will take one new SREC and add it to the database.

A “delete” command that will look up a certain SREC by SID and delete that record from the database.

A “stop” command. This command will save the entire database into a file (you name the file whatever you like). When the program starts again, if the file exists the contents are used to initialize the lists. In this way we can insert records, stop the server, run it again, and the records from before are available again. The data should be saved in the file in binary, by using “fread” and “fwrite” or “read” and “write”, whichever you prefer. Each “sizeof(SREC)” represents one student in the file.

While in memory the records must be maintained on four data structures, ordered by “lname”, “fname”, “SID”, or “GPA”. In the case of the names, use the order returned by “strcmp” as the ordering criteria. You can use singly-linked lists, doubly linked lists, or binary search trees – your choice. (If you choose to do search trees, when deleting a record you can mark the tree nodes as deleted and avoid having to write the actual tree code for a delete.)

Part Two – the Client

The client program asks for an activity, and acts on the activity. The client reads commands from the user, formats them into requests to the server, sends the request, and then takes back replies from the server and displays the results. Here are examples of the commands which correspond to the above, along with comments:

Command

Note

get lname

The server sends back the records, ordered by last name

get fname

The server sends back the records, ordered by first name

get SID

The server sends the records back ordered by SID

get GPA

The server sends back the records by high-to-low GPA.

put Mahoney,Bill,R,12345,4.0

The record is added to the database. There are no spaces in the “data” part of the record, and the fields are split apart by commas. The GPA and SID are guaranteed to be valid values, however note that the names may be longer than the 10 characters allowed by the structure. It is up to you to chop the names if necessary.

delete 12345

The database record with SID 12345 is removed.

stop

The server saves the file and exits. The client exits as well.

The format of commands is exactly as shown, in lower case for the commands and in mixed case for the data. “get” is used, not “Get”, for instance, and there will be exactly one space after the command and before any arguments.

The program should correctly handle the case where the user enters a command that is not in the list.

It is necessary to “pretty print” the results that come back from the server. For example, the output should look like this:

Enter next command> get SID

| SID   | Lname    | Fname      | M | GPA |

+-------+-----------+------------+---+------+

| 33445 | Mahoney | Bill       | R | 4.00 |

| 45678 | Cavanaugh | Patrick    | Z | 4.00 |

| 67890 | Dough    | John       | A | 2.67 |

| 88891 | Smythe   | Jane       | F | 3.78 |

| 99341 | Namethati | Waytoolon | S | 3.81 |

+-------+-----------+------------+---+------+

Enter next command> put Simpson,Homer,A,4,1.3

Enter next command> get SID

| SID   | Lname    | Fname      | M | GPA |

+-------+-----------+------------+---+------+

| 00004 | Simpson   | Homer      | A | 1.30 |

| 33445 | Mahoney   | Bill       | R | 4.00 |

| 45678 | Cavanaugh | Patrick    | Z | 4.00 |

| 67890 | Dough     | John       | A | 2.67 |

| 88891 | Smythe    | Jane       | F | 3.78 |

| 99341 | Namethati | Waytoolon | S | 3.81 |

+-------+-----------+------------+---+------+

The output should look exactly as shown (with different data obviously) including the ‘|’, ‘+’, etc. The “SID” should occupy exactly 5 columns, zero filled if needed, with a space on either side. The names, both the last and first, should occupy exactly nine columns, with a space on either side. The middle initial has a space on each side. The GPA should occupy exactly four columns.

Part Three – the Port Number and usage

It will be necessary to make sure that each student in the class has a unique port number to work from. Also, what happens on a Linux machine if a process dies unexpectedly is that the port remains “in use” for a period of time. Thus we will assign port numbers in ranges, with 10 per student. Suppose your port numbers are 20000-20009. You could write the program so that it uses port 20000 and if that port is in use after a program crashes, you can switch to 20001. The ports remain in use only for a certain amount of time, so assigning things in groups of ten should be fine. From playing around on Loki it seems to take about a minute or so before the socket is once again available.

There is a class list posted on Blackboard along with this assignment that shows your port range assignment.

You do not retrieve the port number from a command line argument, rather create an easy to access (and find for my sake) constant for your port number, for example:

#define PORT_NUM 20000

Neither the Server nor the Client should accept any command line arguments. The port number is going to be previously defined on both sides as discussed above, and your client can assume the host as “loki” or “localhost”.

Hints

It will be necessary for the server to “tell” the client how many records will be coming back on a “get” request. I suggest that when the server receives the request, it first sends back an integer to the client, and then the client and server loop, reading (or sending) each record individually.

I would suggest that in the case of a “put”, the client take the command line and convert it into an SREC and then send the “put” command followed by the record.

For that matter, you might consider having the client send an integer or character representing what action is to be taken. These could include the four types of “get” requests as well.

Hand-in instructions…

Note: After the programs are collected, any compiled program (executable) in that directory is deleted and the program is recompiled for testing. Specifically, make sure you compile the program with the same options that are used for grading:

                gcc -Wall –ansi –pedantic –o server server.c

                gcc -Wall –ansi –pedantic –o client client.c

Command

Note

get lname

The server sends back the records, ordered by last name

get fname

The server sends back the records, ordered by first name

get SID

The server sends the records back ordered by SID

get GPA

The server sends back the records by high-to-low GPA.

put Mahoney,Bill,R,12345,4.0

The record is added to the database. There are no spaces in the “data” part of the record, and the fields are split apart by commas. The GPA and SID are guaranteed to be valid values, however note that the names may be longer than the 10 characters allowed by the structure. It is up to you to chop the names if necessary.

delete 12345

The database record with SID 12345 is removed.

stop

The server saves the file and exits. The client exits as well.

Explanation / Answer

#include <stdio.h>
#include <stdbool.h>
#include <conio.h>
#include <windows.h>
typedef struct{
   char studID[6];
   char fname[30];
   char lname[30];
   char mname[3];
   float qpa;
}STUD;

typedef struct node *nd;
struct node
{
   STUD stu;
   nd left;
   nd rigth;
}NODE;


void inputStud(nd *);
bool treeEmpty(nd *);
void deleteNode(nd *);
void editStud(nd *);
void viewAll(nd);
nd searchNode(nd *,nd *,char[]);

int main(void)
{
int ans,c;
bool e;
nd root = NULL;

do
{
   system("cls");
   printf("1] Add New Student Record ");
   printf("2] Edit Student Record ");
   printf("3] View Specific Record ");
   printf("4] Display All ");
   printf("5] Delete Student Record ");
   printf("6] Exit ");
   printf("Pick your choice: ");
   scanf("%d",&ans);
   switch(ans)
   {
          case 1:     inputStud(&root);
                      break;
          case 2:     editStud(&root);
                      break;
          case 3:     viewAll(root);
                      getch();
                      break;
          case 4:     viewAll(root);
                      getch();
                      break;
          case 5:     deleteNode(&root);
                      break;
          case 6:     printf(" closing the program in 5 seconds....");
                      sleep(5000);

   }
}while (ans != 6);
return 0;
}


void inputStud(nd *root)
{
nd t, t1, temp;
char sID[6];
char lnam[20];
char fnam[30];
char mnam[3];
float qp;
STUD st;

printf("Enter Student ID: /n");
gets(sID);
printf("Enter Student's Last Name: /n");
gets(lnam);
printf("Enter Student's First Name: /n");
gets(fnam);
printf("Enter Student's Middle Initial(s): /n");
gets(mnam);
printf("Enter Student's QPA: /n");
scanf("%f", &qp);
strcpy(st.studID, sID);
strcpy(st.lname, lnam);
strcpy(st.fname, fnam);
strcpy(st.mname, mnam);
st.qpa=qp;

temp = malloc(sizeof(NODE));
temp -> stu = st;
temp -> left = NULL;
temp -> rigth = NULL;

if (*root == NULL)
{
      *root = temp;
}
else
{
      t = *root;
      while (t != NULL)
      {
           t1 = t;
           if (strcmp(temp -> stu.studID,t -> stu.studID)==0)
               t = t -> left;
           else
               t = t -> rigth;
      }
      if (strcmp(temp -> stu.studID,t1 -> stu.studID) < 0)
          t1 -> left = temp;
      else
          t1 -> rigth = temp;
}
t1 = NULL;
t = NULL;
temp = NULL;
return;
}
void editStud(nd *root)
{
char sID[6];
char lnam[20];
char fnam[30];
char mnam[3];
float qp;
STUD st;

printf("Please enter the ID of the Student you would like to edit: ");
gets(sID);

if(strcmp(sID,st.studID)==0)
{
   printf("Enter Student ID: /n");
   gets(sID);
   printf("Enter Student's Last Name: /n");
   gets(lnam);
   printf("Enter Student's First Name: /n");
   gets(fnam);
   printf("Enter Student's Middle Initial(s): /n");
   gets(mnam);
   printf("Enter Student's QPA: /n");
   scanf("%f", &qp);
   strcpy(st.studID, sID);
   strcpy(st.lname, lnam);
   strcpy(st.fname, fnam);
   strcpy(st.mname, mnam);
   st.qpa=qp;
}
else
{
   printf("Student not found..");
}
return;
}              

void deleteNode(nd *root)
{
char iD[6];
bool e,lmoved = false;
nd t,t1,t2;
STUD st;

e = treeEmpty(root);
if (e)
{
       printf(" Sorry, the tree is empty...");
       sleep(3000);
}
else
{
   printf("Please enter ID of student to be deleted: ");
   gets(iD);
   t = searchNode(root,&t1,iD[]);
   if (t == NULL)
   {
           printf(" %s not found...",iD);
           sleep(2000);
   }
   else
   {

           if ((t -> left == NULL)&&(t -> rigth == NULL))
           {
                   if (strcmp( iD, t1 -> stu.studID) <0)
                       t1 -> left = NULL;
                   else
                       t1 -> rigth = NULL;
           }
           else if ((t -> left == NULL)&&(t -> rigth != NULL))
           {

                   if (strcmp( iD,t1 -> stu.studID) <0)
                       t1 -> left = t -> rigth;
                   else
                       t1 -> rigth = t -> rigth;
           }
           else if ((t -> left != NULL)&&(t -> rigth == NULL))
           {

                   if (strcmp( iD,t1 -> stu.studID) <0)
                       t1 -> left = t -> left;
                   else
                       t1 -> rigth = t -> left;
           }
           else
           {

               t1 = t;
               t = t1 -> rigth;
               while(t -> left != NULL)
               {
                   t2 = t;
                   t = t -> left;
                   lmoved = true;
               }
               t1 -> stu.studID = t -> stu.studID;
               if (t -> rigth == NULL)
               {
                   if (lmoved)
                       t2 -> left = NULL;
                   else
                       t1 -> rigth = NULL;
               }
               else
               {
                   if (lmoved)
                       t2 -> left = t -> rigth;
                   else
                       t1 -> rigth = t -> rigth;
               }
           }
           free(t);
           t1 = NULL;
           t2 = NULL;

   }
}
return;
}

nd searchNode(nd *r,nd *t1,char iD[], nd root)
{
nd t;
t = *r;
STUD st;
while((t != NULL) && (strcmp(t -> stu.studID, iD)== 0)
{
       *t1 = t;
       if ((strcmp( iD,t1 -> stu.studID) <0)
          t = t -> left;
       else
           t = t -> rigth;
}
return t;
}

bool treeEmpty(nd *r)
{
bool e = false;
if (*r == NULL)
       e = true;
return e;
}

void viewAll(nd ptr)
{
if (ptr != NULL)
{
      printf("Student ID: %s", stu.studID );
      printf("First Name: %s", stu.fName);
      printf("Middle Initial: %s", stu.mName);
      printf("Last Name: %s", stu.lName);
      printf("QPA: %f", stu.qpa);

}
return;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote