I have the following data in a file, let the columns be [A B C D E]:
a1 b1 c1 d1 e1
a2 b2 c2 d2 e2
a3 b3 c3 d3 e3
.....
All are integers. A is just the serial number. let B be a node having only max of 4 terminals. C represents which terminal of B is taken. Similarly, D is a node having max of 4 terminals and E represents the terminal of D choosen. example: c1 of b1 is connected to e2 of d2.
I have successfully read the data from the files line by line using istringstream and getline as below:
while (getline(infile, line))
{
int i=0;
istringstream iss(line); // string stream
while(getline(iss, temp[i]))
{
cout<< temp[i] << endl;
++i;
};
}
I am not able to conclude on how to save the connections mentions above line to line. I am new to c++ and would appreciate any ideas and implementation. Thanks!
you can just declare a vector<vector<int>> nameVector,(give it a size) and inside of while you put the data. Or be more specific on what you want.
Related
So I am reading a .txt file that has many sets of chess moves. I am able to read data from the file and insert the line into a string.
An example single chess move can look like this:
1. e4 e5
I have written the following function to parse a single chess move:
void parseSingleChessMove(string move)
{
this->moveNumber = stoi(move.substr(0, move.find(".")));
this->move[0] = move.substr(move.find_first_of(" ")+1, move.find_last_of(" ")-move.find_first_of(" ")-1);
this->move[1] = move.substr(move.find_last_of(" ")+1);
}
I am parsing the string and storing it within a self-defined Move Class hence the use of the 'this' operator. This function works perfectly and stores each field of a single chess move. move[0] stores the first move and move[1] stores the second move, while the moveNumber data member stores the number the move was played at.
I am creating an array of the Move Class in order to store every single move of a chess match in order. However, a complete set of chess moves can look something like this:
1. Nf3 Nf6 2. c4 c6 3. g3 g6 4. b3 Bg7 5. Bb2 O-O 6. Bg2 d5 7. O-O Bf5 8. d3
Nbd7 9. Nd4 e6 10. h3 h5
I am having a hard time trying to figure out how to store each of these individual moves in the array of Move Class from a string of a set of chess moves.
The main issue is reading the string only until a move number is found. I then need to obtain a substring for a move (something like 4. b3 Bg7and then parsing this single chess move using the above function so that I can store moveNumber=4, move[0]="b3" and move[1]="Bg7" and finally storing it into the respective index of array type Move Class. And then repeating this until all moves are stored one by one and we reach the end of the string.
EDIT: This is my class definition:
class MoveNode {
public:
array<string, 2> move;
int moveNumber;
void parseSingleChessMove(string move)
{
this->moveNumber = stoi(move.substr(0, move.find(".")));
this->move[0] = move.substr(move.find_first_of(" ")+1, move.find_last_of(" ")-move.find_first_of(" ")-1);
this->move[1] = move.substr(move.find_last_of(" ")+1);
}
}
I am storing all of the moves in this array:
MoveNode *setofMoves = new MoveNode[totalMoves];
#rturrado showcased how you could do this with regex, however I would hesitate to do it like that, since std::regex is heavy, and requires a lot of knowledge regarding regex to use it effectively. Instead, I think it's easier to accomplish it with istream and operator>>.
void parse_moves(std::istream& input)
{
int move_number;
char dot;
std::string move_fisrt, move_second;
int index = 0;
while(input >> move_number >> dot >> move_first >> move_second)
{
setofMoves[index] = MoveNode{{move_first, move_second}, move_number};
++index;
}
}
Here while(is >> ...) will keep parsing the text as long it's following the pattern.
You could use regular expressions for this:
The pattern to search repeatedly for would be: (\d+)\. a number of one or more digits (which we are going to capture, hence the use of parentheses), followed by a dot; then \s+([^\s]+) one or more whitespaces, followed by one or more non-whitespaces (and we capture these latter); we repeat this pattern twice, once for each move; finally (:?\s+|$), one or more whitespaces \s+ or | the end of the expression $ because the input line may end with a second move (and we don't capture this group (:?)).We use std::regex to store the pattern, wrapping it all within R"()", so that we can write the raw expression.
The while loop does a few things: it searches the next match with regex_search, extracts the captured groups (move number, move 0 and move 1), and updates the input line, so that the next search will start where the current one finished.matches is an array whose first element, matches[0], is the part of line matching the whole pattern, and the next elements correspond to the pattern's captured groups.
[Demo]
#include <iostream> // cout
#include <regex> // regex_search, smatch
int main() {
std::string line{"1. Nf3 Nf6 2. c4 c6 3. g3 g6 4. b3 Bg7 5. Bb2 O-O 6. Bg2 d5 7. O-O Bf5 8. d3 Nbd7 9. Nd4 e6 10. h3 h5"};
std::regex pattern{R"((\d+)\.\s+([^\s]+)\s+([^\s]+)(:?\s+|$))"};
std::smatch matches{};
while (std::regex_search(line, matches, pattern))
{
std::cout
<< "moveNum=" << matches[1] << ", "
<< "move[0]=" << matches[2] << ", "
<< "move[1]=" << matches[3] << "\n";
line = matches.suffix();
}
}
// Outputs:
// moveNum=1, move[0]=Nf3, move[1]=Nf6
// moveNum=2, move[0]=c4, move[1]=c6
// moveNum=3, move[0]=g3, move[1]=g6
// moveNum=4, move[0]=b3, move[1]=Bg7
// moveNum=5, move[0]=Bb2, move[1]=O-O
// moveNum=6, move[0]=Bg2, move[1]=d5
// moveNum=7, move[0]=O-O, move[1]=Bf5
// moveNum=8, move[0]=d3, move[1]=Nbd7
// moveNum=9, move[0]=Nd4, move[1]=e6
// moveNum=10, move[0]=h3, move[1]=h5
I would need some universal string of commands, so I could insert it in cells A4, C4, D4, E4 and get those desired numbers across all the other similar cells like these.
So far I tried stuff like: =IF(A1 = A2; SUM(A1 * A2); "1") but still not close with that.
What exactly do you want to do?
Not sure why you reference A11 in your formula, or what you expect from SUM(A1 * A2) (do you want A1+A2? or A1*A2?)
Let's assume you want A4 = 1 if A1 = 1 or A2 = 1 and A4 = 0 otherwise. Then type in A4:
=IF(OR(A1=1; A2=1); 1; 0)
Then copy (or right-drag) A4 and paste it in C4, D4 and E4; the formula will adjust to these columns and work the same.
Please clarify your goal if you want better help :)
I have a 2D array of 3 random chars, a, b, and c. I need to make sure that no column or row has three or more repeating chars.
I'm not really sure how to check the arrays. I think I need to use a loop to go through the array but I'm not sure how to check if there are repeats. To clarify,
if a column has three A's in a row that would not be allowed or could 3 B's be in a column, etc.
So if it is like AAA in 3 consecutive array index (in terms of rows and columns), it should stop.
You need to use for loops if you know the indexes of your array.
so lets say:
char[][] arr= new char[20][20];
for(int i=0; i<18;i++){
for(int j=0; j<18;j++){
if( arr[i][j] ==arr[i][j+1] && arr[i][j] == arr[i][j+2]))
{
break;//mark it as Duplicate
}
else if (arr[i][j] == arr[i+1][j] && arr[i][j] == arr[i+2][j])
{
break;//mark it as Duplicate
}
else
continue;
}
}
So basically you are traversing the array and comparing 3 consecutive elements together. if they are equal it breaks.
You can use an automaton. One to recognize sequences of AAA, BBB and CCC is:
state input next stop
===== ===== ==== ====
0 A a1 no
0 B b1 no
0 C c1 no
a1 A a2 no
a1 B b1 no
a1 C c1 no
b1 A a1 no
b1 B b2 no
b1 C c1 no
c1 A a1 no
c1 B b1 no
c1 C c2 no
a2 A 3 yes
a2 B b1 no
a2 C c1 no
b2 A a1 no
b2 B 3 yes
b2 C c1 no
c2 A a1 no
c2 B b1 no
c2 C 3 yes
3 A 3 yes
3 B 3 yes
3 C 3 yes
you can run this automaton for each row and for each column and stop as soon as you get output yes (it has detected three consecutive letters) You'll get stopped as soon as you recognize the first occurrence of three consecutive letters with only two passes (one for the rows, other for the columns) through the matrix. Of course, initial state is 0.
This is a function I have created to try to deal n cards from a deck class of cards. When a card is dealt, it is supposed to be 'removed' from the deck (i.e. can't be dealt again). I thought I solved this by using pointers, and by drawing from the back of the deck minus the amount of cards I have dealt so far. If I call the function again, I need to be drawing from the deck that has cards missing, and since I am not shuffling the deck at any point I thought this method would work.
'dealt' is defined earlier in my code, and starts at 0.
Card is a class that holds a certain suit and value.
I put a sample output of the hands dealt when I call the dealNumber function 6 times and n = 5.
ST CK D3 HJ D9
HK DK SA SQ DT
HK S4 D2 C9 H5
HK H6 H7 H2 H4
HK HK SK S8 C5
HK H9 S3 D8 H8
The 'deck' is ordered this way however:
S6 S2 S7 D7 S9 CQ D4 CA CJ SJ HQ DQ D5 HA DA C4 HT H3 CT D6 C2 S5 H8 D8 S3 H9 C7
C5 S8 SK HK C8 H4 H2 H7 H6 C3 H5 C9 D2 S4 C6 DT SQ SA DK DJ D9 HJ D3 CK ST
The first hand I deal is correct, but the others are not. 'dealt' should be carried over through the deck class regardless, so where am I going wrong?
Hand* Deck::dealingNumber(int p){
Card* dealtCards = new Card[p];
Card* oldDeck = deck;
for (int i = 0; i<n; i++){
dealtCards[i] = deck[(51-dealt)-i];
}
dealt = dealt+n;
deck = new Card[52-dealt];
for (int i = 0; i < 51-dealt;i++){
deck[i] = oldDeck[i];
}
delete[] oldDeck;
PokerHand* hand = new PokerHand(dealtCards);
return hand;
You have an off-by-one error in your second for loop:
for (int i = 0; i < 52 /* change from 51 to 52 */ - dealt; i++){
deck[i] = oldDeck[i];
}
I would take a very different approach using 2 arrays. The first array holds the deck and the second array holds a dealt/not dealt flag.
char cards[52][3]; // 52 cards # 3 characters each
int dealtFlag[52]; // 0=not dealt, 1=dealt
Then choose a random number from 0-51, check that dealtFlag[randNum] is zero, and use that card if it is zero. If it is 1, get a new random number and try again.
Not sure this itself is the problem. Anyway this is a problem:
Modify
for (int i = 0; i < 51-dealt;i++){
deck[i] = oldDeck[i];
}
to
for (int i = 0; i < 52-dealt;i++){
deck[i] = oldDeck[i];
}
Using STL classes for your purpose will be much more simpler.
But personally, I would prefer a stack using linked list or similar.
I have just made a queue class and I now have to use it to do this.
Write a c++ program to generate all strings using A,B,and C as the letters.
The strings must be generated in the following order:
A
B
C
AA
AB
AC
BA
BB
BC
CA
CB
CC
AAA
AAB
AAC
ABA
ABB
ABC
ACA
ACB
ACC
etc.
It's supposed to do this until my queue overflows.
Now, I simply don't understand the algorithm the teacher suggested using, which is this.
Start with A and B and C in the queue.
“Remove it Display it then Add Add Add ”
The add add add thing throws me off, how does it accomplish getting these letters in this particular order?
I think your teacher meant "Add A, Add B, Add C".
Suppose you have A, B and C in the queue. You pop the first one off the queue and print it. This should print A. Then you add an A to that. This gives you AA, which you push back into the queue. You also add a B and add a C to the string you popped last (giving you AB and AC) and push them back into the queue as well. Now your queue contains [B,C,AA,AB,AC]. Next you will pop B and do the same sequence of operations on that as well, and so on until you run out of space in your stack.
Let our behavior be:
For any token X, add XA, XB, and XC to the queue.
Our flow will be something like:
Start with a Queue
A B C
Pop (and display) off A
B C
Behave on token: "A"
add AA
add AB
add AC
B C AA AB AC
Pop (and display) off B
C AA AB AC
add BA
add BB
add BC
C AA AB AC BA BB BC
If we pretend our function is
main() {
Queue q;
q.add("A");
q.add("B");
q.add("C");
while(true) {
process(q.pop());
}
}
process(String x, Queue q) {
display x;
q.add(x + "A");
q.add(x + "B");
q.add(x + "C");
}
Get it now?
A B C
prints A
new queue state
B C A A A
prints B
new queue state
C A A A B B B
prints C
new queue state
A A A B B B C C C
prints A
new queue state
A A B B B C C C A A A
prints A
new queue state
A B B B C C C A A A A A A
prints A
new queue state
B B B C C C A A A A A A A A A
This was my first interpretation of the cycle, but i must be getting it wrong, because i go from 1 repeat to 3 right away.
Update: definitely read the initial problem wrong after seeing the other responses.