fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. double num1, num2;
  6. char op;
  7. char choice;
  8.  
  9. do {
  10. cout << "Enter first number: ";
  11. cin >> num1;
  12.  
  13. cout << "Enter an operator (+, -, *, /): ";
  14. cin >> op;
  15.  
  16. cout << "Enter second number: ";
  17. cin >> num2;
  18.  
  19. switch (op) {
  20. case '+':
  21. cout << "Result: " << num1 + num2 << endl;
  22. break;
  23. case '-':
  24. cout << "Result: " << num1 - num2 << endl;
  25. break;
  26. case '*':
  27. cout << "Result: " << num1 * num2 << endl;
  28. break;
  29. case '/':
  30. if (num2 != 0)
  31. cout << "Result: " << num1 / num2 << endl;
  32. else
  33. cout << "Error: Division by zero!" << endl;
  34. break;
  35. default:
  36. cout << "Error: Invalid operator!" << endl;
  37. }
  38.  
  39. cout << "Do you want to perform another calculation? (y/n): ";
  40. cin >> choice;
  41. cout << endl;
  42.  
  43. } while (choice == 'y' || choice == 'Y');
  44.  
  45. cout << "Calculator closed. Goodbye!" << endl;
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0.01s 5284KB
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 first number: Enter an operator (+, -, *, /): Enter second number: Error: Invalid operator!
Do you want to perform another calculation? (y/n): 
Calculator closed. Goodbye!