: Well i suppose this is the best i could do..........
: #include <stdio.h>
: #include <stdlib.h>
: #include <time.h>
: #include <conio.h>
Non-standard header.
: #include <string.h>
: #include <dos.h>
: #define TRUTH(a) ((a) != 0)
: /*** Prototype Section* **/
: void expression();
: int main(void)
: {
: int p1, p2;
: clrscr();
Non-standard function call.
: printf("a b ~a a.b a+b a^b\n");
: printf("= = == === === ===\n");
: for (p1 = 0; p1 < 2 ; ++p1)
: {
: for (p2 = 0; p2 < 2; ++p2)
: {
: int a, b;
: a = TRUTH(p1); b = TRUTH(p2);
You don't need these TRUTH() macros. ((0) != 0) evaluates to 0,
((1) != 0) evaluates to 1. All non-zero values are assumed to be true.
: printf("%d %d ", a, b);
: printf("%d ", TRUTH(!a)); /* not A */
: printf("%d ", TRUTH(a && b)); /* A and B */
: printf("%d ", TRUTH(a || b)); /* A or B */
: printf("%d", TRUTH(a ^ b)); /* A xor B */
: printf("\n");
: }
: }
: printf("enter a key to continue");
: getch();
Non-standard function call.
: expression();
: return EXIT_SUCCESS; /* above source is taken from a guy name erm? i
: forgot.. sorry at comp.lang.C*/
: }
: void expression()
: {
: char expression[6], *check,*check2, left='(',right=')';
: int a,b,c,value;
: time_t t;
: do
: {
: printf("\nEnter a expression to be evaluated, (only A,B,C can be use).Enter
: to exit\n: ");
: gets(expression);
Using gets() with a buffer as small as 6 is asking for trouble. Better
use fgets() from the stream stdin.
: check=strchr(expression,left);
: check2=strchr(expression,right); /* idea gotten from Zoran Cutura*/
: if ((check!=0) && (check2== 0)||(check==0) && (check2!= 0)) /* check for
: missing brackets*/
: {
: printf("U have miss a bracket,enter again: ");
: gets(expression);
: }
Note: If the user STILL has a missing bracket, the program will happily
evaluate the incorrect expression now. This should be made into a while-
loop instead of a simple if.
: printf("\nEnter value of A: ");
: scanf("%d",&a);
: printf("\nEnter value of B: ");
: scanf("%d",&b);
: printf("\nEnter value of C: ");
: scanf("%d",&c);
You should check the return values of scanf() to see if the user really
entered numbers, and not something like "true", "yes", "one", "what's it
to you?".
: srand((unsigned)time(&t));
: value=rand()%2; / * generate a random 0 or 1. bah.. my teacher is going
: to kill me..*/
: printf("\nThe value for %s is %d", expression, value); /*50% chance of
: getting a right answer*/
: flushall();
: }
: while (strcmp(expression,'\0')!=0);
strcmp() compares two strings. '\0' is not a string. If you want to
check if expression is an empty string, use:
while (strcmp(expression, ""));
Note that strcmp() returns 0 if the strings DO match, and non-0 if they
DON'T match.
: {
: printf("\n\nThanks for Inputs");
: printf("\nProgram shutting down");
: delay(2000);
Non-standard function.
: return;
: }
: }
: yeah yeah yeah, i know i cannot use conio.h, nor gets, nor clrscr(); nor
: getch(); nor waever...
: but if i do't use them, the program can't works.( i try it before)
Why not? The program doesn't READ the screen, it just WRITES to it. What
possible harm could removing clrscr() do?
: btw if i don't use gets(expression); what can i use? scanf?
No, scanf() is only slightly better. You should use fgets(), it can be
told the maximum size of the input.
: then again.. my program can only have a rate of 50% chance of getting a
: right answer!! bah..
: any antidote or remedy for it?
Right now I have only answered your questions about input and output.
Evaluating the expression is a different kettle of fish altogether, and
I don't know myself yet what would be the correct algorithm in this
case.
--
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Products like that make me wish I could menstruate."
- Andy Richter