|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
就是感觉自己语法上没有问题,但是就是不能读取输出出来struct communicationMethod
{
int num;
char type;
char name[20];
char telephone[20];
char email[40];
}ComMtd[50];
FILE *fp;
int fp_i=0;
fp = fopen("C:\\Users\\ljr\\Desktop\\addressbook\\phonebook.txt", "w+");
for(i=0;i<50;i++)
{
fscanf(fp,"%d %c %s %s %s\n",&ComMtd[i].num,&ComMtd[i].type,ComMtd[i].name,ComMtd[i].telephone,ComMtd[i].email);
}
所有代码#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<dos.h>
#include<windows.h>
#include<time.h>
#include<stdio.h>
struct communicationMethod
{
int num;
char type;
char name[20];
char telephone[20];
char email[40];
}ComMtd[50];
void Show(int j)
{
printf("\n编号:%d", ComMtd[j].num);
printf("\n联系人所属类别:%c", ComMtd[j].type);
printf("\n姓名:%s", ComMtd[j].name);
printf("\n电话:%s", ComMtd[j].telephone);
printf("\n电子邮箱:%s", ComMtd[j].email);
printf("\n\n");
}
void Append(int len)
{
ComMtd[len].num = len;
printf("新增联系人序号为:%d",ComMtd[len].num);
printf("\n请输入联系人分类,A表示办公类,B表示个人类,C表示商务类: ");
scanf("%c", &ComMtd[len].type);
printf("\n请输入姓名: ");
scanf("%s", ComMtd[len].name);
printf("\n请输入联系人联系电话: ");
scanf("%s", &ComMtd[len].telephone);
}
void Delete(int del_i,int len)
{
int del_k;
printf("\n将删除序号为%d的联系人\n",del_i);
for (del_k = del_i; del_k<len; del_k++) /*插入位置后的元素顺序后移*/
{
strcpy(ComMtd[del_k].name, ComMtd[del_k + 1].name); /*交换元素内容*/
ComMtd[del_k].type=ComMtd[del_k + 1].type;
strcpy(ComMtd[del_k].telephone, ComMtd[del_k + 1].telephone);
ComMtd[del_k].num = ComMtd[del_k + 1].num;
strcpy(ComMtd[del_k].email, ComMtd[del_k + 1].email);
}
}
void Update(int upd_i,int len)
{
int upd_j;
char upd_name[20],upd_type,upd_email[40],upd_tel[20];
printf("请输入要修改联系人资料类别:\n1为姓名,2为分类(A表示办公类,B表示个人类,C表示商务类),\n3为电话号码,4为电子邮箱,按其他键退出修改\n");
while (1){
if (upd_j == 1){
printf("请输入修改后姓名:");
scanf("%s", upd_name);
strcpy(ComMtd[upd_j].name, upd_name);
printf("\n已将姓名修改为:%s\n", ComMtd[upd_j].name);
}
else if (upd_j == 2){
printf("请输入修改后分类:");
scanf("%c", upd_type);
ComMtd[upd_j].type=upd_type;
printf("\n已将分类修改为:%c\n", ComMtd[upd_j].type);
}
else if (upd_j == 3){
printf("请输入修改后电话号码:");
scanf("%s", upd_tel);
strcpy(ComMtd[upd_j].telephone, upd_tel);
printf("\n已将电话号码修改为:%s\n", ComMtd[upd_j].telephone);
}
else if (upd_j == 4){
printf("请输入修改后电子邮箱:");
scanf("%s", upd_email);
strcpy(ComMtd[upd_j].email, upd_email);
printf("\n已将电子邮箱修改为:%s\n", ComMtd[upd_j].email);
}
else break;
}
}
void Call(int cal_i)
{
int cal_j;
for (cal_j = 0; ComMtd[cal_i].telephone[cal_j] != '\0'; cal_j++)
{
printf("%c", ComMtd[cal_i].telephone[cal_j]);
switch (ComMtd[cal_i].telephone[cal_j])
{
case 0:Beep(523, 500);
case 1:Beep(578, 500);
case 2:Beep(659, 500);
case 3:Beep(698, 500);
case 4:Beep(784, 500);
case 5:Beep(880, 500);
case 6:Beep(988, 500);
case 7:Beep(1046, 500);
case 8:Beep(1175, 500);
case 9:Beep(1318, 500);
}
Sleep(2000);
}
}
int main()
{
int option,count=0,i;
char type;
int len=sizeof(ComMtd) / sizeof(ComMtd[0]);
int sho_i;
int sea_i;
int app_i;
int sav_i=0;
int del_i=0,del_j=1;
int upd_i = 0,upd_j=1;
int cal_i = 0,cal_j=1;
char delname[20],updname[20],calname[20];
FILE *fp;
int fp_i=0;
fp = fopen("C:\\Users\\ljr\\Desktop\\addressbook\\phonebook.txt", "w+");
for(i=0;i<50;i++)
{
fscanf(fp,"%d %c %s %s %s\n",&ComMtd[i].num,&ComMtd[i].type,ComMtd[i].name,ComMtd[i].telephone,ComMtd[i].email);
}
printf(" 欢迎使用通讯录\n" );
printf("=====================================================\n");
printf(" 按0退出通讯录\n");
printf(" 按1查看所有联系人\n");
printf(" 按2查找某一类联系人\n");
printf(" 按3添加联系人\n" );
printf(" 按4删除联系人\n");
printf(" 按5修改联系人\n");
printf(" 按6选择联系人拨号\n");
printf(" 按7保存修改后信息\n");
printf("=====================================================\n");
while (1)
{
printf("\n\n请选择:\n");
scanf("%d", &option);
switch (option)
{
case 0:exit(0);
case 1://查看所有联系人
{
for(sho_i=0;sho_i<len;sho_i++)
{
Show(sho_i);
}
printf("\n\n所有联系人列出完毕。");
break;
}
case 2://查找联系人
{
printf("\n\n请输入要查找联系人的分类,A表示办公类,B表示个人类,C表示商务类");
scanf("%c", type);
for (sea_i = 0; sea_i <=len; sea_i++)
{
if (type == ComMtd[sea_i].type) Show(sea_i);
}
break;
}
case 3://添加联系人
{
printf("进入添加界面,若继续添加请输入1,否则输入0\n");
while (len < 50)
{
Append(len);
len++;
scanf("%d", &app_i);
if (app_i== 0)break;
}
break;
}
case 4://删除联系人
{
printf("请输入需删除联系人姓名:\n");
scanf("%s", delname);
while (1){
if (strcmp(delname, ComMtd[del_i].name) == 0) break;
else if (del_i<=len)del_i++;
else {
printf("该联系人不存在!");
del_j = 0;
break;
}
}
if (del_j != 0) Delete(del_i,len);
break;
}
case 5://修改联系人
{
printf("请输入要修改联系人的姓名:\n");
scanf("%s", delname);
while (1){
if (strcmp(delname, ComMtd[upd_i].name) == 0) break;
else if (upd_i <= len)upd_i++;
else {
printf("该联系人不存在!");
upd_j = 0;
break;
}
}
if (upd_j != 0) Update(upd_i,len);
break;
}
case 6://拨打号码
{
printf("请输入要拨打的电话号码联系人姓名:");
scanf("%s", calname);
while (1){
if (strcmp(calname, ComMtd[cal_i].name) == 0) break;
else if (cal_i <= len)cal_i++;
else {
printf("该联系人不存在!");
cal_j = 0;
break;
}
}
if (cal_j != 0) Call(cal_i);
break;
}
case 7://保存修改
{
while (sav_i<len)
{
fprintf(fp,"%d %c %s %s %s",ComMtd[sav_i].num,ComMtd[sav_i].type,ComMtd[sav_i].name,ComMtd[sav_i].telephone,ComMtd[sav_i].email);
sav_i++;
}
printf("修改保存完成");
break;
}
default:printf("\n输入有误请重新输入");
}
}
}
|
|