5_42.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:2k
源码类别:

文章/文档

开发平台:

C/C++

  1. # include<iostream.h>
  2. #include <iomanip.h>
  3. # include<string.h>
  4. struct date
  5. {
  6. int year; int month; int day;
  7. };
  8. struct address
  9. {
  10. char name[10]; char sex;char tel[12]; date birthday;
  11. };
  12. void input(address myfriend[],int &n)
  13. {
  14. int i=0;
  15. char name[10];
  16. cout<<"Input my friends'name ,with'#' to end:"<<endl;
  17. cin>>name;
  18. while (strcmp(name,"#")!=0)
  19. {
  20. strcpy(myfriend[i].name,name);
  21. cout<<"Continue to input my friends'sex(F/M),tel and birthday:"<<endl;
  22.         cin>>myfriend[i].sex;
  23. cin>>myfriend[i].tel;
  24. cin>>myfriend[i].birthday.year>>myfriend[i].birthday.month>>
  25. myfriend[i].birthday.day;
  26. i++;
  27. cout<<"n input name,with'#' to end:"<<endl;
  28. cin>>name;
  29. }
  30. n=i;
  31. }
  32. void output(address myfriend[],int n)
  33. {
  34. int i;
  35. cout<<setw(11)<<"name"<<setw(5)<<"sex"<<setw(10)<<"telNO."
  36. <<setw(16)<<"birthday"<<endl;
  37. for(i=0;i<n;i++)
  38. {
  39. cout<<setw(11)<<myfriend[i].name<<setw(4)<<myfriend[i].sex<<setw(16)
  40. <<myfriend[i].tel<<"  ";
  41. cout<<setw(5)<<myfriend[i].birthday.year;
  42.         cout<<setw(3)<<myfriend[i].birthday.month<<"  ";
  43. cout<<setw(3)<<myfriend[i].birthday.day<<endl;
  44. }
  45. cout<<endl;
  46. }
  47. int find(address myfriend[],int n,char findname[])
  48. {
  49. int i;
  50. for (i=0;i<n;i++)
  51. if(strcmp(myfriend[i].name,findname)==0)
  52. return i;
  53. return -1;
  54. }
  55. void main()
  56. {
  57. address myfriend[50];
  58. int n;
  59. int index;
  60. char findname[10];
  61. input(myfriend,n);
  62. output(myfriend,n);
  63. cout<<"find name?";
  64. cin>>findname;
  65. index=find(myfriend,n,findname);
  66. if(index==-1)
  67. cout<<" NO find!"<<endl;
  68. else
  69. {
  70. cout<<endl;
  71.        cout<<setw(11)<<"name"<<setw(5)<<"sex"<<setw(10)<<"telNO."
  72. <<setw(16)<<"birthday"<<endl;
  73.         cout<<setw(11)<<myfriend[index].name<<setw(4)<<myfriend[index].sex
  74. <<setw(16)<<myfriend[index].tel<<"  ";
  75. cout<<setw(5)<<myfriend[index].birthday.year;
  76.         cout<<setw(3)<<myfriend[index].birthday.month;
  77. cout<<setw(3)<<myfriend[index].birthday.day<<endl;
  78. }
  79. }