本文介绍了如何从另一个文件为我的Sudoku游戏C使用.txt文件获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我的问题是,我不知道在哪里去我的数独计划的情况。到目前为止,我有多个程序一起工作来创建我的Sudoku程序,它支持输入9行,每个9个字符,无论它只是空格或数字。
示例:

Alright so my problem is that I have no idea where to go in my situation with my Sudoku Program. As of right now I have multiple programs that work together to create my Sudoku program which is SUPPOSED to take input of 9 lines with 9 characters each whether it be just spaces or numbers.example:

53  7    
6  195   
 98    6
8   6   3
4  8 3  1
7   2   6
 6    28
   419  5
    8   79

程序会产生什么:

534678912
672195348
198342567
859761423
426853791
713924856
961537284
287419635
345286179

我当前的程序包括文件stack.h,stack.cc,sudokuboard.h sudokuboard.cc,sudoku.cc和Makefile,以及test.txt (包含上面输入的例子)

My current programs consist of the files stack.h, stack.cc, sudokuboard.h sudokuboard.cc, sudoku.cc, and Makefile, as well as test.txt(consists the example of input i had above)

程序stack.h:

struct Node {
  StackElementType data;
  Node *next;
};
class Stack {
 public:

 Stack(); //constructor
 ~Stack(); //deconstructor
 Stack(const Stack & orig); // copy constructor
 void output(ostream & ostr) const; //output method
 bool empty() const; // if empty returns true
 void push(const StackElementType & item); // puts new item on top of stack
 void pop(); // removes top element of nonempty stack
 StackElementType top() const; // returns copy of top element of a stack

 private:
 Node*first;
 size_t _size;
}

请注意,由于无法复制,可能会出现拼写错误我的代码直接这样的大部分是新出现的。

Note that there may be misspellings in this due to having to being unable to copy my code directly so most of this is freshly typed out.

我的stack.cc是

My stack.cc is

#include "stack.h"
#include <cstdlib>
#include <cassert>

void destroy(Node *p)
{
  // delete all nodes dominated by p.
  while (p != NULL) {
    Node *old = p;
    p = p->next;
    delete old;
  }
}

Node *copy(Node *p)
{
  // Make a deep copy of linked list dominated by p.
  Node *result = NULL;
  if (p != NULL) {
    result = new Node;
    Node *last = result;
    while (p != NULL) {
      // invariant: last points to a node ready to receive p's data.
      last->data = p->data;
      last->next = NULL;
      p = p->next;
      if (p != NULL) {
    // there's going to more to this copy.  Get it ready.
    last->next = new Node;
    last = last->next;
      }
    }
  }
  return result;
}

Stack::Stack()
{
  first = NULL;
}

Stack::~Stack()
{
  destroy(first);
}

Stack::Stack(const Stack & orig)
{
  first = copy(orig.first);
}

Stack & Stack::operator=(const Stack & rhs)
{
  if (this != &rhs) 
    first = copy(rhs.first);
  return *this;
}

void Stack::output(ostream & ostr) const
{
  ostr << "<";
  for(Node *p = first;p;p=p->next) {
    ostr << p->data;
    if (p->next)
      ostr << ", ";
  }
  ostr << ">";
}

void Stack::push(const ElementType & item)
{
  Node *born = new Node;
  born->data = item;
  born->next = first;
  first = born;
}

void Stack::pop()
{
  assert(!empty());
  Node *rest = first->next;
  delete first;
  first = rest;
}

ElementType Stack::top() const
{
  assert(!empty());
  return first->data;
}

bool Stack::empty() const
{
  return first==NULL;
}

所以这只是我用于我的堆栈

So this is simply what I am using for my stack

我的sudokuboard.h:

My sudokuboard.h:

#include <iostream>

#define SDIM 9

class SudokuBoard {
 public:
  //------------------------------------------------------------------------
  SudokuBoard();
  // Construct a blank sudoku board
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  void print(std::ostream & ostr) const;
  // display it.  duh.
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  void place(size_t r, size_t c, char digit);
  // PRE: safe(r,c,digit)
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  void remove(size_t r, size_t c, char digit); 
  // PRE: get(r,c) == digit
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  char get(size_t r, size_t c) const;
  // Return the digit at (r,c) on the board.  or ' ' if blank.
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  bool safe(size_t r, size_t c, char digit) const;
  // 
  //------------------------------------------------------------------------

