#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
static char *initialization_pas;
static char *password_you_entered;
static char *password_you_entering;
struct info
{
char name[15];
char phone[12];
char sex[2];
char age[4];
char address[20];
};
void Initial();
void InitializationPassword();
void EnterPassword();
void Panel();
void FormatOutputInfo(struct info a);
void New();
void Modify();
void Delet();
void Display();
void Search();
void ModifyPassword();
void AdministratorInterface();
void NormalUserInterface();
void SelectOption(int *a);
void ExecutiveFunction1(int *a);
void ExecutiveFunction2(int *a);
void InterfaceOfAdministratorPrivilege();
void InterfaceOfNormalUserPrivilege();
void Initial()
{
FILE *fp;
if((fp=fopen("password.txt","wb")) == NULL)
{
puts("errow opening file!!!");
getch();
exit(1);
}
printf("please enter the initialization password:");
gets(initialization_pas);
fwrite(initialization_pas,1,10,fp);
printf("initialization_pas is %s\n", initialization_pas);上海整形醫院http://zx.daynews.com.cn/
fclose(fp);
}
void InitializationPassword()
{
long len;
FILE *fp;
if((fp=fopen("password.txt","ab"))==NULL)
{
puts("errow opening file!!!");
getch();
exit(1);
}
else
{
fseek(fp,0,2);
len=ftell(fp);
if(len==0)
{
puts("password.txt is empty!!!");
fclose(fp);
Initial();
}
else
{
fclose(fp);
fp=fopen("password.txt","rb");
fread(password_you_entered,1, 10, fp);
fclose(fp);
}
}
}
void EnterPassword()
{
printf("the password had exist, please enter it correct:");
gets(password_you_entering);
printf("you entered is %s\n", password_you_entering);
getch();
}
void Panel()
{
printf("%-15s %-12s %-4s %-4s %-21s","name","phone","sex","age","address");
}
void FormatOutputInfo(struct info a)
{
printf("%-15s %-12s %-4s %-4s %-21s\n", a.name, a.phone, a.sex, a.age, a.address);
}
void New()
{
char b;
struct info a;
FILE *fp;
if((fp=fopen("information.txt","ab")) == NULL)
{
puts("errow open file!");
getch();
exit(1);
}
do
{
system("CLS");
puts("input information:");
printf("\tname:");
gets(a.name);
printf("\tphone:");
gets(a.phone);
printf("\tsex:");
gets(a.sex);
printf("\tage:");
gets(a.age);
printf("\taddress:");
gets(a.address);
fwrite(&a,sizeof(info),1,fp);
printf("是否繼續?(Y):[ ]\b\b"); /*這裡用空格鍵有個BUG */
b=getchar();getchar();
}while(b=='Y' || b=='y');
fclose(fp);
}
void Modify()
{
struct info a;
char b[20];
FILE *fp1, *fp2;
if((fp1=fopen("information.txt","rb")) == NULL)
{
puts("errow open file!");
getch();
exit(1);
}
if((fp2=fopen("temp.txt","wb")) == NULL)
{
puts("errow open file!");
getch();
exit(1);
}
printf("please enter the name you want to modify:");
gets(b);
while((fread(&a,sizeof(info),1,fp1)) == 1)/*這裡的sizeof()內容是否可以換為結構實例 */
{
if(strcmp(b,a.name) == 0) /*要修改的內容 */
{
printf("\nplease enter the name you want to modify:");
gets(a.name);
printf("\nplease enter the phone you want to modify:");
gets(a.phone);
printf("\nplease enter the sex you want to modify:");
gets(a.sex);
printf("\nplease enter the age you want to modify:");
gets(a.age);
printf("\nplease enter the address you want to modify:");
gets(a.address);
fwrite(&a,sizeof(a),1,fp2);
}
else
fwrite(&a,sizeof(info),1,fp2);
}
fcloseall();
getch();
remove("information.txt");
rename("temp.txt","information.txt");
}
void Delet()
{
char f[20];
char d[20];
struct info a;
FILE *fp1;
FILE *fp2;
printf("please enter the name you want to delet:");
scanf("%s",d);
if((fp1=fopen("information.txt","rb")) == NULL)
{
puts("errow open file!");
getch();
exit(1);
}
if((fp2=fopen("temp.txt","wb")) == NULL)
{
puts("errow open file!");
getch();
exit(1);
}
while((fread(&a,sizeof(info),1,fp1)) == 1)/*這裡的sizeof()內容是否可以換為結構實例 */
{
if(strcmp(d,a.name) != 0)
{
fwrite(&a,sizeof(info),1,fp2);
}
}
fcloseall();
getch();
remove("information.txt");
rename("temp.txt","information.txt");
}
void Display()
{
struct info a;
FILE *fp;
if((fp=fopen("information.txt","rb")) == NULL)
{
puts("errow opening file!!!");
getch();
exit(1);
}
Panel();
puts("");
while((fread(&a,sizeof(info),1,fp)) == 1)
FormatOutputInfo(a);
getch();
fclose(fp);
}
void Search()
{
int c;
char b[10];
struct info a;
FILE *fp;
printf("please enter search mode [1]name [2]phone [3]sex [4]age [5]address:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("please enter the name you want to search:");
scanf("%s",b);
if((fp=fopen("information.txt","rb")) == NULL)
{
puts("errow opening file!!!");
getch();
exit(1);
}
Panel();
puts("");
while((fread(&a,sizeof(a),1,fp)) == 1)
{
if((strcmp(a.name,b))==0)
{
FormatOutputInfo(a);
}
}
getch();
fclose(fp);
break;
case 2:
printf("please enter the phone you want to Search:");
scanf("%s",b);
if((fp=fopen("information.txt","rb")) == NULL)
{
puts("errow opening file!!!");
getch();
exit(1);
}
Panel();
puts("");
while((fread(&a,sizeof(a),1,fp)) == 1)
{
if((strcmp(a.phone,b))==0)
{
FormatOutputInfo(a);
}
}
getch();
fclose(fp);
break;
case 3:
printf("please enter the sex you want to Search:");
scanf("%s",b);
if((fp=fopen("information.txt","rb")) == NULL)
{
puts("errow opening file!!!");
getch();
exit(1);
}
Panel();
puts("");
while((fread(&a,sizeof(a),1,fp)) == 1)
{
if((strcmp(a.sex,b))==0)
{
FormatOutputInfo(a);
}
}
getch();
fclose(fp);
break;
case 4:
printf("please enter the age you want to Search:");
scanf("%s",b);
if((fp=fopen("information.txt","rb")) == NULL)
{
puts("errow opening file!!!");
getch();
exit(1);
}
Panel();
puts("");
while((fread(&a,sizeof(a),1,fp)) == 1)
{
if((strcmp(a.age,b))==0)
{
FormatOutputInfo(a);
}
}
getch();
fclose(fp);
break;
case 5:
printf("please enter the address you want to Search:");
scanf("%s",b);
if((fp=fopen("information.txt","rb")) == NULL)
{
puts("errow opening file!!!");
getch();
exit(1);
}
Panel();
puts("");
while((fread(&a,sizeof(a),1,fp)) == 1)
{
if((strcmp(a.address,b))==0)
{
FormatOutputInfo(a);
}
}
getch();
fclose(fp);
break;
}
}
void ModifyPassword()
{
char a[10]={1,2,3,4,5,1,2,3,4};
printf("please enter the old password:");
gets(a);
if(strcmp(a,password_you_entering) == 0)
{
Initial();
system("CLS");
printf("modification is successful, the new password you modified is %s.\n", initialization_pas);
getch();
}
else
{
puts("the password you enterde is wrong, you will go back to menu.");
getch();
}
}
void AdministratorInterface()
{
system("CLS");
puts("welcome to the administrator privilege`s contacts interface!!!");
puts("\t\t\t[1]New");
puts("\t\t\t[2]Modify");
puts("\t\t\t[3]Delet");
puts("\t\t\t[4]Display");
puts("\t\t\t[5]Search");
puts("\t\t\t[6]ModifyPassword");
puts("\t\t\t[7]exit");
printf("please enter the number to select function what you want to executive:[ ]\b\b");
}
void SelectOption(int *a)
{
char b;
fflush(stdin);
b=getchar();
fflush(stdin);
*a=atoi(&b);
}
void ExecutiveFunction1(int *a)
{
switch(*a)
{
case 1:
New();
break;
case 2:
Modify();
break;
case 3:
Delet();
break;
case 4:
Display();
break;
case 5:
Search();
break;
case 6:
ModifyPassword();
break;
case 7:
exit(0);
break;
}
}
void NormalUserInterface()
{
system("CLS");
puts("welcome to the normal user privilege`s contacts interface!!!");
puts("\t\t\t[1]Display");
puts("\t\t\t[2]Search");
puts("\t\t\t[3]exit");
printf("please enter the number to select function what you want to executive:[ ]\b\b");
}
void ExecutiveFunction2(int *a)
{
switch(*a)
{
case 1:
Display();
break;
case 2:
Search();
break;
case 3:
exit(0);
break;
}
}
void InterfaceOfAdministratorPrivilege()
{
do
{
int a, *p;
p=&a;
AdministratorInterface();
SelectOption(p);
ExecutiveFunction1(p);
}while(1);
}
void InterfaceOfNormalUserPrivilege()
{
do
{
int a, *p;
p=&a;
NormalUserInterface();
SelectOption(p);
ExecutiveFunction2(p);
}while(1);
}
void main()
{
char a[10], b[10], c[10];
initialization_pas=a;
password_you_entered=b;
password_you_entering=c;
InitializationPassword();
EnterPassword();
if((strcmp(initialization_pas,password_you_entering)==0) || (strcmp(password_you_entered,password_you_entering)==0))
{
puts("the password is right!!!\nyou will enter the administrator privilege`s contacts!!!");
getch();
InterfaceOfAdministratorPrivilege();
}
else
{
puts("the password is wrong!!!\nyou will enter the normal user privilege`s contacts!!!");
getch();
InterfaceOfNormalUserPrivilege();
}
}
**********************************************************************************
#include <stdio.h>
static int year,month,days;
static int moon[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i;
/*全局變量和函數對位置有順序的要求*/
void EnterDate()
{
puts("yyyy.mm");
scanf("%d.%d",&year,&month);
}
void CheckDate()
{
if(year>=1900 && month>=1 && month<=12)
puts("The date is qualified");
else
{
puts("The date entered is not qualified,please input it again.");
EnterDate();
}
}
void EnterAndCheckDate()
{
EnterDate();
CheckDate();
}
void February()
{
if(year%4==0 && year%100!=0 || year%400==0)
moon[2]=29;
else
moon[2]=28;
}
void TotalDays()
{
days=1;
for(i=1900;i<year;i++)
{
if(i%4==0 && i%100!=0 || i%400==0)
days+=366;
else
days+=365;
}
for(i=1;i<month;i++)
days+=moon[i];
}
void Calendar ()
{
puts("SUM\tMON\tTUE\tWED\tTRE\tFRI\tSAT");
for(i=days%7;i>0;i--)
printf("\t");
}
void Showing()
{
for(i=1;i<=moon[month];i++)
{
printf("%d",i);
if(days%7==6)
puts("");
else
printf("\t");
days++;
}
puts("");
}
void DisplayTheCalendar()
{
February();
TotalDays();
Calendar ();
Showing();
}
void main()
{
EnterAndCheckDate();
DisplayTheCalendar();
}