left to right evaluation - c

I am trying to carry out left to right evaluation in C. No order of precedence whatsoever. So 5+3*2 should be 16. I know how to do that with 2 numbers and an operator, however, I cannot figure out how to do the same thing for an expression like 2+4-5+2.
This is what I have for 2 numbers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
int main(void)
{
char exp[SIZE];
int ans,c, i=0;
int length;
printf("Enter your expression: ");
fgets(exp, 20, stdin);
length = strlen(exp);
--length;
for(int j=0; j<length; j++)
{
while (exp[i]!='\n')
{
// putchar(exp[i]);
i++;
switch (exp[i])
{
case '+':
ans = (exp[i]-'0') + (exp[2]-'0');
printf("The answer is %d\n", ans);
break;
case '-':
ans = (exp[0]-'0') - (exp[2]-'0');
printf("The answer is %d\n", ans);
break;
case '*':
ans = (exp[0]-'0') * (exp[2]-'0');
printf("The answer is %d\n", ans);
break;
case '/':
ans = (exp[0]-'0') / (exp[2]-'0');
printf("The answer is %d\n", ans);
break;
default:
break;
}
}
}
exit(0);
}
Any help is appreciated.

Assuming alternating numbers and operators with single-character numbers (which seems to be what you assumed), a minimal implementation similar to your original implementation is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
int main(void) {
char exp[SIZE];
int ans, length, i;
printf("Enter your expression: ");
fgets(exp, 20, stdin);
ans = exp[0] - '0';
length = strlen(exp) - 1;
for (i = 0; i < length && exp[i] != '\n'; i++) {
switch (exp[i]) {
case '+':
ans += exp[i+1] - '0';
break;
case '-':
ans -= exp[i+1] - '0';
break;
case '*':
ans *= exp[i+1] - '0';
break;
case '/':
ans /= exp[i+1] - '0';
break;
default:
break;
}
}
printf("The answer is %d\n", ans);
return 0;
}

As per your given example, assumed that each number will be single digit number.
Try this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
int main(void)
{
char exp[SIZE];
int ans,c, i=0;
int length,j;
printf("Enter your expression: ");
fgets(exp, 20, stdin);
length = strlen(exp);
char ch;
ans = exp[i]-'0';
i++;
while (i <= length)
{
switch (exp[i])
{
case '+':
i++;
ans += (exp[i]-'0');
break;
case '-':
i++;
ans -=(exp[i]-'0');
break;
case '*':
i++;
ans *=(exp[i]-'0');
break;
case '/':
i++;
ans /= (exp[i]-'0');
break;
default:
break;
}
i++;
//printf("The answer is %d\n", ans);
}
printf("The answer is %d\n", ans);
exit(0);
}

Related

Printing the wrong value when evaluating postfix expressions