  //------------------------------------------------------------------------
  bool done() const; 
  // Return true iff every cell has a number
  //------------------------------------------------------------------------
 private:
  std::string rows[SDIM];
};

我的sudokuboard.cc

While my sudokuboard.cc

#include <iostream>
#include <cassert>
#include "sudokuboard.h"

#define ASSERTBOUNDS assert(0 <= r and r < SDIM and 0 <= c and c < SDIM)

SudokuBoard::SudokuBoard()
{
  for (size_t i = 0;i<SDIM;i++) {
    rows[i] = "";
    for (size_t j=0;j<SDIM;j++)
      rows[i] += ' ';
  }
}

void SudokuBoard::place(size_t r, size_t c, char digit)
{
  ASSERTBOUNDS;
  assert(safe(r,c,digit));
}

void SudokuBoard::remove(size_t r, size_t c, char digit)
{
  ASSERTBOUNDS;
  assert(get(r,c)==digit);
  rows[r][c] = ' ';
}

char SudokuBoard::get(size_t r, size_t c) const
{
  ASSERTBOUNDS;
  return rows[r][c];
}


void SudokuBoard::print(std::ostream & ostr) const
{
  for (size_t i=0;i<SDIM;i++)
    ostr << rows[i] << std::endl;
}
bool SudokuBoard::safe(size_t r, size_t c, char digit) const
{
 for(size_t r=0; r<SDIM; r++)
    for(size_t c=0; c<SDIM; c++)
       if (get(r,c) == digit)
           return false;

 for(size_t c=0; c<SDIM; c++)
    for(size_t r=0; r<SDIM; r++)
       if (get(r,c) == digit)
           return false;
  return true;
}
bool SudokuBoard::done() const
{
  for (size_t r=0;r<SDIM;r++)
    for (size_t c=0;c<SDIM;c++)
      if (rows[r][c]==' ')
    return false;
  return true;
}

现在我的sudoku.cc几乎是空白的,因为我只有一个关于如何追求我的目标的想法。我要填充空白空间的方式是,我要采取一个区域,并在其当前行/列中放入尽可能小的数字,如果其行/列中有更大的数字, +1。

My sudoku.cc right now is pretty much blank because I have only a general idea on how to pursue my goal. The way I am going to fill in the empty spaces is that I am going to take to take a single area and put in the smallest possible number in its current row/column and if there is a higher number in its row/column is goes up +1. I then go down the column and so on.

我的问题是我如何在sudoku.cc中整合以下程序sudokuboard.cc和stack.cc。我知道我必须得到test.txt的输入一些如何,并将每一行转换为空板,但我不知道如何语句的cin输入的所有!

My question is how do i integrate the following programs of sudokuboard.cc and stack.cc in my sudoku.cc. I know I have to get the input of test.txt some how and convert each of the lines to the empty board, but I don't know how to phrase the cin input for that at all!

换句话说,我正在寻找任何帮助,让我sudok.cc开始,我应该如何处理?

In other words I am looking for any help to get my sudok.cc started and how should I approach it?

sudoku.cc

sudoku.cc

#include <iostream>
#include <cassert>
#include "sudokuboard.h"
#include "stack.h"

int main()
{


}

我只想说谢谢你给了我这些答案,现在我的实验室它的方式感谢你们!

I just want to say thank you who gave me these answers, now my lab is well on its way thanks to you guys! I cannot give any of you points b/c my rep is too low but otherwise i would!

推荐答案

我会使用的方法读取文本文件:

The method I would use to read the text file:

//Code here
std::ifstream infile("test.txt", std::ios::binary);
if(infile.is_open())
{
    char szFileData[32]; //I think we can assume that each line won't be above 32 bytes
    infile.getline(szFileData, 32);
    do
    {
       //Set the cells to the data you just pulled
       infile.getline(szFileData, 32);
    }while(infile.good());
}
else
{
   std::cout<<"Unable to open file!"<<std::endl;
}

这篇关于如何从另一个文件为我的Sudoku游戏C使用.txt文件获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:09