记录跳转

第零部分:C++ 练习汇总
第一部分:C++学习记录 基础篇
第二部分:C++学习记录 核心篇


基础篇习题

图片-1659787427191

struct person
{
 int id;
 string name;
 string sex;
 int age;
 string tel;
 string address;
};

struct book
{
 person pArray[999];
 int size;
};

void menu()
{
cout<<"\t欢迎使用通讯录管理系统!"<<endl<<endl;
cout<<"\t1.添加联系人"<<endl;
cout<<"\t2.显示联系人"<<endl;
cout<<"\t3.删除联系人"<<endl;
cout<<"\t4.查找联系人"<<endl;
cout<<"\t5.修改联系人"<<endl;
cout<<"\t6.清空联系人"<<endl;
cout<<"\t0.退出通讯录"<<endl<<endl;
cout<<"请输入序号!"<<endl;
}

void add(book* Telbook)
{
  if ( Telbook->size > 1000)
  {
    cout<<"通讯录人员大于1000人,无法添加!"<<endl;
    return ;
   }
  else
   {
    Telbook->pArray[Telbook->size].id = Telbook->size +1;
    cout<<endl<<"请输入联系人姓名:"<<endl;     
    cin>>Telbook->pArray[Telbook->size].name;
    cout<<"请输入联系人性别:"<<endl;
    cin>>Telbook->pArray[Telbook->size].sex;
    cout<<"请输入联系人年龄:"<<endl;
    cin>>Telbook->pArray[Telbook->size].age;
    cout<<"请输入联系人电话:"<<endl;
    cin>>Telbook->pArray[Telbook->size].tel;
    cout<<"请输入联系人住址:"<<endl;
    cin>>Telbook->pArray[Telbook->size].address;
    Telbook->size ++;
    cout<<endl<<"添加成功!"<<endl;
   }
  system("pause");
  return ;
}

void printinfo(book Telbook)
{
 if( Telbook.size == 0)
 {
   cout<<"当前记录为空!"<<endl;
 }
 else
 {
  cout<<"ID:"<<"\t姓名:"<<"\t性别:"<<"\t年龄:"<<"\t电话:"<<"\t\t住址:"<<endl;
  for(int c = 0; c<Telbook.size; c++)
  {
   cout <<Telbook.pArray[c].id<<"\t"<<Telbook.pArray[c].name<<"\t"<<Telbook.pArray[c].sex<<"\t"<<Telbook.pArray[c].age<<"\t"<<Telbook.pArray[c].tel<<"\t"<<Telbook.pArray[c].address<<endl;
  }
 }
}

void delet(book* Telbook, string e)
{
 int f = 0;
 int j = 1;
 while(f<=Telbook->size-1)
 {
  if( e == Telbook->pArray[f].name )
  {
List5:  
   cout<<endl<<"已定位ID"<<f+1<<":"<<e<<",输入“y/n”执行“删除/保留”操作!"<<endl;
   char g;
   cin>>g;
   switch(g)
   {
    case 'y':
    for(int h = f;h<Telbook->size;h++)   
    {
      Telbook->pArray[h] = Telbook->pArray[h+1];
    }
    Telbook->size --;
    Telbook->pArray[0].id = 1;
    for (int i=0; i <= Telbook->size ; i++)
	{
	 Telbook->pArray[i].id = j;
	  j++;
     }
	 cout <<endl<< "已完成删除操作!" << endl;
	 return;
    case 'n':
      cout<<endl<<"已取消删除操作!"<<endl;
      return ;
    default:
      cout<<endl<<"请勿输入y/n以外的字符!"<<endl;
     goto List5;
   }
  }
  f++;
 }
  cout<<endl<<"未找到该联系人!"<<endl;
  return;
}

