C++中set容器

#include<iostream>#include<stdlib.h>#include<stdio.h>#include<time.h>#include<string>... #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string>
#include<vector>
#include<set>
using namespace std;
string randArray[30]={"programming", //declare a array that contain the 30 words
"pop quiz",
"assignment",
"reference drive",
"visual art",
"visual studio",
"web development",
"digital entertainment system",
"photoshop",
"maya",
"lighting",
"internet",
"computer",
"laptop",
"practical",
"lecture",
"lab group",
"tutorial",
"block m",
"array",
"pointers",
"inspiration",
"creativity",
"variables",
"potential",
"imagination",
"production",
"instincts",
"entrepreneurial",
"effects"
};

void checkInput(char useInput, string str); // declare the function first
int score = 5;//declare the variable and the first value is 5
set <char> inside;//declare the vector to save the character which is correct
set <char> outside;//declare the vector to save the character which is not correct
void outputLetter ()//this function is used to print the variable which is saved in the vector
{
set<char>::iterator pos1;
cout << "Characters used: ";
for (pos1 = inside.begin();pos1 != inside.end();pos1++)//for loop
cout << *pos1<< " ";
set<char>::iterator pos2;
cout << endl;
cout << "Characters not used: ";
for (pos2 = outside.begin();pos2 != outside.end();pos2++)//for loop
cout << *pos2 << " ";
cout << endl;
}
void getRand()//this function is used to catch a letter randomization
{
int k;//declare the variable
char input;
srand((unsigned)time(NULL));//to make the seed equal time of system

k = rand()%30;//just get first 30 numbers
cout << "Please guess this word ."<<endl;
cout << "You can enter a letter in one time."<<endl;
for (int i = 0 ;i < randArray[k].length();i++)//it means how many character the letter have , then output how many "_".
{

cout << "_";
cout << " ";

}
cout << randArray[k]<<endl;
while (score!= 0){// i declare a variable which is used to jude the whether the game is over
cout << "Your input:";
cin >> input;
checkInput(input,randArray[k]);
}

}
void checkInput(char userInput, string str)//the function is used to check the character whether is correct
{
for (int i = 0;i < str.length();i++)
{
if (userInput == str[i])
{
cout << "Right!" << endl;
inside.insert(userInput);//save data in the inside vector
outputLetter();
return;
}

}
cout << "Wrong!" << endl;
outside.insert(userInput);//save data in the outside vector
outputLetter();
score--;//if the player enter a wrong character , the score will get decrease , i get the first value is 5
if (score == 0)
{
cout << "Sorry ,Game Over!"<<endl;
}
else
cout << "You only have " << score << " chance to guess the word."<<endl;
}
void main ()//the main function
{
getRand();
system("pause");
}
要怎么修改才能实现当用户输入相同字母的时候,提示错误警告?
展开
 我来答
手机用户67326
2009-08-26 · TA获得超过245个赞
知道小有建树答主
回答量:223
采纳率:0%
帮助的人:207万
展开全部
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string>
#include<vector>
#include<set>
using namespace std;
string randArray[30]={"programming", //declare a array that contain the 30 words
"pop quiz",
"assignment",
"reference drive",
"visual art",
"visual studio",
"web development",
"digital entertainment system",
"photoshop",
"maya",
"lighting",
"internet",
"computer",
"laptop",
"practical",
"lecture",
"lab group",
"tutorial",
"block m",
"array",
"pointers",
"inspiration",
"creativity",
"variables",
"potential",
"imagination",
"production",
"instincts",
"entrepreneurial",
"effects"
};

void checkInput(char useInput, string str); // declare the function first
int score = 5;//declare the variable and the first value is 5
set <char> inside;//declare the vector to save the character which is correct
set <char> outside;//declare the vector to save the character which is not correct
void outputLetter ()//this function is used to print the variable which is saved in the vector
{
set<char>::iterator pos1;
cout << "Characters used: ";
for (pos1 = inside.begin();pos1 != inside.end();pos1++)//for loop
cout << *pos1<< " ";
set<char>::iterator pos2;
cout << endl;
cout << "Characters not used: ";
for (pos2 = outside.begin();pos2 != outside.end();pos2++)//for loop
cout << *pos2 << " ";
cout << endl;
}

void getRand()//this function is used to catch a letter randomization
{
int k;//declare the variable
char input;
srand((unsigned)time(NULL));//to make the seed equal time of system

k = rand()%30;//just get first 30 numbers
cout << "Please guess this word ."<<endl;
cout << "You can enter a letter in one time."<<endl;
for (int i = 0 ;i < randArray[k].length();i++)//it means how many character the letter have , then output how many "_".
{

cout << "_";
cout << " ";

}
cout << randArray[k]<<endl;
while (score!= 0){// i declare a variable which is used to jude the whether the game is over
cout << "Your input:";
cin >> input;

///////////////////////////////////////////
////////////////////////////////////////////
while(1)
{
if(inside.find(input)!=inside.end())
{
cout<<"You have inputed the characte:"<<input<<endl;
cout<<"Please input a character:";
cin>>input;
}
else
break;
}
///////////////////////////////////////////
////////////////////////////////////////////

checkInput(input,randArray[k]);
}

}
void checkInput(char userInput, string str)//the function is used to check the character whether is correct
{
for (int i = 0;i < str.length();i++)
{
if (userInput == str[i])
{
cout << "Right!" << endl;
inside.insert(userInput);//save data in the inside vector
outputLetter();
return;
}

}
cout << "Wrong!" << endl;
outside.insert(userInput);//save data in the outside vector
outputLetter();
score--;//if the player enter a wrong character , the score will get decrease , i get the first value is 5
if (score == 0)
{
cout << "Sorry ,Game Over!"<<endl;
}
else
cout << "You only have " << score << " chance to guess the word."<<endl;
}

