logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jul15/andorxor.c

/*

doing an AND OR XOR calculator using command line arguments

this shows how we can handle command line arguments, and making
sure it can handle when the end user enters input that isn't valid.

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){

	if(argc < 4){
		printf("Usage: %s <number> <operator> <number>\n", argv[0]);
		exit(1);
	}

	
	if((strcmp(argv[2], "and") != 0) && (strcmp(argv[2], "or") != 0) && (strcmp(argv[2], "xor") !=0)){
		printf("This operation is not supported\n");
		exit(1);
	} 
	
	int a, b, c;

	a = atoi(argv[1]);
	b = atoi(argv[3]);


	if(strcmp(argv[2], "and") == 0){
		c = a & b;
	}
	
	if(strcmp(argv[2], "or") == 0){
		c = a | b;
	}

	if(strcmp(argv[2], "xor") == 0){
		c = a ^ b;
	}


	printf("Value: %d\n", c);

	return 0;
}