/****contacts.h***/は
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
struct Address {
char street[41];
int streetNumber;
int apartmentNumber;
char postalCode[8];
char city[41];
};
struct Numbers {
char cell[10];
char home[10];
char business[10];
};
struct Contact {
struct Name name;
struct Address address;
struct Numbers number;
};
void getName(struct Name *name);
void getAddress(struct Address *address);
void getNumbers(struct Numbers *number);
void getContact(struct Contact *contact);
/****Contacts.c****/
別の.cファイルにする.Cで定義された関数を呼び出す必要があります#include <stdio.h>
#include "contacts.h"
#include "contactHelpers.h"
void getName(struct Name *name)
{
printf("Please enter the contact's first name: ");
scanf(" %30[^\n]s",name->firstName);
clearKeyboard();
printf("Do you want to enter a middle initial(s)? (y or n): ");
if(yes())
{
printf("Please enter the contact's middle initial(s): ");
scanf(" %6[^\n]s",name->middleInitial);
clearKeyboard();
}
else
name->middleInitial[0]='\0';
printf("Please enter the contact's last name: ");
scanf(" %35[^\n]s",name->lastName);
clearKeyboard();
}
void getAddress(struct Address *address)
{
printf("Please enter the contact's street number: ");
address->streetNumber=getInt();
printf("Please enter the contact's street name: ");
scanf(" %40[^\n]s",address->street);
clearKeyboard();
printf("Do you want to enter an apartment number? (y or n): ");
if(yes())
{
printf("Please enter the contact's apartment number: ");
address->apartmentNumber=getInt();
}
else
address->apartmentNumber='\0';
printf("Please enter the contact's postal code: ");
scanf(" %7[^\n]s",address->postalCode);
clearKeyboard();
printf("Please enter the contact's city: ");
scanf(" %40[^\n]s",address->city);
clearKeyboard();
}
void getNumbers(struct Numbers *numbers)
{
printf("Please enter the contact's cell phone number: ");
getTenDigitPhone(numbers->cell);
printf("Do you want to enter a home phone number? (y or n): ");
if(yes())
{
printf("Please enter the contact's home phone number: ");
getTenDigitPhone(numbers->home);
}
else
numbers->home[0]='\0';
printf("Do you want to enter a business phone number? (y or n): ");
if(yes())
{
printf("Please enter the contact's business phone number: ");
getTenDigitPhone(numbers->business);
}
else
numbers->business[0]='\0';
}
void getContact(struct Contact *contact)
{
getName(&(contact->name));
getAddress(&(contact->address));
getNumbers(&(contact->number));
}
/****contactHelpers.h*****/
#include"contacts.h"
void clearKeyboard(void);
void pause(void);
int getInt(void);
int getIntInRange(int, int);
int yes(void);
int menu(void);
void ContactManagerSystem(void);
void getTenDigitPhone(char[11]);
int findContactIndex(const struct Contact contacts[], int size, const char cellNum[]);
void displayContactHeader(void);
void displayContactFooter(int);
void displayContact(const struct Contact* contacts);
void displayContacts(const struct Contact[], int);
void searchContacts(const struct Contact[], int);
void addContact(struct Contact[], int);
void updateContact(struct Contact[], int);
void deleteContact(struct Contact[], int);
void sortContacts(struct Contact[], int);
/****ContactsHelpers.c****/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include "contactHelpers.h"
#define MAXCONTACTS 5
int a=5;
void clearKeyboard(void)
{
while (getchar() != '\n') ;
}
void pause(void)
{
char ch;
printf("(Press Enter to continue)");
scanf("%c", &ch);
if(ch==10)
{
clearKeyboard();
}
}
int getInt(void)
{
int i;
char cha;
printf("<Please enter an integer>: ");
scanf("%d%c", &i,&cha);
while (cha !='\n')
{
clearKeyboard();
printf("*** INVALID INTEGER *** <Please enter an integer>: ");
scanf("%d%c", &i,&cha);
}
return i;
}
int getIntInRange(int min, int max)
{
int i;
i=getInt();
while(i< min || i > max)
{
printf("*** OUT OF RANGE *** <Enter a number between %d and %d>: ", min , max);
scanf("%d", &i);
}
}
int yes(void)
{
char a,b;
printf("<Please enter a character>: ");
scanf("%c%c", &a,&b);
while ((a !='Y' && a !='y' && a !='N' && a!='n') || (b!='\n'))
{
if (b!='\n') ungetc(b, stdin),scanf("%*[^\n]%c", &b);
a='n',b='\n';
printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ");
scanf("%c%c", &a,&b);
}
if (a =='Y' || a=='y')
{
return 1;
printf("Contact Management System: terminated\n");
}
else
{
if (a =='N' || a=='n')
{
return 0;
}
}
}
int menu(void)
{
int i;
printf("Contact Management System\n-------------------------\n1. Display contacts\n2. Add a contact\n3. Update a contact\n4. Delete a contact\n5. Search contacts by cell phone number\n6. Sort contacts by cell phone number\n0. Exit\n\nSelect an option:> ");
scanf("%d", &i);
while (i<0 || i>6)
{
printf("*** OUT OF RANGE *** <Enter a number between 0 and 6>: ");
scanf("%d", &i);
}
return i;
}
void ContactManagerSystem(void)
{
struct Contact contacts[a];
int i=menu();
int choice, exit =0;
do
{
i = menu();
switch(i)
{
case 0 :
printf("Exit the program? (Y)es/(N)o: ");
choice=yes();
printf("\n");
if(choice == 1)
{
printf("Contact Management System: terminated\n");
exit = 1;
}
if (choice == 0)
{
break;
}
case 1 :
printf("\n");
displayContacts(contacts, a);
pause();
printf("\n");
break;
case 2 :
printf("\n");
addContact(contacts,a);
pause();
printf("\n");
break;
case 3 :
printf("\n");
updateContact(contacts,a);
pause();
printf("\n");
break;
case 4 :
printf("\n");
deleteContact(contacts,a);
pause();
printf("\n");
break;
case 5 :
printf("\n");
searchContacts(contacts,a);
pause();
printf("\n");
break;
case 6 :
printf("\n");
sortContacts(contacts,a);
pause();
printf("\n");
break;
}
}
while (exit == 0);
}
void getTenDigitPhone(char telNum[11])
{
int needInput = 1;
while (needInput == 1) {
scanf("%10s", telNum);
clearKeyboard();
if (strlen(telNum) == 10)
needInput = 0;
else
printf("Enter a 10-digit phone number: ");
}
}
int findContactIndex(const struct Contact contacts[], int size, const char cellNum[])
{
int i;
for(i=0; i<=size; i++)
{
if(strcmp(contacts[i].number.cell, cellNum)==0)
{
return i;
}
}
return -1;
}
void displayContactHeader(void)
{
printf("+-----------------------------------------------------------------------------+\n");
printf("| Contacts Listing |\n");
printf("+-----------------------------------------------------------------------------+ \n");
}
void displayContactFooter(int total)
{
printf("+-----------------------------------------------------------------------------+\n");
printf("Total contacts: %d\n\n", total);
}
void displayContact(const struct Contact* contacts)
{
if(strcmp((*contacts).name.middleInitial,"")==0)
printf(" %s %s\n",(*contacts).name.firstName,(*contacts).name.lastName);
else
printf(" %s %s %s\n",(*contacts).name.firstName,(*contacts).name.middleInitial,(*contacts).name.lastName);
printf(" C: %-10s H: %-10s B: %-10s\n",(*contacts).number.cell,(*contacts).number.home,(*contacts).number.business);
if((*contacts).address.apartmentNumber>0)
printf(" %d %s, Apt# %d, %s, %s\n",(*contacts).address.streetNumber,(*contacts).address.street,(*contacts).address.apartmentNumber,(*contacts).address.city,(*contacts).address.postalCode);
else
printf(" %d %s, %s, %s\n",(*contacts).address.streetNumber,(*contacts).address.street,(*contacts).address.city,(*contacts).address.postalCode);
}
void displayContacts(const struct Contact contacts [], int size)
{
int i, tot=0;
displayContactHeader();
for(i=0;i<size;i++)
{
if(strlen(contacts[i].number.cell)>0)
{
tot=tot+1;
displayContact(&contacts[i]);
}
}
displayContactFooter(tot);
}
void searchContacts(const struct Contact contacts[], int size)
{
char num[11];
int result;
printf("Enter the cell number for the contact: ");
getTenDigitPhone(num);
result = findContactIndex(contacts,size, num);
if(result== -1)
{
printf("\n");
displayContact(&contacts[result]);
printf("\n");
}
else
printf("*** Contact NOT FOUND ***\n");
}
void addContact(struct Contact contacts [], int size)
{
int i, available=-1;
for(i=0;i<size;i++)
{
if(strlen(contacts[i].number.cell)==0)
{
available=i;
break;
}
}
if(available>-1)
{
getContact(&contacts[available]);
printf("--- New contact added! ---\n");
}
else
{
printf("*** ERROR: The contact list is full! ***");
printf("\n");
}
}
void updateContact(struct Contact contacts[], int size)
{
char myNum[11];
int myIndex;
printf("Enter the cell number for the contact: ");
getTenDigitPhone(myNum);
myIndex = findContactIndex(contacts,size,myNum);
if(myIndex>-1)
{
printf("\nContact found:\n");
displayContact(&contacts[myIndex]);
printf("\nDo you want to update the name? (y or n): ");
if(yes())
getName(&contacts[myIndex].name);
printf("Do you want to update the address? (y or n): ");
if(yes())
getAddress(&contacts[myIndex].address);
printf("Do you want to update the numbers? (y or n): ");
if(yes())
getNumbers(&contacts[myIndex].number);
printf("--- Contact Updated! ---\n");
}
else
printf("*** Contact NOT FOUND ***\n");
}
void deleteContact(struct Contact contacts [], int size)
{
char num[11];
char c;
printf("Enter the cell number for the contact: ");
scanf("%s%c", num), &c;
getTenDigitPhone(num);
int b = findContactIndex(contacts, size ,num);
if(b == -1)
{
printf("*** Contact NOT FOUND ***\n");
}
else
{
printf("\nContact found:\n");
displayContact(&contacts[b]);
printf("CONFIRM: Delete this contact? (y or n): ");
int c = yes();
if(c == 1)
{
contacts[b].number.cell[0]='\0';
} printf("--- Contact deleted! ---\n");
}
}
void sortContacts(struct Contact contacts[], int a)
{
int i, j;
struct Contact temp;
for (i = 0; i < a - 1; i++)
{
for (j = 0; j < (a - 1-i); j++)
{
if (contacts[j].number.cell < contacts[j + 1].number.cell)
{
temp = contacts[j];
contacts[j] = contacts[j + 1];
contacts[j + 1] = temp;
}
}
}
}
/**** **** MAIN/
int main(void)
{
ContactManagerSystem();
return 0;
}
だから私のコードで私は2つの.cファイルを持っています。 contacts.cでは、関数getContactを定義しており、ContactHelpers.cでそれを使用しています。私はcontactHelpers.cにヘッダファイル(getcontactのプロトタイプを含む)をインクルードしていますが、 `getContact(Contact *) 'の定義されていないエラーをmaに返します。 contactHelpers.cにcontacts.cを含めると、コンパイルが中断されることはありません。助けてください
コメントは、拡張された議論のためではありません。この会話は[チャットに移動されました](http://chat.stackoverflow.com/rooms/162006/discussion-on-question-by-s-younas-need-to-call-function-defined-in-c- to-to-ano)を使用します。 –