?? 通訊錄管理系統.cpp
字號:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define N 101
struct student
{
char num[20];
char name[20];
char phone[20];
char youbian[20];
char address[20];
}st[N];
void manage();
void input();
void display();
void research();
void add();
void del();
int flag;
char password[10]="tongxunlu";
char mima[10]="000000";
FILE *fp;
//*********************************************************************************************
void main()
{ system("title 通訊錄管理系統");
char s[10];
char s1[10];
int m=0,k;
int n=3;
system("color ce");
printf("\n\t\t\t\t通訊錄密碼保護系統 ");
printf("\n\n\t\t\t\t\t設計制作——\n\t\t\t\t\t\t ***");
printf(" \n\t\t\t\t\t\t Z07040138\n");
do
{ printf("\n\n\n\t\t請輸入用戶名:");
scanf("%s",s);
printf("\n\t\t請輸入密碼:");
for(k=0;k<6;k++)
{
s1[k]=getch();
if(s1[k]=='x0d')break;;
printf("*");
}
s1[k]='\0';
if((strcmp(s,password)==0)&&(strcmp(s1,mima)==0))
{printf("\n\n\n\t\t\t密 碼 驗 證 通 過\n\n\t\t\t按 任 意 鍵 進 入");getch(); break;}
else
{
printf("\n\t\t\t密碼錯誤!請從新輸入!\n");
n--;
m++;
}
}
while((n>0)&&(flag<4));
if(m==3)
{ printf("輸入用戶名超過了三次!!");
exit(0);
}
system("cls");
system("color a0");
int choicemanage;
do
{
printf("\t\t\t歡迎使用通訊錄系統\n");
printf("\t\t\t\t *******\n");
printf("\t\t\t\t ***\n");
printf("====================================================================\n");
printf("\t\t輸入總數\n");
printf("\t\t1 輸入學生信息\n");
printf("\t\t2 顯示學生信息\n");
printf("\t\t3 查找學生信息.\n");
printf("\t\t4 增加一個信息.\n");
printf("\t\t5 刪除一個學生信息.\n");
printf("\t\t0 退出.\n");
printf("====================================================================\n");
scanf("%d",&choicemanage);
switch(choicemanage)
{
case 1:
input();
display();
break;
case 2:
display();
break;
case 3:
research();
display();
break;
case 4:
add();
display();
break;
case 5:
del();
display();
break;
case 0:
display();
printf("\n\n\t\t\t 感謝使用通訊錄系統!\n");
printf("\n\n\t\t\t 祝您天天好心情!\n");
fp=fopen("addrbook.txt","w");
fwrite(&st[1],sizeof(struct student),flag,fp);
exit(1);
default:
{printf("請輸入一個正確的數據.\n");
printf("按任意鍵返回");
getch();
system("cls");
}
}
}while(1);
}
//**********************************************************************************************
void save()
{
FILE *fp1;
if((fp1=fopen("ad.dat","w"))==NULL)
return;
fwrite(&flag,sizeof(int),1,fp1);
rewind(fp1);
fclose(fp1);
}
//**********************************************************************************************
void load()
{ FILE *fp1;
if((fp1=fopen("ad.dat","r"))==NULL)
return;
fread(&flag,sizeof(int),1,fp1);
rewind(fp1);
flag=getw(fp1);
fclose(fp1);
}
//**********************************************************************************************
void input()
{ system("color 9b");
FILE *fp;
int i;
int b;
printf("輸入你想輸入的學生數:");
scanf("%d",&b);
if(b>100)
{
printf("太大了!受不了了!\n");
printf("按任意鍵返回");
getch();
system("cls");
return;
}
flag=0;
load();
i=flag;
b=b+flag;
if((fp=fopen("addrbook.txt","ab"))==NULL)
return;
while(i<b)
{
i++;
printf("第 %d 學生\n",i);
printf("\n");
printf("學號:");
scanf("%s",&st[i].num);
printf("姓名:");
scanf("%s",&st[i].name);
printf("電話:");
scanf("%s",&st[i].phone);
printf("郵編:");
scanf("%s",&st[i].youbian);
printf("地址:");
scanf("%s",&st[i].address);
printf("\n");
};
rewind(fp);
flag=b;
save();
fclose(fp);
system("cls");
return;
}
//**********************************************************************************************
void display()
{ system("color d0");
system("cls");
FILE *fp;
int i;
if((fp=fopen("addrbook.txt","r"))==NULL)
return;
printf("---------------------------通訊錄------------------------------\n");
printf("學號\t\t姓名\t\t電話\t\t郵編\t\t地址\n");
load();
fread(&st[1],sizeof(struct student),flag,fp);
for(i=1;i<flag+1;i++)
{printf("%-17s%-17s%-17s%-17s%-17s\n",st[i].num,st[i].name,st[i].phone,st[i].youbian,st[i].address);}
printf("按任意鍵返回\n");
getch();
fclose(fp);
system("cls");
return;
}
//**********************************************************************************************
void research()
{ system("color a3");
FILE *fp;
int i;
char chname[20];
printf("請輸入你想要查找的名字:\n");
scanf("%s",&chname);
if((fp=fopen("addrbook.txt","r"))==NULL)
{
printf("不能打開文件\n");
printf("按任意鍵返回\n");
getch();
system("cls");
return;
}
load();
for(i=1;i<flag+1;i++)
{if (strcmp(chname,st[i].name)==0)
{ system("cls");
printf("你所查詢的學生信息 \n\n\n");
printf("學號: %s\n\n姓名: %s\n\n電話: %s\n\n郵編: %s\n\n地址: %s\n\n",st[i].num,st[i].name,st[i].phone,st[i].youbian,st[i].address);
break;
}
if (strcmp(chname,st[i].name)!=0)
system("cls");
printf("查找不到,請添加!\n");
}
fclose(fp);
printf("按任意鍵返回\n");
getch();
system("cls");
return;
}
//**********************************************************************************************
void add()
{ system("color b4");
FILE *fp;
if((fp=fopen("addrbook.txt","r"))==NULL)
{
printf("不能打開\n");
getch();
return;
}
load();
flag++;
printf("請輸入一個新的學生信息:\n");
printf("請輸入學號:");
scanf("%s",&st[flag].num);
printf("請輸入名字:");
scanf("%s",&st[flag].name);
printf("請輸入電話:");
scanf("%s",&st[flag].phone);
printf("請輸入郵編");
scanf("%s",&st[flag].youbian);
printf("請輸入地址");
scanf("%s",&st[flag].address);
fwrite(&st[flag],sizeof(struct student),1,fp);
rewind(fp);
save();
printf("輸入完成\n按任意鍵返回\n");
getch();
fclose(fp);
system("cls");
return;
}
//**********************************************************************************************
void del()
{ system("color 80");
int i;
char delname[20];
FILE *fp;
if((fp=fopen("addrbook.txt","w"))==NULL)
{
printf("不能打開\n");
printf("按任意鍵返回\n");
getch();
system("cls");
return;
}
load();
printf("\n\t\t敬告:\t請謹慎操作,防止誤刪!\n");
printf("請輸入你想刪除的學生姓名:");
scanf("%s",delname);
for(i=1;i<flag+1;i++)
{
if (strcmp(delname,st[i].name)==0)
{
for(;i<flag+1;i++)
{
strcpy(st[i].num,st[i+1].num);
strcpy(st[i].name,st[i+1].name);
strcpy(st[i].phone,st[i+1].phone);
strcpy(st[i].youbian,st[i+1].youbian);
strcpy(st[i].address,st[i+1].address);
system("cls");
printf("刪除成功\n");
}continue;
}
else
system("cls");
printf("您要刪除的名字不存在\n");
}
fwrite(&st[1],sizeof(struct student),flag-1,fp);
save();
rewind(fp);
fclose(fp);
printf("按任意鍵返回");
getch();
system("cls");
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -