通讯录源代码
- 格式:pdf
- 大小:234.45 KB
- 文档页数:10
设计开发源代码1.AddContactsActivity类package .demo.pr3;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.EditText;import android.widget.Toast;import .demo.pr3.datax.ContactsTable;import er;/*增加记录操作界面*/public class AddContactsActivity extends Activity { private EditText nameEditText; //输入框private EditText mobileEditText; //手机输入框private EditText qqEditText; //qqprivate EditText danweiEditText; //单位private EditText addressEditText; //地址Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.edit);setTitle("添加联系人");//从已设置的页面布局查找对应的控件nameEditText=(EditText)findViewById();mobileEditText=(EditText)findViewById(R.id.mobile); danweiEditText=(EditText)findViewById(R.id.danwei); qqEditText=(EditText)findViewById(R.id.qq);addressEditText=(EditText)findViewById(R.id.address); }/*创建菜单 */public boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE,1, Menu.NONE, "保存");menu.add(Menu.NONE,2, Menu.NONE, "返回");return super.onCreateOptionsMenu(menu);}/* 菜单事件*/public boolean onOptionsItemSelected(MenuItem item){ // TODO Auto-generated method stubswitch (item.getItemId()) {case 1://保存if(!nameEditText.getText().toString().equals("")){User user=new User();user.setName(nameEditText.getText().toString());user.setMoblie(mobileEditText.getText().toString());user.setDanwei(danweiEditText.getText().toString());user.setQq(qqEditText.getText().toString());user.setAddress(addressEditText.getText().toString());ContactsTable ct=new ContactsTable(AddContactsActivity.this);if(ct.addData(user)){Toast.makeText(AddContactsActivity.this, "添加成功!",Toast.LENGTH_SHORT).show();finish();}else{Toast.makeText(AddContactsActivity.this, "添加失败!",Toast.LENGTH_SHORT).show();}}else{Toast.makeText(AddContactsActivity.this, "请先输入数据!",Toast.LENGTH_SHORT).show();}break;case 2://返回finish();break;default:break;}return super.onOptionsItemSelected(item);}}2. ContactsMessageActivity类package .demo.pr3;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;import .demo.pr3.datax.ContactsTable;import er;/*显示联系人界面 */public class ContactsMessageActivity extends Activity { private TextView nameTextView; //输入框private TextView mobileTextView; //手机输入框private TextView qqTextView; //qqprivate TextView danweiTextView; //单位private TextView addressTextView; //地址private User user; //修改的联系人Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.message);setTitle("联系人信息");//从已设置的页面布局查找对应的控件nameTextView=(TextView)findViewById();mobileTextView=(TextView)findViewById(R.id.mobile); danweiTextView=(TextView)findViewById(R.id.danwei); qqTextView=(TextView)findViewById(R.id.qq);addressTextView=(TextView)findViewById(R.id.address);//将要修改的联系人数据付值到用户界面显示Bundle localBundle = getIntent().getExtras();int id=localBundle.getInt("user_ID");ContactsTable ct=new ContactsTable(this);user =ct.getUserByID(id);nameTextView.setText(":"+user.getName());mobileTextView.setText(":"+user.getMoblie());qqTextView.setText("QQ:"+user.getQq());danweiTextView.setText("单位:"+user.getDanwei());addressTextView.setText("地址:"+user.getAddress());}/*创建菜单*/public boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE, 1, Menu.NONE, "返回");return super.onCreateOptionsMenu(menu);}/* 菜单事件 */public boolean onOptionsItemSelected(MenuItem item){// TODO Auto-generated method stubswitch (item.getItemId()) {case 1://返回finish();break;default:break;}return super.onOptionsItemSelected(item);}}3. MyContactsActivity类package .demo.pr3;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.AlertDialog.Builder;import android.content.ContentUris;import android.content.ContentValues;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.graphics.Color;import .Uri;import android.os.Bundle;import android.provider.ContactsContract.RawContacts;import monDataKinds.Phone;import monDataKinds.StructuredName; import android.provider.ContactsContract.Contacts.Data;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;import .demo.pr3.datax.ContactsTable;import er;/*主界面*/public class MyContactsActivity extends Activity {private ListView listView; //结果列表private BaseAdapter listViewAdapter; //ListView 列表适配器private User users[];//通讯录用户private int selecteItem=0; //当前选择Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);setTitle("通讯录");listView = (ListView) findViewById(R.id.listView);loadContacts();}/*加载联系人列表*/private void loadContacts(){//获取所以通讯录联系人ContactsTable ct=new ContactsTable(this);users=ct.getAllUser();//listView列表现实适配器listViewAdapter=new BaseAdapter() {Overridepublic View getView(int position,View convertView, ViewGroup parent) {if(convertView==null){TextView textView =new TextView(MyContactsActivity.this);textView.setTextSize(22);convertView=textView;}String moblie=users[position].getMoblie()==null?"":users[position].getMoblie();((TextView)convertView).setText(users[position].getName()+"---"+moblie);if(position==selecteItem){convertView.setBackgroundColor(Color.YELLOW);}else{convertView.setBackgroundColor(0);}return convertView;}Overridepublic long getItemId(int position) {return position;}Overridepublic Object getItem(int position) {return users[position];}Overridepublic int getCount() {return users.length;}};//设置listView控件的适配器listView.setAdapter(listViewAdapter);listView.setOnItemClickListener(new OnItemClickListener() {Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stub//记录点击列selecteItem=arg2;//刷新列表listViewAdapter.notifyDataSetChanged();}});}/*创建菜单*/public boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE, 1, Menu.NONE, "添加");menu.add(Menu.NONE, 2, Menu.NONE, "编辑");menu.add(Menu.NONE, 3, Menu.NONE, "查看信息");menu.add(Menu.NONE, 4, Menu.NONE, "删除");menu.add(Menu.NONE, 5, Menu.NONE, "查询");menu.add(Menu.NONE, 6, Menu.NONE, "导入到手机薄");menu.add(Menu.NONE, 7, Menu.NONE, "退出");return super.onCreateOptionsMenu(menu);}/*菜单事件*/public boolean onOptionsItemSelected(MenuItem item){// TODO Auto-generated method stubswitch (item.getItemId()) {case 1://添加Intent intent = new Intent(MyContactsActivity.this,AddContactsActivity.class);startActivity(intent);break;case 2://编辑if(users[selecteItem].getId_DB()>0)//根据数据库ID判断当前记录是否可以操作{intent = new Intent(MyContactsActivity.this,UpdateContactsActivity.class);intent.putExtra("user_ID", users[selecteItem].getId_DB());startActivity(intent);}else{Toast.makeText(this, "无结果记录,无法操作!",Toast.LENGTH_SHORT).show();}break;case 3://查看信息if(users[selecteItem].getId_DB()>0){intent = new Intent(MyContactsActivity.this,ContactsMessageActivity.class);intent.putExtra("user_ID", users[selecteItem].getId_DB());startActivity(intent);}else{Toast.makeText(this, "无结果记录,无法操作!",Toast.LENGTH_SHORT).show();}break;case 4://删除if(users[selecteItem].getId_DB()>0){delete();}else{Toast.makeText(this, "无结果记录,无法操作!",Toast.LENGTH_SHORT).show();}break;case 5://查询new FindDialog(this).show();break;case 6://导入到手机薄if(users[selecteItem].getId_DB()>0){importPhone(users[selecteItem].getName(),users[selecteItem].getMoblie());Toast.makeText(this, "已经成功导入‘"+users[selecteItem].getName()+"’到手机薄!",Toast.LENGTH_SHORT).show();}else{Toast.makeText(this, "无结果记录,无法操作!",Toast.LENGTH_SHORT).show();}break;case 7://退出finish();break;default:break;}return super.onOptionsItemSelected(item);}Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();//重新加载数据ContactsTable ct=new ContactsTable(this);users=ct.getAllUser();//刷新列表listViewAdapter.notifyDataSetChanged();}/*查询*/public class FindDialog extends Dialog{public FindDialog(Context context) {super(context);}protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.find);setTitle("联系人查询");Button find=(Button)findViewById(R.id.find);Button cancel=(Button)findViewById(R.id.cancel);find.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {// TODO Auto-generated method stubEditText value=(EditText)findViewById(R.id.value);ContactsTable ct=new ContactsTable(MyContactsActivity.this);users=ct.findUserByKey(value.getText().toString());for(int i=0;i<users.length;i++){System.out.println("是"+users[i].getName()+",是" +users[i].getMoblie());}listViewAdapter.notifyDataSetChanged();selecteItem=0;dismiss();}});cancel.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {// TODO Auto-generated method stubdismiss();}});}}/*删除联系人*/public void delete(){Builder alert = new AlertDialog.Builder(this);alert.setTitle("系统信息");alert.setMessage("是否要删除联系人?");alert.setPositiveButton("是",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) { ContactsTable ct=new ContactsTable(MyContactsActivity.this);//删除联系人信息if(ct.deleteByUser(users[selecteItem])){//重新获取数据users=ct.getAllUser();//刷新列表listViewAdapter.notifyDataSetChanged();selecteItem=0;Toast.makeText(MyContactsActivity.this, "删除成功!",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MyContactsActivity.this, "删除失败!",Toast.LENGTH_SHORT).show();}}});alert.setNegativeButton("否",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {}});alert.show();}///导入到手机薄public void importPhone(String name,String phone){//系统通信录ContentProvider的URIUri phoneURL=android.provider.ContactsContract.Data.CONTENT_URI;ContentValues values = new ContentValues();//首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactIdUri rawContactUri = this.getContentResolver().insert(RawContacts.CONTENT_URI, values);long rawContactId = ContentUris.parseId(rawContactUri);//往data表插入数据values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);values.put(StructuredName.GIVEN_NAME, name);this.getContentResolver().insert(phoneURL, values);//往data表插入数据values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);values.put(Phone.NUMBER, phone);values.put(Phone.TYPE, Phone.TYPE_MOBILE);this.getContentResolver().insert(phoneURL, values);}}4. UpdateContactsActivity类package .demo.pr3;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.EditText;import android.widget.Toast;import .demo.pr3.datax.ContactsTable;import er;/*修改记录操作界面*/public class UpdateContactsActivity extends Activity {/** Called when the activity is first created. */ private EditText nameEditText; //输入框private EditText mobileEditText; //手机输入框private EditText qqEditText; //qqprivate EditText danweiEditText; //单位private EditText addressEditText; //地址private User user; //修改的联系人Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.edit);setTitle("修改联系人");//从已设置的页面布局查找对应的控件nameEditText=(EditText)findViewById();mobileEditText=(EditText)findViewById(R.id.mobile);danweiEditText=(EditText)findViewById(R.id.danwei);qqEditText=(EditText)findViewById(R.id.qq);addressEditText=(EditText)findViewById(R.id.address);//将要修改的联系人数据赋值到用户界面显示Bundle localBundle = getIntent().getExtras();int id=localBundle.getInt("user_ID");ContactsTable ct=new ContactsTable(this);user =ct.getUserByID(id);nameEditText.setText(user.getName());mobileEditText.setText(user.getMoblie());qqEditText.setText(user.getQq());danweiEditText.setText(user.getDanwei());addressEditText.setText(user.getAddress());}/*** 创建菜单*/public boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE, 1, Menu.NONE, "保存");menu.add(Menu.NONE, 2, Menu.NONE, "返回");return super.onCreateOptionsMenu(menu);}/*** 菜单事件*/public boolean onOptionsItemSelected(MenuItem item){// TODO Auto-generated method stubswitch (item.getItemId()) {case 1://保存if(!nameEditText.getText().toString().equals("")){user.setName(nameEditText.getText().toString());user.setMoblie(mobileEditText.getText().toString());user.setDanwei(danweiEditText.getText().toString());user.setQq(qqEditText.getText().toString());user.setAddress(addressEditText.getText().toString());ContactsTable ct=new ContactsTable(UpdateContactsActivity.this);//修改数据库联系人信息if(ct.updateUser(user)){Toast.makeText(UpdateContactsActivity.this, "修改成功!",Toast.LENGTH_SHORT).show();}else{Toast.makeText(UpdateContactsActivity.this, "修改失败!",Toast.LENGTH_SHORT).show();}}else{Toast.makeText(UpdateContactsActivity.this, "数据不能为空!",Toast.LENGTH_SHORT).show();}break;case 2://返回finish();break;default:break;}return super.onOptionsItemSelected(item);}}。
通讯录管理系统调试源代码预览说明:预览图片所展示的格式为文档的源格式展示,下载源文件没有水印,内容可编辑和复制#include#include#include#include#includeusing namespace std;#define FILENAME "C:\\\\phonebook.txt"class Person{public:string name;string sex;string address;stringtel;stringshuxing;Person(string na){name=na;}Person(string na,stringse,stringadd,stringte,stringsx){name=na;sex=se;address=add;tel=te;shuxing=sx;}void display(){cout<<name<<" "<<address<<"="" "<<sex<<"="" "<<shuxing<<endl;<="" "<<tel<<"="" p="">}void add_Person1();void add_Person2();voidlist_Person();void Reach();voiddelete_Person();voiddisplay_diff();voidrenew_Person();};int main(){Person person1("hh");loop:cout<<"\★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\"<<endl;< p="">cout<<"\★★**********************欢迎使用通讯录系统******************* ★★\"<<endl;< p="">cout<<"\★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\"<<endl;< p="">cout<<"\★★************************************************* ***********★★"<<endl;< p="">cout<<"\★★ 1.查看所有联系人2.查找联系人3.添加联系人★★\"<<endl;< p="">cout<<"\★★************************************************* ***********★★\"<<endl;< p="">cout<<"\★★ 4.删除联系人5.修改联系人6.显示类别联系人★★\"<<endl;< p="">cout<<"\★★************************************************* ***********★★\"<<endl;< p="">cout<<"\★★7.退出程序★★\"<<endl;< p="">cout<<"\★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\"<<endl;< p="">cout<<"\ 开发者:"<<"黄龙吉"<<" "<<"张帆"<<" "<<"陈续旭"<<" "<<"程传奇"<<endl;< p="">cout<<"请输入菜单选项:";int a;do{cin>>a;if(a<0||a>8)cout<<"输入有误请重新输入!(1-7)"<<endl;< p=""> } while(a<0||a>8);switch(a){case 1: //显示所有联系人{system("cls");person1.list_Person();system("pause");system("cls");break;}case 2: //按姓名搜索 {system("cls");person1.Reach();system("pause");system("cls");break;}case 3: //添加联系人 { system("cls");person1.add_Person2();system("pause");system("cls");break;}case 4: //删除联系人{ system("cls");person1.delete_Person();system("pause");system("cls");break;}case 5: //修改联系人信息 { system("cls"); person1.renew_Person();system("pause");system("cls");break;}case 6: //按类别显示{ system("cls"); person1.display_diff();system("pause");system("cls");break;}case 7:{//退出break;}default:break;}if (a!=7){goto loop;}return 0;}void Person::add_Person1(){ofstreamfout;fout.open(FILENAME,ios::app);//文件不存在时会主动创建if (fout.fail()){cerr<<"open file with write error"<<endl;< p="">}fout<<name<<" "<<address<<"="" "<<sex<<"="" "<<shuxing<<endl;<="" "<<tel<<"="" p="">fout.close();}void Person::add_Person2(){string name;string sex;string address;stringtel;stringshuxing;ofstreamfout;fout.open(FILENAME,ios::app);//文件不存在时会主动创建if (fout.fail()){cerr<<"open file with write error"<<endl;< p="">}cout<<"请输入姓名:"<<endl;< p="">cin>>name;cout<<"请输入性别:"<<endl;< p="">cin>>sex;cout<<"请输入地址:"<<endl;< p="">cin>>address;cout<<"请输入电话:"<<endl;< p="">cin>>tel;cout<<"请输入属性:"<<endl;< p="">cin>>shuxing;fout<<setw(19)<<left<<name<<setw(5)<<left<<sex<<set w(13)<<left<<<setw(14)<<left<<tel<<setw(10)<<left<<shuxi ng<<endl;<="" p="">fout.close();}void Person::list_Person(){ //全部显示记录//read from fileifstream fin(FILENAME);if (fin.fail()){cerr<< "open file with read error" <<endl;< p="">_exit(-127);}//////////////////////////string s;//存储返回的字符串,即一行的内容//fin.seekg(20,ios::cur);cout<<"================================= ===================="<<endl;< p="">cout<<setw(19)<<left<<"姓名"<<setw(8)<<left<<"性别"<<setw(14)<<left<<"地址"<<setw(9)<<left<<"电话"<<setw(15)<<left<<"属性"<<endl<<endl;< p="">while(getline(fin,s)){//cout<<s.length()<<endl;< p="">cout<<s<<endl;< p="">}cout<<"================================= ===================="<<="" p="">}void Person::Reach(){ //查找记录ifstreaminput_file;char h[100];string s;string name;cout<<"请输入要查找人的姓名:"<<endl;< p="">cin>>name;input_file.open(FILENAME);if(!input_file){cout<<"Codefile.txt can't open file!"<<endl;< p="">return ;}int flag=0;while(input_file>>h){ //使用文件读取来判断文件是否到末尾//字符串运算使用双等于来比较比使用函数更方便if(h==name){cout<< h;getline(input_file,s);cout<<s<<endl;< p="">}else{flag=1;}}if(flag)cout<<"对不起通讯录中没有"+name+"的信息!"<<endl;< p="">input_file.close();//在c++中字符数组可以直接跟字符串作比较}void Person::delete_Person() //删除记录{ifstream fin(FILENAME);ofstreamfout("temp.txt");if (fin.fail()){cerr<< "open file with read error" <<endl;< p="">_exit(-127);}charch;//fin.seekg(20,ios::cur);while(fin.get(ch))//cout<<s.length()<<endl;< p="">fout.put(ch);cout<<="">cout<<"================================= ===================="<<="" p="">fout.close();ifstreamfinfile("temp.txt");ofstreamfoutfile(FILENAME);char h[100];string s;string name;cout<<"请输入要删除人的姓名:"<<endl;< p="">cin>>name;string name1;if(!finfile){cout<<"Codefile.txt can't open file!"<<endl;< p="">return ;}int flag=0;while(finfile>>h){ //使用文件读取来判断文件是否到末尾//字符串运算使用双等于来比较比使用函数更方便if(h!=name){foutfile<<h<<" ";<="" p="">getline(finfile,s);foutfile<<s<<endl;< p="">}else{flag=1;name1=name;getline(finfile,s);}}if(flag==1)cout<<"联系人"+name1+"已经被删除!"<<endl;< p=""> elsecout<<"对不起通讯录中无联系人"+name+"!"<<endl;< p=""> }void Person::renew_Person(){ifstream fin(FILENAME);ofstreamfout("temp.txt");if (fin.fail()){cerr<< "open file with read error" <<endl;< p="">_exit(-127);}charch;//fin.seekg(20,ios::cur);while(fin.get(ch))//cout<<s.length()<<endl;< p="">fout.put(ch);cout<<="">cout<<"================================= ===================="<<="" p="">fout.close();ifstreamfinfile("temp.txt");ofstreamfoutfile(FILENAME);char h[100];string s;string name;cout<<"请输入要修改的姓名:"<<endl;< p="">cin>>name;string name1;int flag;if(!finfile){cout<<"Codefile.txt can't open file!"<<endl;< p="">return ;}while(finfile>>h){ //使用文件读取来判断文件是否到末尾//字符串运算使用双等于来比较比使用函数更方便if(h!=name){foutfile<<h;< p="">getline(finfile,s);foutfile<<s<<endl;< p="">}else{flag=1;getline(finfile,s);string name;string sex;stringtel;string address;stringshuxing;cout<<"请输入记录:"<<endl;< p="">cout<<"姓名:";cin>>name;name1=name;cout<<"性别:";cin>>sex;cout<<"地址:";cin>>address;cout<<"电话:";cin>>tel;cout<<"属性:";cin>>shuxing;foutfile<<setw(19)<<left<<name<<setw(5)<<left<<sex<< setw(13)<<left<<address<<setw(14)<<left<<tel<<setw(10)<<left<<shuxing<< endl;<="" p="">}}if(flag==1)cout<<"联系人"+name+"已经成功修改为"+name1+"!"<<endl;< p="">elsecout<<"通讯录中没有联系人"+name+" 无法进行修改!"<<endl;< p="">}void Person::display_diff(){//分类显示函数ifstreaminfile(FILENAME);char name[100];char sex[100];chartel[100];charsx[100];char add[100];stringshuxing;cout<<"请输入想要查询的类型:"<<endl;< p="">cin>>shuxing;while(infile>>name){infile>>sex;infile>>add;infile>>tel;infile>>sx;if(sx==shuxing){cout<<setw(19)<<left<<name<<setw(5)<<left<<sex<<set w(13)<<left<<<setw(14)<<left<<tel<<setw(10)<<left<<shuxi ng<<endl;<="" p="">}}infile.close();}</setw(19)<<left<<name<<setw(5)<<left<<sex<<setw(13 )<<left<</endl;<></endl;<></endl;<></setw(19)<<left<<name<<setw(5)<<left<<sex<<setw(13 )<<left<<></endl;<></s<<endl;<></h;<></endl;<></endl;<></s.length()<<endl;<></endl;<></endl;<></endl;<></s<<endl;<></h<<"></endl;<></endl;<></s.length()<<endl;<></endl;<></endl;<></s<<endl;<></endl;<></endl;<></s<<endl;<></s.length()<<endl;<></setw(19)<<left<<"姓名"<<setw(8)<<left<<"性别"<<setw(14)<<left<<"地址"<<setw(9)<<left<<"电话"<<setw(15)<<left<<"属性"<<endl<<endl;<></endl;<></endl;<></setw(19)<<left<<name<<setw(5)<<left<<sex<<setw(13 )<<left<</endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></name<<"></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></endl;<></name<<">。
个人通讯录管理系统import javax。
swing。
*;import java。
awt.*;import java。
awt.event。
*;import java.io.*;public class TongXunLu {public static void main(String[] args) {new menu();}}class menu implements ActionListener{JMenuItem zengjia,suoyou,beifen,quit,select,del;JFrame f;Container con;JMenuBar bar;String str1,str2,str3;JFileChooser fc1 = new JFileChooser();File fc2=new File("D://test。
txt”);menu(){f=new JFrame(”通讯录管理系统");f。
setDefaultCloseOperation(JFrame。
DISPOSE_ON_CLOSE);f。
setSize(500, 400);f。
setLocation(130, 150);con=f。
getContentPane();Color c=Color。
YELLOW ;Font fo=new Font("黑体",Font.BOLD,40); JPanel pan=new JPanel();JLabel lab=new JLabel(”通讯录管理系统");lab.setForeground(c);lab.setFont(fo);pan.add(lab);con。
add(pan);bar=new JMenuBar();f。
setJMenuBar(bar);JMenu menu1=new JMenu("文件"); JMenu menu2 =new JMenu(”查询"); JMenu menu3 =new JMenu("删除");zengjia=new JMenuItem("增加记录");suoyou=new JMenuItem(”所有记录”);beifen=new JMenuItem(”文件备份”);quit=new JMenuItem(”退出");select=new JMenuItem(”查询");del=new JMenuItem("删除");menu1.add(zengjia);(完整word版)个人通讯录管理系统源代码menu1。
c语言通讯录源代码打印Document serial number【LGGKGB-LGG98YT-LGGT8CB-LGUT-#include <>#include <>#include <>#include <>struct person{char name[15];char sex[2];int nianling;long int youbian;char addr[50];char celephone[22];char homephone[22];char company[20];char email[40];long int QQ;}per[500];int num=0;2aame);if(strlen(per[num].name)>15){printf("对不起!您的输入有误!请重新输入");goto name;}printf("\n请输入性别:\n");sex: scanf("%s",&per[num].sex);if(strlen(per[num].sex)==0){printf("对不起!您的输入有误!请重新输入");goto sex;}printf("\n请输入年龄:"); nianling:scanf("%d",&per[num].nianling);if(per[num].nianling<10||per[num].nianling>1 10) {printf("对不起!您的输入有误!请重新输入");goto nianling;}printf("\n请输入邮编\n");youbian: scanf("%d",&per[num].youbian); if(per[num].youbian<10000||per[num].youbia n>999999){printf("对不起!您的输入有误!请重新输入");goto youbian;}printf("\n请输入地址\n");addr: scanf("%s",&per[num].addr);if(strlen(per[num].addr)>50||strlen(per[num].a ddr)<4){printf("对不起!您的输入有误!请重新输入\n");goto addr;}printf("\n请输入手机号码:\n"); celephone:scanf("%s",&per[num].celephone);if(strlen(per[num].celephone)!=11){printf("对不起!您的输入有误!请重新输入\n");goto celephone;}printf("\n请输入家庭电话号码:\n"); homephone:scanf("%s",&per[num].homephone);if(strlen(per[num].homephone)!=11){printf("对不起!您的输入有误!请重新输入\n");goto homephone;}printf("\n请输入公司名称:"); company: scanf("%s",&per[num]pany);if(strlen(per[num]pany)>20||strlen(per[ num]pany)<10){printf("对不起!您的输入有误!请重新输入");goto company;}printf("\n输入电子邮箱:"); email: scanf("%s",&per[num].email);if(strlen(per[num].email)>30||strlen(pe r[num].email)<4){printf("对不起!您的输入有误!请重新输入");goto email;}printf("\n输入QQ号码:");QQ: scanf("%ld",&per[num].QQ);{printf("对不起!您的输入有误!请重新输入");goto QQ;}num++;printf("\n是否继续添加\n");printf("\n请按1和2(1代表继续;2代表不继续)\n");printf("请输入您的选择 ");scanf("%d",&a);if(a==1){goto loop;}else{return;}}void searchmenu()2aelephone,celephone)==0){printf("\n 以下是您查找的联系人的信息 ");printf("\n_______________________ _________");printf("\n名字: %s",per[i].name);printf("\n性别: %s",per[i].sex);printf("\n年龄: %d",per[i].nianling);printf("\n邮编: %ld",per[i].youbian);printf("\n地址: %s",per[i].addr);printf("\n手机号码: %s",per[i].celephone);printf("\n家庭电话号码:%s",per[i].homephone);printf("\n公司名称:%s",per[i]pany);printf("\n电子邮件:%s",per[i].email);printf("\nQQ号码:%ld",per[i].QQ);printf("\n_______________________ _________");printf("\n请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n对不起!没有该联系人的信息!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}void searchbyname()ame,name)==0){findmark++;printf("\n\t\t 以下是您查找的联系人的信息 ");printf("\n\t\t_____________________ ___________");printf("\n\t\t名字: %s",per[i].name);printf("\n\t\t性别: %s",per[i].sex);printf("\n\t\t年龄: %d",per[i].nianling);printf("\n\t\t邮编: %ld",per[i].youbian);printf("\n\t\t地址: %s",per[i].addr);printf("\n\t\t手机号码: %s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i]pany);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t_____________________ ___________");if ((i+1)<num){printf("\n是否继续查找相同名字的联系人的信息(1代表继续;2代表不继续)");scanf("%d",&j);if (j==1){a=i;mark++;continue;}elsebreak;}else{printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}}if(mark!=0){printf("\n对不起! 没有相同名字的该联系人的信息!!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}else if(findmark==0){printf("\n对不起!没有该联系人的信息!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}voidsearchbyhomephone()omephone,homephone) ==0){printf("\n\t\t 以下是您要查找的联系人的信息");printf("\n\t\t_____________________ ___________");printf("\n\t\t名字: %s",per[i].name);printf("\n\t\t性别: %s",per[i].sex);printf("\n\t\t年龄: %d",per[i].nianling);printf("\n\t\t邮编: %ld",per[i].youbian);printf("\n\t\t地址: %s",per[i].addr);printf("\n\t\t手机号码: %s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i]pany);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t_____________________ ___________");printf("\n\t\t请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n\t\t对不起!没有该联系人的信息!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}}void searchbyqq()Q==QQ){printf("\n\t\t 以下是您查找的联系人的信息");printf("\n\t\t_____________________ ___________");printf("\n\t\t名字: %s",per[i].name);printf("\n\t\t性别: %s",per[i].sex);printf("\n\t\t年龄: %d",per[i].nianling);printf("\n\t\t邮编: %ld",per[i].youbian);printf("\n\t\t地址: %s",per[i].addr);printf("\n\t\t手机号码: %s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i]pany);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t_____________________ ___________");printf("\n\t\t请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n\t\t对不起!没有该联系人的信息!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;} }void deletemenu()ame,name)==0){printf("\n\t\t以下是您要删除的联系人的纪录:");findmark++;printf("\n\t\t_____________________ ___________");printf("\n\t\t名字: %s",per[i].name);printf("\n\t\t性别: %s",per[i].sex);printf("\n\t\t年龄: %d",per[i].nianling);printf("\n\t\t邮编: %ld",per[i].youbian);printf("\n\t\t地址: %s",per[i].addr);printf("\n\t\t手机号码: %s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i]pany);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t_____________________ ___________");printf("\n\t\t是否删除(1代表删除;2代表不删除)\n");printf("\n\t\t请输入您的数字选择: ");scanf("%d",&k);if(k==1){for (j=i;j<num-1;j++) elephone,celephone)==0){deletemark++;printf("\n\t\t以下是您要删除的联系人的纪录:");printf("\n\t\t_____________________ ___________");printf("\n\t\t名字: %s",per[i].name);printf("\n\t\t性别: %s",per[i].sex);printf("\n\t\t年龄: %d",per[i].nianling);printf("\n\t\t邮编: %ld",per[i].youbian);printf("\n\t\t地址: %s",per[i].addr);printf("\n\t\t手机号码: %s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i]pany);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t_____________________ ___________");printf("\n\t\t是否删除(1代表删除;2代表不删除)\n");printf(" 请输入您的数字选择: ");scanf("%d",&h);if(h==1){for (j=i;j<num-1;j++) /*纪录移动,从per数组中删除之*/per[j]=per[j+1];num--;printf("\n\t\t删除成功");printf("\n是否继续删除(1代表继续;2代表不继续)\n");printf(" 请输入您的数字选择: ");scanf("%d",&h);if(h==1)deletebycelephone();}}}if(deletemark==0){printf("\n\t\t对不起!没有该联系人的纪录!");printf("\n\t\t是否继续删除(1代表继续;2代表不继续)\n");printf(" 请输入您的数字选择: ");scanf("%d",&m);if(m==1)deletebycelephone();}}void xiugaimenu()3fame,name)==0){printf("\n\t\t以下是您要修改的联系人的纪录:");findmark++;printf("\n\t\t_____________________ ___________");printf("\n\t\t名字: %s",per[i].name);printf("\n\t\t性别: %s",per[i].sex);printf("\n\t\t年龄: %d",per[i].nianling);printf("\n\t\t邮编: %ld",per[i].youbian);printf("\n\t\t地址: %s",per[i].addr);printf("\n\t\t手机号码: %s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i]pany);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否修改(1代表修改;2代表不修改)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){xiugaixiangmu(i);xiugaimark++;printf("\n\t\t修改成功");if((i+1)<num){printf("\n\t\t是否继续修改相同姓名的联系人的信息(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){a=i;findmark++; continue;}}printf("\n\t\t是否继续修改(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}}}if ((xiugaimark==0)&&(findmark==0)){printf("\n\t\t没有该联系人的纪录");printf("\n\t\t是否继续修改(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}else if (findmark!=0){printf("\n\t\t没有重名信息");printf("\n\t\t是否继续修改(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}}voidxiugaibycelephone()elephone,celephone)==0){printf("\n\t\t以下是您要修改的联系人的纪录:");findmark++;printf("\n\t\t_____________________ ___________");printf("\n\t\t名字: %s",per[i].name);printf("\n\t\t性别: %s",per[i].sex);printf("\n\t\t年龄: %d",per[i].nianling);printf("\n\t\t邮编: %ld",per[i].youbian);printf("\n\t\t地址: %s",per[i].addr);printf("\n\t\t手机号码: %s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i]pany);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t_____________________ ___________");printf("\n\t\t是否修改(1代表修改;2代表不修改)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){xiugaixiangmu(i);printf("\n\t\t修改成功");printf("\n\t\t是否继续修改(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);printf(" 请输入您的数字选择: ");if (j==1) xiugaixiangmu(i);break;}}}if (findmark==0){printf("\n\t\t对不起!没有该联系人的纪录!");printf("\n\t\t是否继续修改(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibycelephone();return;}}void xiugaixiangmu(int a)elephone);break;case 2:printf("请输入姓名:");scanf("%s",&per[a].name);break;case 3:printf("请输入家庭电话号码:");scanf("%s",&per[a].homephone);break;case 4:printf("请输入QQ号码:");scanf("%s",&per[a].QQ);break;case 5:printf("请输入地址:");scanf("%s",&per[a].addr);break;case 6:printf("请输入邮编:");scanf("%s",&per[a].youbian);break;case 7:printf("请输入email:");scanf("%s",&per[a].email);break;case 8:mainmenu();break;default: printf("对不起!您的输入有误!请重新输入: ");goto loop9;}}void listmenu()ame,per[i-1].name)<0){tmp=per[i];j=i-1;do{per[j+1]=per[j];j--;}while((strcmp,per[j].name)<0&&j>=0));per[j+1]=tmp;}}printf("\n\t\t排序成功,是否显示(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if (k==1)showall();return;}void listbycelephone()elephone,per[i-1].celephone)<0){tmp=per[i];j=i-1;do{per[j+1]=per[j];j--;}while ((strcmp,per[j].celephone)<0)&&j>=0);per[j+1]=tmp;}}printf("\n\t\t排序成功,是否显示(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if (k==1)showall();return;}void showall()ame);printf("\n\t\t性别: %s",per[i].sex);printf("\n\t\t年龄: %d",per[i].nianling);printf("\n\t\t邮编: %ld",per[i].youbian);printf("\n\t\t地址: %s",per[i].addr);printf("\n\t\t手机号码: %s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i]pany);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t_____________________ ___________");printf("\t\t");if (i+1<num){printf("\n\t\t_____________________ _____");system("pause");}}printf("\n\t\t********************* ***************************");}elseprintf("\n\t\t对不起!通讯录中无任何纪录!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}void writetofile()//*写入文件*//{int i,k;system("cls");system("color 84");if ((fp=fopen("","wb"))==NULL){printf("\n\t\t文件打开失败");}for (i=0;i<num;i++){if (fwrite(&per[i],sizeof(struct person),1,fp)!=1){printf("\n\t\t写入文件错误!\n");}}fclose(fp);printf("\n\t\t通讯录文件已保存");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&k);return;}void readfromfile()//*读取文件*//{int i,j,k;system("cls");system("color 2b");if((fp=fopen("","rb"))==NULL){printf("\n\t\t********************* *******");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 通讯录文件不存在! *");if((fp=fopen("","wb"))==NULL){printf("\n* 建立失败! *");printf("\n* *");printf("\n* *");printf("\n*********************** *****");exit(0);}else{printf("\n\t\t* 通讯录文件已建立! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t********************* *******");printf("\n\t\t 按任意键进入主菜单 ");printf("\n\t\t 请输入您的数字选择: ");scanf("%d",&k);return;}exit(0);}fseek(fp,0,2); //*文件位置指针移动到文件末尾*//if (ftell(fp)>0) //*文件不为空*//{rewind(fp); //*文件位置指针移动到文件开始位置*//for (num=0;!feof(fp) &&fread(&per[num],sizeof(structperson),1,fp);num++);printf("\n\t\t****************************");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 文件导入成功! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t****************************");printf("\n\t\t 按1显示所有信息,按2回主菜单! "); printf("\t\t 请输入您的数字选择: ");scanf("%d",&j);if(j==1)showall();}else{printf("\n\t\t********************* *******");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 文件导入成功! *");printf("\n\t\t* 通讯录文件中无任何纪录! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t********************* *******");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&i);return;}}void deleteall()//*删除所有信息*//{int i,j;system("cls");system("color 50");printf("\n\t\t确认删除\n");printf("请按1和2(1代表确认;2代表不删除)\n ");scanf("%d",&i);if (i==1){fclose(fp);if((fp=fopen("","wb"))==NULL){printf("\n\t\t不能打开文件,删除失败");readfromfile();}num=0;printf("\n\t\t纪录已删除!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}}。
/*** 业务类*/public class PABmanager {/*** 系统启动*/public static void main(String[] args) {Scanner input = new Scanner(System.in);UserDao userDao = new UserDaoImpl();TypeDao typeDao = new TypeDaoImpl();PersonDao personDao = new PersonDaoImpl();String in = input.next();if ("1".equals(in)) {boolean islogin = userDao.login();if(islogin){}else{System.exit(-1);}}else if ("2".equals(in)) {boolean modiFlag = userDao.modify();if(modiFlag){}else{}System.exit(-1);}else{System.exit(-1);}while(true){String in2 = input.next();if ("1".equals(in2)) {while(true){String num = input.next();if ("1".equals(num)) {String lbmc = input.next();String lbsm = input.next();String lbbz = input.next();Type type = new Type(lbmc,lbsm,lbbz);typeDao.createType(type);}else if ("2".equals(num)) {List<Type> types = typeDao.queryType();for (int i = 0; i < types.size(); i++) {Type type =types.get(i);}}else if ("3".equals(num)) {String lbmc = input.next();Type type = new Type(lbmc,null,null);typeDao.deleteType(type);}else if ("4".equals(num)) {break;}else{}}}else if ("2".equals(in2)) {while(true){String num = input.next();if ("1".equals(num)) {String lb = input.next();String xm = input.next();String dh = input.next();String sjh = input.next();String gzdw = input.next();String zz = input.next();String yzbm = input.next();Person person = new Person(lb,xm,dh,sjh,gzdw,zz,yzbm);personDao.createPerson(person);}else if ("2".equals(num)) {String name = input.next();Person p = personDao.queryPerson(name);}else if ("3".equals(num)) {int id = input.nextInt();String item = input.next();String val = input.next();personDao.updatePerson(id,item, val);}else if ("4".equals(num)) {String name = input.next();personDao.deletePerson(name);}else if ("5".equals(num)) {break;}else{}}}else if ("3".equals(in2)) {System.exit(-1);}else{}}}}。
#include<fstream>#include<iostream>#include<string>using namespace std;void main();//头文件的声明struct Student//结构定义一个人,存放基本信息{ public://公有的struct Student(){n=0;}void add();//添加void show();//显示void search();//查询void delect();//删除void sort();//排序void load(); //读取文件void save(); //保存通讯录private://私有的int n;string name;//姓名int num;//学号int number;//电话号码char addr[30];//地址char eip[6];//邮编char email[30];//Email}Stu[500];//定义一个类peoplevoid Student::add()//添加函数{cout<<endl;cout<<endl;cout<<endl;system("color 4e");cout<<" ※※※※※※※※※欢迎进入班级通讯录管理系统※※※※※※※※"<<endl<<endl;cout<<" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = "<<endl;cout<<" ☆信管1班方绍晟☆"<<endl<<endl;cout<<"************************************************************************"<<endl;cout<<" ********** -------------- ※$$ 这是添加功能:$$※-------------*********"<<endl;cout<<"请输入姓名:";cin>>Stu[n].name;cout<<endl;cout<<"学号:";cin>>Stu[n].num;cout<<endl;cout<<"电话号码:";cin>>Stu[n].number;cout<<endl;cout<<"地址:";cin>>Stu[n].addr;cout<<endl;cout<<"邮编:";cin>>Stu[n].eip;cout<<endl;cout<<"Email:";cin>>Stu[n].email;cout<<endl;n++;char m;cout<<"是否继续添加?(y/n)"; //选择cin>>m;if (m=='y')add();}void Student::show()//显示函数{system("color 5e");//颜色调用char l;cout<<endl;cout<<endl;cout<<endl;cout<<" ※※※※※※※※※欢迎进入班级通讯录管理系统※※※※※※※※"<<endl<<endl;cout<<" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = "<<endl;cout<<" ☆信管1班方绍晟☆"<<endl<<endl;cout<<"************************************************************************"<<endl;cout<<" ****** ---------- ※$$ 这是本通讯录的全部联系人:$$※---------*****"<<endl;cout<<endl;cout<<"姓名学号电话号码地址邮编Email "<<endl;for(int i=0;i<n;i++){cout<<Stu[i].name<<" "<<Stu[i].num<<" "<<Stu[i].number<<""<<Stu[i].addr<<" "<<Stu[i].eip<<" "<<Stu[i].email<<endl;cout<<"----------------------------------------------------"<<endl;}//71cout<<endl;cout<<endl;cout<<"退出请按y/返回请按n"<<endl;cin>>l;if(l=='y')exit(0);//退出程序结构语}void Student::search()//查询函数{system("color 6e");//颜色调用cout<<endl;cout<<endl;cout<<endl;cout<<" ※※※※※※※※※欢迎进入班级通讯录管理系统※※※※※※※※"<<endl<<endl;cout<<" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = "<<endl;cout<<" ☆信管1班方绍晟☆"<<endl<<endl;cout<<"************************************************************************"<<endl; //80cout<<" ********** -------------- ※$$ 这是查询功能:$$※-------------*********"<<endl;cout<<"************ ------------------<1> 按姓名查询-----------------************"<<endl;cout<<"************ ------------------<2>按学号查询----------------************"<<endl;cout<<"************ ----------------- <3> 按电话号码查询-------------************"<<endl;cout<<"************ ----------------- <4>退出-------------************"<<endl;cout<<"请选择查询方式:";int select,i;cin>>select;switch(select){case 1://按姓名查询{cout<<"请输入想要查询的姓名:"<<endl;string name1;cin>>name1;//输入查询姓名loop:for( i=0;i<=n;i++)//{if(Stu[i].name==name1){cout<<"以下是你要查询的联系人:"<<endl;cout<<"姓名学号电话号码地址邮编Email "<<endl;//100cout<<Stu[i].name<<" "<<Stu[i].num<<" "<<Stu[i].number<<" "<<Stu[i].addr<<" "<<Stu[i].eip<<" "<<Stu[i].email<<endl;cout<<"----------------------------------------------------"<<endl;break;}if(Stu[i].name!=name1)//找不到该联系人{cout<<"该学生不存在,请重新输入。
#include <stdio.h>#include <stdio.h>#include <string.h>#define maxlen 100struct persons{char name[10];char addr[20];char phnum[10];}persons[maxlen];;;;typedef struct lnode{char name[10];char addr[20];char phnum[10];struct lnode *next;}listnode,*linklist;;;;linklist head=NULL,r=NULL;listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7; int i;char name1[10],ch;char str1[20];FILE *fp; ;void creat(){ int j;long k;fp=fopen("people.txt","r+t");if(fp!=NULL){for(i=1;i<maxlen;i++){ j=fgetc(fp);if(j==EOF)return;k=i-1;fseek(fp,k*sizeof(struct persons),0);fread(&persons[i],sizeof(struct persons),1,fp); s=(linklist)malloc(sizeof(listnode));strcpy(s->name,persons[i].name);strcpy(s->addr,persons[i].addr);strcpy(s->phnum,persons[i].phnum);if(head==NULL)head=s;elser->next=s;r=s;}}else{ fp=fopen("people.txt","w"); i=1;}};;;void List(){ p1=head;while(p1!=NULL){ printf("\n\n\tname:%s\n",p1->name);printf("\n\n\taddr:%s",p1->addr);printf("\n\n\tphnum:%s",p1->phnum);p1=p1->next;}};;;void Delete(){ printf("\n\n\tplease input the name:");gets(name1); p4=head;if(strcmp(p4->name,name1)==0){ p4=p4->next;head=p4;}else{ while(strcmp(p4->next->name,name1)!=0) p4=p4->next;p5=p4->next;p4->next=p5->next;free(p5);}};;;void Find(){ printf("\n\n\tplease input the name:");p0=head;gets(name1);while(strcmp(name1,p0->name)!=0&&p0!=NULL) p0=p0->next;if(p0==NULL)printf("\n\n\tIt is not exit in the addr-book!");else{ printf("\n\n\tname:%s\n",p0->name);printf("\n\n\taddr:%s",p0->addr);printf("\n\n\tphnum:%s",p0->phnum);}};;;void Input(){ s=(linklist)malloc(sizeof(listnode));printf("\n\n\tplease input the sb's meg:");printf("\n\n\t\tname:");scanf("%s",s->name);printf("\n\n\t\tAddr:");scanf("%s",s->addr);printf("\n\n\t\tphnum:");scanf("%s",s->phnum);if(head==NULL)head=s;elser->next=s;r=s;};;;void Alter(){int j;printf("\n\n\tPlease input the name:");gets(name1);p3=head;while(strcmp(name1,p3->name)!=0&&p3!=NULL)p3=p3->next;if(p3==NULL)printf("\n\n\tIt is not exit in the addr-book!");else{ printf("\n\n\tplease input the new meg!");printf("\n\n\t\tname:");scanf("%s",name1);strcpy(p3->name,name1);printf("\n\n\t\tAddr:");scanf("%s",name1);strcpy(p3->addr,name1);printf("\n\n\t\tphnum:");scanf("%s",name1);strcpy(p3->phnum,name1);}};;;void Save(){ int j;fp=fopen("people.txt","w");for(p2=head,j=0;p2!=NULL;j++,p2=p2->next){ strcpy(persons[j].name,p2->name);strcpy(persons[j].addr,p2->addr);strcpy(persons[j].phnum,p2->phnum);fwrite(&persons[j],sizeof(struct persons),1,fp);}};;;void main(){ creat();do{printf("\n\t***********************************************\n");printf("\n\n\tWELCOME TO USE XIESHENGQING's Address book\n");printf("\n\t**********************************************\n"); printf("\n\n\t\tPlease make a choice below:");printf("\n\t\t1.List all the meg");printf("\n\t\t2.Delete a piece of meg");printf("\n\t\t3.Find a piece of meg");printf("\n\t\t4.Add a piece of meg");printf("\n\t\t5.Alter a piece of meg");printf("\n\t\t6.Save and Quit");printf("\n\t\t7.Create an address book");printf("\n\n\n");printf("\tInput Your Choice:");ch=getche();switch(ch){ case '1': List(); break;case '2': Delete();break;case '3': Find();break;case '4': Input();break;case '5': Alter();break;case '7': Save();break;case '6': creat();fclose(fp);exit(0);break;default:printf("\n\t********************************************\n");printf("\n\t The num should 1-4!!! \n");printf("\n\t********************************************\n");break;}}while(1);}。
/*** 业务类*/public class PABmanager {/*** 系统启动*/public static void main(String[] args) {Scanner input = new Scanner(System.in);UserDao userDao = new UserDaoImpl();TypeDao typeDao = new TypeDaoImpl();PersonDao personDao = new PersonDaoImpl();String in = input.next();if ("1".equals(in)) {boolean islogin = userDao.login();if(islogin){}else{System.exit(-1);}}else if ("2".equals(in)) {boolean modiFlag = userDao.modify();if(modiFlag){}else{}System.exit(-1);}else{System.exit(-1);}while(true){String in2 = input.next();if ("1".equals(in2)) {while(true){String num = input.next();if ("1".equals(num)) {String lbmc = input.next();String lbsm = input.next();String lbbz = input.next();Type type = new Type(lbmc,lbsm,lbbz);typeDao.createType(type);}else if ("2".equals(num)) {List<Type> types = typeDao.queryType();for (int i = 0; i < types.size(); i++) {Type type =types.get(i);}}else if ("3".equals(num)) {String lbmc = input.next();Type type = new Type(lbmc,null,null);typeDao.deleteType(type);}else if ("4".equals(num)) {break;}else{}}}else if ("2".equals(in2)) {while(true){String num = input.next();if ("1".equals(num)) {String lb = input.next();String xm = input.next();String dh = input.next();String gzdw = input.next();String zz = input.next();String yzbm = input.next();Person person = new Person(lb,xm,dh,sjh,gzdw,zz,yzbm);personDao.createPerson(person);}else if ("2".equals(num)) {String name = input.next();Person p = personDao.queryPerson(name);}else if ("3".equals(num)) {int id = input.nextInt();String item = input.next();String val = input.next();personDao.updatePerson(id,item, val);}else if ("4".equals(num)) {personDao.deletePerson(name);}else if ("5".equals(num)) {break;}else{}}}else if ("3".equals(in2)) {System.exit(-1);}else{}}}}。
源代码#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct _EmployeeInformation{int number;char name[20];char sex[20];char birthday[20];char job[20];char salary[20];char telephone[20];}EmployeeInformation;void viewall(EmployeeInformation employee[], int a);//查询所有职工的信息函数声明void viewa(EmployeeInformation employee[], int b);//查询一个职工的信息函数声明void add(EmployeeInformation employee[], int c);//新增一个职工的信息函数声明void delet(EmployeeInformation employee[], int d);//删除一个职工的信息函数声明void modify(EmployeeInformation employee[], int e);//修改一个职工的信息函数声明int main(){EmployeeInformation employee[100];EmployeeInformation a[1] = {0};int choice2, x = 0, i, j;char choice1 = 'y';FILE *fp;if((fp = fopen("d:\\EmployeeInfo.txt","r")) == NULL) //判断该文件是否存在,如否则提示,否则读出{printf("File open error!\n");exit(0);}fp = fopen("d:\\EmployeeInfo.txt","r");while(! feof(fp)){fscanf(fp,"%d %s %s %s %s %s %s\n",&employee[x].number,employee[x].name,employee[x].sex,employee[x].birthday,employee[x].job,employee[x].salary,employee[x].telephone);x ++;}fclose(fp);do{system("color 1f");printf("=====Welcome to Employee Management System!=====\n");printf("* 1.View All Employee Information. #\n");printf("# 2.View A Employee Information. *\n");printf("* 3.Add Employee Information. #\n");printf("# 4.Delete Employee Information. *\n");printf("* 5.Modify Employee Information. #\n");printf("# 6.Exit. *\n");printf("================================================\n");printf("Please Select:");scanf("%d", &choice2);switch(choice2){case 1: //查询所有职工的信息system("color 2f");viewall(employee, x);printf("y -> 继续系统n-> 退出系统\n");fflush(stdin);scanf("%c", &choice1);system("cls");break;case 2: //查询一个职工的信息system("color 3f");while(choice2 == 2){viewa(employee, x);printf("2->Countinue 0->Stop\n");scanf("%d",&choice2);}printf("y -> 继续系统n -> 退出系统\n");fflush(stdin);scanf("%c", &choice1);system("cls");break;case 3: //新增一个职工的信息system("color 4f");while(choice2 == 3){add(employee, x);x ++;printf("3->Countinue 0->Stop\n");scanf("%d", &choice2);}printf("y -> 继续系统n-> 退出系统\n");fflush(stdin);scanf("%c", &choice1);system("cls");break;case 4: //删除一个职工的信息system("color 5f");while(choice2 == 4){printf("Please input you want to delete number:");delet(employee, x);x --;printf("4->Countinue 0->Stop\n");scanf("%d", &choice2);}printf("y -> 继续系统n -> 退出系统\n");fflush(stdin);scanf("%c", &choice1);system("cls");break;case 5: //修改一个职工的信息system("color 6f");while(choice2 == 5){printf("Please input you want to modify number:");modify(employee, x);printf("5->Countinue 0->Stop\n");scanf("%d",&choice2);}printf("y -> 继续系统n -> 退出系统\n");fflush(stdin);scanf("%c", &choice1);system("cls");break;case 6: //退出系统system("color 7f");printf("Thank you for using this system!\n");printf("Press enter to exit……\n");exit(0);default: //提醒输入有误system("color 8f");printf("Your input is wrong.\n");printf("y -> 继续系统n -> 退出系统\n");fflush(stdin);scanf("%c", &choice1);system("cls");break;}}while(choice1 == 'y');for(j = 0; j < x-1; j ++) //冒泡排序,将职工按职工号大小进行排序{for(i = 0; i <= x-2 ; i ++){if(employee[i].number > employee[i+1].number){a[0] = employee[i];employee[i] = employee[i+1];employee[i+1] = a[0];}}}fp=fopen("d:\\EmployeeInfo.txt","w"); //新建文本并写入职工信息for(i = 0; i < x; i ++){fprintf(fp,"%d %s %s %s %s %s %s\n",employee[i].number,employee[i].name,employee[i].sex,employee[i].birthday,employee[i].job,employee[i].salary,employee[i].telephone);}fclose(fp);system("color 9f");printf("Thank you for using this system!\n");printf("Pres s enter to exit……\n");return 0;}void viewall(EmployeeInformation employee[], int a) //查询所有职工的信息{int i,j;EmployeeInformation b[1] = {0};for(j = 0; j < a-1; j ++) //冒泡排序,将从文本读出的职工信息按职工号大小进行排序{for(i = 0; i <= a-2 ; i ++){if(employee[i].number > employee[i+1].number){b[0] = employee[i];employee[i] = employee[i+1];employee[i+1] = b[0];}}}printf("Number\t \tName\tSex\tBir\tJob\tSalary\tTelephone\n");for(i = 0; i < a; i ++) //打出所有职工的信息{printf("%d\t",employee[i].number);printf("%s\t",employee[i].name);printf("%s\t",employee[i].sex);printf("%s\t",employee[i].birthday);printf("%s\t",employee[i].job);printf("%s\t",employee[i].salary);printf("%s\n",employee[i].telephone);}}void viewa(EmployeeInformation employee[], int b) //查询一个职工的信息{int i, flag, num;printf("Please input you want to search number:\n");scanf("%d", &num);flag = -1;for(i = 0; i < b; i ++) //将输入的号码与原有比较,如没有则提醒,否则继续{if(num == employee[i].number){flag = i;break;}}if(flag >= 0){printf("Number\t \tName\tSex\tBir\tJob\tSalary\tTelephone\n");printf("%d\t", employee[i].number);printf("%s\t", employee[i].name);printf("%s\t", employee[i].sex);printf("%s\t", employee[i].birthday);printf("%s\t", employee[i].job);printf("%s\t", employee[i].salary);printf("%s\n", employee[i].telephone);}else{printf("Not have this employee information.\n");}}void add(EmployeeInformation employee[], int c) //新增一个职工的信息{int i, num, flag;printf("Please input you want to add employee's informatoin:\n");printf("Number:");scanf("%d", &num);flag = -1;for(i = 0; i < c; i ++) //将输入的号码与原有比较,如已有则提醒,否则继续{if(num == employee[i].number){flag = i;break;}}if(flag >= 0){printf("There have had this employee information.\n");printf("Number\t \tName\tSex\tBir\tJob\tSalary\tTelephone\n");printf("%d\t", employee[i].number);printf("%s\t", employee[i].name);printf("%s\t", employee[i].sex);printf("%s\t", employee[i].birthday);printf("%s\t", employee[i].job);printf("%s\t", employee[i].salary);printf("%s\n", employee[i].telephone);}else{employee[c].number = num;printf("Name:");scanf("%s", employee[c].name);printf("Sex:");scanf("%s", employee[c].sex);printf("Birthday:");scanf("%s", employee[c].birthday);printf("Job:");scanf("%s", employee[c].job);printf("Salary:");scanf("%s", employee[c].salary);printf("Telephone:");scanf("%s", employee[c].telephone);printf("Add success!\n");}}void delet(EmployeeInformation employee[], int d) //删除一个职工的信息{int i, k, flag, num, choice4;scanf("%d", &num);flag = -1;for(i = 0; i < d; i ++) //将输入的号码与原有比较,如没有该职工则提醒,否则继续{if(num == employee[i].number){flag = i;k = i;break;}}if(flag >= 0){printf("Number\t \tName\tSex\tBir\tJob\tSalary\tTelephone\n");printf("%d\t",employee[i].number);printf("%s\t",employee[i].name);printf("%s\t",employee[i].sex);printf("%s\t",employee[i].job);printf("%s\t",employee[i].salary);printf("%s\n",employee[i].telephone);printf("Do you really want to delet this employee information.\n");printf("1->Yes 2->No\n");scanf("%d", &choice4);if(choice4 == 1) //将要删除的信息用后面的覆盖掉,以达到删除的目的{for(k = i; k < d-1; k ++){employee[k] = employee[k + 1];}}printf("Delete success!\n");}else{printf("Not have this employee information.\n");}}void modify(EmployeeInformation employee[], int e) //修改一个职工的信息{int i, flag, num, choice5;scanf("%d", &num);flag = -1;for(i = 0; i < e; i ++) //将输入的号码与原有比较,如没有该职工则提醒,否则继续{if(num == employee[i].number){flag = i;break;}}if(flag >= 0){printf("Number\t \tName\tSex\tBir\tJob\tSalary\tTelephone\n");printf("%d\t", employee[i].number);printf("%s\t", employee[i].name);printf("%s\t", employee[i].sex);printf("%s\t", employee[i].birthday);printf("%s\t", employee[i].job);printf("%s\n", employee[i].telephone);printf("Do you really want to change this employee information.\n");printf("1->Yes 2->No\n");scanf("%d", &choice5);if(choice5 == 1) //重新编辑职工的信息{printf("New number:");scanf("%d", &employee[i].number);printf("New name:");scanf("%s", employee[i].name);printf("New sex:");scanf("%s", employee[i].sex);printf("New birthday:");scanf("%s", employee[i].birthday);printf("New job:");scanf("%s", employee[i].job);printf("New salary:");scanf("%s", employee[i].salary);printf("New telephone:");scanf("%s", employee[i].telephone);printf("Modify success!\n");}}else{printf("Not have this employee information.\n");}}。
源代码:#include "stdio.h"#include "stdlib.h"#include "string.h"#include "conio.h"#include "stdlib.h"#define null 0struct record{char name[20];char phone[20];char adress[40];char postcode[10];char e_mail[30];}student[500];struct LinkList{struct record US;struct LinkList *next;}a;struct LinkList *head=null;int num=0;FILE *fp;int menu_select();int adduser();int list();int search();int display();int add();int listbyname();int dele();int save();int exit();void main(){system("cls");for(;;){switch(menu_select()){case 0:adduser();break;case 1:list();break;case 2:search();break;case 3:display();break;case 4:add();break;case 5:listbyname();break;case 6:dele();break;case 7:save();break;case 8:exit(0);}}}menu_select(){char s[80];int a;printf("*_* press any key enter menu! *_* \n");getch();system("cls");printf("\t\t********************MENU*********************\n\n"); printf("\t\t 0. 输入记录\n");printf("\t\t 1. 显示记录 \n");printf("\t\t 2. 按查找\n");printf("\t\t 3. 按查找\n");printf("\t\t 4. 插入记录 \n");printf("\t\t 5. 按排序\n");printf("\t\t 6. 删除记录\n");printf("\t\t 7. 记录保存文件\n");printf("\t\t 8. Quit\n");printf("\t\t***********************************************\n"); do{printf("\n Enter you choice(0~11):");scanf("%s",s);a=atoi(s);}while (a<0||a>11);return a;}adduser(){printf("\n\t\t**************** 请输入用户信息 ****************\n"); printf("\n\t\t输入:");scanf("%s",&student[num].name);printf("\n\t\t输入:");scanf("%s",&student[num].phone);printf("\n\t\t输入地址:");scanf("%s",&student[num].adress);printf("\n\t\t输入邮编:");scanf("%s",&student[num].postcode);printf("\n\t\t输入:");scanf("%s",&student[num].e_mail);num++;printf("\n\t\t是否继续添加?(Y/N):");if (getch()=='y')adduser();return(0);}list(){int i;system("cls");if(num!=0){printf("\n\t\t*************** 以下为通讯录所有信息************"); for (i=0;i<num;i++){printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress);printf("\n\t\t邮编: %s",student[i].postcode);printf("\n\t\t:%s",student[i].e_mail);printf("\t\t");if (i+1<num){printf("\n\t\t__________________________");system("pause");}}printf("\n\t\t************************************************"); }elseprintf("\n\t\t通讯录中无任何纪录");printf("\n\t\t按任意键返回主菜单:");getch();return(0);}search(){int mark=0;int i;int a=0;printf("\n\t\t***************** 按查找 *******************");char name[20];printf("\n\t\t请输入:");scanf("%s",name);for(i=a;i<num;i++){if (strcmp(student[i].name,name)==0){printf("\n\t\t************* 以下是您查找的用户信息 ***********"); printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress);printf("\n\t\t:%s",student[i].e_mail);printf("\n\t\t************************************************"); mark++;if ((i+1)<num){printf("\n\t\t是否继续查找相同名字的用户信息:(y/n)");if (getch()=='y'){a=i;continue;}elsereturn(0);}else{printf("\n\t\t按任意键返回主菜单");getch();return(0);}}}if(mark!=0){printf("\n\t\t没有相同的用户纪录");printf("\n\t\t按任意键返回主菜单");getch();return(0);}else{printf("\n\t\t没有相同的用户纪录");printf("\n\t\t按任意键返回主菜单");getch();return(0);}}display(){int mark=0;int i;int a=0;printf("\n\t\t****************** 按查找 ******************");char phone[10];printf("\n\t\t请输入:");scanf("%s",phone);for(i=0;i<num;i++){if (strcmp(student[i].phone,phone)==0){printf("\n\t\t************** 以下是您查找的用户信息 **********"); printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress);printf("\n\t\t:%s",student[i].e_mail);printf("\n\t\t************************************************"); printf("\n\t\t按任意键返回主菜单:");mark++;getch();return(0);}}if (mark==0){printf("\n\t\t没有改用户的信息");printf("\n\t\t按任意键返回主菜单");getch();return(0);}return(0);}add(){int i;if ((fp=fopen("student.bin","wb"))==NULL){printf("\n\t\t文件打开失败");}for (i=0;i<num;i++){if (fwrite(&student[i],sizeof(struct record),1,fp)!=1){printf("\n\t\t写入文件错误!\n");}printf("\n\t\t**************** 请输入用户信息 ****************\n"); printf("\n\t\t输入:");scanf("%s",&student[num].name);printf("\n\t\t输入:");scanf("%s",&student[num].phone);printf("\n\t\t输入地址:");scanf("%s",&student[num].adress);printf("\n\t\t输入邮编:");scanf("%s",&student[num].postcode);printf("\n\t\t输入:");scanf("%s",&student[num].e_mail);num++;printf("\n\t\t是否继续添加?(Y/N):");if (getch()=='y')adduser();return(0);}fclose(fp);printf("\n\t\t通讯录文件已保存");printf("\n\t\t按任意键退出程序\n\t\t");exit(0);return(0);}void deletebyphone(){int i,j;int deletemark=0;char phone[20];printf("\n\t\t请输入要删除用户:");scanf("%s",phone);if(num==0){printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t按任意键返回主菜单");getch();return;}for (i=0;i<num;i++){if (strcmp(student[i].phone,phone)==NULL){printf("\n\t\t以下是您要删除的用户纪录:"); printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress); printf("\n\t\t:%s",student[i].e_mail);printf("\n\t\t是否删除?(y/n)");if (getch()=='y'){for (j=i;j<num-1;j++)student[j]=student[j+1];num--;deletemark++;printf("\n\t\t删除成功");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyphone();return;}elsereturn;}continue;}if (deletemark==0){printf("\n\t\t没有该用户的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyphone();return;}}void deletebyname(){int a=0;int findmark=0;int j;int deletemark=0;int i;char name[20];printf("\n\t\t请输入要删除用户:");scanf("%s",name);for (i=a;i<num;i++){if (strcmp(student[i].name,name)==NULL){printf("\n\t\t以下是您要删除的用户纪录:");findmark++;printf("\n\t\t________________________________"); printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress);printf("\n\t\t:%s",student[i].e_mail);printf("\n\t\t________________________________"); printf("\n\t\t是否删除?(y/n)");if (getch()=='y'){for (j=i;j<num-1;j++)student[j]=student[j+1];num--;deletemark++;printf("\n\t\t删除成功");if((i+1)<num){printf("\n\t\t是否继续删除相同的用户信息?(y/n)"); if (getch()=='y'){a=i;continue;}}printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyname();return;}if((i+1)<num){printf("\n\t\t是否继续删除相同的用户信息?(y/n)"); if (getch()=='y'){a=i;continue;}}}elsecontinue;}if ((deletemark==0)&&(findmark==0)){printf("\n\t\t没有该用户的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyphone();return;return;}else if (findmark!=0){printf("\n\t\t没有重名信息");printf("\n\t\t没有该用户的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyphone();return;return;}}dele(){char choic;printf("\n\t\t1-按删除 2-按删除");printf("\n\t\t请选择:");choic=getch();switch (choic){case '1':deletebyphone();break;case '2':deletebyname();break;}return(0);}listbyname(){int i,j;struct record tmp;for (i=1;i<num;i++){if(strcmp(student[i].name,student[i-1].name)<0){tmp=student[i];j=i-1;do{student[j+1]=student[j];j--;}while ((strcmp(,student[j].name)<0&&j>=0)); student[j+1]=tmp;}}printf("\n\t\t排序成功,是否显示?(y/n)");if (getch()=='y')list();return(0);}save(){int j;FILE*fp;fp=fopen("student.txt","w");if (fp==NULL)printf("can't open the file.");if(num!=0){for(j=0;j<num;j++){fwrite(student,sizeof(student),1,fp);}}printf("保存成功!");fclose(fp);return(0);}。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <conio.h>struct person{char name[15];char sex[2];int nianling;long int youbian;char addr[50];char celephone[22];char homephone[22];char company[20];char email[40];long int QQ;}per[500];int num=0;//*外部变量num为文件中的纪录数*//FILE *fp;void mainmenu();//*主菜单*//void newrecord();//新添纪录*//void xiugaimenu();//*修改菜单*//void xiugaibyname();//*按姓名修改*//void xiugaibycelephone();//*按手机号码修改*//void xiugaixiangmu(int a);//*修改项目*//void searchmenu();//*查询菜单*//void searchbyname();//*按姓名查询*//void searchbycelephone();//*按手机号码查询*//void searchbyhomephone();//*按家庭电话号码查询*// void searchbyqq();//*按QQ查询*//void deletemenu();//*删除菜单*//void deleteall();//*删除所有信息*//void deleteone();//*删除单个*//void showall();//*输出全部信息*//void readfromfile();//*读取文件*//void writetofile();//*写入文件*// /void deletebycelephone(); //*按手机号码删除*//void deletebyname();//*按姓名删除*/void listbycelephone();//*按手机号码排序*//void listbyname();//*按姓名排序*//void listmenu();//*排序菜单*//void main()//*主函数*//{system("cls");readfromfile();while (1){mainmenu();}} //*主函数结束*//void mainmenu()//*主菜单函数*//{int a;system("cls");system("color 2a");printf("\n\t 您好! 欢迎使用通讯录!\n");printf(" 1: 添加个人信息\n");printf(" 2: 查询个人信息\n");printf(" 3: 删除个人信息\n");printf(" 4: 修改个人信息\n");printf(" 5: 信息排序\n");printf(" 6: 输出全部信息\n");printf(" 7: 写入文件\n");printf(" 8: 读取文件\n");printf(" 9: 删除所有\n");printf(" 0: 退出\n");printf(" 请输入您的数字选择: ");loop1: scanf("%d",&a);switch (a){case 1:newrecord();break;case 2:searchmenu();break;case 3:deletemenu();break;case 4:xiugaimenu();break;case 5:listmenu();break;case 6:showall();break;case 7:writetofile();break;case 8:readfromfile();break;case 9:deleteall();break;case 0:exit(0);default:printf("对不起!您的输入有误!请重新输入: ");goto loop1;}}void newrecord()//*添加纪录*//{int a;system("cls");system("color 2a");loop: printf("\n\n\n\n\t\t 请输入您要添加的联系人的信息:\n");printf("\n请输入姓名:\n");name: scanf("%s",&per[num].name);if(strlen(per[num].name)>15){printf("对不起!您的输入有误!请重新输入");goto name;}printf("\n请输入性别:\n");sex: scanf("%s",&per[num].sex);if(strlen(per[num].sex)==0){printf("对不起!您的输入有误!请重新输入");goto sex;}printf("\n请输入年龄:");nianling: scanf("%d",&per[num].nianling);if(per[num].nianling<10||per[num].nianling>110){printf("对不起!您的输入有误!请重新输入");goto nianling;}printf("\n请输入邮编\n");youbian: scanf("%d",&per[num].youbian);if(per[num].youbian<10000||per[num].youbian>999999){printf("对不起!您的输入有误!请重新输入");goto youbian;}printf("\n请输入地址\n");addr: scanf("%s",&per[num].addr);if(strlen(per[num].addr)>50||strlen(per[num].addr)<4){printf("对不起!您的输入有误!请重新输入\n");goto addr;}printf("\n请输入手机号码:\n");celephone: scanf("%s",&per[num].celephone);if(strlen(per[num].celephone)!=11){printf("对不起!您的输入有误!请重新输入\n");goto celephone;}printf("\n请输入家庭电话号码:\n");homephone: scanf("%s",&per[num].homephone);if(strlen(per[num].homephone)!=11){printf("对不起!您的输入有误!请重新输入\n");goto homephone;}printf("\n请输入公司名称:");company: scanf("%s",&per[num].company);if(strlen(per[num].company)>20||strlen(per[num].company)<10){printf("对不起!您的输入有误!请重新输入");goto company;}printf("\n输入电子邮箱:");email: scanf("%s",&per[num].email);if(strlen(per[num].email)>30||strlen(per[num].email)<4){printf("对不起!您的输入有误!请重新输入");goto email;}printf("\n输入QQ号码:");QQ: scanf("%ld",&per[num].QQ);if(per[num].QQ>00||per[num].QQ<){printf("对不起!您的输入有误!请重新输入");goto QQ;}num++;printf("\n是否继续添加?\n");printf("\n请按1和2(1代表继续;2代表不继续)\n");printf("请输入您的选择");scanf("%d",&a);if(a==1){goto loop;}else{return;}}void searchmenu()//*查询菜单*//{int a;system("cls");system("color 2a");printf("\n\t\t 欢迎使用查询菜单");printf("\n\t\t 1-按手机号码查询");printf("\n\t\t 2-按姓名查询");printf("\n\t\t 3-按家庭电话号码查询");printf("\n\t\t 4-按QQ查询");printf("\n\t\t 5-返回主菜单");printf("\n\t\t");printf("请输入您的选择");scanf("%d",&a);switch (a){case 1:searchbycelephone();break;case 2:searchbyname();break;case 3:searchbyhomephone();break;case 4:searchbyqq();break;case 5:mainmenu();break;}}void searchbycelephone()//*按手机号码查询*//{int mark=0;int i,j;int a=0;char celephone[11];printf("\n 按手机号码查找\n");printf("\n请输入手机号码:\n");scanf("%s",celephone);for(i=0;i<num;i++){if (strcmp(per[i].celephone,celephone)==0){printf("\n 以下是您查找的联系人的信息");printf("\n________________________________");printf("\n名字:%s",per[i].name);printf("\n性别:%s",per[i].sex);printf("\n年龄:%d",per[i].nianling);printf("\n邮编:%ld",per[i].youbian);printf("\n地址:%s",per[i].addr);printf("\n手机号码:%s",per[i].celephone);printf("\n家庭电话号码:%s",per[i].homephone);printf("\n公司名称:%s",per[i].company);printf("\n电子邮件:%s",per[i].email);printf("\nQQ号码:%ld",per[i].QQ);printf("\n________________________________");printf("\n请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n对不起!没有该联系人的信息!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}void searchbyname()//*按姓名查询*//{int mark=0;int i,j,findmark=0;int a=0;char name[15];printf("\n 按姓名查找");printf("\n请输入姓名:");scanf("%s",name);for(i=a;i<num;i++){if (strcmp(per[i].name,name)==0){findmark++;printf("\n\t\t 以下是您查找的联系人的信息");printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");if ((i+1)<num){printf("\n是否继续查找相同名字的联系人的信息?(1代表继续;2代表不继续)");scanf("%d",&j);if (j==1){a=i;mark++;continue;}elsebreak;}else{printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}}if(mark!=0){printf("\n对不起! 没有相同名字的该联系人的信息!!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}else if(findmark==0){printf("\n对不起!没有该联系人的信息!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}void searchbyhomephone()//*按家庭电话号码查询*//{int mark=0;int i,j;int a=0;char homephone[11];printf("\n\t\t 按家庭电话号码查找");printf("\n请输入家庭电话号码:");scanf("%s",homephone);for(i=0;i<num;i++){if (strcmp(per[i].homephone,homephone)==0){printf("\n\t\t 以下是您要查找的联系人的信息");printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n\t\t对不起!没有该联系人的信息!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}}void searchbyqq()//*按QQ查询*//{int mark=0;int i,j;long int QQ;printf("\n\t\t 按QQ号码查找\n");printf("\n请输入QQ号码:");scanf("%ld",&QQ);for(i=0;i<num;i++){if(per[i].QQ==QQ){printf("\n\t\t 以下是您查找的联系人的信息");printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n\t\t对不起!没有该联系人的信息!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}}void deletemenu()//*删除菜单*//{int i,j;system("cls");system("color 3b");if(num==0){printf("\n\t\t对不起!文件中无任何纪录!");printf("\n\t\t请先添加个人信息!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}else{printf("\n\t\t 删除菜单");printf("\n\t\t 1-删除单个");printf("\n\t\t 2-返回主菜单");printf("\n\t\t ");printf("\n\t\t请输入您的数字选择: ");loop4: scanf("%d",&i);switch (i){case 1:deleteone();break;case 2:mainmenu();break;default:printf("对不起!您的输入有误!请重新输入: ");goto loop4;}}}void deleteone()//*删除单个*//{int i;printf("\n\t\t1-按手机号码删除2-按姓名删除");printf("\n\t\t请输入您的选择:");loop5:scanf("%d",&i);;switch (i){case 1:deletebycelephone();break;case 2:deletebyname();break;default:printf("对不起!您的输入有误!请重新输入: ");goto loop5;}}void deletebyname()//姓名删除*//{int a=0;int findmark=0;int i,j,k;int deletemark=0;char name[15];printf("\n\t\t请输入您要删除的联系人的姓名:");scanf("%s",name);for (i=a;i<num;i++){if (strcmp(per[i].name,name)==0){printf("\n\t\t以下是您要删除的联系人的纪录:");findmark++;printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否删除?(1代表删除;2代表不删除)\n");printf("\n\t\t请输入您的数字选择: ");scanf("%d",&k);if(k==1){for (j=i;j<num-1;j++) //*纪录移动,从per数组中删除之*//per[j]=per[j+1];num--;deletemark++;printf("\n\t\t删除成功");if((i+1)<num){printf("\n\t\t是否继续删除相同姓名的联系人的信息?(1代表删除;2代表不删除)");printf("\n\t\t请输入您的数字选择: ");scanf("%d",&k);if(k=1){a=i;findmark++;continue;}}printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");printf("\n\t\t请输入您的数字选择: ");scanf("%d",&k);if(k==1)deleteone();elsebreak;}}elsecontinue;}if((deletemark==0)&&(findmark==0)){printf("\n\t\t对不起!没有该联系人的纪录!");printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if(k==1)deleteone();}else if(findmark!=0){printf("\n\t\t对不起!没有重名信息!");printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if(k==1)deleteone();}}void deletebycelephone()//*按手机号码删除*//{int i,j,h,m;int deletemark=0;char celephone[11];printf("\n\t\t请输入要删除的联系人的手机号码:");scanf("%s",celephone);for(i=0;i<num;i++){if(strcmp(per[i].celephone,celephone)==0){deletemark++;printf("\n\t\t以下是您要删除的联系人的纪录:");printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否删除?(1代表删除;2代表不删除)\n");printf(" 请输入您的数字选择: ");scanf("%d",&h);if(h==1){for (j=i;j<num-1;j++) /*纪录移动,从per数组中删除之*/per[j]=per[j+1];num--;printf("\n\t\t删除成功");printf("\n是否继续删除?(1代表继续;2代表不继续)\n");printf(" 请输入您的数字选择: ");scanf("%d",&h);if(h==1)deletebycelephone();}}}if(deletemark==0){printf("\n\t\t对不起!没有该联系人的纪录!");printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)\n");printf(" 请输入您的数字选择: ");scanf("%d",&m);if(m==1)deletebycelephone();}}void xiugaimenu()//*修改菜单*//{int i,j;system("cls");system("color 3f");if(num==0){printf("\n\t\t对不起!文件中无任何纪录!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}else{printf("\n\t\t 1-按姓名修改2-按手机号码修改\n");printf(" 请输入您的数字选择: ");loop6:scanf("%d",&i);switch (i){case 1:xiugaibyname();break;case 2:xiugaibycelephone();break;default:printf("对不起!您的输入有误!请重新输入: ");goto loop6;}}}void xiugaibyname()//*按姓名修改*//{int a=0;int findmark=0;int xiugaimark=0;int i,j;char name[15];printf("\n\t\t请输入要修改联系人的姓名:");scanf("%s",name);for (i=a;i<num;i++){if (strcmp(per[i].name,name)==0){printf("\n\t\t以下是您要修改的联系人的纪录:");findmark++;printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否修改?(1代表修改;2代表不修改)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){xiugaixiangmu(i);xiugaimark++;printf("\n\t\t修改成功");if((i+1)<num){printf("\n\t\t是否继续修改相同姓名的联系人的信息?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){a=i;findmark++;continue;}}printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}}}if ((xiugaimark==0)&&(findmark==0)){printf("\n\t\t没有该联系人的纪录");printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}else if (findmark!=0){printf("\n\t\t没有重名信息");printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}}void xiugaibycelephone()//*按手机号码修改*//{int findmark=0;int i,j;char celephone[11];printf("\n\t\t请输入要修改联系人的手机号码:");scanf("%s",celephone);for (i=0;i<num;i++){if (strcmp(per[i].celephone,celephone)==0){printf("\n\t\t以下是您要修改的联系人的纪录:");findmark++;printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否修改?(1代表修改;2代表不修改)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){xiugaixiangmu(i);printf("\n\t\t修改成功");printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);printf(" 请输入您的数字选择: ");if (j==1)xiugaixiangmu(i);break;}}}if (findmark==0){printf("\n\t\t对不起!没有该联系人的纪录!");printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibycelephone();return;}}void xiugaixiangmu(int a)//*修改项目*//{int i;system("cls");system("color c0");printf("\n\t\t 请输入您要修改的项目");printf("\n\t\t\n");printf("\n\t\t1-手机号码\n");printf("\n\t\t2-姓名\n");printf("\n\t\t3-家庭电话号码\n");printf("\n\t\t4-QQ号码\n");printf("\n\t\t5-地址\n");printf("\n\t\t6-邮编\n");printf("\n\t\t7-电子邮件\n");printf("\n\t\t8-回主菜单\n");printf("\n\t\t\n");printf(" 请输入您的数字选择: ");scanf("%d",&i);loop9: switch (i){case 1:printf("请输入手机号码:");scanf("%s",&per[a].celephone);break;case 2:printf("请输入姓名:");scanf("%s",&per[a].name);break;case 3:printf("请输入家庭电话号码:");scanf("%s",&per[a].homephone);break;case 4:printf("请输入QQ号码:");scanf("%s",&per[a].QQ);break;case 5:printf("请输入地址:");scanf("%s",&per[a].addr);break;case 6:printf("请输入邮编:");scanf("%s",&per[a].youbian);break;case 7:printf("请输入email:");scanf("%s",&per[a].email);break;case 8:mainmenu();break;default: printf("对不起!您的输入有误!请重新输入: ");goto loop9;}}void listmenu()//*排序菜单*//{int i,j;system("cls");system("color b0");if(num==0){printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}else{system("cls");system("color 12");printf("\n\t\t 排序菜单");printf("\n\t\t 1-按姓名排序");printf("\n\t\t 2-按手机号码排序");printf("\n\t\t 3-返回主菜单");printf(" 请输入您的数字选择: ");loop10: scanf("%d",&i);switch(i){case 1:listbyname();break;case 2:listbycelephone();break;case 3:mainmenu();break;default:printf("对不起!您的输入有误!请重新输入: ");goto loop10;}}}void listbyname()//*按姓名排序*//{int i,j,k;struct person tmp;for (i=1;i<num;i++){if(strcmp(per[i].name,per[i-1].name)<0){tmp=per[i];j=i-1;do{per[j+1]=per[j];j--;}while ((strcmp(,per[j].name)<0&&j>=0));per[j+1]=tmp;}}printf("\n\t\t排序成功,是否显示?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if (k==1)showall();return;}void listbycelephone()//*按手机号码排序*//{int i,j,k;struct person tmp;for (i=1;i<num;i++){if(strcmp(per[i].celephone,per[i-1].celephone)<0){tmp=per[i];j=i-1;do{per[j+1]=per[j];j--;}while ((strcmp(tmp.celephone,per[j].celephone)<0)&&j>=0);per[j+1]=tmp;}}printf("\n\t\t排序成功,是否显示?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if (k==1)showall();return;}void showall()//*输出全部信息*//{int i,j;system("cls");system("color 3b");if(num!=0){printf("\n\t\t*************** 以下为通讯录所有信息************");for (i=0;i<num;i++){printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\t\t");if (i+1<num){printf("\n\t\t__________________________");system("pause");}}printf("\n\t\t************************************************");}elseprintf("\n\t\t对不起!通讯录中无任何纪录!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}void writetofile()//*写入文件*//{int i,k;system("cls");system("color 84");if ((fp=fopen("per.bin","wb"))==NULL){printf("\n\t\t文件打开失败");}for (i=0;i<num;i++){if (fwrite(&per[i],sizeof(struct person),1,fp)!=1){printf("\n\t\t写入文件错误!\n");}}fclose(fp);printf("\n\t\t通讯录文件已保存");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&k);return;}void readfromfile()//*读取文件*//{int i,j,k;system("cls");system("color 2b");if((fp=fopen("per.bin","rb"))==NULL){printf("\n\t\t****************************");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 通讯录文件不存在! *");if ((fp=fopen("per.bin","wb"))==NULL){printf("\n* 建立失败! *");printf("\n* *");printf("\n* *");printf("\n****************************");exit(0);}else{printf("\n\t\t* 通讯录文件已建立! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t****************************");printf("\n\t\t 按任意键进入主菜单");printf("\n\t\t 请输入您的数字选择: ");scanf("%d",&k);return;}exit(0);}fseek(fp,0,2); //*文件位置指针移动到文件末尾*//if (ftell(fp)>0) //*文件不为空*//{rewind(fp); //*文件位置指针移动到文件开始位置*//for (num=0;!feof(fp) && fread(&per[num],sizeof(struct person),1,fp);num++);printf("\n\t\t****************************");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 文件导入成功! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t****************************");printf("\n\t\t 按1显示所有信息,按2回主菜单!");printf("\t\t 请输入您的数字选择: ");scanf("%d",&j);if(j==1)showall();}else{printf("\n\t\t****************************");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 文件导入成功! *");printf("\n\t\t* 通讯录文件中无任何纪录! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t****************************");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&i);return;}}void deleteall()//*删除所有信息*//{int i,j;system("cls");system("color 50");printf("\n\t\t确认删除?\n");printf("请按1和2(1代表确认;2代表不删除)\n ");scanf("%d",&i);if (i==1){fclose(fp);if ((fp=fopen("per.bin","wb"))==NULL){printf("\n\t\t不能打开文件,删除失败");readfromfile();}num=0;printf("\n\t\t纪录已删除!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}}。
头文件:/* 单链表中每个结点的联系人结构体定义*/typedef struct Node/* 每个联系人结构体*/{char name[10];//姓名char telnum[20];//电话号码char address[256];//地址struct Node *next;}SLNode;void Initiate(SLNode ** head)/* 初始化单链表*/{if((*head=(SLNode *)malloc(sizeof(SLNode)))== NULL){printf("内存空间不足!\n");exit(1) ;}(*head)->next=NULL;/* 置链尾标记NULL */}int Length(SLNode *head)//求当前通讯录中的联系人数{SLNode *p;/* p指向头结点*/int size;/* size初始值为0 */p = head->next;size = 0 ;while(p != NULL && head != NULL)/* 循环计数*/{k = k->next;size++;}return size;/* 返回其大小*/}void DataWrite(SLNode *head){SLNode *p;int i;FILE *fp;fp = fopen("Address list.dat","wb+");//打开文件,覆盖以前的文件p = head->next;for( i = 0;i < Length(head);i++){fwrite(p,sizeof(SLNode),1,fp);p =p->next;}fclose(fp);//关闭文件}void Load(SLNode *head)/* 初始化*/{SLNode *q,*p,*m;FILE *fp;int count = 0 ;fp = fopen("Address list.dat","rb");//以读的方式打开二进制文件//rewind(fp);if(fp == NULL){fp = fopen("Address list.dat","wb+");if(fp != NULL){printf("通讯录打开失败!创建新的通讯录!\n");}else{printf("通讯录打开失败!创建新的通讯录失败!\n");exit(0);}}else{q = (SLNode *)malloc(sizeof(SLNode ));while(fread(q,sizeof(SLNode),1,fp)){//fread(q,sizeof(SLNode),1,fp);if(ferror(fp)){printf("error!\n");break;}p = head;m = (SLNode *)malloc(sizeof(SLNode));strcpy(m->name,q->name);strcpy(m->telnum,q->telnum);strcpy(m->address,q->address);count++;m->next=p->next;/* 将新结点插入队头结点*/p->next=m;memset(q,'\0',sizeof(SLNode));}}fclose(fp);printf("\t\t\t\t 通讯录总联系人数:%d \n",Length(head));}int PrintAll(SLNode *head)/* 打印所有出所有联系人信息*/{int i;SLNode *q;q = head->next;if(q == NULL){printf("通讯录为空!\n");return -1;}else{for(i = 0 ; i< Length(head);i++)//逐个打印联系人信息{printf("编号:%d\t姓名:%s\t号码:%s\t地址:%s\n",i+1,q->name,q->telnum,q->address);q = q->next;}return 0;}}void ANParam(SLNode *head)/* 在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*//* 插入成功则返回1,失败则返回0 */SLNode *p,*q;q=(SLNode*)malloc(sizeof(SLNode));/* 生成新结点由指针p指示*/printf("请输入新联系人姓名:");fflush(stdin);gets(q->name);printf("请输入新联系人的电话号码:");fflush(stdin);gets(q->telnum);printf("请输入新联系人的地址:");fflush(stdin);gets(q->address);p = head;q->next=p->next;/* 将新结点插入队头结点*/p->next=q;DataWrite(head);//将信息以覆盖方式重新写入通讯录中printf("添加成功!\n");}void AWParam(SLNode *head,char telnum[11],char name[10],char address[256])//带形参的添加函数/* 在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*//* 插入成功则返回1,失败则返回0 */{SLNode *p,*q;q=(SLNode*)malloc(sizeof(SLNode));/* 生成新结点由指针p指示*/strcpy(q->name,name);strcpy(q->telnum,telnum);strcpy(q->address,address);p = head;q->next=p->next;/* 将新结点插入队头结点*/p->next=q;DataWrite(head);printf("添加成功!\n");}void Delete(SLNode *head)/* 删除带头结点的单链表head的第i(1~n,n为结点个数)个结点/* 删除结点的元素有x带回,删除成功返回1,否则返回0 */{SLNode *p,*s;int choice,i,m,ret;if(PrintAll(head))//如果通讯录为空,则无法删除,退出返回主菜单{return ;}m = Length(head);//求当前通讯录人数printf("请输入你要删除联系人的前面编号:");ret = scanf("%d",&choice);while(!ret || choice > m || choice < 1)//判断输入是否为数字,是否为,若否,则重新输入{printf("请正确输入你要删除联系人的前面编号!\n");fflush(stdin);ret = scanf("%d",&choice);}p=head;i = 0;//由于我们打印出来时是由1开始,所以此处定义为0while(p->next != NULL && p->next->next != NULL && i < (choice-1) )//查找所选择编号前一个结点{p = p->next;i++;}s = p->next;p->next = p->next->next;free(s);DataWrite(head);//将更新的数据重新写入文件中printf("删除成功!");}void SBName(SLNode *head)/* 查找函数,如果不存在,可添加*/{SLNode *p;int choice;char telnum[11], name[10], address[256];p=head->next;printf("请输入你要查找联系人的姓名:");fflush(stdin);gets(name);while(p != NULL){if(!strcmp(p->name,name)){printf("姓名:%s\t电话:%s\t地址:%s\n",p->name,p->telnum,p->address);break;}p=p->next;}if(p == NULL){printf("错误!所查找联系人不在通讯录,是否将该联系人添加至通讯录?\n");printf("请按0确认添加,否则不添加\n");scanf("%d",&choice);//判断输入是否为数字,且数字范围是1~m之间,否则重新输入if(!choice){printf("该联系人姓名:%s\n",name);printf("请输入该联系人的电话:\n");fflush(stdin);gets(telnum);printf("请输入该联系人的地址:\n");fflush(stdin);gets(address);AWParam(head,telnum,name,address);}}}void SBTell(SLNode *head)/* 查找函数,如果不存在,可添加*/{SLNode *p;int choice;char telnum[20], name[10], address[256];p=head->next;printf("请输入你要查找联系人的号码:");fflush(stdin);gets(telnum);while(p != NULL){if(!strcmp(p->telnum,telnum)){printf("姓名:%s\t电话:%s\t地址:%s\n",p->name,p->telnum,p->address);break;}p=p->next;}if(p == NULL){printf("错误!所查找联系人不在通讯录,是否将该联系人添加至通讯录?\n");printf("请按0确认添加,否则不添加\n");scanf("%d",&choice);if(!choice){printf("该联系人电话号码:%s\n",telnum);printf("请输入该联系人的姓名:\n");fflush(stdin);gets(name);printf("请输入该联系人的地址:\n");fflush(stdin);gets(address);AWParam(head,telnum,name,address);}}}void Destroy(SLNode **head)/* 因为单链表的结点空间是在程序运行中申请的,而系统只负责自动收回程序中静态分配的内存空间,所以在程序退出前释放动态申请的内存空间*/{SLNode *p,*p1;p= *head;while(p!=NULL)/* 通过循环释放所有的动态开辟的内存空间*/{p1=p;p=p->next;free(p1);}*head=NULL;}void Alter(SLNode *head){SLNode *p;int choice,num,i,m;if(PrintAll(head))//通讯录为空无法进行修改,退出返回主菜单{return ;}printf("请输入你要更改的信息人的前面编号:");fflush(stdin);m = Length(head);//获取当前通讯录人数i = scanf("%d",&num);//判断是否输入数字while( !i || num > m || num < 1 )//判断输入是否为数字,若否,则重新输入{printf("请正确输入你要更改的信息人的前面编号!\n");fflush(stdin);}p = head->next;//p指向第一个联系人for(i = 0 ; i< Length(head);i++){if((i+1) == num){printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address); lable1: printf("请输入你要更改的信息:\n");printf("1、姓名\t2、号码\t3、地址\t0、全部修改\n");m = scanf("%d",&choice);while(!m || choice > 3 || choice < 0)//判断输入是否为数字,若否,则重新输入{printf("请正确输入选项!\n");fflush(stdin);m = scanf("%d",&choice);}switch(choice){case 1:printf("请输入新姓名:");fflush(stdin);gets(p->name);break;case 2:printf("请输入新号码:");fflush(stdin);gets(p->telnum);break;case 3:printf("请输入新地址:");fflush(stdin);gets(p->address);break;default:printf("请输入新姓名:");fflush(stdin);gets(p->name);printf("请输入新号码:");fflush(stdin);gets(p->telnum);printf("请输入新地址:");fflush(stdin);gets(p->address);break;}printf("以下是刚更改的信息,请确认:");printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address);printf("是否继续更改该联系人?\n");printf("继续修改请按0,否则保存修改!\n");printf("输入:");scanf("%d",&choice);if(!choice){goto lable1;}else{DataWrite(head);//将修改后的文件全部写入通讯录中}}p = p->next;}}int menu(){int ret,i;printf("\t\t******************通讯录******************\n\n");printf("\t\t 1、查找联系人\t");printf("2、添加联系人\n\n");printf("\t\t 3、显示所有联系人信息\t");printf("4、删除联系人\n\n");printf("\t\t 5、更改联系人信息\t");printf("6、退出\n\n");printf("\t\t**************请选择相应功能**************\n");printf("\t\t输入对应功能编号:");i = scanf("%d",&ret);while(!i || ret > 6 || ret <1)//判断输入是否为数字,若否,则重新输入{printf("\t\t请正确输入功能编号!\n");fflush(stdin);printf("\t\t输入对应功能编号:");i = scanf("%d",&ret);}return ret;}void DeleteAll(SLNode *head){FILE *fp;fp = fopen("Address list.dat","wb+");//已创建新的文件覆盖以前的文件,一达到清除目的Destroy(&head);Initiate(&head);//初始化单链表,创建新的头结点printf("清除成功!\n");}测试主函数:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <windows.h>#include "LinkList.h"int main(){int choice = 0 ,ret;SLNode *head;char s[500] = "\t\t**谢谢使用**\n",a[] = "\t\t华中农业大学楚天学院通讯录系统\n";system("title 华中农业大学楚天学院通讯录系统");system("color 30");while(choice < strlen(a)){putchar(a[choice]);Sleep(80);choice++;}Initiate(&head);Load(head);while(1){switch(menu()){case 1:system("cls");if(head->next == NULL){printf("通讯录为空!\n");break ;}else{printf("请选择查找方式:");printf("1、姓名\t 2、电话号码\t 3、退出\n");ret = scanf("%d",&choice);while(!ret || choice >3 || choice <1){printf("输入有误!请正确选择查找方式!\n");fflush(stdin);ret = scanf("%d",&choice);}switch(choice){case 1:SBName(head);break;case 2:SBTell(head);break;case 3:break;default:break;}}break;case 2:system("cls");ANParam(head);break;case 3:system("cls");PrintAll(head);break;case 4:system("cls");printf("请选择删除方式:\n");printf("1、清空通讯录\t2、删除指定联系人\n");ret = scanf("%d",&choice);while(!ret || choice <1 || choice >2){printf("输入错误!请正确选择删除方式!\n");ret = scanf("%d",&choice);}if(choice == 1){DeleteAll(head);}else if(choice == 2){Delete(head);}break;case 5:system("cls");Alter(head);break;case 6:choice = 0;while(choice < strlen(s)){putchar(s[choice]);Sleep(80);choice++;}exit(0);break;default:printf("输入错误!请重新输入!\n");break;}}Destroy(&head);//清除动态申请空间system("pause");return 0;}。
通俗易懂java通讯录源代码简单易实现importimportimportimportimportimportimportimport class AddList {private String filePath = ""; private String bakPath = ""; private String content = "";Scanner sc = new Scanner;public String readFile(){content = "";if (isNull(filePath)) {"文件存储路径:");filePath = ();}File file = new File(filePath); FileReader fr = null;try {if ()) {fr = new FileReader(file);char[] chars = new char[1024];int n = 0;while((n = (chars)) != -1){String string = new String(chars, 0, n);content = content + string;}} else {"文件不存在");}} catch (Exception e) {();} finally {if (fr != null) {try {();} catch (IOException e) {();}}}return content;}public void writeFile(String path){File file = new File(path);FileOutputStream fos = null;mkDirs(path);try {fos = new FileOutputStream(file);BufferedOutputStream bos = new BufferedOutputStream(fos);PrintWriter pw = new PrintWriter(bos, true);(content);();} catch (FileNotFoundException e) {();} finally {if (fos != null) {try {();} catch (IOException e) {();}}}}public void writeFile(){if (isNull(filePath)) {"文件存储路径:");filePath = ();}File file = new File(filePath);FileOutputStream fos = null;mkDirs(filePath);try {fos = new FileOutputStream(file);BufferedOutputStream bos = new BufferedOutputStream(fos);PrintWriter pw = new PrintWriter(bos, true);(content);();} catch (FileNotFoundException e) {();} finally {if (fos != null) {try {();} catch (IOException e) {();}}}}public void mkDirs(String filepath){if ("\\") != -1) {filepath = ("\\", "/");}int n = ("ndexOf(name) != -1) {imfors[i] = "";}}content = "";for (int i = 0; i < ; i++) {if (!isNull(imfors[i])) {content = content + imfors[i] + "<==>";}}writeFile();"删除胜利");} else {"此人不存在");}"0、Exit 1、持续");int i = ();if (i == 0) {break;}}}public void viewAll(){"----------显现一切------------");content = readFile();if (!isNull(content)) {String[] imfors = ("<==>");"姓名\t电话\tEmail");for (int i = 0; i < ; i++) {String[] imfor = imfors[i].split("<>");for (int j = 0; j < ; j++) {+ "\t");}}} else {"暂时还没有记录");}}public void queryImfor(){"----------查找记录-----------");content = readFile();if (!isNull(content)) {String result = "";String[] imfors = null;String[] imfor = null;String name = "";boolean bool = false;while(true){result = "";"请输入关键字(按姓名查找):");name = ();bool = false;if (name) != -1) {imfors = ("<==>");for (int i = 0; i < ; i++) {if (imfors[i].indexOf(name) != -1) {imfor = imfors[i].split("<>");if (imfor[0].equals(name)) {bool = true;result = result + imfors[i] + "<==>";}}}if (bool) {imfors = ("<==>");"姓名\t电话\tEmail");for (int i = 0; i < ; i++) {imfor = imfors[i].split("<>");for (int j = 0; j < ; j++) {+ "\t");}}} else {"无此人信息");}} else {"无此人信息");}"0、Exit 1、持续");int i = ();if (i == 0) {break;}}} else {"文件还没有记录");}}public void copy(){"----------备份-----------");content = readFile();if (isNull(bakPath)) {"备份全路径:");bakPath = ();}writeFile(bakPath);"备份胜利");}public boolean isNull(String string){if (null == string || "" == string || 0 == ()) { return true;} else {return false;}}public static void main(String[] args) {AddList add = new AddList();Scanner sc = new Scanner;int operater = 0;while(true){"选择功用:\n1、增加记录 2、删除记录 3、显现一切 4、查询记录 5、备份 6、退出");operater = ();if (1 == operater) {();} else if (2 == operater) {();} else if (3 == operater) {();} else if (4 == operater) {();} else if (5 == operater) {();} else if (6 == operater) {"谢谢使用");break;}}}}。
大一c语言通讯录管理系统源代码以下是一个简单的C语言通讯录管理系统的源代码示例:#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_CONTACTS 100// 定义联系人结构体typedef struct {char name[50];char phone[20];char email[50];} Contact;// 全局通讯录数组Contact contacts[MAX_CONTACTS];int numContacts = 0;// 添加联系人void addContact() {if (numContacts >= MAX_CONTACTS) {printf("通讯录已满,无法添加新联系人。
\n");return;}Contact newContact;printf("请输入联系人姓名:");scanf("%s", );printf("请输入联系人电话:");scanf("%s", newContact.phone);printf("请输入联系人邮箱:");scanf("%s", newContact.email);contacts[numContacts] = newContact; numContacts++;printf("联系人添加成功。
\n");}// 显示所有联系人void displayContacts() {if (numContacts == 0) {printf("通讯录为空。
\n");return;}printf("所有联系人:\n");for (int i = 0; i < numContacts; i++) {printf("姓名:%s\t电话:%s\t邮箱:%s\n", contacts[i].name, contacts[i].phone, contacts[i].email); }}// 主菜单void showMenu() {printf("******************************\n"); printf("***** 通讯录管理系统*****\n"); printf("******************************\n"); printf("请选择操作:\n");printf("1. 添加联系人\n");printf("2. 显示所有联系人\n");printf("0. 退出\n");printf("******************************\n"); }int main() {int choice;while (1) {showMenu();printf("请选择操作(输入数字):");scanf("%d", &choice);switch (choice) {case 0:printf("已退出通讯录管理系统。