1. 程式人生 > >小學期c++ | 電話本

小學期c++ | 電話本

see ptr book 修改信息 文件查看 fst return 退出 delete

#include<iostream>
#include<string.h>
#include<fstream>
#include<cstdlib>
using namespace std;

class phoneBook{
       public:
              phoneBook(){};
              phoneBook(string a,string b){
                     name=a;
                     number=b;
              }

              
~phoneBook(){} void GetName(string a){name=a;} void GetNumber(string a){number=a;} string ReturnName(){return name;} string ReturnNumber(){return number;} void input(); void store(); void seek();
void change(); void Delete(); void read(); void storeNew(); private: string name; string number; }; phoneBook p[100]; void phoneBook::input(){ //添加信息 姓名電話號碼 int i=0; char name[30]; string
number; cout<<"請輸入信息[姓名為#代表輸入信息結束]:"<<endl; cout<<"姓名:"; cin>>name; p[i].GetName(name); while(p[i].ReturnName()!="#"){ cout<<"電話號碼:"; cin>>number; p[i].GetNumber(number); i++; cout<<"姓名:"; cin>>name; p[i].GetName(name); } store(); cout<<endl; } void phoneBook::seek(){ //輸入姓名查找電話號碼 string temp; int j=0; cout<<"請輸入要查找的姓名:"; cin>>temp; for(int i=0;i<=99;i++){ if(p[i].ReturnName()==temp){ cout<<"該用戶電話號碼為:"<<p[i].ReturnNumber()<<endl<<endl; } else if((p[i].ReturnName())!=temp){ j++; if(j==100) cout<<"該用戶不存在"<<endl<<endl; } } } void phoneBook::change(){ //修改指定信息 char temp[20]; string a,b; int j=0; cout<<"請輸入要修改的姓名:"; cin>>temp; for(int i=0;i<=99;i++){ if((p[i].ReturnName())==temp){ cout<<"修改信息:\n姓名:"; cin>>a; p[i].GetName(a); cout<<"電話號碼:" ; cin>>b; p[i].GetNumber(b); cout<<"修改成功"<<endl<<endl; } else if((p[i].ReturnName())!=temp){ j++; if(j==100) cout<<"該用戶不存在"<<endl<<endl; } } this->store(); } void phoneBook::Delete(){ //刪除指定電話信息 string temp; int i=0,j=0,truth=0; cout<<"請輸入要刪除的姓名:"; cin>>temp; while(p[i].ReturnName()!="#"){ if((p[i].ReturnName())==temp){ truth=1; while((p[i+1].ReturnName())!="#"){ p[i]=p[i+1]; i++; } p[i]=p[i+1]; cout<<"刪除成功"<<endl<<endl; }
i
++; } if(truth==0){ cout<<"該用戶不存在"<<endl<<endl; } this->store(); } void phoneBook::store(){ //寫入文件 ofstream font("電話本.txt"); for(int i=0;i<=99;i++){ if(p[i].ReturnName()!="#") font<<"姓名:"<<p[i].name<<endl; font<<"電話號碼:"<<p[i].number<<endl; } font.close(); } void phoneBook::storeNew(){ //寫入指定文件 char ch,filename2[30];
    cout
<<"請輸入用來儲存電話信息的文件名:"; cin>>filename2; getchar(); ofstream font2(filename2); ifstream font("電話本.txt"); while(font.get(ch)){   font2<<ch; } font2.close(); cout<<"信息保存成功,請關閉程序後打開文件查看"<<endl<<endl; } void phoneBook::read(){ //從指定文件中讀取 char filename1[30]; int i=0; cout<<"請輸入要讀取電話信息的文件名:"; cin>>filename1; getchar(); ifstream font1(filename1,ios_base::in); while(font1>>name>>number) { p[i]=phoneBook(name,number); p[i].GetName(name); p[i].GetNumber(number); i++; } font1.close(); cout<<"文件讀取成功"<<endl<<endl; } int main(){ int n,truth=1; phoneBook ptr; while(truth==1){ cout<<"請輸入您的選擇:"<<endl<<"1--添加姓名電話信息"<<endl<<"2--查找指定電話信息"<<endl; cout<<"3--刪除指定電話號碼"<<endl<<"4--修改指定電話信息"<<endl<<"5--從指定文件讀取電話信息"<<endl; cout<<"6--將電話信息保存至指定文件(若不選擇此項信息將會儲存在默認文件“電話本.txt”中)"<<endl; cout<<"7--退出電話本"<<endl<<"[選擇]:"; cin>>n; cout<<endl; switch (n) { case 1: ptr.input(); break; case 2: ptr.seek(); break; case 3: ptr.Delete(); break; case 4: ptr.change(); break; case 5: ptr.read(); break; case 6: ptr.storeNew(); break; case 7: truth=0; break; default: cout<<"您的輸入有誤,請重新輸入"<<endl<<endl; } } cout<<"感謝您的使用,您現在可以打開文件查看電話信息。祝您生活愉快,再見!"<<endl<<endl; system("pause"); return 0; }

小學期c++ | 電話本