fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. char operator;
  5. double num1, num2;
  6. printf("Enter an operator (+, -, *, /, %%): ");
  7. scanf(" %c", &operator);
  8. printf("Enter two operands: ");
  9. scanf("%lf %lf", &num1, &num2);
  10.  
  11. switch (operator) {
  12. case '+':
  13. printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
  14. break;
  15.  
  16. case '-':
  17. printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
  18. break;
  19.  
  20. case '*':
  21. printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
  22. break;
  23.  
  24. case '/':
  25. if (num2 != 0.0) {
  26. printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
  27. } else {
  28. printf("Error! Division by zero is not allowed.\n");
  29. }
  30. break;
  31.  
  32. case '%':
  33. if (num2 != 0.0) {
  34. printf("%d %% %d = %d\n", (int)num1, (int)num2, (int)num1 % (int)num2);
  35. } else {
  36. printf("Error! Division by zero is not allowed.\n");
  37. }
  38. break;
  39. default:
  40. printf("Error! Operator is not correct.\n");
  41. }
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 5316KB
stdin
/*  Berechnung des Hamming-Abstandes zwischen zwei 128-Bit Werten in 	*/
/*	einer Textdatei. 													*/
/*  Die Werte müssen auf einer separaten Zeile gespeichert sein			*/
/* 																		*/
/*	Erstellt: 17.5.2010													*/
/*  Autor: Thomas Scheffler												*/

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

#define ARRAY_SIZE 32

unsigned Hamdist(unsigned x, unsigned y)
{
  unsigned dist = 0, val = x ^ y;
 
  // Count the number of set bits
  while(val)
  {
    ++dist; 
    val &= val - 1;
  }
 
  return dist;
}



int main (void)
{
	char hex;
	int i;
	int a[ARRAY_SIZE];
	int b[ARRAY_SIZE];
	int hamDist = 0;
	FILE* fp;
	
	//Arrays mit 0 initialisieren
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
  		a[i] = 0;
  		b[i] = 0;
	}

	
	fp = fopen("hex.txt","r");
	if (fp == NULL) 
	{
		printf("Die Datei hex.txt wurde nicht gefunden!");
		exit(EXIT_FAILURE);
	}

	i=0;
	printf("1.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
        a[i]=strtol(&hex,0,16);
		i++;
    }
	i=0;
	printf("2.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
    	b[i]=strtol(&hex,0,16);
        i++;
    }
	fclose(fp);

	printf("Hamming-Abweichung pro Nibble:\n");
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
		printf ("%i\t%i\t%i\n",a[i],b[i],Hamdist(a[i],b[i]));
		hamDist += Hamdist(a[i],b[i]);
	}
	printf ("\nHamming-Abweichung der Hash-Werte:%d\n",hamDist);
}

stdout
Enter an operator (+, -, *, /, %): Enter two operands: 0.00 / 0.00 = 0.67