C++程序 名字搜索与程序标记
一:
下面的程序要求用户输入一个名字,然后程序将在用于保存名字的vector中进行搜索以判断vector中是否包含这一名字。如果包含,则程序输出这一名字的含义。如果vector中不包含这一名字,则程序将告诉用户:没有这个名字。
-
#include<iostream>
-
#include<string>
-
#include<vector>
-
-
using namespace std;
-
-
int main()
-
{
-
cout << "Welcome to the C++ Name Search Programe.";
-
-
//构造两个用于存放string的vector
-
vector<string> names;
-
vector<string> origins;
-
-
//将数据存入vector
-
names.push_back("Barbara");
-
origins.push_back("greek,meaning stranger.");
-
-
names.push_back("Kelly");
-
origins.push_back("celtic,meaning warrior or defender.");
-
-
names.push_back("Claire");
-
origins.push_back("french,meaning bright and clear");
-
-
//推测使用的变量
-
string answer,userName;
-
-
do
-
{
-
bool bDidNotFindIt = true;
-
-
cout << "\nPlease enter a name,such as Kelly: ";
-
getline(cin,userName);
-
-
int index = 0;
-
while(index < names.size() && bDidNotFindIt == true)
-
{
-
if(userName == names.at(index))
-
{
-
cout << "\n Here is your name: " << names.at(index);
-
cout << "\n Origin: " << origins.at(index)<< endl;
-
-
bDidNotFindIt = false;
-
}
-
else
-
{
-
++index;
-
}
-
}
-
-
if(bDidNotFindIt)
-
{
-
cout << "\n Sorry. "<< userName << " isn't in voctor. "<< endl;
-
}
-
-
cout << "Do anther name? yes/no" << endl;
-
getline(cin,answer);
-
}while(answer == "yes");
-
return 0;
-
}
输出结果为:
-
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:
-
//NameFunctions.h
-
-
#include<string>
-
#include<vector>
-
using namespace std;
-
-
void FillVectors(vector<string> &names, vector<string> &origins);
-
-
string AskUserForName();
-
-
bool SearchForName(vector<string> &names, vector<string> &origins, string &userName, string &nameOrigin );
NameFunctions.cpp
-
//NameFunctions.cpp中含有在NameFunctions.h中声明的函数的定义
-
-
#include<iostream>
-
#include<string>
-
#include<vector>
-
using namespace std;
-
-
bool SearchForName(vector<string> &names, vector<string> &origins, string &userName, string &nameOrigin )
-
{
-
//在vector中进行查找,如果找到则程序结束
-
int index = 0;
-
while(index < names.size())
-
{
-
if(userName == names.at(index))
-
{
-
//找到后对nameOrigin赋值并返回main
-
nameOrigin = origins.at(index);
-
return true;
-
-
}
-
else
-
{
-
++index;
-
}
-
-
//如果程序执行到这里则说明没有找到
-
return false;
-
}
-
}
-
-
-
void FillVectors(vector<string> &names, vector<string> &origins)
-
{
-
//将名字和含义储存在vector中,这些是main中的vector,我们有对它们进行引用的变量
-
names.push_back("Barbara");
-
origins.push_back("greek,meaning stranger.");
-
-
names.push_back("Kelly");
-
origins.push_back("celtic,meaning warrior or defender.");
-
-
names.push_back("Claire");
-
origins.push_back("french,meaning bright and clear");
-
}
-
-
string AskUserForName()
-
{
-
string uName;
-
cout << "\nPlease enter a name,such as Kelly: ";
-
getline(cin,uName);
-
return uName;
-
}
main.cpp:
-
//程序提示用户输入一个名字,在vector中查找这个名字,如果有,则输出该名字的含义
-
-
#include<iostream>
-
#include<string>
-
#include<vector>
-
using namespace std;
-
-
#include "NameFunctions.h"
-
-
int main()
-
{
-
cout <<"\n Welcome to the C++ Name Search Programe.";
-
//构造两个用于存放string的vector
-
vector<string> names;
-
vector<string> origins;
-
-
//调用函数来对着两个vector进行赋值
-
FillVectors(names,origins);
-
-
//声明用来储存名字和结果的变量
-
string userName, nameOrigin, answer;
-
bool bFoundIt;
-
-
do
-
{
-
userName = AskUserForName();
-
-
//bFoundIt指示名字找到
-
bFoundIt = SearchForName(names,origins,userName,nameOrigin);
-
if(bFoundIt)
-
{
-
cout << "\n Here is your name: " << userName;
-
cout << "\n origin: " << nameOrigin << endl;
-
}
-
else
-
{
-
cout << "\n Sorry. "<< userName << " isn't in voctor. "<< endl;
-
}
-
-
cout << "Do anther name? yes/no ";
-
getline(cin,answer);
-
-
}while(answer == "yes");
-
-
return 0;
-
}
输出结果为:
-
Welcome to the C++ Name Search Programe.
-
Please enter a name,such as Kelly: Kelly
-
-
Sorry. Kelly isn't in voctor.
-
Do anther name? yes/no yes
-
-
Please enter a name,such as Kelly: aaa
-
-
Sorry. aaa isn't in voctor.
-
Do anther name? yes/no no
-
请按任意键继续. . .
阅读(1289) | 评论(0) | 转发(0) |