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

📄 cpp2.cpp

📁 小型商务通程序
💻 CPP
字号:
//小型商务通程序,业余练习使用
//程序开始
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct TeleList
{
	char name[20];
	char title[80];
	char add[200];
	char num[50];
	struct TeleList *NextNode;
};//end struct

void AppendNewNode(void);
void ListAll(void);
void SearchItem(void);
void DeleteItem(void);
int WriteItem(void);
int ReadItem(void);
int myfgets(FILE *, char *);
struct TeleList *head, *ThisNode, *New;
//将要使用的函数声明结束

void main()
{
	char ch;
	int flag = 1;
	//ReadItem();
	while(flag)
	{
		printf("\n************************");
		printf("\n*          main menu,              *");
		printf("\n1.type 'E' or 'e' to append new node,      *");
		printf("\n2.type 'L' or 'l' to list all,      *");
		printf("\n3.type 'S' or 's' to search a item,      *");
		printf("\n4.type 'D' or 'd' to delete a item,      *");
		printf("\n5.type 'W' or 'w' to Write to disk,      *");
		printf("\n6.type 'R' or 'r' to Read from disk,      *");
		printf("\n7.type any other key to exit      *");
		printf("\n***************************\n");

		ch = getchar();
		//getchar();//?????why
		switch(ch)
		{
			case 'e':
			case 'E':
				AppendNewNode();
				break;
			case 'l':
			case 'L':
				ListAll();
				break;
			case 's':
			case 'S':
				SearchItem();
				break;
			case 'd':
			case 'D':
				DeleteItem();
				break;
			case 'w':
			case 'W':
				WriteItem();
				break;
			case 'r':
			case 'R':
				ReadItem();
			    break;
			default : printf("\n exit");flag = 0;
		}//end swith
	}//end while

	int m;
	m = 1;
	//cout<<m<<endl;
	printf("%d\n", m);
}//end main

/////////////////////////////////////////////////////////////////
void AppendNewNode(void)
{
//	char *tmp;
	New = (struct TeleList*)malloc(sizeof(struct TeleList));
	if(head == NULL)
		head = New;
	else 
	{
		ThisNode = head;
		while(ThisNode->NextNode != NULL)
		{
			ThisNode = ThisNode->NextNode;
		}
		ThisNode->NextNode = New;
	}//end if

	ThisNode = New;
	printf("\n enter name: ");
	gets(ThisNode->name);
	printf("\n enter title: ");
	gets(ThisNode->title);	
	printf("\n enter address: ");
	gets(ThisNode->add);	
	printf("\n enter num: ");
	gets(ThisNode->num);
	
	ThisNode->NextNode = NULL;
}//end function

//////////////////////////////////////////////////////////

void ListAll(void)
{
	int i = 0;
	if(head == NULL)
	{
		printf("empty list.");
		return;
	}//end if block
	ThisNode = head;
	do
	{
		printf("\n record number: %d\n",++i);
		printf("name: %s\n",ThisNode->name);
		printf("title:%s\n",ThisNode->title);
		printf("address: %s\n",ThisNode->add);
		printf("number: %s\n",ThisNode->num);
		ThisNode = ThisNode->NextNode;
	}while(ThisNode != NULL);
}//end ListAll

//////////////////////////////////////////////////////////////////
void SearchItem(void)
{
	char TmpName[20];
	int i = 0;
	if(head == NULL)
	{
		printf("empty list.");
		return;
	}//end if block

	printf("enter seatch name:");
	gets(TmpName);
	ThisNode = head;
	do
	{
		printf("\n record number:%d\n",++i);
		if(strcmp(ThisNode->name, TmpName) == 0)
		{
			printf("\n find it!");
			printf("name:%s\n",ThisNode->name);
			printf("title:%s\n",ThisNode->title);
		    printf("address: %s\n",ThisNode->add);
		    printf("number: %s\n",ThisNode->num);
			return;
		}//end if block
		ThisNode = ThisNode->NextNode;

	}while(ThisNode != NULL); //end do-while loop
	printf("no this item");
}//end of search

void DeleteItem(void)
{
	char TmpName[20];
	struct TeleList *PreNode = head;
	int i = 0;

	if(head == NULL)
	{
		printf("empty list.");
		return;
	}//end if block

	printf("enter delete name:");
	gets(TmpName);
	ThisNode = head;
	
	do
	{
		printf("\n record number:%d\n",++i);
		if(strcmp(ThisNode->name, TmpName) == 0)
		{
			printf("find it.\n");
			if(ThisNode == head)
				head = ThisNode->NextNode;
			else if(ThisNode->NextNode = NULL)
				PreNode->NextNode = NULL;
			else
				PreNode->NextNode = ThisNode->NextNode;
			printf("name:%s\n",ThisNode->name);
			return;
		}//end if block
		PreNode = ThisNode;
		ThisNode = ThisNode->NextNode;
	}while(ThisNode != NULL);
	printf("\n no this item.");

}//end of fuc

///////////////////////////////////////////////////////
int WriteItem(void)
{
	FILE *fp;
	int i = 0;
	if(head == NULL)
	{
		printf("\nempty list. ");
		return -1;
	}

	if((fp = fopen("E:\telelist.dat","w")) == NULL)
	{
		printf("open telelist.dat error.\n");
		return -1;
	}
	ThisNode = head;
	do{
		printf("\n record number:%d\n", ++i);
		fputs(ThisNode->name, fp);	
        fputs("\n", fp); 
		fputs(ThisNode->title, fp);	
        fputs("\n", fp); 
		fputs(ThisNode->add, fp);	
        fputs("\n", fp); 
		fputs(ThisNode->num, fp);	
        fputs("\n", fp); 
		ThisNode = ThisNode->NextNode;
	}while(ThisNode != NULL);
	fclose(fp);
	return 1;
} //end func

////////////////////////////////////////////////////////

int ReadItem(void)
{
	FILE *fp;
	char tmpname[20], tmptitle[80], tmpadd[200], tmpnum[50];
	head = NULL;
	if((fp = fopen("telelist.dat","r")) == NULL)
	{
		printf("can not open telelist.dat\n");
		return -1;
	}
	while(1)
	{
	if(myfgets(fp,tmpname) == 0)
		break;
	New  = (struct TeleList*)malloc(sizeof(struct TeleList));

	if(head == NULL)
		head = New;
	else
	{
		ThisNode = head;
		while(ThisNode->NextNode != NULL)
		{
			ThisNode = ThisNode->NextNode;
		}
		ThisNode->NextNode = New;
	}
	ThisNode = New;

	strcpy(ThisNode->name,tmpname);

	if(myfgets(fp,tmptitle) == 0)
		break;

	strcpy(ThisNode->title,tmptitle);

	if(myfgets(fp,tmpadd) == 0)
		break;

	strcpy(ThisNode->add,tmpadd);

	if(myfgets(fp,tmpnum) == 0)
		break;

	strcpy(ThisNode->num,tmpnum);

	ThisNode->NextNode = NULL;
	}
	fclose(fp);
	return 0;

}

////////////////////////////////////////////////////////


int myfgets(FILE *fp, char *str)
{
	int i = 0;
	char ch;
	while(1)
	{
		ch = fgetc(fp);
		if(ch == EOF)
			return 0;
		if(ch == '\n')
			break;
		str[i] = ch;
		i++;
	}
	str[i] = '\0';
	return 1;
}


/////////////////////////////////////////////////////////////


⌨️ 快捷键说明

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