I wrote a program which is used to evaluate postfix expression, I am not getting any compiler error/warnings but I am not getting the correct output, which probably means the issue is with the calculation but I don't know where.
My Code:
#include <ctype.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 20
char s[MAX], top = 0;
void main() {
char postfix[MAX], ch;
int i, op1, op2, res;
clrscr();
printf("\n\t\t program to evaluate postfix expression");
printf("\n\t\t.......");
printf("\n enter the postfix expression:\n");
scanf("%s", &postfix);
for (i = 0; i < strlen(postfix); i++) {
ch = postfix[i];
if (isdigit(ch))
push(ch = '0');
else {
op2 = pop();
op1 = pop();
switch (ch) {
case '+':
res = op1 + op2;
break;
case '-':
res = op1 - op2;
break;
case '*':
res = op1 * op2;
break;
case '/':
res = op1 / op2;
break;
case '^':
res = pow(op1, op2);
break;
default:
printf("invalid choice");
}
push(res);
}
}
printf("result of above expression is:%d\n", pop());
getch();
}
push(int element) {
++top;
s[top] = element;
}
int pop() {
int element;
element = s[top];
--top;
return (element);
}
You should fix the typo and change push(ch = '0'); to
push(ch - '0');
ch is a character, isdigit(ch), or better isdigit((unsigned char)ch) tells you it is a digit, ch - '0'is the digit value, a number in the range0to9`.
Your code ch = '0' stores the digit '0' into ch and pushes this value, which is the character code or 0 on your system, 48 in ASCII.

C switch returns default inside if/else

I have the following problem: i have two switch statements. They work perfectly when separated, but the minute I put both of them into an if/else. switch always returns the default(error). I am sorry for the difficult wording, now I have copied in the whole program so you can check it, I have tried what you said, but it didn't seem to help. So for an example, when it asks for the number, my input is 2. Then it asks for what character do I want to choose (+,-,/,*), and no matter which I write in, it gives me the default output like I have type a wrong character.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void tomb_beolvas(int *szamok);
void tomb_kiir(int *szamok);
void tomb_sorrend(int *szamok);
void sorrend_kiir(int *szamok);
void kalkulator(int *szamok);
int main() {
int szamok[10];
tomb_beolvas(szamok);
tomb_kiir(szamok);
tomb_sorrend(szamok);
sorrend_kiir(szamok);
kalkulator(szamok);
return 0;
}
void tomb_beolvas(int *szamok) {
int i;
srand(time(0));
for (i = 0; i < 10; i++) {
szamok[i] = rand() % (10) + 1;
}
return;
}
void tomb_kiir(int *szamok) {
int i;
for (i = 0; i < 10; i++) {
printf("%d\n", szamok[i]);
}
return;
}
void tomb_sorrend(int *szamok) {
int i;
int a, j;
for (i = 0; i < 10; ++i) {
for (j = i + 1; j < 10; j++) {
if (szamok[i] > szamok[j]) {
a = szamok[i];
szamok[i] = szamok[j];
szamok[j] = a;
}
}
}
return;
}
void sorrend_kiir(int *szamok) {
int i;
for (i = 0; i < 10; i++) {
printf("\n%d", szamok[i]);
}
return;
}
void kalkulator(int *szamok) {
char jel;
int a, b, donto;
printf("\nKerem valassza ki milyen modon szeretne megadni az adatokat:\n"
" 1.: egyben(peldaul 5. + 8.)\n"
" 2.: kulon(peldaul + aztan 5. es 8.)\n");
scanf("%d", &donto);
if (donto == 1) {
printf("\nKerem irja be hanyadik szamokat szeretne es koze hogy milyen kalkulaciot szeretne vegezni(pl.: 5 + 8):\n");
while (scanf("%d %c %d", &a, &jel, &b)) {
switch (jel) {
case '+':
printf("%d", szamok[a-1] + szamok[b-1]);
break;
case '-':
printf("%d", szamok[a-1] - szamok[b-1]);
break;
case '*':
printf("%d", szamok[a-1] * szamok[b-1]);
break;
case '/':
printf("%d", szamok[a-1] / szamok[b-1]);
break;
}
}
} else
if (donto == 2) {
printf("Adj meg egy jelet (+, -, *, /): ");
scanf("%c", &jel);
printf("add meg hanyadik szamokkal akarsz szamolni: ");
scanf("%d %d", &a, &b);
switch (jel) {
case '+':
printf("%d + %d = %d", szamok[a], szamok[b], szamok[a] + szamok[b]);
break;
case '-':
printf("%d - %d = %d", szamok[a], szamok[b], szamok[a] - szamok[b]);
break;
case '*':
printf("%d * %d = %d", szamok[a], szamok[b], szamok[a] * szamok[b]);
break;
case '/':
printf("%d / %d = %d", szamok[a], szamok[b], szamok[a] / szamok[b]);
break;
// operator doesn't match any case constant +, -, *, /
default:
printf("Error! operator is not correct");
}
}
return;
}
There is nothing wrong with your switch statements, however:
int szamok[]={};
You can't make an empty array, you should declare it with a size:
const int size = 10;
int szamok[10];
From what I can tell, you invoked undefined behavior by accessing szamok with an out of bounds index (which is any positive number, as you got an empty array).
I'm afraid your problem is a classic scanf() issue: instead of scanf("%c", &jel); you should use
scanf(" %c", &jel); // notice the initial space before the `%c`
This skips the newline left pending after the scanf("%d", &donto);.

Why my converter doesn't count the last digit?

This program is about converting Roman number to decimal number. The program can convert the alphabet to number but it can not process the last roman digit. I think my flow is alright but the output is not right. Can any body give me a helping hand?
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int roman_to_int(const char s[], int length) {
// Please complete the function body
int ans = 0, value[length];
for (int i = 0; i < length; i++) {
switch (s[i]) {
case 'I': value[i] = 1; break;
case 'V': value[i] = 5; break;
case 'X': value[i] = 10; break;
case 'L': value[i] = 50; break;
case 'C': value[i] = 100; break;
case 'D': value[i] = 500; break;
case 'M': value[i] = 1000; break;
}
}
for (int i = 0; i < length - 1; i++) {
if (value[i] >= value[i+1])
ans += value[i];
else {
ans = ans + value[i+1] - value[i];
i++;
}
}
return ans;
}
int main() {
char roman_num[] = "III";
char roman_num_2[] = "CXXIII";
char roman_num_3[] = "MMMCDLIX";
printf("roman_to_int(%s) = %d\n", roman_num,
roman_to_int(roman_num, strlen(roman_num)));
printf("roman_to_int(%s) = %d\n", roman_num_2,
roman_to_int(roman_num_2, strlen(roman_num_2)));
printf("roman_to_int(%s) = %d\n", roman_num_3,
roman_to_int(roman_num_3, strlen(roman_num_3)));
}
You should add the value of the last roman digit after the end of the second loop.
As an alternative, you could make value on entry longer than n and set the last entry to 0 so you won't need the make a special case of the last roman digit.
Note that you should also handle the case of unrecognised roman digits: either by ignoring them or by returning an error code, such as a negative value -1.
It is also simpler for roman_to_int to take a null terminated C string and compute the length there.
Here is a modified version:
#include <stdio.h>
#include <string.h>
int roman_to_int(const char s[]) {
// Please complete the function body
int length = strlen(s);
int ans = 0, value[length + 1];
for (int i = 0; i < length; i++) {
switch (s[i]) {
case 'I': value[i] = 1; break;
case 'V': value[i] = 5; break;
case 'X': value[i] = 10; break;
case 'L': value[i] = 50; break;
case 'C': value[i] = 100; break;
case 'D': value[i] = 500; break;
case 'M': value[i] = 1000; break;
default: return -1;
}
}
value[length] = 0;
for (int i = 0; i < length; i++) {
if (value[i] >= value[i + 1])
ans += value[i];
else
ans -= value[i];
}
return ans;
}
int main() {
char roman_num[] = "III";
char roman_num_2[] = "CXXIII";
char roman_num_3[] = "MMMCDLIX";
char roman_num_4[] = "MMMCDLIZ"; // error
printf("roman_to_int(%s) = %d\n", roman_num, roman_to_int(roman_num));
printf("roman_to_int(%s) = %d\n", roman_num_2, roman_to_int(roman_num_2));
printf("roman_to_int(%s) = %d\n", roman_num_3, roman_to_int(roman_num_3));
printf("roman_to_int(%s) = %d\n", roman_num_4, roman_to_int(roman_num_4));
return 0;
}

Turn character into number and arrange the number in c

int T, i;
scanf("%d", &T);
char a[T], b[T], c[T];
int temp[T], temp2[T], temp3[T];
int point1[T], point2[T], point3[T];
for(i=0;i<T; i++){
scanf("%c %c %c", &a[i], &b[i], &c[i]);
switch(a[i]){
case '!':
point1[i] = 5;
break;
case '%':
point1[i] = 4;
break;
case '&':
point1[i] = 3;
break;
case '^':
point1[i] = 2;
break;
case '|':
point1[i] = 1;
break;
default :
point1[i]=10;
}
switch(b[i]){
case '!':
point2[i] = 5;
break;
case '%':
point2[i] = 4;
break;
case '&':
point2[i] = 3;
break;
case '^':
point2[i] =2;
break;
case '|':
point2[i] =1;
break;
default :
point2[i]=10;
}
switch(c[i]){
case '!':
point3[i] = 5;
break;
case '%':
point3[i] = 4;
break;
case '&':
point3[i] = 3;
break;
case '^':
point3[i] =2;
break;
case '|':
point3[i] =1;
break;
default :
point3[i]=10;
}
if(point1[i]<point2[i]) {
temp[i]=point1[i];
point1[i]=point2[i];
point2[i]=temp[i];
}
if(point1[i]<point3[i]){
temp2[i]=point1[i];
point1[i]=point3[i];
point3[i]=temp2[i];
}
if(point2[i]<point3[i]){
temp3[i]=point2[i];
point2[i]=point3[i];
point3[i]=temp3[i];
}
printf("%d %d %d\n", point1[i], point2[i], point3[i]);
}
return 0;
So first of all, i was asked to input certain characters randomly arranged and make them printed arranged from the highest precedence.
The precedence of the operators (from the highest to the lowest) are "!" (logical NOT), "%" (remainder), "&"
(bitwise AND), "^" (bitwise XOR), , "|" (bitwise OR).
So i try to change the characters into numbers and try to arrange the number first then change the number again to the characters.
But when i try to check if the numbers have been correctly arranged, it's not.
Any idea what's wrong with my code?
Or any idea to make my code simpler without having to turn the characters into numbers?
Here's the sample
Sample Input
3
& ^ %
& ^ !
& ^ !
Sampe Output
Case #1: % & ^
Case #2: ! & ^
Case #3: ! & ^
A few issues have already been noted in the comments. Specifically, scanf leaves the trailing newline in the input buffer. You can fix that using the suggestion by #4386427.
You can avoid manually assigning numbers for each character by noting that their ASCII is already in required order. That is, '!' < '%' < '&' < '^' < '|'. So you can simply read them into a char array of the appropriate size and just sort them in ascending order before printing the char array. This will significantly shorten and clean up your code.
#include <stdio.h>
#include <stdlib.h>
int cmpfunc (const void * a, const void * b) {
return (*((char*)a) > *((char*)b)) - (*((char*)a) < *((char*)b));
}
int main(int argc, char *argv[])
{
int T, i, j;
scanf(" %d", &T);
/* TODO: Check if number of chars per line is T or 3. */
char *a = malloc(T * sizeof(char));
for(i = 0; i < T; i++) {
for (j = 0; j < T; j++) {
scanf(" %c", &a[j]);
}
printf("Before sort\n");
for (j = 0; j < T; j++) {
printf("%c ", a[j]);
}
printf("\n");
qsort(a, T, sizeof(a[0]), cmpfunc);
printf("After sort\n");
for (j = 0; j < T; j++) {
printf("%c ", a[j]);
}
printf("\n");
}
free(a);
return 0;
}

How do I detect an operator vs. int in C using scanf?

How do I read in the following input in my RPN calculator so that it will find the operator no matter what order?
2
2+
4
As of now my scanf only sees the first char in the string and I can only do this:
2
2
+
4
I'm also trying to add an option for integer vs floating point mode. (ex. when 'i' is entered, operate in floating point and vice versa.)
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int *p;
int *tos;
int *bos;
void push(int i);
int pop(void);
int main (void)
{
int a, b;
//float c, d;
char s[80];
//char op; //declare string of 80 chars
p = (int *) malloc(MAX*sizeof(int)); //get stack memory
if (!p){
printf("Allocation Failure\n");
exit(1);
}
tos = p;
bos = p + MAX-1;
printf("\nRPN Calculator\n");
printf("Enter 'i' for integer mode\n");
printf("Enter 'f' for floating point mode\n");
printf("Enter 'q' to quit\n");
do {
printf("> ");
// gets(s);
// scanf("%s", s); //read integer
scanf("%s", s);
// switch (*s) {
switch(*s) {
case 'i':
printf("(Integer Mode)\n");
break;
case 'f':
printf("(Floating Point Mode)\n");
break;
case '+':
a = pop();
b = pop();
printf("%d\n", a+b);
push(a+b);
break;
case '-':
a = pop();
b = pop();
printf("%d\n", b-a);
push(b-a);
break;
case '*':
a = pop();
b = pop();
printf("%d\n", a*b);
push(a*b);
break;
case '/':
a = pop();
b = pop();
if(a == 0){
printf("Cannot divide by zero\n");
break;
}
printf("%d\n", b/a);
push(b/a);
break;
case '.':
a = pop();
push(a);
printf("Current value on top of stack: %d\n", a);
break;
default:
push(atoi(s));
}
} while (*s != 'q');
return 0;
}
// Put an element on the stack
void push (int i)
{
if (p > bos){
printf("Stack Full\n");
return;
}
*p = i;
p++;
}
// Get the element from the top of the stack
int pop (void)
{
p--;
if(p < 0) {
printf("Stack Underflow\n");
return 0;
}
return *p;
}
Your scanf reads the whole string. It's the following switch that judges by the first character and misses that + in 2+.
To improve it you can use the strtol function. It will parse an integer out of the string and return to you the location where the integer ended - if that's still not the end of the string, there may be an operator there.
A similar function for floating point numbers is strtod.
Here's some sample code of strtol applicable to your example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* input = "25+";
char* endptr;
int val = strtol(input, &endptr, 10);
if (*endptr == '\0')
{
printf("Got only the integer: %d\n", val);
}
else
{
printf("Got an integer %d\n", val);
printf("Leftover: %s\n", endptr);
}
return 0;
}
I'm not sure if I fully understood your question, but you could iterate through the string like this:
for(i = 0; i < strlen(s); i++)
{
// Here comes your switch section like this
switch(s[i]) {
.....
}
}
Remember also to include string.h.
I really didn't understand your code.
If expect the user to enter one character each time, I mean one character + enter, you should use a simple char instead of char[]. And if you pretend to use a string you should receive it and parse it pzico said.
You could do something like that. The problem would be in the treatment of numbers with multiple digits, but thinking a little bit you can fix this problem. I wrote an attempt, but I'm pretty sure it's not going to work.
printf("\nRPN Calculator\n");
printf("Enter 'i' for integer mode\n");
printf("Enter 'f' for floating point mode\n");
printf("Enter 'q' to quit\n");
scanf("%c", s);
switch(*s){
case 'i':
printf("(Integer Mode)\n");
break;
case 'f':
printf("(Floating Point Mode)\n");
break;
case 'q':
printf("Bye Bye\n");
return;
break;
}
printf("Enter the expression one character each time\n");
do {
scanf("%c", s);
switch(s) {
case '+':
a = pop();
b = pop();
printf("%d\n", a+b);
push(a+b);
break;
case '-':
a = pop();
b = pop();
printf("%d\n", b-a);
push(b-a);
break;
case '*':
a = pop();
b = pop();
printf("%d\n", a*b);
push(a*b);
break;
case '/':
a = pop();
b = pop();
if(a == 0){
printf("Cannot divide by zero\n");
break;
}
printf("%d\n", b/a);
push(b/a);
break;
case '.':
a = pop();
push(a);
printf("Current value on top of stack: %d\n", a);
break;
default:
a = pop()*10+atoi(s);
push(a);
}
} while (s != 'q');
Another problem in your code is in your pop function. What do you want to do with this test:
if(p < 0) {
printf("Stack Underflow\n");
return 0;
}
You are expecting your pointer to reach the address 0?
Anyway I hope this is not your homework.

Resources