Δημοσιεύτηκε: 28 Ιούλ 2011, 14:41
από stamatiou
Τέλειωσα την άσκηση
Spoiler: show
Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>

char *s_get(char *s, size_t len)
{
char *cp;
for (cp=s; (*cp=getc(stdin)) != '\n' && (cp-s) < len-1; cp++ ); // for-loop with empty body
*cp = '\0'; // null-terminate last character
return s;
}

int main(void) {
char i3,inbuf[255+1];
int i1,i2;
printf("Insert the first number: ");
s_get(inbuf,256);
i1 = atoi(inbuf);
printf("Insert the second number: ");
s_get(inbuf,256);
i2 = atoi(inbuf);
printf("Insert the operator (+ - * /): ");
s_get(inbuf,256);
i3 = *inbuf;
switch (i3) {
case '*':
printf("%d * %d = %d\n",i1,i2,i1*i2);
break;
case '-':
printf("%d - %d = %d\n",i1,i2,i1-i2);
break;
case '+':
printf("%d + %d = %d\n",i1,i2,i1+i2);
break;
case '/':
printf("%d / %d = %d\n",i1,i2,i1/i2);
break;
}
return 0;
}