I'm trying to write a function, that would check for a matching parentheses.
For example, if the given string is "(1+1))" it would print false otherwise it's true.
However, in my code it's printing false no matter what the case is.
bool isMatched(char pran[]) {
bool completetd = true;
int count = 0;
for (int i = 0; pran[i] != '\0'; i++) {
if (pran[i] == '('){
count++;
}
else {
// It is a closing parenthesis
count--;
}
if (count < 0) {
// there are more Closing parenthesis
completetd = false;
break;
}
// If count is not zero, there are more opening parenthesis
if (count != 0) {
completetd = false;
}
}
return completetd;
}
int main() {
char arr[] = "((1+a))";
if (isMatched(arr)) {
printf("TRUE \n");
}
else {
printf("FALSE \n");
}
return 0;
}
I would appreciate any help.
You can try this not sure if this is what you are looking for.
bool isMatched(char pran[]) {
int open = 0;
int close = 0;
for (int i = 0; pran[i] != '\0'; i++) {
if (pran[i] == '('){
open++;
}
if (pran[i] == ')'){
close++;
}
}
// Check if both match
if(open == close){
return true;
}
return false;
}
int main() {
char arr[] = "((1+a))";
if (isMatched(arr)) {
printf("TRUE \n");
}
else {
printf("FALSE \n");
}
return 0;
}
By adding a
printf("got 1 (!\n"); next to count++;
and a
printf("got 1 )!\n"); next to count--;,
you get:
Got 1 (!
Got 1 (!
Got 1 )!
Got 1 )!
Got 1 )!
FALSE
This shows that you have a validation problem with your checking logic
As pointed-out in the comments, replace your else with else if (pran[i] == ')') { will fix that part for you.
But the real problem lies with your last validation.
Take it out of the for loop. It sets the value to false as soon as you detect a parenthesis.
Thus, take this:
// If count is not zero, there are more opening parenthesis
if (count != 0) {
printf("Count: %d\n",count);
completetd = false;
}
}
and make it this:
}
// If count is not zero, there are more opening parenthesis
if (count != 0) {
printf("Count: %d\n",count);
completetd = false;
}
Related
Every time I try and run only the first loop runs. It ask how many items were sold and then just stops running. I'm not really sure what I did wrong. If anyone has any tips that would be great(story if there's some formatting issues, SO wouldn't let me post the question without them).
Everything else looks fine but if any other improvements could be made please let me know.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int digit(char term[])
{
int i = 0;
int val = 0;
while (term[i] != '\0')
{
val = val * 10 + term[i] - '0';
i++;
}
return val;
}
void error()
{
printf("Error: Sales figures must be numbers.\n");
printf("Please try again.\n");
}
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if( isdigit(term[i]) == 0)
{
return false;
i++;
}
}
return true;
}
int main()
{
int sales[3][2], costs[3] = {3, 4, 1}, weekends[2] = {0, 0};
int i, j, val;
char term[100];
while (1)
{
printf("Number of Bagel sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[0][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[1][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Saturday: ");
scanf("%s", term);
if (isnumber(term) == false)
{
error;
}
else
{
sales[2][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Bagel sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[0][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[1][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[2][1] = digit(term);
break;
}
}
for (i = 0; i < 2, i++;)
{
for (j = 0; j < 3, j++;)
{
weekends[i] += costs[j] * sales[i][j];
}
}
printf("\n");
for (i = 0; i < 3, i++;)
{
printf("%d", costs[i]);
}
printf(".");
for (i = 0; i < 3, i++;)
{
for (j = 0; j < 2, j++;)
{
printf("%d", sales[i][j]);
}
if (i == 0)
{
printf(" = ");
printf("%d %d", weekends[0], weekends[1]);
}
printf("\n ");
}
printf("\nTotal sales on Saturday: $%d", weekends[0]);
printf("\nTotal sales on Sunday: $%d", weekends[1]);
printf("\nTotal sales over the weekend: $%d", weekends[0] + weekends[1]);
return 0;
}
You have an infinite loop in isnumber(). It will return false if the first character is not a digit. But if the first character is a digit, it never increments i, so it keeps testing the first character repeatedly.
i++ should not be in the if statement.
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if(!isdigit(term[i]))
{
return false;
}
i++;
}
return true;
}
And as others have pointed out, you need to put () after error to call it.
As for your programming stopping; you use isdigit on line 30 without declaring it.
Along that, you use error everywhere without invoking it. Use error() to invoke the function and print the error-information.
Just a small formatting tip as well: instead of if (something == false) use if (!something)
Your use of error; is incorrect. You should write error(); instead.
I'm trying to check the return value of the function in an if statement so that I will return its value if its 1, but it gets called when I do it in an if statement. Is there a way where I prevent the call in the if statement. I want to check all three function but if I checked one without if statement then it returns 0 and stops.
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
else
{
string chars = argv[1];
if (check_length(chars) == 1)
{
return check_length(chars);
}
else if (check_rc(chars) == 1)
{
return check_rc(chars);
}
else if (check_alpha(chars) == 1)
{
return check_alpha(chars);
}
}
string ptext;
ptext = get_string("Input Text: \n");
}
int check_rc(string chars)
{
for (int i = 0, n = strlen(chars); i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (!(i == j))
{
if (chars[i] == chars[j])
{
printf("Key must not contain repeated characters. \n");
return 1;
}
}
}
}
return 0;
}
int check_alpha(string chars)
{
for (int i = 0, n = strlen(chars); i < n; i++)
{
if (isdigit(chars[i]))
{
printf("Key must only contain alphabetic characters. \n");
return 1;
}
}
return 0;
}
int check_length(string chars)
{
int charLength = strlen(chars);
if( charLength !=26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
return 0;
}
Your Question is hard to understand but i am assuming that if you want to reduce theamount of unnessecary check you could apply the concept of short-circuit evalution of &&
or u could just not use (else if). and use 3 if statement instead.
all following(else if) is skip if one of the if statement before it is true.
hope this help.
You can save the return value in a local variable and use it all the way:
int check_rc_val = check_length(chars);
if (check_rc_val == X)
{
return check_rc_val;
}
int check_rc_val = check_rc(chars);
if (check_rc_val == Y)
{
return check_rc_val;
}
int check_alpha_val = check_alpha(chars);
if (check_alpha_val == Z)
{
return check_alpha_val;
}
I have to scan this file which partly contains
SNOL
INTO num IS 8
INTO res IS 9
and the output of the code below is
Program starts...
Set value of num to 0
Set value of res to 8
input msg
which is wrong because num should be 8 and res should be 9
why is it num scanning 0 instead of 8?
and why doesn't the code work anymore if I assign number to num and number to res?
num = number;
//Tokenizer functions//
bool isLowerCase(const char *object)
{
int i;
int len = strlen(object);
for(i = 0; i < len; i++) {
if(object[i] >= 'a' && object[i] <= 'z') {
return true;
}
}
return false;
}
//function to check if character is Float.
bool objectFloat(const char* object) {
//check if 1st character is a digit, if not then return false,
otherwise
return true.
if(!isdigit(object[0]))
return false;
// Check if the 2nd character to the last are digits or periods.
// If not, return false otherwisereturn true
int periods = 0; //initialize how many periods in the object to zero
int i;
//if character is a period then increment periods.
for(i = 1; i < strlen(object); i++) {
if(object[i] == '.') {
periods++;
}
//return false if character is not a digit
else if(!isdigit(object[i])) {
return false;
}
}
// return true if there is only one period.
return periods == 1;
}
//function to check if character is a keyobject.
bool isKeyobject(const char* object) {
char keyobjects[11][11] = { "SNOL", "LONS", "INTO", "IS", "MULT", "BEG",
"PRINT", "ADD", "SUB", "DIV", "MOD" };
int i;
for(i = 0; i < 11; i++) {
// Check if object is equal to keyobjects at index i
// If yes, return true
if(isLowerCase(object))
return false;
if(strcmp(object, keyobjects[i]) == 0) {
return true;
}
}
//object is not equal to any of the keyobjects so return false
return false;
}
//Function to check if every character is an integer
// If not, return false otherwise return true
bool objectInt(const char* object) {
int i;
for(i = 0; i < strlen(object); i++) {
if(!isdigit(object[i])) return false;
}
return true;
}
bool objectIsVariable(const char* object) {
// Check if alphanumeric character & lower case
// If not, return false
int i;
for(i = 0; i < strlen(object); i++) {
if(!isalnum(object[i]) && !isLowerCase(object)) return false;
}
return true;
}
int main() {
FILE *s_path = fopen("test.snol", "r");
int number = 0;
int num, res;
if(isKeyobject(object) && strcmp(object, IsitSNOL) == 0) {
printf("Program starts...\n");
}
else if(isKeyobject(object) && strcmp(object, IsitINTO) == 0) {
printf("Set value of ");
}
if(objectInt(object)) {
number = atoi(object);
}
else if(objectFloat(object)) {
number = atof(object);
}
if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
//if float
printf("num to %d\n", number);
num == number;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
//if float
printf("res to %d\n", number);
res == number;
}
else if(isKeyobject(object) && strcmp(object, IsitBEG) == 0) {
printf("input msg\n");
scanf("%s", msg);
fscanf(s_path, " %s", &object);
printf("INPUT(%s): %s\n", object, msg);
}
}
} // END MAIN -----------------------------------//
The problem seem to be that you read the number after the variable name but you do the print before.
So your sequence is:
Keyobject INTO
objectIsVariable num // Now you print the value
objectInt // Now you read the value
You need to postpone the printing until you have actually read the value.
This is not a very elegant solution, but you can try like:
int flag = 0;
if(objectInt(object)) {
number = atoi(object);
if (flag == 1)
{
num = number;
printf("num to %d\n", number);
}
else if (flag == 2)
{
res = number;
printf("res to %d\n", number);
}
else
{
printf("Illegal flag\n");
}
flag = 0;
}
if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
flag = 1;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
flag = 2;
}
When I execute my code, scanf("%d", &n); don't scan anything, I mean, if I introduce any number it doesn't do anything, regardless of the numbers I introduce.
void testEsPrimo() {
int n;
printf("Comprobando si un número es o no primo\n");
printf("Teclee un número entero: ");
fflush(stdout);
scanf("%d", &n); //<---- The problem ?
if(esPrimo(n) == cierto){
printf("%d es primo\n", n);
}else{
printf("%d NO es primo\n", n);
}
fflush(stdout);
}
Logico esPrimo(int n){
int divisor;
int esPrimox;
for(divisor = 2; sqrt(n); divisor++) {
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
}
}
}
if(esPrimox == 1) {
return cierto;
}
return falso;
}
This is my esPrimo code that is about decide if a number is prime or not.
typedef enum {falso, cierto} Logico;
and this is Logico, defined in a .h file
PD: This are my first steps on C so my code might be bad.
PD2: Excuse me for my bad English I'm not native and my English isn't really good.
Your scanf is perfect.
I think that your mistake is the loop for from esPrimo. Actually you have an infinite loop, because sqrt(n) has always the same value and it isn't a boolean expression.
Change your loop:
for(divisor = 2; sqrt(n); divisor++) {
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
}
}
}
for this:
for(divisor = 2; divisor < sqrt(n); divisor++) {
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
}
}
}
But then you have a problem when you know that your number is not prime: you have to finish the loop.
You can do this:
for(divisor = 2; divisor < sqrt(n); divisor++) {
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
break;
}
}
}
But if you can avoid using breakinside a loop for, don't use that.
With complicated algorithms you have a clean code with that, but when you read a loop for, usually your understand that the loop do a exactly number of iterations. If you have another flag to end the loop, use while.
While (divisor < sqrt(n) && esPrimox == 0){
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
}
}
}
There are 2 main problems in esPrimo.
First, the for loop will not terminate:
for(divisor = 2; sqrt(n); divisor++) {
Change the condition to:
for(divisor = 2; divisor <= sqrt(n); divisor++) {
Second is in the logic. If you find that n is not a prime, you need to break the loop or the function will always return true. You could do it either with the break statement or by checking the value of esPrimox in the loop condition.
Here's how to do it using break:
for(divisor = 2; divisor <= sqrt(n); divisor++) { /* fixed loop condition */
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
break; /* break the loop */
} else {
esPrimox =1;
}
}
}
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.