void main ()//the main function
{
getRand();
system("pause");
}
tang803397
2009-08-26 · TA获得超过826个赞
知道小有建树答主
回答量:811
采纳率:0%
帮助的人:772万
展开全部
void checkInput(char userInput, string str)//the function is used to check the character whether is correct
{
for (int i = 0;i < str.length();i++)
{
if (userInput == str[i])
{
cout << "Right!" << endl;
inside.insert(userInput);//save data in the inside vector
outputLetter();
return;
}

}
cout << "Wrong!" << endl;
if(checkrepead(userInput))//在这里进行重复处理
{//下面全包在括号里 这样重复输入就不减分了
outside.insert(userInput);//save data in the outside vector
outputLetter();
score--;//if the player enter a wrong character , the score will get decrease , i get the first value is 5
if (score == 0)
{
cout << "Sorry ,Game Over!"<<endl;
}
else
cout << "You only have " << score << " chance to guess the word."<<endl;
}
}
bool checkrepead(char w)//新添加以个bool型的函数进行判断
{
set<char>::iterator pos1;
for (pos1 = outside.begin();pos1 != outside.end();pos1++)//for loop
if( *pos1==w)
{
cout << "error! repead"<<endl;
return false;
}
return true;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
科技云享
2019-06-10 · TA获得超过3614个赞
知道大有可为答主
回答量:3149
采纳率:26%
帮助的人:197万
展开全部
#include
<iostream>
#include
<stdlib.h>
#include
<stdio.h>
#include
<time.h>
#include
<string>
#include<vector>
#include<set>
using
namespace
std;
string
randArray[30]={"programming",
//declare
a
array
that
contain
the
30
words
"pop
quiz",
"assignment",
"reference
drive",
"visual
art",
"visual
studio",
"web
development",
"digital
entertainment
system",
"photoshop",
"maya",
"lighting",
"internet",
"computer",
"laptop",
"practical",
"lecture",
"lab
group",
"tutorial",
"block
m",
"array",
"pointers",
"inspiration",
"creativity",
"variables",
"potential",
"imagination",
"production",
"instincts",
"entrepreneurial",
"effects"
};
void
checkInput(char
useInput,
string
str);
//
declare
the
function
first
int
score
=
5;//declare
the
variable
and
the
first
value
is
5
set
<char>
inside;//declare
the
vector
to
save
the
character
which
is
correct
set
<char>
outside;//declare
the
vector
to
save
the
character
which
is
not
correct
void
outputLetter
()//this
function
is
used
to
print
the
variable
which
is
saved
in
the
vector
{
set<char>::iterator
pos1;
cout
<<
"Characters
used:
";
for
(pos1
=
inside.begin();pos1
!=
inside.end();pos1++)//for
loop
cout
<<
*pos1<<
"
";
set<char>::iterator
pos2;
cout
<<
endl;
cout
<<
"Characters
not
used:
";
for
(pos2
=
outside.begin();pos2
!=
outside.end();pos2++)//for
loop
cout
<<
*pos2
<<
"
";
cout
<<
endl;
}
void
getRand()//this
function
is
used
to
catch
a
letter
randomization
{
int
k;//declare
the
variable
char
input;
srand((unsigned)time(NULL));//to
make
the
seed
equal
time
of
system
k
=
rand()%30;//just
get
first
30
numbers
cout
<<
"Please
guess
this
word
."<<endl;
cout
<<
"You
can
enter
a
letter
in
one
time."<<endl;
for
(int
i
=
0
;i
<
randArray[k].length();i++)//it
means
how
many
character
the
letter
have
,
then
output
how
many
"_".
{
cout
<<
"_";
cout
<<
"
";
}
cout
<<
randArray[k]<<endl;
while
(score!=
0){//
i
declare
a
variable
which
is
used
to
jude
the
whether
the
game
is
over
cout
<<
"Your
input:";
cin
>>
input;
///////////////////////////////////////////
////////////////////////////////////////////
while(1)
{
if(inside.find(input)!=inside.end())
{
cout<<"You
have
inputed
the
characte:"<<input<<endl;
cout<<"Please
input
a
character:";
cin>>input;
}
else
break;
}
///////////////////////////////////////////
////////////////////////////////////////////
checkInput(input,randArray[k]);
}
}
void
checkInput(char
userInput,
string
str)//the
function
is
used
to
check
the
character
whether
is
correct
{
for
(int
i
=
0;i
<
str.length();i++)
{
if
(userInput
==
str[i])
{
cout
<<
"Right!"
<<
endl;
inside.insert(userInput);//save
data
in
the
inside
vector
outputLetter();
return;
}
}
cout
<<
"Wrong!"
<<
endl;
outside.insert(userInput);//save
data
in
the
outside
vector
outputLetter();
score--;//if
the
player
enter
a
wrong
character
,
the
score
will
get
decrease
,
i
get
the
first
value
is
5
if
(score
==
0)
{
cout
<<
"Sorry
,Game
Over!"<<endl;
}
else
cout
<<
"You
only
have
"
<<
score
<<
"
chance
to
guess
the
word."<<endl;
}
void
main
()//the
main
function
{
getRand();
system("pause");
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式