Advertisement
vallec

Untitled

May 7th, 2023
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.01 KB | None | 0 0
  1. //1
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int main()
  7. {
  8.     int guests;
  9.     float chairs, persons, cups, dishes, totalPrice = 0;
  10.  
  11.     char objects[50];
  12.  
  13.     printf("Enter guests: ");
  14.     scanf("%d", &guests);
  15.  
  16.     printf("Enter an object: ");
  17.     scanf("%s", &objects);
  18.  
  19.     while (strcmp(objects, "PARTY!") != 0)
  20.     {
  21.         if (strcmp(objects, "Chair") == 0)
  22.         {
  23.             totalPrice += 13.99;
  24.             chairs += 1;
  25.         }
  26.         else if (strcmp(objects, "Table") == 0)
  27.         {
  28.             totalPrice += 42.00;
  29.             persons += 8;
  30.         }
  31.         else if (strcmp(objects, "Cups") == 0)
  32.         {
  33.             totalPrice += 5.98;
  34.             cups += 6;
  35.         }
  36.         else if(strcmp(objects, "Dishes") == 0)
  37.         {
  38.             totalPrice += 21.02;
  39.             dishes += 6;
  40.         }
  41.  
  42.         printf("Enter an object: ");
  43.         scanf("%s", &objects);
  44.     }
  45.  
  46.     printf("Total price: %.2f\n", totalPrice);
  47.     float neededChairs = guests - chairs;
  48.     float neededPlacesOnTables = guests - persons;
  49.     float neededCups = guests - cups;
  50.     float neededDishes = guests - dishes;
  51.  
  52.     if (neededChairs > 0)
  53.     {
  54.         printf("%d Chairs\n", neededChairs);
  55.     }
  56.  
  57.     if (neededCups > 0)
  58.     {
  59.         float cupsComplect = ceil(neededCups / 6);
  60.  
  61.         if (cupsComplect == 0)
  62.         {
  63.             cupsComplect = 1;
  64.         }
  65.         printf("%.0f Cups\n", neededCups);
  66.     }
  67.  
  68.     if (neededPlacesOnTables > 0)
  69.     {
  70.         float neededTables = ceil(neededPlacesOnTables / 8);
  71.  
  72.         if (neededTables == 0)
  73.         {
  74.             neededTables = 1;
  75.         }
  76.  
  77.         printf("%.0f Tables\n", neededTables);
  78.     }
  79.  
  80.     if (neededDishes > 0)
  81.     {
  82.         float neededDishesComplekt = ceil(neededDishes / 6);
  83.  
  84.         if (neededDishesComplekt == 0)
  85.         {
  86.             neededDishesComplekt = 1;
  87.         }
  88.  
  89.         printf("%.0f Dishes\n", neededDishesComplekt);
  90.     }
  91.    
  92.     return 0;
  93. }
  94.  
  95. //2
  96. #include <stdio.h>
  97. #include <string.h>
  98.  
  99. typedef struct
  100. {
  101.     char name[50];
  102.     float price;
  103.     int uniqueProductNum;
  104. } Product;
  105.  
  106. typedef struct
  107. {
  108.     char address[50];
  109.     int productNum;
  110. } Order;
  111.  
  112. Product* remove(Product* products, int size, int index);
  113.  
  114. int main()
  115. {
  116.     char option[50];
  117.     printf("Choose: Product/Order: ");
  118.     scanf("%s", option);
  119.  
  120.     Product* products;
  121.     int index = 0;
  122.     int sizeArr = 0;
  123.  
  124.     while (strcmp(option, "END") != 0)
  125.     {
  126.         if (strcmp(option, "Product") == 0)
  127.         {
  128.             Product currentStudent;
  129.  
  130.             printf("Enter product name: ");
  131.             scanf("%s", currentStudent.name);
  132.  
  133.             printf("Enter price: ");
  134.             scanf("%f", &currentStudent.price);
  135.  
  136.             printf("Enter product number: ");
  137.             scanf("%d", &currentStudent.uniqueProductNum);
  138.  
  139.             products[index] = currentStudent;
  140.             index++;
  141.             sizeArr++;
  142.         }
  143.  
  144.         else if (strcmp(option, "Order") == 0)
  145.         {
  146.             Order currentOrder;
  147.  
  148.             printf("Enter address: ");
  149.             scanf("%s", currentOrder.address);
  150.  
  151.             printf("Enter product number: ");
  152.             scanf("%d", &currentOrder.productNum);
  153.  
  154.             int size = sizeArr;
  155.            
  156.             for (int i = 0; i < size; i++)
  157.             {
  158.                 if (products[i].uniqueProductNum == currentOrder.productNum)
  159.                 {
  160.                     Product product = products[i];
  161.                     products = remove(products, size, i);
  162.                     printf("Client %s ordered %s\n", currentOrder.address , product.name);
  163.  
  164.                     sizeArr--;
  165.                 }
  166.             }
  167.         }  
  168.  
  169.         printf("Choose: Product/Order: ");
  170.         scanf("%s", option);
  171.     }
  172.  
  173.     printf("------------\n");
  174.  
  175.     for (int i = 0; i < sizeArr; i++)
  176.     {
  177.         printf("Name: %s\n", products[i].name);
  178.         printf("Price: %.2f\n", products[i].price);
  179.         printf("Product number: %d\n", products[i].uniqueProductNum);
  180.     }
  181.    
  182.     return 0;
  183. }
  184.  
  185.   Product* remove(Product* products, int size, int index)
  186.     {
  187.         Product* newPorducts = (Product*)malloc(size * sizeof(Product));
  188.  
  189.         for (int i = 0; i < size; i++)
  190.         {
  191.             if (i < index)
  192.             {
  193.                 newPorducts[i] = products[i];
  194.             }
  195.  
  196.             else if (i > index)
  197.             {
  198.                 newPorducts[i - 1] = products[i];
  199.             }
  200.         }
  201.  
  202.         return newPorducts;
  203.     }
  204.    
  205. //3
  206. #include <stdio.h>
  207. #include <stdlib.h>
  208. #include <string.h>
  209.  
  210.  
  211.  
  212. int main()
  213. {
  214.  
  215.     FILE* fp = fopen("input.txt", "rt");
  216.     FILE* fpout = fopen("output.bin", "wb");
  217.  
  218.     char word[50];
  219.     int resultsize = 0;
  220.     char *result = malloc(0);
  221.  
  222.  
  223.     char lastChar = 0;
  224.  
  225.     for(;;)
  226.     {
  227.  
  228.         if(fscanf(fp, "%s", word) == EOF)
  229.             break;
  230.  
  231.         if(lastChar == 0 || lastChar == word[0])
  232.         {
  233.  
  234.             lastChar = word[strlen(word)-1];
  235.             result = realloc(result, (++resultsize) * 50);
  236.             memcpy(&result[(resultsize-1)*50], &word, strlen(word)+1);
  237.             printf("%s - %d - %c - %c\n", word, strlen(word), word[strlen(word)-1], word[0]);
  238.  
  239.         }
  240.  
  241.  
  242.     }
  243.  
  244.     fwrite(result, 1, resultsize * 50, fpout);
  245.  
  246.  
  247.     return 0;
  248. }
  249.  
  250. input.txt -
  251. apple car price elephant head tire cool soft eleven output night tent story movies pleasure memory time search shortcut river compare evening
  252.  
  253. //4
  254.  
  255. #include <stdio.h>
  256. #include <stdlib.h>
  257.  
  258. int main() {
  259.   int key;
  260.   char input_file_name[100], output_file_name[100];
  261.   FILE *input_file, *output_file;
  262.  
  263.   printf("Enter key: ");
  264.   scanf("%d", &key);
  265.   if (key < 2 || key > 10) {
  266.     printf("Invalid key. Please try again!\n");
  267.     return 1;
  268.   }
  269.   printf("Enter input file: ");
  270.   scanf("%s", input_file_name);
  271.   printf("Enter output file: ");
  272.   scanf("%s", output_file_name);
  273.  
  274.   input_file = fopen(input_file_name, "r");
  275.   if (input_file == NULL) {
  276.     printf("Error while opening the input file!\n");
  277.     return 1;
  278.   }
  279.   output_file = fopen(output_file_name, "w");
  280.   if (output_file == NULL) {
  281.     printf("Error while opening the output file!\n");
  282.     return 1;
  283.   }
  284.  
  285.   char c;
  286.   while ((c = fgetc(input_file)) != EOF) {
  287.     if (c >= 'a' && c <= 'z') {
  288.       c = 'a' + (c - 'a' + key) % 26;
  289.     } else if (c >= 'A' && c <= 'Z') {
  290.       c = 'A' + (c - 'A' + key) % 26;
  291.     }
  292.     fputc(c, output_file);
  293.   }
  294.  
  295.   fclose(input_file);
  296.   fclose(output_file);
  297.  
  298.   printf("Success!\n");
  299.   return 0;
  300. }
  301.  
  302. //5
  303. #include <stdio.h>
  304. #include <string.h>
  305.  
  306. int main() {
  307.     char word[50], dashes[50], letter;
  308.     int length, i, j, guesses, maxGuesses, found = 0;
  309.  
  310.     printf("Enter word: ");
  311.     scanf("%s", word);
  312.     length = strlen(word);
  313.  
  314.     for (i = 0; i < length; i++) {
  315.         dashes[i] = '_';
  316.     }
  317.     dashes[length] = '\0';
  318.  
  319.     maxGuesses = length + 2;
  320.  
  321.     while (guesses < maxGuesses) {
  322.         printf("\n%s\n", dashes);
  323.         printf("Enter letter: ");
  324.         scanf(" %c", &letter);
  325.  
  326.         found = 0;
  327.         for (i = 0; i < length; i++) {
  328.             if (word[i] == letter) {
  329.                 dashes[i] = letter;
  330.                 found = 1;
  331.             }
  332.         }
  333.  
  334.         if (!found) {
  335.             printf("No match with that letter!\n");
  336.             guesses++;
  337.         }
  338.  
  339.         if (strcmp(word, dashes) == 0) {
  340.             printf("You have guessed the \"%s\" word!\n", word);
  341.             printf("Attempts: %d\n", guesses);
  342.             return 0;
  343.         }
  344.     }
  345.  
  346.     printf("You lost! The word is \"%s\".\n", word);
  347.     printf("Attempts: %d\n", guesses);
  348.  
  349.     return 0;
  350. }
  351.  
  352.  
  353. //6
  354. #include <stdio.h>
  355. #include <string.h>
  356.  
  357. int findAnagram(char* str1, char* str2) {
  358.     int len1 = strlen(str1);
  359.     int len2 = strlen(str2);
  360.     if (len1 != len2) {
  361.         return 0;
  362.     }
  363.  
  364.     int char_count[256] = {0};
  365.  
  366.     for (int i = 0; i < len1; i++) {
  367.         char_count[str1[i]]++;
  368.     }
  369.  
  370.     for (int i = 0; i < len2; i++) {
  371.         char_count[str2[i]]--;
  372.     }
  373.  
  374.     for (int i = 0; i < 256; i++) {
  375.         if (char_count[i] != 0) {
  376.             return 0;
  377.         }
  378.     }
  379.  
  380.     return 1;
  381. }
  382.  
  383. int main() {
  384.     char word1[50], word2[50];
  385.     printf("Enter word 1: ");
  386.     scanf("%s", word1);
  387.     printf("Enter word 2: ");
  388.     scanf("%s", word2);
  389.    
  390.     printf("%d\n", findAnagram(word1, word2));
  391.  
  392.     return 0;
  393. }
  394.  
  395. //7
  396. #include <stdio.h>
  397. #include <stdlib.h>
  398. #include <string.h>
  399.  
  400. typedef struct {
  401.     int id;
  402.     float hourlyPrice;
  403.     float hoursPerWeek;
  404.     float salaryPerWeek;
  405. } Employee;
  406.  
  407. void addEmployee(Employee* employees, int* employees);
  408. void getSalary(Employee* employee);
  409. void print(Employee* employees, int employees);
  410.  
  411. int main() {
  412.     Employee employees[30];
  413.     int employees, choice = 0;
  414.  
  415.     while (choice != 3) {
  416.         printf("Menu:\n");
  417.         printf("1. Add employee\n");
  418.         printf("2. Print employees\n");
  419.         printf("3. Exit\n");
  420.         printf("Enter choice: ");
  421.         scanf("%d", &choice);
  422.  
  423.         switch (choice) {
  424.             case 1:
  425.                 addEmployee(&employees[employees], &employees);
  426.                 break;
  427.             case 2:
  428.                 print(employees, employees);
  429.                 break;
  430.             case 3:
  431.                 printf("Goodbye!\n");
  432.                 break;
  433.             default:
  434.                 printf("Invalid choice\n");
  435.                 break;
  436.         }
  437.     }
  438.  
  439.     FILE* fp = fopen("employees.bin", "wb");
  440.     if (fp == NULL) {
  441.         printf("Error opening file\n");
  442.         return 1;
  443.     }
  444.  
  445.     fwrite(employees, sizeof(Employee), employees, fp);
  446.  
  447.     fclose(fp);
  448.  
  449.     return 0;
  450. }
  451.  
  452. void addEmployee(Employee* employees, int* employees) {
  453.     if (*employees >= MAX_EMPLOYEES) {
  454.         printf("Maximum number of employees reached\n");
  455.         return;
  456.     }
  457.  
  458.     Employee new_employee;
  459.     printf("Enter ID: ");
  460.     scanf("%d", &new_employee.id);
  461.     printf("Enter hourly pay: ");
  462.     scanf("%f", &new_employee.hourlyPrice);
  463.     printf("Enter weekly hours: ");
  464.     scanf("%f", &new_employee.hoursPerWeek);
  465.  
  466.     getSalary(&new_employee);
  467.  
  468.     employees[*employees] = new_employee;
  469.     (*employees)++;
  470. }
  471.  
  472. void getSalary(Employee* employee) {
  473.     if (employee->hoursPerWeek > 40) {
  474.         employee->salaryPerWeek = 40 * employee->hourlyPrice
  475.             + (employee->hoursPerWeek - 40) * employee->hourlyPrice * 1.5;
  476.     } else {
  477.         employee->salaryPerWeek = employee->hoursPerWeek * employee->hourlyPrice;
  478.     }
  479.  
  480.     float taxes = employee->salaryPerWeek * 0.0365;
  481.     employee->salaryPerWeek -= taxes;
  482. }
  483.  
  484. void print(Employee* employees, int employees) {
  485.     printf("ID\tHourly Pay\tWeekly Hours\tWeekly Salary\n");
  486.     printf("-------------------------------------------------\n");
  487.     for (int i = 0; i < employees; i++) {
  488.         printf("%d\t%.2f\t\t%.2f\t\t%.2f\n",
  489.             employees[i].id, employees[i].hourlyPrice,
  490.             employees[i].hoursPerWeek, employees[i].salaryPerWeek);
  491.     }
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement