如何将该程序转换为C ++? [英] How to convert this program into C++?

查看:89
本文介绍了如何将该程序转换为C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
struct person
{
	char name[35];
	char address[50];
	char father_name[35];
	char mother_name[30];
	long int mble_no;
	char sex[8];
	char mail[100];
	char citision_no[20];

};
void menu();
void got();
void start();
void back();
void addrecord();
void listrecord();
void modifyrecord();
void deleterecord();
void searchrecord();
int main()
{
	system("color 5f");
	start();
	return 0;
}
void back()
{
	start();
}
void start()
{
	menu();
}
void menu()
{
	system("cls");
	printf("\t\t**********WELCOME TO PHONEBOOK*************");

	printf("\n\n\t\t\t  MENU\t\t\n\n");
	printf("\t1.Add New   \t2.List   \t3.Exit  \n\t4.Modify \t5.Search\t6.Delete");

	switch(getch())
	{
	case '1':
		addrecord();
		break;
	case '2': listrecord();
		break;
	case '3': exit(0);
		break;
	case '4': modifyrecord();
		break;
	case '5': searchrecord();
		break;
	case '6': deleterecord();
		break;
	default:
		system("cls");
		printf("\nEnter 1 to 6 only");
		printf("\n Enter any key");
		getch();

		menu();
	}
}
void addrecord()
{
	system("cls");
	FILE *f;
	struct person p;
	f=fopen("project","ab+");
	printf("\n Enter name: ");
	got(p.name);
	printf("\nEnter the address: ");
	got(p.address);
	printf("\nEnter father name: ");
	got(p.father_name);
	printf("\nEnter mother name: ");
	got(p.mother_name);
	printf("\nEnter phone no.:");
	scanf("%ld",&p.mble_no);
	printf("Enter sex:");
	got(p.sex);
	printf("\nEnter e-mail:");
	got(p.mail);
	printf("\nEnter citizen no:");
	got(p.citision_no);
	fwrite(&p,sizeof(p),1,f);

	fflush(stdin);
	printf("\nrecord saved");

	fclose(f);

	printf("\n\nEnter any key");

	getch();
	system("cls");
	menu();
}
void listrecord()
{
	struct person p;
	FILE *f;
	f=fopen("project","rb");
	if(f==NULL)
	{
		printf("\nfile opening error in listing :");
		exit(1);
	}
	while(fread(&p,sizeof(p),1,f)==1)
	{
		printf("\n\n\n YOUR RECORD IS\n\n ");
		printf("\nName=%s\nAdress=%s\nFather name=%s\nMother name=%s\nMobile no=%ld\nSex=%s\nE-mail=%s\nCitizen no=%s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.citision_no);

		getch();
		system("cls");
	}
	fclose(f);
	printf("\n Enter any key");
	getch();
	system("cls");
	menu();
}
void searchrecord()
{
	struct person p;
	FILE *f;
	char name[100];

	f=fopen("project","rb");
	if(f==NULL)
	{
		printf("\n error in opening\a\a\a\a");
		exit(1);
	}
	printf("\nEnter name of person to search\n");
	got(name);
	while(fread(&p,sizeof(p),1,f)==1)
	{
		if(strcmp(p.name,name)==0)
		{
			printf("\n\tDetail Information About %s",name);
			printf("\nName:%s\naddress:%s\nFather name:%s\nMother name:%s\nMobile no:%ld\nsex:%s\nE-mail:%s\nCitision no:%s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.citision_no);
		}
		else
			printf("file not found");
	}
	fclose(f);
	printf("\n Enter any key");

	getch();
	system("cls");
	menu();
}
void deleterecord()
{
	struct person p;
	FILE *f,*ft;
	int flag;
	char name[100];
	f=fopen("project","rb");
	if(f==NULL)
	{
		printf("CONTACT'S DATA NOT ADDED YET.");
	}
	else
	{
		ft=fopen("temp","wb+");
		if(ft==NULL)
			printf("file opaning error");
		else
		{
			printf("Enter CONTACT'S NAME:");
			got(name);

			fflush(stdin);
			while(fread(&p,sizeof(p),1,f)==1)
			{
				if(strcmp(p.name,name)!=0)
					fwrite(&p,sizeof(p),1,ft);
				if(strcmp(p.name,name)==0)
					flag=1;
			}
			fclose(f);
			fclose(ft);
			if(flag!=1)
			{
				printf("NO CONACT'S RECORD TO DELETE.");
				remove("temp.txt");
			}
			else
			{
				remove("project");
				rename("temp.txt","project");
				printf("RECORD DELETED SUCCESSFULLY.");
			}
		}
	}
	printf("\n Enter any key");

	getch();
	system("cls");
	menu();
}

void modifyrecord()
{
	int c;
	FILE *f;
	int flag=0;
	struct person p,s;
	char  name[50];
	f=fopen("project","rb+");
	if(f==NULL)
	{
		printf("CONTACT'S DATA NOT ADDED YET.");
		exit(1);
	}
	else
	{
		system("cls");
		printf("\nEnter CONTACT'S NAME TO MODIFY:\n");
		got(name);
		while(fread(&p,sizeof(p),1,f)==1)
		{
			if(strcmp(name,p.name)==0)
			{
				printf("\n Enter name:");
				got(s.name);
				printf("\nEnter the address:");
				got(s.address);
				printf("\nEnter father name:");
				got(s.father_name);
				printf("\nEnter mother name:");
				got(s.mother_name);
				printf("\nEnter phone no:");
				scanf("%ld",&s.mble_no);
				printf("\nEnter sex:");
				got(s.sex);
				printf("\nEnter e-mail:");
				got(s.mail);
				printf("\nEnter citizen no\n");
				got(s.citision_no);
				fseek(f,-sizeof(p),SEEK_CUR);
				fwrite(&s,sizeof(p),1,f);
				flag=1;
				break;
			}
			fflush(stdin);
		}
		if(flag==1)
		{
			printf("\n your data id modified");
		}
		else
		{
			printf(" \n data is not found");
		}
		fclose(f);
	}
	printf("\n Enter any key");
	getch();
	system("cls");
	menu();

}
void got(char *name)
{
	int i=0,j;
	char c,ch;
	do
	{
		c=getch();
		if(c!=8&&c!=13)
		{
			*(name+i)=c;
			putch(c);
			i++;
		}
		if(c==8)
		{
			if(i>0)
			{
				i--;
			}
			// printf("h");
			system("cls");
			for(j=0;j<i;j++)
			{
				ch=*(name+j);
				putch(ch);
			}
		}
	}while(c!=13);
	*(name+i)='\0';
}





我的尝试:



[edit]删除上述代码(未格式化)的副本[/ edit]



What I have tried:

[edit]Duplicate of above code (unformatted) removed[/edit]

推荐答案

不要。

它已经在C中 - 这是C ++的一个子集 - 所以从技术上讲它不需要任何转换:它应该按原样运行。



但是转换C程序到C ++并不是一个好主意 - 它们并不是设计成好的C ++,因为它们根本不知道OOP原则(因为C不 任何OOP支持)。

更好的想法是查看它的作用,并使用它作为功能规范重写一个新的应用程序。



此外:这是垃圾代码!它没有任何理由而不是循环使用递归:

Don't.
It's already in C - which is a subset of C++ - so technically it doesn't need any conversion: it should run as it is.

But "converting" C programs to C++ isn't really a good idea - they aren't designed to be "good" C++ because they were written with no idea of OOPs principles at all (since C doesn't have any OOPs support).
A better idea would be to look at what it does, and rewrite a new application using that as a functional specification.

And besides: that's rubbish code! It's using recursion for no good reason instead of a loop:
void menu()
    {
...     
    switch(getch())
    {
        case '1':
...
        default:
...
            menu();
        }
    }



它几乎没有错误检查,原作者甚至不愿意缩进它,...

我会说:如果它有效,请不要管它 - 或者正确地重写它并完全忽略该代码!


It's got little or no error checking, the original author couldn't even be bothered to indent it, ...
I'd say: if it works, leave it alone - or rewrite it properly and ignore that code completely!


这里有一些关于利用C ++改进的建议你有什么。



首先,用STL字符串替换固定字符字段。这也应该解决缓冲区溢出的问题。



Here are some suggestions on leveraging C++ to improve what you have.

First, replace the fixed character fields with STL strings. This should also fix the issue of buffer overflows.

#include <string>

struct person
{
	std::string name;
	std::string address;
	std::string father_name;
	std::string mother_name;
	long int mble_no;
	std::string sex;
	std::string mail;
	std::string citision_no;
};





上面的类(结构是一个类)只是一个POD(普通旧数据)类型。



将UI代码组织到控制台类中可能很有用。类似于:





The above class (a struct IS a class) is just a POD (plain old data) type.

It might be useful to organize your UI code into a console class. Something like:

class console
{
public:
    void menu();
    void got(person &);
    void show(const person &);

    void cls() { system("cls"); }
};





您还可以像这样抽象数据访问层或持久层:





You could also abstract your data access layer or persistence layer like so:

class persist
{
    FILE *file;
public:
    persist() : file(nullptr) { }
    bool open(const char *path);
    void addrecord(const person &);
    void readrecord(person &);
    void modifyrecord(person &);
    void deleterecord(const person &);
    void searchrecord(person &);
    void close();
};





back()和start()函数毫无意义。考虑重写菜单功能,以便在用户选择退出选项时退出循环。



有很多空白。你的代码不需要完全遵循这个例子,但这应该给你一些想法。



The back() and start() functions were pointless. Consider rewriting the menu function to operate in a loop that exits when the user chooses a "quit" option.

There are lots of gaps. You code need not exactly follow this example but this should give you some ideas.


这篇关于如何将该程序转换为C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