此代码的数据结构必须与编写时保持一致。我有打印和添加到内存位置的问题。我认为我有添加功能工作正常,但我不确定。我认为我没有正确地在内存中移动来打印和添加。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pointer.h"
/********************************************
Creates more memory for size (strut * rec+1)
*********************************************/
employee *create(int record){
employee *new_employee = malloc(sizeof(employee) * (record+1));
return new_employee;
}
/********************************************
Copies the data from one structure to a new structure with
size "structure" multipled by rec+1
***********************************************/
employee *copy(employee *data, int record){
employee *new_employee = create(record);
memcpy(new_employee, data, record * sizeof(employee));
/*
int i;
for(i = 0; i<record;i++){
new_employee->first = data->first;
new_employee->last = data->last;
new_employee->start_date = data->start_date;
new_employee->sal = data->sal;
data++;
}
*/
/********************
Needs to free the old struct
*********************/
//deleteData(data, record);
return new_employee;
}
/********************************************
Function prints everything in the struct
*********************************************/
void printStruct(employee *data, int record){
employee *head = data;
employee *employeeDB = head;
int i;
for(i = 0; i<record; i++){
printf("nEntry: %dn", i+1);
printf("The employee's name is %s %sn", employeeDB->first, employeeDB->last);
printf("The employee was hired on: %sn", employeeDB->start_date);
printf("The employee make $%fnn", employeeDB->sal);
employeeDB++;
}
}
/******************************************
Function frees the old data base
*******************************************/
void deleteData(employee *data, int record){
int i;
for(i = 0; i<record; i++){
free(data->first);
free(data->last);
free(data->start_date);
data++;
}
free(data);
}
/******************************************
Adds an employee to the new structure
*******************************************/
employee *add(employee *data,char *fname, char *lname, char *date, float salary, int record){
fprintf(stderr, "nnEmployee data = %s for record # %dnn", data->first, record);
employee *head = create(record);
employee *employeeDB = head;
/*
employeeDB = copy(data, record);
fprintf(stderr, "Copied %d recordsn", record);
*/
//
//
//
// BUILD NEW EMPLOYEE
employee * new_employee = malloc(sizeof(employee));
new_employee->first = fname;
new_employee->last = lname;
new_employee->start_date = date;
new_employee->sal = salary;
// COPY EXISING DATA TO EMPLOYEEDB
memmove(employeeDB,data,sizeof(employee));;
fprintf(stderr, "Now the first record has first = %sn", employeeDB->first);
fprintf(stderr, "Now the first record(head) has first = %sn", head->first);
// MOVE HEAD
employeeDB = employeeDB + record;
memmove(employeeDB, new_employee, sizeof(employee));
fprintf(stderr, "Now the first record has first = %sn", employeeDB->first);
fprintf(stderr, "Now the first record(head) has first = %sn", head->first);
//
//
//
//
return head;
}
/**************************
Starts of the main function
***************************/
int main(void){
//Keeps track of the number of records that are in the structure
int rec = 0;
//Keeps the number of accesses to the structure. Even with the one entry the structure has not been accessed.
int acc = 0;
//Holds the input information for the menu
int input;
//holds the information for inputing user first name
char fname[MAXSIZE];
//holds the information for inputing user last name
char lname[MAXSIZE];
//holds the information for for the startdate
char start[MAXSIZE];
//holds the information for the salary;
float sal;
/*********************************
This next section adds an employee to the record
************************************/
//This creates the first entry to the dynamic structure.
employee *data_base = create(rec);
data_base->first = "FIRST";
data_base->last = "LAST";
data_base->start_date = "June-20th-2006";
data_base->sal = 55555.55;
//increase the number of records
rec = rec+1;
/**********************************
/*
This starts the while loop that creates the menu for the user to choose from
**********************************/
while(1){
printf("nWELCOME TO myEMPLOYEE DATABASEn");
printf("n------------------n");
printf("nMENU");
printf("n1. Print All Recordsn");
printf("2. Print Number of Recordsn");
printf("3. Print Size of databasen");
printf("4. Add an Employeen");
printf("5. Delete all Employeen");
printf("6. Print number of accesses to databasen");
printf("7. EXITn");
printf("nENTER SELECTION: ");
/* This scanf functions scans for the appropiate input, if the input is
not one of the selection items the program exits*/
scanf("%d", &input);
switch (input) {
case 1:
printStruct(data_base, rec);
acc = acc + 1;
break;
case 2:
printf("The number of records are %dn", rec);
acc = acc + 1;
break;
case 3:
printf("The size of the data base is %dn", sizeof(employee) * rec);
acc = acc + 1;
break;
case 4:
printf("nPlease enter the employee's first name:");
scanf("%s", fname);
printf("nPlease enter the employee's last name:");
scanf("%s", lname);
printf("nPlease enter the employee's start date (Month-Day-Year):");
scanf("%s", start);
printf("nPlease enther the employee's salaray:");
scanf("%f", &sal);
acc = acc + 1;
break;
case 5:
break;
case 6:
printf("The number of accesses to the data is: %d", acc);
break;
case 7:
printf("The program has exited");
return 1;
default:
// If a wrong selection is entered it will display this message.
printf("Please enter a selection 1-7");
}
}
}
/*
Header file for pointer.c
*/
#include <string.h>
#define MAXSIZE 255;
typedef struct info {
char *first;
char *last;
char *start_date;
float sal;
}employee;
employee *create(int record);
employee *copy(employee *data, int record);
void printStruct(employee *data, int record);
void deleteData(employee *data, int record);
employee *add(employee *data,char *fname, char *lname, char *date, float salary, int record);
删除#define MAXSIZE 255;
末尾的;
。当你有MAXSIZE
时,它将被255;
取代,因此char fname[255;];
显然是无效的。
#define MAXSIZE 255