fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. void square() {
  6. double side;
  7. cout << "Enter the side length of the square: ";
  8. cin >> side;
  9. cout << "Area: " << side * side << ", Circumference: " << 4 * side << endl;
  10. }
  11.  
  12. void circle() {
  13. double radius;
  14. cout << "Enter the radius of the circle: ";
  15. cin >> radius;
  16. cout << "Area: " << M_PI * radius * radius << ", Circumference: " << 2 * M_PI * radius << endl;
  17. }
  18.  
  19. void triangle() {
  20. double base, height, side1, side2;
  21. cout << "Enter the base of the triangle: ";
  22. cin >> base;
  23. cout << "Enter the height of the triangle: ";
  24. cin >> height;
  25. cout << "Enter the first side of the triangle: ";
  26. cin >> side1;
  27. cout << "Enter the second side of the triangle: ";
  28. cin >> side2;
  29. cout << "Area: " << 0.5 * base * height << ", Circumference: " << base + side1 + side2 << endl;
  30. }
  31.  
  32. void cube() {
  33. double side;
  34. cout << "Enter the side length of the cube: ";
  35. cin >> side;
  36. cout << "Volume: " << pow(side, 3) << ", Surface Area: " << 6 * pow(side, 2) << endl;
  37. }
  38.  
  39. void sphere() {
  40. double radius;
  41. cout << "Enter the radius of the sphere: ";
  42. cin >> radius;
  43. cout << "Volume: " << (4.0 / 3.0) * M_PI * pow(radius, 3) << ", Surface Area: " << 4 * M_PI * pow(radius, 2) << endl;
  44. }
  45.  
  46. void pyramid() {
  47. double base_length, base_width, height;
  48. cout << "Enter the base length of the pyramid: ";
  49. cin >> base_length;
  50. cout << "Enter the base width of the pyramid: ";
  51. cin >> base_width;
  52. cout << "Enter the height of the pyramid: ";
  53. cin >> height;
  54. double volume = (1.0 / 3.0) * base_length * base_width * height;
  55. double surface_area = base_length * base_width + base_length * height + base_width * height;
  56. cout << "Volume: " << volume << ", Surface Area: " << surface_area << endl;
  57. }
  58.  
  59. void cone() {
  60. double radius, height;
  61. cout << "Enter the radius of the cone: ";
  62. cin >> radius;
  63. cout << "Enter the height of the cone: ";
  64. cin >> height;
  65. double slant_height = sqrt(pow(radius, 2) + pow(height, 2));
  66. double volume = (1.0 / 3.0) * M_PI * pow(radius, 2) * height;
  67. double surface_area = M_PI * radius * (radius + slant_height);
  68. cout << "Volume: " << volume << ", Surface Area: " << surface_area << endl;
  69. }
  70.  
  71. void trapezoid() {
  72. double base1, base2, height, side1, side2;
  73. cout << "Enter the first base of the trapezoid: ";
  74. cin >> base1;
  75. cout << "Enter the second base of the trapezoid: ";
  76. cin >> base2;
  77. cout << "Enter the height of the trapezoid: ";
  78. cin >> height;
  79. cout << "Enter the first side of the trapezoid: ";
  80. cin >> side1;
  81. cout << "Enter the second side of the trapezoid: ";
  82. cin >> side2;
  83. cout << "Area: " << 0.5 * (base1 + base2) * height << ", Circumference: " << base1 + base2 + side1 + side2 << endl;
  84. }
  85.  
  86. void pentagon() {
  87. double side;
  88. cout << "Enter the side length of the pentagon: ";
  89. cin >> side;
  90. double area = (1.0 / 4.0) * sqrt(5 * (5 + 2 * sqrt(5))) * pow(side, 2);
  91. cout << "Area: " << area << ", Circumference: " << 5 * side << endl;
  92. }
  93.  
  94. void cuboid() {
  95. double length, width, height;
  96. cout << "Enter the length of the cuboid: ";
  97. cin >> length;
  98. cout << "Enter the width of the cuboid: ";
  99. cin >> width;
  100. cout << "Enter the height of the cuboid: ";
  101. cin >> height;
  102. double volume = length * width * height;
  103. double surface_area = 2 * (length * width + width * height + height * length);
  104. cout << "Volume: " << volume << ", Surface Area: " << surface_area << endl;
  105. }
  106.  
  107. int main() {
  108. int choice;
  109. do {
  110. cout << "\nChoose a shape to calculate:\n";
  111. cout << "1. Square\n2. Circle\n3. Triangle\n4. Cube\n5. Sphere\n6. Pyramid\n7. Cone\n8. Trapezoid\n9. Pentagon\n10. Cuboid\n0. Exit\n";
  112. cout << "Enter your choice: ";
  113. cin >> choice;
  114.  
  115. switch (choice) {
  116. case 1: square(); break;
  117. case 2: circle(); break;
  118. case 3: triangle(); break;
  119. case 4: cube(); break;
  120. case 5: sphere(); break;
  121. case 6: pyramid(); break;
  122. case 7: cone(); break;
  123. case 8: trapezoid(); break;
  124. case 9: pentagon(); break;
  125. case 10: cuboid(); break;
  126. case 0: cout << "Exiting the program. Goodbye!\n"; break;
  127. default: cout << "Invalid choice. Please select a valid option.\n";
  128. }
  129. } while (choice != 0);
  130.  
  131. return 0;
  132. }
  133.  
Success #stdin #stdout 0s 5280KB
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
Choose a shape to calculate:
1. Square
2. Circle
3. Triangle
4. Cube
5. Sphere
6. Pyramid
7. Cone
8. Trapezoid
9. Pentagon
10. Cuboid
0. Exit
Enter your choice: Exiting the program. Goodbye!