void find(book Telbook , string j)
{
 int k = 0;
 while(k<=Telbook.size-1)
 {
  if(j == Telbook.pArray[k].name)
  {
   cout <<endl<<"ID:"<<Telbook.pArray[k].id<<endl<<"姓名:"<<Telbook.pArray[k].name<<endl<<"性别:"<<Telbook.pArray[k].sex<<endl<<"年龄:"<<Telbook.pArray[k].age<<endl<<"电话:"<<Telbook.pArray[k].tel<<endl<<"住址:"<<Telbook.pArray[k].address<<endl<<endl;
   return;
  }
  k++;
 }
 cout<<endl<<"未找到该联系人!"<<endl;
 return;
}

void revise(book* Telbook , string l)
{
 int m = 0;
 while(m<=Telbook->size-1)
 {
  if(l == Telbook->pArray[m].name)
  {
   cout<<endl<<"已找到"<<l<<endl;
   cout<<"请输入修改后姓名:"<<endl;     
    cin>>Telbook->pArray[m].name;
    cout<<"请输入修改后性别:"<<endl;
    cin>>Telbook->pArray[m].sex;
    cout<<"请输入修改后年龄:"<<endl;
    cin>>Telbook->pArray[m].age;
    cout<<"请输入修改后电话:"<<endl;
    cin>>Telbook->pArray[m].tel;
    cout<<"请输入修改后住址:"<<endl;
    cin>>Telbook->pArray[m].address;
    cout<<endl<<"修改成功!"<<endl;
   }
   return;
   m++;
  }
 cout<<endl<<"未找到该联系人,无法修改!"<<endl;
 return;
}

-----main-----

int select;
book Telbook;
Telbook.size = 0;
char d;
string e;
int f;
string j;
string l;
char n;

List1:
menu();
List2:
cin>>select;
switch(select)
{
 case 1:
   add(&Telbook);
   goto List3;
 case 2:
   printinfo(Telbook);
List3:
   cout<<endl<<"输入“y/n“决定“返回/退出”管理系统!"<<endl;
List4:
   cin >> d;
   switch(d)
   {
    case 'y':
      system("cls");
      goto List1;
    case 'n':
      cout<<endl<<"欢迎下次使用!"<<endl;
      system("pause");
      return 0;
    default:
      cout<<endl<<"请勿输入y/n以外的字符!"<<endl;
     goto List4;
   }
 case 3:
   cout<<endl <<"请输入要删除的联系人姓名:"<<endl;
   cin>>e;
   delet(&Telbook , e);
   goto List3;
 case 4:
   cout<<endl <<"请输入要查找的联系人姓名:"<<endl;
   cin>>j;
   find(Telbook , j);
   goto List3;
 case 5:
   cout<<endl <<"请输入要修改的联系人姓名:"<<endl;
   cin>>l;
   revise(&Telbook , l);
   goto List3;
 case 6:
List6:
   cout<<endl <<"目前共有"<<Telbook.size<<"位联系人,输入“y/n”执行“清除/保留”!"<<endl;
   cin>>n;
   if(n== 'y')
   {
    Telbook.size = 0;
    cout<<endl<<"已清空所有联系人!"<<endl;
    goto List3;
    }
    else if(n == 'n')
    {
     cout<<endl<<"已取消清空通讯录操作!"<<endl;
     goto List3;
    }
    else
    {
     cout<<"请勿输入y/n以外的字符!"<<endl;
     goto List6;
    }
 case 0:
   cout<<endl <<"欢迎下次使用!"<<endl;
   system("pause");
   return 0;
 default:
   cout<<endl <<"未找到相关功能,请重新输入序号!"<<endl;
   goto List2;
}

注意:

  1. for循环括号内用分号分隔
  2. 结构体数组初始化多个string数据类型都要分别要加引号
  3. 字符型变量的值加单引号,字符串型变量的值变量加双引号
  4. 尽量把所有功能封装成多个函数
  5. 电话类数据用string储存,因为太长了
  6. &Telbook:添加功能的实质是形参改变实参,因此使用地址传递
  7. system(“cls”):清屏
  8. switch中每个case下都加break才能实现根据case筛选单个后续流程。
    若一个case下不加break则进行完该case内流程后无视它后面所有case条件执行所有case中的所有流程直到遇到break
  9. 在循环或者是选择条件时:==才是相等的意思,用于判断
  10. case语段中若语句过多,则要在头尾加花括号