我觉得这是愚蠢的,我不知道。我已经搜索了一下但是在解决方案方面找不到多少。我尝试了一些我在网上找到的解决方案(从控制台到Windows的类型改为无等)。我没有任何运气,所以任何评论都表示赞赏。
注意:我尝试了其他的东西(并在Stack Overflow上搜索)但没有运气:/
这是我的来源:
Main.cpp的
#include "FizzBuzz.h"
#include <tchar.h>
int _tmain(){
FizzBuzz f;
//Main loop
while (f.isRunning())
{
f.UserInput();
}
std::system("Pause");
return 0;
}
FizzBuzz.cpp
#include <iostream>
#include <sstream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <time.h>
#include <string>
class FizzBuzz
{
int FizzBuzz::fbArray[10][10];
bool FizzBuzz::running;
int FizzBuzz::score;
int FizzBuzz::guesses;
FizzBuzz::FizzBuzz(){
fbArray[10][10] = {};
running = true;
score = 0;
guesses = 0;
}
bool FizzBuzz::isRunning(void)
{
return running;
}
void FizzBuzz::setRunning(bool var)
{
running = var;
}
int FizzBuzz::getScore(void)
{
return score;
}
void FizzBuzz::setScore(int var)
{
score = var;
}
int FizzBuzz::getGuesses(void)
{
return guesses;
}
void FizzBuzz::setGuesses(int var)
{
guesses = var;
}
void FizzBuzz::UserInput(void)
{
std::cout << "Please enter which you would like to do: '1' for the Guessing Game, '2' for generating an array, '-1' to quit!" << std::endl;
int choice;
std::cin >> choice;
switch (choice)
{
case 1:
int row, column;
populateArray(fbArray);
validateFizzBuzz();
std::cout << "Enter a row and column below, -1 to exit on either." << std::endl << std::endl;
std::cout << "Please enter a row: ";
std::cin >> row;
std::cout << std::endl;
std::cout << "Please enter a column: ";
std::cin >> column;
if (row == -1 || column == -1)
{
displayScore();
}
else{
score += FizzBuzzCalc(row, column); //Add score to current score
std::cout << "You got: " << score << " points for that guess." << std::endl;
}
break;
case 2:
populateArray(fbArray);
printArray(fbArray);
break;
case -1:
std::cout << "Thanks for playing!" << std::endl;
running = false;
break;
default:
break;
}
}
int FizzBuzz::FizzBuzzCalc(int row, int column)
{
guesses++;
if (getFizzBuzz(fbArray[row][column]) == "FizzBuzz")
{
return 1;
}
else
{
return -1;
}
}
void FizzBuzz::displayScore()
{
std::cout << "You scored: " << score << " in " << guesses << " guesses!" << std::endl;
}
bool FizzBuzz::validateFizzBuzz()
{
int count = 0;
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
if (getFizzBuzz(fbArray[i][j]) == "FizzBuzz")
{
count++;
}
}
}
if (count > 20 || count < 6)
{
for (int k = 0; k < 11; k++)
{
for (int l = 0; l < 11; l++)
{
if (count > 20)
{
if (getFizzBuzz(fbArray[k][l]) == "FizzBuzz")
{
fbArray[k][l] = 1;
}
}
else if (count < 6)
{
if (getFizzBuzz(fbArray[k][l]) == "Fizz")
{
fbArray[k][l] = 1;
}
}
}
}
}
return true;
}
//Populates an array with numbers randomly between 1-100
void FizzBuzz::populateArray(int arrayIn[10][10])
{
srand(time(NULL));
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
arrayIn[i][j] = rand() % 100 + 1;
}
}
}
//Prints a 2x2 array
void FizzBuzz::printArray(int arrayin[10][10]){
for (int h = 0; h < 10; h++)
{
for (int w = 0; w < 10; w++)
{
printf("%i,", arrayin[h][w]);
}
printf("\n");
}
}
//Prompts user to enter an array of numbers, seperated by commas, then passes this to getFizzBuzzArray and prints them to the screen.
void FizzBuzz::fizzBuzzArrayPrompt()
{
std::cout << "Please enter numbers in a comma seperated list (EX:'8,9,10,11,12')";
std::string str;
std::cin >> str;
std::istringstream ss(str);
std::string token;
while (std::getline(ss, token, ',')){
if (getFizzBuzzArray(std::stoi(token)) == "")
{
std::cout << "Invalid FizzBuzz Number: " << token << " ";
}
else{
std::cout << "Output: Number: " << token << " - " << getFizzBuzzArray(std::stoi(token)) << " ";
}
}
}
//Prompts the user to enter a number and then writes the FizzBuzz to the screen (from getFizzBuzz())
void FizzBuzz::fizzBuzzPrompt()
{
int number;
std::cout << "Please enter number, then press Enter key: ";
std::cin >> number;
std::string str = getFizzBuzz(number);
if (getFizzBuzz(number) == "")
{
fizzBuzzPrompt();
}
else{
std::cout << std::endl << "Output: Number: " << number << " - " << str << " ";
}
}
//getFizzBuzzArray returns the respective fizzBuzz String to the array function - this was made special for the function.
//getFizzBuzz so it could re-call for the user to re-prompt. Gets this from string format (converted)
std::string FizzBuzz::getFizzBuzzArray(int number)
{
if (!(number % 2) && !(number % 3) && !(number % 5) || number == 1 || number == -1){
std::cout << "Please enter a number value, not a character!" << std::endl;
return "";
}
else{
if (number % 3 == 0 && number % 5 == 0)
{
return "FizzBuzz";
}
else
{
if (number % 3 == 0)
{
return "Fizz";
}
else if (number % 5 == 0)
{
return "Buzz";
}
else
{
return "";
}
}
}
}
//Gets a single Fizzbuzz string from a number
std::string FizzBuzz::getFizzBuzz(int number)
{
if (!(number % 2) && !(number % 3) && !(number % 5) || number == 1 || number == -1){
std::cout << "Please enter a number value, not a character!" << std::endl;
return "";
}
else{
if (number % 3 == 0 && number % 5 == 0)
{
return "FizzBuzz";
}
else
{
if (number % 3 == 0)
{
return "Fizz";
}
else if (number % 5 == 0)
{
return "Buzz";
}
else
{
std::cout << "Please enter another number!" << std::endl;
return "";
}
}
}
}
};
和FizzBuzz.h
#ifndef FIZZ_BUZZ_H
#define FIZZ_BUZZ_H
#include <string>
class FizzBuzz
{
public:
FizzBuzz::FizzBuzz();
bool FizzBuzz::isRunning(void);
void FizzBuzz::setRunning(bool var);
int FizzBuzz::getScore();
void FizzBuzz::setScore(int var);
int FizzBuzz::getGuesses();
void FizzBuzz::setGuesses(int var);
void FizzBuzz::UserInput();
int FizzBuzz::FizzBuzzCalc(int row, int column);
void FizzBuzz::displayScore();
bool FizzBuzz::validateFizzBuzz();
void FizzBuzz::populateArray(int arrayIn[10][10]);
void FizzBuzz::printArray(int arrayin[10][10]);
void FizzBuzz::fizzBuzzArrayPrompt();
void FizzBuzz::fizzBuzzPrompt();
private:
std::string FizzBuzz::getFizzBuzzArray(int number);
std::string FizzBuzz::getFizzBuzz(int number);
};
#endif FIZZ_BUZZ_H