What is my syntax error in this c code? - c

I have just written some C code for checking for a checkmate (in chess), but I really can't understand what's wrong with my syntax, this is the compile error:
main.c:2:30: error: expected ';', ',' or ')' before 'board'
int is_check(const char[][8] board,int i,int j){
^ main.c:117:27: error: expected ';', ',' or ')' before 'board'
int check(const char[][8] board)
and this is my code:
#include <stdio.h>
int is_check(const char[][8] board,int i,int j){
int row = i;
int clmn = j;
//check clmn , up
do{
i--;
}
while(board[i][j] == 'z');
if(i>=0){
if(board[i][j] == 'H'){
return 1;
}
}
i = row;//init
//check clmn , down
do{
i++;
}
while(board[i][j] == 'z');
if(i<8){
if(board[i][j] == 'H'){
return 1;
}
}
i = row;//init
//check row , up
do{
j--;
}
while(board[i][j] == 'z');
if(j>=0){
if(board[i][j] == 'H'){
return 1;
}
}
j = clmn;//init
//check row , down
do{
j++;
}
while(board[i][j] == 'z');
if(j<8){
if(board[i][j] == 'H'){
return 1;
}
}
j = clmn;//init
//check orib!4
do{
j++;
i++;
}
while(board[i][j] == 'z');
if(j<8 && i<8){
if(board[i][j] == 'H'){
return 1;
}
}
j = clmn;
i = row;
//check orib!1
do{
j++;
i--;
}
while(board[i][j] == 'z');
if(j<8 && i>=0){
if(board[i][j] == 'H'){
return 1;
}
}
j = clmn;
i = row;
//check orib!3
do{
j--;
i++;
}
while(board[i][j] == 'z');
if(j>=0 && i<8){
if(board[i][j] == 'H'){
return 1;
}
}
j = clmn;
i = row;
//check orib!2
do{
j--;
i--;
}
while(board[i][j] == 'z');
if(j>=0 && i>=0){
if(board[i][j] == 'H'){
return 1;
}
}
return 0;
}//end func
int check(const char[][8] board)
{
int i = 0;
int j = 0;
for(;i<8;i++){
for(;j<8;j++){
if(board[i][j] == 'q')
return is_check(board,i,j);
}
}
return 0;
}
int main(){
char x[8][8] ={{'R','z','B','Q','H','z','q','R'},
{'A','A','A','A','z','z','A','A'},
{'z','z','z','d','z','z','D','z'},
{'z','z','z','z','z','z','z','b'},
{'z','z','z','z','a','z','z','z'},
{'a','z','z','a','z','z','z','z'},
{'z','a','a','z','z','a','a','a'},
{'r','d','b','z','h','z','z','r'}};
printf("%d",check(x));
return 0;
}

Because it should be
const char board[][8];

Related

Find index of word in string

I want to write a function which will find index of word in string.
For example if string is
This is word.
my function for string "word" should return number 3.
Note: functions from string.h library and auxiliary strings are not allowed.
How could I do this in C?
I can't think of a solution better than this (though there might be better ones).
#include <stdio.h>
int main() {
char word[] = "This is a word";
int flag = 0, space = 0, pos = -1;
for (int i = 0; word[i] != '\0'; i++) {
if (flag == 1) {
break;
}
for (int j = 0; word[j] != '\0'; j++) {
if (flag == 1) {
break;
}
else if (word[j+1] == '\0' || word[j+2] == '\0' || word[j+3] == '\0') {
break;
}
else {
if (word[j] == 'w' && word[j+1] == 'o' && word[j+2] == 'r' && word[j+3] == 'd') {
flag = 1;
pos = j;
}
}
}
}
for (int i = 0; word[i] != '\0'; i++) {
if (word[i] == ' ' || word[i] == '!' || word[i] == '#') {// And many more symbols
fchars++;
}
else {
break;
}
}
if (flag == 1 && pos-1 > 0 && word[pos-1] == ' ') {
for (int i = 0; i < pos; i++) {
if (word[i] == ' ') {
space++;
}
}
printf("Found at position = %i\n", space+1-fchars);
}
else {
printf("Not found!\n");
}
}
You can split the sentence by space to get the words and then match each word in the sentence with the word you want to match
Please check this modified code:
#include<stdio.h>
int main()
{
char word[] = "word";
char string[100];
gets(string);
int curWordStart = -1;
int curWordEnd = -1;
int wordCount = 0;
int i = 0;
for (i = 0; string[i] != '\0'; i++)
{
if (string[i] == ' ')
{
int curWordLength = curWordEnd - curWordStart + 1;
if (curWordStart != -1 && curWordLength > 0)
{
wordCount++;
int foundMatch = 1;
int j;
int k = 0;
for (j = curWordStart; j <= curWordEnd; j++) {
if (word[k] == '\0') {
foundMatch = 0;
break;
}
if (word[k] != string[j])
{
foundMatch = 0;
break;
}
k++;
}
if (word[k] != '\0')
{
foundMatch = 0;
}
if (foundMatch == 1)
{
printf("%d\n", wordCount);
}
}
curWordStart = -1;
curWordEnd = -1;
}
else if ((string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z'))
{
if (curWordStart == -1) {
curWordStart = i;
}
curWordEnd = i;
}
}
int curWordLength = curWordEnd - curWordStart + 1;
if (curWordStart != -1 && curWordLength > 0)
{
wordCount++;
int foundMatch = 1;
int j;
int k = 0;
for (j = curWordStart; j <= curWordEnd; j++) {
if (word[k] == '\0') {
foundMatch = 0;
break;
}
if (word[k] != string[j])
{
foundMatch = 0;
break;
}
k++;
}
if (word[k] != '\0')
{
foundMatch = 0;
}
if (foundMatch == 1)
{
printf("%d\n", wordCount);
}
}
return 0;
}
It will print each position of the searched word in the sentence. If you want to just print the first one, you can easily modify it.
Here are steps to follow:
you must specify precisely what is a word in the string.
measure the length len of the word to search
define an int index = 1
in a loop, using a pointer p starting at the beginning of the string:
advance p past all word delimiters (spaces, punctuation or non letters?)
if p is at end of string return 0 (not found).
measure the length len1 of the current word in the string
if len1 == len and all bytes are identical to those of the word, return index
otherwise skip the word by advancing p by len1, increment index and continue the loop.
Here is an implementation:
#include <stddef.h>
int isletter(char c) {
/* assuming ASCII */
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
int word_index(const char *str, const char *word) {
const char *p = str;
size_t len, len1, i;
int index = 1;
for (len = 0; word[len]; len++)
continue;
for (;;) {
while (!is_letter(*p))
p++;
if (*p == '\0')
return 0;
for (len1 = 0; is_letter(p[len1]); len1++)
continue;
if (len1 == len) {
for (i = 0; i < len && p[i] == word[i]; i++)
continue;
if (i == len)
return index;
}
p += len1;
index++;
}
}

Minimax going in order(Tic Tac Toe)

I'm currently trying to make use of the Minimax Algorithm and create a "Unbeatable Computer Player". I'm banging my head with this one for hours now and I cant seem to figure out why the "Computer" is going in order and not making the right decisions. Does anybody have an idea? maybe something sticks out for you I can't put my finger on what's wrong honestly. I bet it has something to do with the recursive call but I'm lost here.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char BOARD[9][4];
void createBoard(){
for(int i = 0;i < 9;i++){
strcpy(BOARD[i], "[]");
}
strcpy(BOARD[6], "[X]");
strcpy(BOARD[1], "[O]");
strcpy(BOARD[3], "[X]");
}
void printBoard(){
printf("\n\n");
for(int i = 0;i < 9;i++){
printf("%s", BOARD[i]);
if((i + 1) % 3 == 0){
printf("\n");
}
}
printf("\n\n");
}
void makeMove(char* player, int position){
char playerFinal[5];
strcpy(playerFinal, "[");
strcat(playerFinal, player);
strcat(playerFinal, "]");
if(strcmp(BOARD[position], "[]") == 0){
strcpy(BOARD[position], playerFinal);
}
}
bool movesLeft(){
for(int i = 0;i < 9;i++){
if(strcmp(BOARD[i], "[]") == 0){
return true;
}
}
return false;
}
char* isWinner(){
for(int i = 0;i < 9;i = i + 3){
if(strcmp(BOARD[i], BOARD[i + 1]) == 0 && strcmp(BOARD[i + 1], BOARD[i + 2]) == 0){
if(strcmp(BOARD[i], "[X]") == 0){
return "X";
}
else if(strcmp(BOARD[i], "[O]") == 0){
return "O";
}
}
}
for(int i = 0;i < 3;i++){
if(strcmp(BOARD[i], BOARD[i + 3]) == 0 && strcmp(BOARD[i + 3], BOARD[i + 6]) == 0){
if(strcmp(BOARD[i], "[X]") == 0){
return "X";
}
else if(strcmp(BOARD[i], "[O]") == 0){
return "O";
}
}
}
if(strcmp(BOARD[0], BOARD[4]) == 0 && strcmp(BOARD[4], BOARD[8]) == 0){
if(strcmp(BOARD[0], "[X]") == 0){
return "X";
}
else if(strcmp(BOARD[0], "[O]") == 0){
return "O";
}
}
if(strcmp(BOARD[2], BOARD[4]) == 0 && strcmp(BOARD[4], BOARD[6]) == 0){
if(strcmp(BOARD[2], "[X]") == 0){
return "X";
}
else if(strcmp(BOARD[2], "[O]") == 0){
return "O";
}
}
if(!movesLeft()){
return "tie";
}
return "";
}
int minimax(bool isMaximizing){
char* winner = isWinner();
printf("Function Called\n");
printf("%s", winner);
if(strcmp(winner, "O") == 0){
return 10;
}
if(strcmp(winner, "X") == 0){
return -10;
}
if(strcmp(winner, "tie") == 0){
return 0;
}
if(isMaximizing){
int score;
int bestScore = -1000000;
for(int i = 0;i < 9;i++){
if(strcmp(BOARD[i], "[]") == 0){
makeMove("O", i);
score = minimax(false);
strcpy(BOARD[i], "[]");
if(score > bestScore){
bestScore = score;
}
}
}
return bestScore;
}
else{
int score;
int bestScore = 1000000;
for(int i = 0;i < 9;i++){
if(strcmp(BOARD[i], "[]") == 0){
makeMove("X", i);
score = minimax(true);
strcpy(BOARD[i], "[]");
if(score < bestScore){
bestScore = score;
}
}
}
return bestScore;
}
}
void bestMove(){
int bestScore = -1000;
int position;
for(int i = 0;i < 9;i++){
if(strcmp(BOARD[i], "[]") == 0){
int score;
makeMove("O", i);
score = minimax(false);
strcpy(BOARD[i], "[]");
printf("Position %d\n", i);
printf("Best Score %d\n", bestScore);
printf("Score %d\n", score);
if(score > bestScore){
bestScore = score;
position = i;
}
}
}
makeMove("O", position);
}
int main(){
int position;
createBoard();
printBoard();
bool player = true;
char* winner = "";
while(strcmp(winner, "") == 0){
if(player){
printf("Its the Players Turn\nChoose a Position: ");
scanf("%d", &position);
makeMove("X", position -1);
player = false;
winner = isWinner();
}
else{
printf("Its the Computers Turn");
bestMove();
player = true;
winner = isWinner();
}
printBoard();
}
printf("The Winner is: %s\n", winner);
return 0;
}

C programming, only the first if statement works

I was wondering if anyone could help me understand why only my first if statements is working. Basically, I am working on a l33t speak convertor (lol) and only my first if statement works, here is my code:
#include<stdio.h>
#include<string.h>
void translate (char blurp[]);
int main(void) {
char message[1024];
printf("enter a message: \n");
fgets(message, 1024, stdin);
translate(message);
return 0;
}
void translate (char blurp[]) {
int i;
int length;
length = strlen(blurp);
printf("\nHere it is translated: \n");
for ( i = 0; i != length; i++) {
if (blurp[i] == 'a') {
blurp[i] = '4';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'b') {
blurp[i] = '8';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'e') {
blurp[i] = '3';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'i') {
blurp[i] = '|';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'o') {
blurp[i] = '0';
printf("%c", blurp[i]);
}
else if (blurp[i] == 's') {
blurp[i] = '5';
printf("%c", blurp[i]);
}
else {
printf("%c", blurp[i]);
}
}
}

Moving characters in 2d array

I tried to create a 8 x 8 checkers game. I am trying to move the hyphen ' _ ' in the 2d array to select the character 'X' that I want. I have created the if statement for detecting the hyphen ' _ ' but seem that my code isn't working, I really need help. I am new to programming.
#include <stdio.h>
void gameboard(char board[8][8])
{
int x, y;
for(x=0; x<8; x++)
{
for(y=0; y<8; y++)
{
printf("=---=");
}
printf("\n\n");
for(y=0;y<8;y++)
{
printf("| %c |",board[x][y]);
}
printf("\n\n");
}
for(x=0;x<8;x++)
{
printf("=---=");
}
}
void character(char board[8][8])
{
int x,y;
for(x=0;x<8;x++){
for(y=0;y<8;y++){
if(x<3){
if(x%2 == 0){
if(x%2 == 0){
board[x][y] = 'O';
}
if(y%2==1){
board[x][y]= ' ';
}
}
if(x%2 == 1){
if(y%2 == 0){
board[x][y] = ' ';
}
if(y%2 ==1){
board[x][y]= 'O';
}
}
}
if((x==3) || (x==4)){
board[x][y] = ' ';
}
if(x>4)
{
if(x%2 == 0){
if(y%2 == 0){
board[x][y] = 'X';
}
if(y%2 ==1){
board[x][y]= ' ';
}
}
if(x%2 == 1){
if(y%2 == 0){
board[x][y] = ' ';
}
if(y%2 ==1){
board[x][y]= 'X';
}
}
if(x==5 && y ==1)
{
if(x%2 == 1){
if(y%2 == 1){
board[x][y] = '_';
}
}
}
}
}
}
}
void playgame(char board[8][8])
{
int x=0, y=0, a, b, c=0,input;
char token;
printf("\n\n---START GAME---\n");
if(token == '_')
{
printf("Please select your token : ");
}
for(a=0; a<8; a++)
{
for(b=0; b<8; b++)
{
if(board[a][b] == token & c == 0)
{
x = a;
y = b;
c++;
}
}
}
printf("1 to go right\n");
printf("2 to go left\n");
printf("3 to go up left\n");
printf("4 to go up right\n");
printf("5 to go down left\n");
printf("6 to go down right\n");
printf("7 to select token\n");
fflush(stdin);
scanf("%i", &input);
if(input == 1)
{
board[x][y+2] = token;
y++;
}
else if(input == 2)
{
board[x][y-2] = token;
y--;
}
else if(input == 3)
{
board[x-1][y-1] = token;
x--;
y--;
}
else if(input == 4)
{
board[x-1][y+1] = token;
x--;
y++;
}
else if(input == 5)
{
board[x+1][y-1] = token;
x++;
y--;
}
else if(input == 6)
{
board[x+1][y+1] = token;
x++;
y++;
}
else
{
board[x][y] = token;
}
}
int main()
{
char bx[8][8];
gameboard(bx);
playgame(bx);
return 0;
}
You have to initialize the array, then use switch statement to calculate the new position based on input.
Save the old position and swap the content of the new cell with that of the old cell in the saved position.
int main()
{
char board[8][8];
int x, y;
for (x = 0; x < 8; x++)
for (y = 0; y < 8; y++)
board[x][y] = '.';
board[0][0] = '_';
int xpos = 0;
int ypos = 0;
while (1)
{
system("cls||clear");
//print board
for (y = 0; y < 8; y++)
{
for (x = 0; x < 8; x++)
printf("%c", board[x][y]);
printf("\n");
}
printf("Menu\n 1 left \n2 right \n3 up \n4 down \n");
int savex = xpos;
int savey = ypos;
int move = 0;
scanf("%d", &move);
char c;
while ((c = getchar()) != '\n' && c != EOF);
switch (move)
{
case 1: if (xpos > 0) xpos--; break;
case 2: if (xpos < 7) xpos++; break;
case 3: if (ypos > 0) ypos--; break;
case 4: if (ypos < 7) ypos++; break;
}
//swap position:
board[savex][savey] = '.';
board[xpos][ypos] = '_';
}
return 0;
}

Printing string pointers in c

So, essentially I have two files:
File 1:
//
// main.c
// frederickterry
//
// Created by Rick Terry on 1/15/15.
// Copyright (c) 2015 Rick Terry. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int size (char *g) {
int ofs = 0;
while (*(g+ofs) != '\0') {
++ofs;
}
return ofs;
}
int parse(char *g) {
// Setup
char binaryConnective;
int negated = 0;
// Looking for propositions
int fmlaLength = size(g);
if(fmlaLength == 0) {
return 1;
}
if(fmlaLength == 1) {
if(g[0] == 'p') {
return 1;
} else if (g[0] == 'q') {
return 1;
} else if (g[0] == 'r') {
return 1;
} else {
return 0;
}
}
// Now looking for negated preposition
if(fmlaLength == 2) {
char temp[100];
strcpy(temp, g);
if(g[0] == '-') {
negated = 1;
int negatedprop = parse(g+1);
if(negatedprop == 1) {
return 2;
}
}
}
// Checking if Binary Formula
char arrayleft[50];
char arrayright[50];
char *left = "";
char *right = "";
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int binarypresent = 0;
if(fmlaLength != 1 && fmlaLength != 2) {
if(g[0] == '-') {
int negatedBinary = parse(g+1);
if(negatedBinary == 1 || negatedBinary == 2 || negatedBinary == 3) {
return 2;
} else {
return 0;
}
}
int i = 0;
int l = 0;
int p = strlen(g);
for(l = 0; l < strlen(g)/2; l++) {
if(g[l] == '(' && g[p-l-1] == ')') {
i++;
}
}
for(int q = i; q < strlen(g); q++) {
if(g[q] == '(') {
numLeft++;
} else if(g[q] == ')') {
numRight++;
}
arrayleft[q] = g[q];
//printf("%c", arrayleft[i]);
//printf("%s", left);
if((numRight == numLeft) && (g[q+1] == 'v' || g[q+1] == '>' || g[q+1] == '^')) {
arrayleft[q+1] = '\0';
bclocation = q+1;
binaryConnective = g[q+1];
binarypresent = 1;
// printf("The binary connecive is: %c\n", binaryConnective);
break;
}
}
if(binarypresent == 0) {
return 0;
}
int j = 0;
for(int i = bclocation+1; i < strlen(g)-1; i++) {
arrayright[j] = g[i];
j++;
}
arrayright[j] = '\0';
left = &arrayleft[1];
right = &arrayright[0];
//printf("Printed a second time, fmla 1 is: %s", left);
int parseleft = parse(left);
// printf("Parse left result: %d\n", parseleft);
if(parseleft == 0) {
return 0;
}
int parseright = parse(right);
if(parseright == 0) {
return 0;
}
// printf("Parse right result: %d\n", parseleft);
if(negated == 1) {
return 2;
} else {
return 3;
}
}
return 0;
}
int type(char *g) {
if(parse(g) == 1 ||parse(g) == 2 || parse(g) == 3) {
if(parse(g) == 1) {
return 1;
}
/* Literals, Positive and Negative */
if(parse(g) == 2 && size(g) == 2) {
return 1;
}
/* Double Negations */
if(g[0] == '-' && g[1] == '-') {
return 4;
}
/* Alpha & Beta Formulas */
char binaryConnective;
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int binarypresent = 0;
int i = 0;
if(g[0] == '(') {
i++;
}
if(g[0] == '-') {
i++;
if(g[1] == '(') {
i++;
}
}
for(i; i < strlen(g); ++i) {
if(g[i] == '(') {
numLeft++;
} else if(g[i] == ')') {
numRight++;
}
if(numRight == numLeft) {
if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
bclocation = i+1;
binaryConnective = g[i+1];
binarypresent = 1;
break;
}
}
}
/* Connective established */
if(binaryConnective == '^') {
if(g[0] == '-') {
return 3;
} else {
return 2;
}
} else if(binaryConnective == '>') {
if(g[0] == '-') {
return 2;
} else {
return 3;
}
} else if (binaryConnective == 'v') {
if(g[0] == '-') {
return 2;
} else {
return 3;
}
}
}
return 0;
}
char bin(char *g) {
char binaryConnective;
char arrayLeft[50];
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int i = 0;
if(g[0] == '(') {
i++;
}
if(g[0] == '-') {
i++;
if(g[1] == '(') {
i++;
}
}
for(i; i < strlen(g); ++i) {
if(g[i] == '(') {
numLeft++;
} else if(g[i] == ')') {
numRight++;
}
int j = 0;
arrayLeft[j++] = g[i];
if(numRight == numLeft) {
if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
arrayLeft[i+1] = '\0';
bclocation = i+1;
binaryConnective = g[i+1];
return binaryConnective;
}
}
}
return binaryConnective;
}
char *partone(char *g) {
char binaryConnective;
char arrayLeft[50];
char arrayRight[50];
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int i = 0;
if(g[0] == '(') {
i++;
}
if(g[0] == '-') {
i++;
if(g[1] == '(') {
i++;
}
}
int j = 0;
for(i; i < strlen(g); ++i) {
if(g[i] == '(') {
numLeft++;
} else if(g[i] == ')') {
numRight++;
}
arrayLeft[j] = g[i];
if(numRight == numLeft) {
if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
arrayLeft[j+1] = '\0';
bclocation = i+1;
binaryConnective = g[i+1];
break;
}
}
j++;
}
int m = 0;
for(int k = bclocation+1; k < strlen(g)-1; k++) {
arrayRight[m] = g[k];
m++;
}
arrayRight[m] = '\0';
char* leftSide = &arrayLeft[0];
// printf("%s\n", leftSide);
// printf("%s\n", rightSide);
int k = 0;
k++;
return leftSide;
}
char *parttwo(char *g) {
char binaryConnective;
char arrayLeft[50];
char arrayRight[50];
int numLeft = 0;
int numRight = 0;
int bclocation = 0;
int i = 0;
if(g[0] == '(') {
i++;
}
if(g[0] == '-') {
i++;
if(g[1] == '(') {
i++;
}
}
int j = 0;
for(i; i < strlen(g); ++i) {
if(g[i] == '(') {
numLeft++;
} else if(g[i] == ')') {
numRight++;
}
arrayLeft[j] = g[i];
if(numRight == numLeft) {
if(g[i+1] == 'v' || g[i+1] == '>' || g[i+1] == '^') {
arrayLeft[j+1] = '\0';
bclocation = i+1;
binaryConnective = g[i+1];
break;
}
}
j++;
}
int m = 0;
int n = size(g) - 1;
if(g[strlen(g)-1] != ')') {
n++;
}
for(int k = bclocation+1; k < n; k++) {
arrayRight[m] = g[k];
m++;
}
arrayRight[m] = '\0';
char* leftSide = &arrayLeft[0];
char* rightSide = &arrayRight[0];
// printf("%s\n", leftSide);
// printf("%s\n", rightSide);
return rightSide;
}
char *firstexp(char *g) {
char* left = partone(g);
char leftArray[50];
int i = 0;
for(i; i < strlen(left); i++) {
leftArray[i] = left[i];
}
leftArray[i] = '\0';
char binConnective = bin(g);
int typeG = type(g);
if(typeG == 2) {
if(binConnective == '^') {
return &leftArray;
} else if(binConnective == '>') {
return &leftArray;
}
} else if(typeG == 3) {
if(binConnective == 'v')
return &leftArray;
}
char temp[50];
for(int i = 0; i < strlen(leftArray); i++) {
temp[i+1] = leftArray[i];
}
temp[0] = '-';
char* lefttwo = &temp[0];
if(typeG == 2) {
if(binConnective == 'v') {
return lefttwo;
}
} else if(typeG == 3) {
if(binConnective == '>' || binConnective == '^') {
return lefttwo;
}
}
return "Hello";
}
char *secondexp(char *g) {
// char binaryConnective = bin(g);
// char* right = parttwo(g);
// char rightArray[50];
// int i = 0;
// for(i; i< strlen(right); i++) {
// rightArray[i+1] = right[i];
// }
// rightArray[i] = '\0';
// int typeG = type(g);
// if(type(g) == 2) {
// if(binaryConnective == '^') {
// return &rightArray;
// }
// } else if(type(g) == 3) {
// if(binaryConnective == 'v' || binaryConnective == '>') {
// return &rightArray;
// }
// }
return "Hello";
}
typedef struct tableau tableau;
\
\
struct tableau {
char *root;
tableau *left;
tableau *right;
tableau *parent;
int closedbranch;
};
int closed(tableau *t) {
return 0;
}
void complete(tableau *t) {
}
/*int main(int argc, const char * argv[])
{
printf("Hello, World!\n");
printf("%d \n", parse("p^q"));
printf("%d \n", type("p^q"));
printf("%c \n", bin("p^q"));
printf("%s\n", partone("p^q"));
printf("%s\n", parttwo("p^q"));
printf("%s\n", firstexp("p^q"));
printf("Simulation complete");
return 0;
}*/
File 2:
#include <stdio.h>
#include <string.h> /* for all the new-fangled string functions */
#include <stdlib.h> /* malloc, free, rand */
#include "yourfile.h"
int Fsize = 50;
int main()
{ /*input a string and check if its a propositional formula */
char *name = malloc(Fsize);
printf("Enter a formula:");
scanf("%s", name);
int p=parse(name);
switch(p)
{case(0): printf("not a formula");break;
case(1): printf("a proposition");break;
case(2): printf("a negated formula");break;
case(3): printf("a binary formula");break;
default: printf("what the f***!");
}
printf("\n");
if (p==3)
{
printf("the first part is %s and the second part is %s", partone(name), parttwo(name));
printf(" the binary connective is %c \n", bin(name));
}
int t =type(name);
switch(t)
{case(0):printf("I told you, not a formula");break;
case(1): printf("A literal");break;
case(2): printf("An alpha formula, ");break;
case(3): printf("A beta formula, ");break;
case(4): printf("Double negation");break;
default: printf("SOmewthing's wrong");
}
if(t==2) printf("first expansion fmla is %s, second expansion fmla is %s\n", firstexp(name), secondexp(name));
if(t==3) printf("first expansion fmla is %s, second expansion fmla is %s\n", firstexp(name), secondexp(name));
tableau tab;
tab.root = name;
tab.left=0;
tab.parent=0;
tab.right=0;
tab.closedbranch=0;
complete(&tab);/*expand the root node then recursively expand any child nodes */
if (closed(&tab)) printf("%s is not satisfiable", name);
else printf("%s is satisfiable", name);
return(0);
}
If you look at the first file, you'll see a method called * firstexp(char * g).
This method runs perfectly, but only if another method called * secondexp(char * g) is commented out.
If * secondexp(char * g) is commented out, then *firstexp runs like this:
Enter a formula:((pvq)>-p)
a binary formula
the first part is (pvq) and the second part is -p the binary connective is >
A beta formula, first expansion fmla is -(pvq), second expansion fmla is Hello
((pvq)>-p) is satisfiableProgram ended with exit code: 0
otherwise, if *secondexp is not commented out, it runs like this:
Enter a formula:((pvq)>-p)
a binary formula
the first part is (pvq) and the second part is -p the binary connective is >
A beta formula, first expansion fmla is \240L, second expansion fmla is (-
((pvq)>-p) is satisfiable. Program ended with exit code: 0
As you can see, the outputs are completely different despite the same input. Can someone explain what's going on here?
In the commented-out parts of secondexp and in parttwo, you return the address of a local variable, which you shouldn't do.
You seem to fill a lot of ad-hoc sized auxiliary arrays. These have the problem that they might overflow for larger expressions and also that you cannot return them unless you allocate them on the heap with malloc, which also means that you have to free them later.
At first glance, the strings you want to return are substrings or slices of the expression string. That means that the data for these strings is already there.
You could (safely) return pointers into that string. That is what, for example strchr and strstr do. If you are willing to modify the original string, you could also place null terminators '\0' after substrings. That's what strtok does, and it has the disadvantage that you lose the information at that place: If you string is a*b and you modify it to a\0b, you will not know which operator there was.
Another method is to create a struct that stores a slice as pointer into the string and a length:
struct slice {
const char *p;
int length;
};
You can then safely return slices of the original string without needing to worry about additional memory.
You can also use the standard functions in most cases, if you stick to the strn variants. When you print a slice, you can do so by specifying a field width in printf formats:
printf("Second part: '%.*s'\n", s->length, s->p);
In your parttwo() function you return the address of a local variable
return rightSide;
where rightSide is a pointer to a local variable.
It appears that your compiler gave you a warning about this which you solved by making a pointer to the local variabe arrayRight, that may confuse the compiler but the result will be the same, the data in arrayRight will no longer exist after the function returns.
You are doing the same all over your code, and even worse, in the secondexp() function you return a the address of a local variable taking it's address, you are not only returning the address to a local variabel, but also with a type that is not compatible with the return type of the function.
This is one of many probable issues that your code may have, but you need to start fixing that to continue with other possible problems.
Note: enable extra warnings when compiler and listen to them, don't try to fool the compiler unless you know exactly what you're doing.

Resources