⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5_1.cpp

📁 对工人的工资进行管理
💻 CPP
字号:
 
/*第1题	工资管理--源代码及关键源代码注解如下:*/
/* This program shows how to use a structure variable to represent a record in a hard disk file.This program will help you learn how to add,delete & edit records stored in a file using a FILE pointer. The source is well commented.
I will appreciate your response at vijaympan@yahoo.com Please report any bugs you find.*/

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h> 
/* structure that represents an employee record */
struct EMP {
char name[25]; 
char x[12];
float salary;  
int deleted;   
};
void main() {
	long curpos, length;// Variables for finding file size
			
	EMP e; //Employee object for data storing & manipulation
	strcpy(e.name,"NULL"); // Default values are
	e.salary = 0;		   // assigned	
	e.deleted = 0;
	while(1) {
		char c;            // User command is recieved in this variable
		        printf("\n\t\t\t  ********************\n");
                printf("\t\t\t  ----工资管理系统----\n");
				printf("\t\t\t  ********************\n");
				printf("\n1 - 添加工资记录\n");
				printf("\n2 - 显示工资记录\n");
				printf("\n3 - 根据姓名查询工资数据\n");
				printf("\n4 - 根据姓名删除工资数据\n");
				printf("\n5 - 根据姓名修改工资数据\n");
				printf("\n0 - 退出系统\n");
		        printf("\n请输入(1,2,3,4,5,6,0)进行选择:");
		c = getch();        // Getting user input. Type 'h' for possible values
		if(c == '1') {   //New data is recieved int e
			system("cls");
			printf("\t\t\t\t1.添加工资记录\n"); 
			printf("\n请输入姓名: ");		// ( but it is not saved into a file)
			scanf("%s",e.name);
			printf("请输入工资值: ");
			scanf("%f",&e.salary);
			e.deleted = 0;
            
			   FILE* file;
			   if((file = fopen("EMP.DAT","a"))==NULL) 
			   {
		       printf("\n数据保存失败");
			  continue;
			   }
			  fwrite((char*)&e,sizeof(e),1,file);
			   fclose(file);
		 	  printf("\n此人数据已经成功保存");
			
			  printf("\n按M键回到主菜单");
		      scanf("%s",e.x);
						  
			/* If new data was entered a file ,EMP.dat,
			is opened in append mode & employee record is
			written to it */ 
			
	}   

		
		else if(c == '2') {   // If user typed 'l' records in EMP.DAT is read int e in a loop
								//  & displayed to the user
			system("cls");
			FILE* file;
            	printf("\t\t\t\t2.已存档工资记录\n"); 
			if((file = fopen("EMP.DAT","r")) == NULL) {
			printf("\n没有可导入的数据文件,请先建立一个新的工资数据管理文件");
			continue;
			}
			 
			curpos = ftell(file); // Current position of filepointer is stored in curpos
			
			fseek(file, 0L, SEEK_END);// File pointer is moved 0 bytes from file end 
									  // ie,file pointer is at the file's last byte
			
			length = ftell(file);     // Now current position is file's length
			
			fseek(file, curpos, SEEK_SET); // File pointer is restored to file's begining
			
			long num_recs = length / sizeof(e);// Length of each record in the file
			
			for(long l = 0;l<num_recs;l++) { // Recursivley retrieves & prints each record in file
				fread((char*)&e,sizeof(e),1,file);
				printf("\n姓名: %s",e.name);
				printf("\n工资: %.2f",e.salary);
			}
			fclose(file);
			printf("\n\n\t所有记录条目总和: %d",num_recs);					
			
		}
		else if(c == '0') { // Exists while loop & program
			break;
		
		}else if(c == '3') 
		{
		{ // Searches the database for an employee based on employee name
								// entered by user
			system("cls");
				printf("\t\t\t3.根据姓名查询工资数据\n"); 
			FILE* file;
			if((file = fopen("EMP.DAT","r")) == NULL) { //Checks to see if file is accesible
				printf("没有已记录的工资数据文件,请先建立");
				continue;
			}
			
			//Gets employee name to perform search
			printf("请输入姓名: ");
			char f[25];
			scanf("%s",f);
			
			//Finds file size
			long curpos, length;
			curpos = ftell(file);
			fseek(file, 0L, SEEK_END);
			length = ftell(file);
			fseek(file, curpos, SEEK_SET);
			long num_recs = length / sizeof(e);
			
			int found = 0; // To check if record was found or not
			
			for(long l = 0;l<num_recs;l++) {
 // Recursivly checks each name if found prints details
											// & sets found to 1		
				fread((char*)&e,sizeof(e),1,file);
		 		if((strcmp(e.name,f))==0) {
					printf("\n已找到,记录如下:");
				printf("\n姓名: %s",e.name);
				printf("\n工资: %.2f",e.salary);
				found = 1;
				}
			}
			if(found == 0)
				printf("此人记录不存在。");
			}
		
		}else if(c == '4') {// Deletes an employee 
			
			FILE* file;
			FILE* temp_file;
            system("cls");
				printf("\t\t\t\t4.根据姓名删除工资数据\n");
			if((file = fopen("EMP.DAT","r")) == NULL) {
				printf("没有已记录的工资数据文件,请先建立");
				continue;
			}
			
			if((temp_file = fopen("EMP_TEMP.DAT","w")) == NULL) {
				printf("Failed to create temp file");
				continue;
			}
			printf("请输入姓名: ");
			char f[25];
			scanf("%s",f);
			long curpos, length;
			curpos = ftell(file);
			fseek(file, 0L, SEEK_END);
			length = ftell(file);
			fseek(file, curpos, SEEK_SET);
			long num_recs = length / sizeof(e);
			int found = 0;
			for(long l = 0;l<num_recs;l++) {
				fread((char*)&e,sizeof(e),1,file);
				if((strcmp(e.name,f))==0) {
				e.deleted = 1; // If record found sets it deleted flag to true
				found = 1;
				}
				fwrite((char*)&e,sizeof(e),1,temp_file); 
// Write all records read to a temporory file
			}
			fclose(file);
			fclose(temp_file);
			if(found == 0) {
				printf("此人记录不存在,请重新操作");
			unlink("EMP_TEMP.DAT");//If record was not found deletes temp file
									//& continues main command loop
			continue;
			}
			//If record was found rewrites all records from temp file to EMP.DAT
			//where deleted flag is 0
			//& deletes the tem file in the end
			file = fopen("EMP.DAT","w");
			temp_file = fopen("EMP_TEMP.DAT","r");
			curpos = ftell(temp_file);
			fseek(temp_file, 0L, SEEK_END);
			length = ftell(temp_file);
			fseek(temp_file, curpos, SEEK_SET);
			num_recs = length / sizeof(e);
			for(l = 0;l<num_recs;l++) {
			fread((char*)&e,sizeof(e),1,temp_file);
			if(e.deleted == 0) {
			fwrite((char*)&e,sizeof(e),1,file);
			}
		}
			fclose(file);
			fclose(temp_file);
			printf("此人记录成功删除");
			unlink("EMP_TEMP.DAT");
	
		}
		 else if(c == '5') { //Edits a record
			FILE* file;
				system("cls");
			printf("\t\t\t5.根据姓名修改工资数据\n"); 
			if((file = fopen("EMP.DAT","r")) == NULL) {
				printf("没有已保存的工资数据文件,请先建立");
				continue;
			}
            printf("请输入姓名: ");
			char f[25];
			scanf("%s",f);
			long curpos, length;
			curpos = ftell(file);
			fseek(file, 0L, SEEK_END);
			length = ftell(file);
			fseek(file, curpos, SEEK_SET);
			long num_recs = length / sizeof(e);
			int found = 0;
			//Variables to store new name & salary
			char new_name[25];
			float new_sal;
			EMP new_emp;
			long write_at;//Position of record in to edit
			for(long l = 0;l<num_recs;l++) {
				fread((char*)&e,sizeof(e),1,file);
			   	if((strcmp(e.name,f))==0) {//If record exists in file get new values
					printf("输入新名字: ");
					scanf("%s",new_name);
					printf("输入新工资数: ");
					scanf("%f",&new_sal);
					//Copies values to a new EMP object
					strcpy(new_emp.name,new_name);
					new_emp.salary = new_sal;
					new_emp.deleted = 0;
					found = 1;
					write_at = l; // Position to write new record
					break;
					}
				}
			fclose(file);
			if(found == 1) {
			//Calculates byte posiotn to write
				write_at = write_at * sizeof(new_emp);
				//Mode r+b opens a file to be writable & readable in binary
				if((file = fopen("EMP.DAT","r+b")) == NULL) {
					printf("Editing failed");
					continue;
				}
				fseek(file,write_at,SEEK_SET);
				fwrite((char*)&new_emp,sizeof(new_emp),1,file);
				fclose(file);
				printf("记录修改成功");
			}else {
				printf("没有找到此人的记录");
			}

		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -