21xrx.com
2024-12-26 23:58:22 Thursday
登录
文章检索 我的文章 写文章
C++简单系统源代码
2023-07-13 17:02:53 深夜i     --     --
C++编程 系统源代码 简单 源代码示例 C++项目

C++是一种面向对象的编程语言,它的设计目标是提高程序的效率和可靠性。C++的一个重要特点是支持多重继承和运算符重载,这使得程序员可以更加灵活地编写程序。下面是一个简单的C++系统源代码示例。


#include<iostream>

#include<conio.h>

#include<fstream>

#include<ctype.h>

#include<iomanip>

using namespace std;

class book

{

int bno;

char bname[50];

char aname[20];

public:

void create_book()

{

cout<<"\nNEW BOOK ENTRY...\n";

cout<<"\nEnter The book number:";

cin>>bno;

cin.ignore();

cout<<"\n\nEnter The Name of The Book:";

cin.getline(bname,50);

cout<<"\nEnter The Author's Name:";

cin.getline(aname,20);

cout<<"\n\nBook Created...";

}

void show_book()

{

cout<<"\nBook number:"<<bno;

cout<<"\nBook Name:"<<bname;

cout<<"\nAuthor Name:"<<aname;

}

void modify_book()

{

cout<<"\nBook number:"<<bno;

cout<<"\nModify Book Name:";

cin.getline(bname,50);

cout<<"\nModify Author's Name of Book:";

cin.getline(aname,20);

}

char* retbname(){return bname;}

int retbno(){return bno;}

};

fstream fp;

book bk;

void write_book()

{

fp.open("book.dat",ios::out|ios::app);

bk.create_book();

fp.write((char*)&bk,sizeof(book));

fp.close();

cout<<"\n\nThe book Has Been Created...";

getch();

}

void display_allb()

{

system("cls");

cout<<"\n\n\n\tBook LIST\n\n";

cout<<"====================================================\n";

cout<<"Book Number"<<setw(10)<<"Book Name"<<setw(20)<<"Author\n";

cout<<"====================================================\n";

fp.open("book.dat",ios::in);

while(fp.read((char*)&bk,sizeof(book)))

{

bk.show_book();

cout<<"\n";

}

fp.close();

getch();

}

void modify_book()

{

int bn,found=0;

system("cls");

cout<<"\n\n\tMODIFY BOOK REOCORD.... ";

cout<<"\n\n\tEnter The book number:";

cin>>bn;

fp.open("book.dat",ios::in|ios::out);

while(fp.read((char*)&bk,sizeof(book)) && found==0)

{

if(bk.retbno()==bn)

{

bk.show_book();

cout<<"\nEnter The New Details of book"<<endl;

bk.modify_book();

int pos=-1*sizeof(bk);

fp.seekp(pos,ios::cur);

fp.write((char*)&bk,sizeof(book));

cout<<"\n\n\t Record Updated...";

found=1;

}

}

fp.close();

if(found==0)

cout<<"\n\n Record Not Found...";

getch();

}

void display_spb(int n)

{

system("cls");

cout<<"\nBOOK DETAILS\n";

int flag=0;

fp.open("book.dat",ios::in);

while(fp.read((char*)&bk,sizeof(book)))

{

if(bk.retbno()==n)

{

bk.show_book();

flag=1;

}

}

fp.close();

if(flag==0)

cout<<"\n\nBook does not exist";

getch();

}

void delete_book()

{

int bn;

system("cls");

cout<<"\n\n\n\tDELETE BOOK...";

cout<<"\n\nEnter The Book's number You Want To Delete:";

cin>>bn;

fstream fp2;

fp.open("book.dat",ios::in|ios::out);

fp2.open("Temp.dat",ios::out);

fp.seekg(0,ios::beg);

while(fp.read((char*)&bk,sizeof(book)))

{

if(bk.retbno()!=bn)

{

fp2.write((char*)&bk,sizeof(book));

}

}

fp2.close();

fp.close();

remove("book.dat");

rename("Temp.dat","book.dat");

cout<<"\n\n\tRecord Deleted ..";

getch();

}

void menu()

{

system("cls");

fp.open("book.dat",ios::in);

if(!fp)

{

cout<<"ERROR!!! FILE COULD NOT BE OPEN\n\n\n Go To Entry Menu to create File";

cout<<"\n\n\n Program is closing ....";

getch();

exit(0);

}

cout<<"\n\n\n\t\tMAIN MENU\n\n";

cout<<"\n\n\t1.CREATE BOOK RECORDS\n";

cout<<"\n\n\t2.DISPLAY ALL BOOKS RECORD\n";

cout<<"\n\n\t3.SEARCH BOOK RECORD ";

cout<<"\n\n\t4.MODIFY BOOK RECORD";

cout<<"\n\n\t5.DELETE BOOK RECORD";

cout<<"\n\n\t6.EXIT";

cout<<"\n\n\tPlease Enter Your Choice (1-6) ";

char ch;

cin>>ch;

switch(ch)

{

case '1':write_book();break;

case '2':display_allb();break;

case '3':

{

int num;

system("cls");

cout<<"\n\n\tPlease Enter The Book's Number: ";

cin>>num;

display_spb(num);

}break;

case '4':modify_book();break;

case '5':delete_book();break;

case '6':exit(0);

default:cout<<"\a";menu();

}

}

int main()

{

menu();

return 0;

}

在这段源代码中,定义了一个C++类`book`,这个类有三个属性,即书籍编号(`bno`)、书名(`bname`)和作者(`aname`)。这个类有四个方法,分别用于创建一本新的书籍、显示一本书籍的信息、修改一本书籍的信息和返回书名的方法。这个程序还包括了写入书籍信息到文件、从文件读取并显示所有书籍信息、根据书籍编号查找并显示一本书籍的信息、修改一本书籍的信息、删除一本书籍的信息以及菜单管理程序的方法。这个程序可以方便地管理一本书店的书籍,可以实现增加、修改、查找和删除书籍等功能,适用于初学C++的编程爱好者进行学习和使用。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复