Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43763
  • 博文数量: 11
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 126
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-11 23:27
文章分类

全部博文(11)

文章存档

2014年(2)

2013年(9)

我的朋友

分类: C/C++

2013-11-26 23:34:03

C++程序   名字搜索与程序标记


一:

下面的程序要求用户输入一个名字,然后程序将在用于保存名字的vector中进行搜索以判断vector中是否包含这一名字。如果包含,则程序输出这一名字的含义。如果vector中不包含这一名字,则程序将告诉用户:没有这个名字。



点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>

  4. using namespace std;

  5. int main()
  6. {
  7.     cout << "Welcome to the C++ Name Search Programe.";

  8.     //构造两个用于存放string的vector
  9.     vector<string> names;
  10.     vector<string> origins;

  11.     //将数据存入vector
  12.     names.push_back("Barbara");
  13.     origins.push_back("greek,meaning stranger.");

  14.     names.push_back("Kelly");
  15.     origins.push_back("celtic,meaning warrior or defender.");

  16.     names.push_back("Claire");
  17.     origins.push_back("french,meaning bright and clear");

  18.     //推测使用的变量
  19.     string answer,userName;

  20.     do
  21.     {
  22.         bool bDidNotFindIt = true;

  23.         cout << "\nPlease enter a name,such as Kelly: ";
  24.         getline(cin,userName);

  25.         int index = 0;
  26.         while(index < names.size() && bDidNotFindIt == true)
  27.         {
  28.             if(userName == names.at(index))
  29.             {
  30.                 cout << "\n Here is your name: " << names.at(index);
  31.                 cout << "\n Origin: " << origins.at(index)<< endl;

  32.                 bDidNotFindIt = false;
  33.             }
  34.             else
  35.             {
  36.                 ++index;
  37.             }
  38.         }

  39.         if(bDidNotFindIt)
  40.         {
  41.             cout << "\n Sorry. "<< userName << " isn't in voctor. "<< endl;
  42.         }

  43.         cout << "Do anther name? yes/no" << endl;
  44.         getline(cin,answer);
  45.     }while(answer == "yes");
  46.     return 0;
  47. }

输出结果为:

点击(此处)折叠或打开

  1. Welcome to the C++ Name Search Programe.
    Please enter a name,such as Kelly: Kelly


     Here is your name: Kelly
     Origin: celtic,meaning warrior or defender.
    Do anther name? yes/no
    yes


    Please enter a name,such as Kelly: Barbara


     Here is your name: Barbara
     Origin: greek,meaning stranger.
    Do anther name? yes/no
    yes


    Please enter a name,such as Kelly: aa


     Sorry. aa isn't in voctor.
    Do anther name? yes/no
    no
    请按任意键继续. . .




二:

下面使用指针和引用来重新实现一次上面的程序。定义两个使用参数的函数:FillVectors以及SearchFcrName,还定义了一个AskUserForName函数,该函数输入输出都为string对象。程序有三个文件组成。仔细查看源代码,注意程序是如何在NameFunctions.h中把string和vector库包含进来的。之所以在该函数中包含这两个库,是因为我们要在函数的原型声明中用到string和vector。一定要根据代码文件的需要来进行类库的包含操作,不需要多,但也不能少。

NameFunctions.h:

点击(此处)折叠或打开

  1. //NameFunctions.h

  2. #include<string>
  3. #include<vector>
  4. using namespace std;

  5. void FillVectors(vector<string> &names, vector<string> &origins);

  6. string AskUserForName();

  7. bool SearchForName(vector<string> &names, vector<string> &origins, string &userName, string &nameOrigin );

NameFunctions.cpp

点击(此处)折叠或打开

  1. //NameFunctions.cpp中含有在NameFunctions.h中声明的函数的定义

  2. #include<iostream>
  3. #include<string>
  4. #include<vector>
  5. using namespace std;

  6. bool SearchForName(vector<string> &names, vector<string> &origins, string &userName, string &nameOrigin )
  7. {
  8.     //在vector中进行查找,如果找到则程序结束
  9.     int index = 0;
  10.         while(index < names.size())
  11.         {
  12.             if(userName == names.at(index))
  13.             {
  14.                 //找到后对nameOrigin赋值并返回main
  15.                 nameOrigin = origins.at(index);
  16.                 return true;

  17.             }
  18.             else
  19.             {
  20.                 ++index;
  21.             }

  22.             //如果程序执行到这里则说明没有找到
  23.             return false;
  24.         }
  25. }


  26. void FillVectors(vector<string> &names, vector<string> &origins)
  27. {
  28.     //将名字和含义储存在vector中,这些是main中的vector,我们有对它们进行引用的变量
  29.     names.push_back("Barbara");
  30.     origins.push_back("greek,meaning stranger.");

  31.     names.push_back("Kelly");
  32.     origins.push_back("celtic,meaning warrior or defender.");

  33.     names.push_back("Claire");
  34.     origins.push_back("french,meaning bright and clear");
  35. }

  36. string AskUserForName()
  37. {
  38.     string uName;
  39.     cout << "\nPlease enter a name,such as Kelly: ";
  40.     getline(cin,uName);
  41.     return uName;
  42. }

main.cpp:

点击(此处)折叠或打开

  1. //程序提示用户输入一个名字,在vector中查找这个名字,如果有,则输出该名字的含义

  2. #include<iostream>
  3. #include<string>
  4. #include<vector>
  5. using namespace std;

  6. #include "NameFunctions.h"

  7. int main()
  8. {
  9.     cout <<"\n Welcome to the C++ Name Search Programe.";
  10.      //构造两个用于存放string的vector
  11.     vector<string> names;
  12.     vector<string> origins;

  13.     //调用函数来对着两个vector进行赋值
  14.     FillVectors(names,origins);

  15.     //声明用来储存名字和结果的变量
  16.     string userName, nameOrigin, answer;
  17.     bool bFoundIt;
  18.     
  19.     do
  20.     {
  21.         userName = AskUserForName();

  22.         //bFoundIt指示名字找到
  23.         bFoundIt = SearchForName(names,origins,userName,nameOrigin);
  24.         if(bFoundIt)
  25.         {
  26.             cout << "\n Here is your name: " << userName;
  27.             cout << "\n origin: " << nameOrigin << endl;
  28.         }
  29.         else
  30.         {
  31.              cout << "\n Sorry. "<< userName << " isn't in voctor. "<< endl;
  32.         }

  33.         cout << "Do anther name? yes/no ";
  34.         getline(cin,answer);

  35.     }while(answer == "yes");

  36.     return 0;
  37. }
输出结果为:

点击(此处)折叠或打开

  1. Welcome to the C++ Name Search Programe.
  2. Please enter a name,such as Kelly: Kelly

  3.  Sorry. Kelly isn't in voctor.
  4. Do anther name? yes/no yes

  5. Please enter a name,such as Kelly: aaa

  6.  Sorry. aaa isn't in voctor.
  7. Do anther name? yes/no no
  8. 请按任意键继续. . .





























阅读(1289) | 评论(0) | 转发(0) |
0

上一篇:queue类

下一篇:数组

给主人留下些什么吧!~~