“百年党史”——示例代码(赵老师上课版)

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#define N 20
#define M 50
struct History{
	char date[50];
	int year;
	char address[50];
	char item[50];
	char introduction[500];
}h2;
int countHistory=0;
struct History h1[N];
struct{
	char username[50];
	char password[50];
}newUser,registerUser,user[M];
int count=0;
void readAllUser(){
	FILE *fp;
	if((fp=fopen("User.txt","r"))==NULL){
		printf("此文件无法打开!\n");
		exit(0);
	}
	while(!feof(fp)){
		fscanf(fp,"%s",user[count].username);
		fscanf(fp,"%s",user[count].password);
		count++;
	}
	fclose(fp);	
}					
void myRegister(){
	while(1){
	printf("*************************************************\n");
	printf("*                                               *\n");
	printf("*                     用户注册                  *\n");
	printf("*                                               *\n"); 
	printf("*************************************************\n"); 
	printf("请输入账号:\n");
	gets(registerUser.username);
	printf("请输入密码:\n");
  	gets(registerUser.password);
	int i;
	for(i=0;i<count;i++){
		  	if(strcmp(user[i].username,registerUser.username)==0){
   				printf("该用户已存在,请重新输入\n");
   				break;
			}
		}
 	if(i==count){
		 user[count]= registerUser;
		FILE *fp;
		if((fp=fopen("User.txt","a"))==NULL){
			printf("此文件无法打开!\n");
			exit(0);
		}
		fprintf(fp,"\n%s",registerUser.username);
		fprintf(fp,"\n%s",registerUser.password);
		count++;
		fclose(fp);
		break;
	}
		system("pause");
		system("cls");  
	}
}
void login(){
	int num=0;
	while(1){
		printf("*************************************************\n");
	    printf("*                                               *\n");
   	    printf("*                     用户登录                  *\n");
        printf("*                                               *\n"); 
	    printf("*************************************************\n"); 
		printf("请输入账号:\n");
 		gets(newUser.username);
		printf("请输入密码:\n");
  		gets(newUser.password);
  		int i;
  		for(i=0;i<count;i++){
		  	if(!strcmp(user[i].username,newUser.username)&&!strcmp(user[i].password,newUser.password)){
   				break;
			}
		}
 		if(i<count){
   			printf("欢迎使用本程序!\n");
   			break;
 		}else{
 			if(num<=3){
 				num++;
 				printf("您输入的密码不正确,请重新输入!\n"); 
 			}else{
 				printf("您输入的次数已达上限,将退出系统!\n");
 				exit(0);
 			}
 		}
		system("pause");
		system("cls");  
	}
}
void readAllHistory(char filename[50]){
	FILE *fp;
	countHistory=0;
	if((fp=fopen(filename,"r"))==NULL){
		printf("此文件无法打开!\n");
		exit(0);
	}
	while(!feof(fp)){
		fscanf(fp,"%s",h1[countHistory].date);
		h1[countHistory].year=((h1[countHistory].date[6]-'0')*1000+(h1[countHistory].date[7]-'0')*100+(h1[countHistory].date[8]-'0')*10+(h1[countHistory].date[9]-'0'));
		fscanf(fp,"%s",h1[countHistory].address);
		fscanf(fp,"%s",h1[countHistory].item);
		fscanf(fp,"%s",h1[countHistory].introduction);
		countHistory++;
	}
	fclose(fp);
} 
void addOneHistoryToFile(char filename[50],struct History newHistory){
	FILE *fp;
	if((fp=fopen(filename,"a"))==NULL){
			printf("此文件无法打开!\n");
			exit(0);
		}
		h1[countHistory]=newHistory;
		fprintf(fp,"\n%s",newHistory.date);
		fprintf(fp,"\n%s",newHistory.address);
		fprintf(fp,"\n%s",newHistory.item);
		fprintf(fp,"\n%s",newHistory.introduction);
		countHistory++;
		fclose(fp);	
}
struct History readOneHistory(char filenameOne[50]){
	FILE *fp;
	struct History newHistory;
	if((fp=fopen(filenameOne,"r"))==NULL){
			printf("此文件无法打开!\n");
			exit(0);
		}
		fscanf(fp,"%s",newHistory.date);
		newHistory.year=((newHistory.date[6]-'0')*1000+(newHistory.date[7]-'0')*100+(newHistory.date[8]-'0')*10+(newHistory.date[9]-'0'));
		fscanf(fp,"%s",newHistory.address);
		fscanf(fp,"%s",newHistory.item);
		fscanf(fp,"%s",newHistory.introduction);
		h1[countHistory]=newHistory;
		fclose(fp);
		return	newHistory;
}
void writeAllHistoryToFile(char filename[50]){
	FILE *fp;
	if((fp=fopen(filename,"w"))==NULL){
			printf("此文件无法打开!\n");
			exit(0);
		}
		for(int i=0;i<countHistory;i++){ 
			fprintf(fp,"%s\n",h1[i].date);
			fprintf(fp,"%s\n",h1[i].address);
			fprintf(fp,"%s\n",h1[i].item);
			if(i==countHistory-1){
				fprintf(fp,"%s",h1[i].introduction);
			}else{
				fprintf(fp,"%s\n",h1[i].introduction);
			}
		}
		fclose(fp);
}
void insertOneHistryToFile(char filename[50],char filenameOne[50]){
	struct History newHistory=readOneHistory(filenameOne);
	int i;
	for(i=0;i<countHistory;i++){
		if(h1[i].year>newHistory.year){
			break;
		}
	}
	if(i<countHistory){
		for(int j=countHistory;j>=i;j--){
			h1[j+1]=h1[j];
		}
	}
	h1[i]=newHistory;
	countHistory++;
	writeAllHistoryToFile(filename); 
}
void delOneHistryToFile(char filename[50],char item[50]){
	int i;
	for(i=0;i<countHistory;i++){
		if(strcmp(h1[i].item+6,item)==0){
			break;
		}
	}
	if(i<countHistory){
		for(int j=i+1;j<countHistory;j++){
			h1[j-1]=h1[j];
		}
	}else{
		printf("您要删除的党史信息不存在\n");
	} 
	countHistory--;
	writeAllHistoryToFile(filename); 
}
void displayAllHistory(){
	printf("==========================================================\n");
	for(int i=0;i<countHistory;i++){
	printf("%s\n",h1[i].date);
	printf("%s\n",h1[i].address);
	printf("%s\n",h1[i].item);
	printf("%s\n",h1[i].introduction);
	printf("==========================================================\n");
	}
} 
int main(int argc, char *argv[])
{
	system("color f6"); 
	//readAllUser();
	//myRegister();
	//login();
	readAllHistory("History - 第4讲.txt");
	insertOneHistryToFile("History - 第4讲.txt","建党百年.txt"); 
	delOneHistryToFile("History - 第4讲.txt","建党百年"); 
	displayAllHistory();
	return 0;
}

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值