Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- int main()
- {
- int guests;
- float chairs, persons, cups, dishes, totalPrice = 0;
- char objects[50];
- printf("Enter guests: ");
- scanf("%d", &guests);
- printf("Enter an object: ");
- scanf("%s", &objects);
- while (strcmp(objects, "PARTY!") != 0)
- {
- if (strcmp(objects, "Chair") == 0)
- {
- totalPrice += 13.99;
- chairs += 1;
- }
- else if (strcmp(objects, "Table") == 0)
- {
- totalPrice += 42.00;
- persons += 8;
- }
- else if (strcmp(objects, "Cups") == 0)
- {
- totalPrice += 5.98;
- cups += 6;
- }
- else if(strcmp(objects, "Dishes") == 0)
- {
- totalPrice += 21.02;
- dishes += 6;
- }
- printf("Enter an object: ");
- scanf("%s", &objects);
- }
- printf("Total price: %.2f\n", totalPrice);
- float neededChairs = guests - chairs;
- float neededPlacesOnTables = guests - persons;
- float neededCups = guests - cups;
- float neededDishes = guests - dishes;
- if (neededChairs > 0)
- {
- printf("%d Chairs\n", neededChairs);
- }
- if (neededCups > 0)
- {
- float cupsComplect = ceil(neededCups / 6);
- if (cupsComplect == 0)
- {
- cupsComplect = 1;
- }
- printf("%.0f Cups\n", neededCups);
- }
- if (neededPlacesOnTables > 0)
- {
- float neededTables = ceil(neededPlacesOnTables / 8);
- if (neededTables == 0)
- {
- neededTables = 1;
- }
- printf("%.0f Tables\n", neededTables);
- }
- if (neededDishes > 0)
- {
- float neededDishesComplekt = ceil(neededDishes / 6);
- if (neededDishesComplekt == 0)
- {
- neededDishesComplekt = 1;
- }
- printf("%.0f Dishes\n", neededDishesComplekt);
- }
- return 0;
- }
- //2
- #include <stdio.h>
- #include <string.h>
- typedef struct
- {
- char name[50];
- float price;
- int uniqueProductNum;
- } Product;
- typedef struct
- {
- char address[50];
- int productNum;
- } Order;
- Product* remove(Product* products, int size, int index);
- int main()
- {
- char option[50];
- printf("Choose: Product/Order: ");
- scanf("%s", option);
- Product* products;
- int index = 0;
- int sizeArr = 0;
- while (strcmp(option, "END") != 0)
- {
- if (strcmp(option, "Product") == 0)
- {
- Product currentStudent;
- printf("Enter product name: ");
- scanf("%s", currentStudent.name);
- printf("Enter price: ");
- scanf("%f", ¤tStudent.price);
- printf("Enter product number: ");
- scanf("%d", ¤tStudent.uniqueProductNum);
- products[index] = currentStudent;
- index++;
- sizeArr++;
- }
- else if (strcmp(option, "Order") == 0)
- {
- Order currentOrder;
- printf("Enter address: ");
- scanf("%s", currentOrder.address);
- printf("Enter product number: ");
- scanf("%d", ¤tOrder.productNum);
- int size = sizeArr;
- for (int i = 0; i < size; i++)
- {
- if (products[i].uniqueProductNum == currentOrder.productNum)
- {
- Product product = products[i];
- products = remove(products, size, i);
- printf("Client %s ordered %s\n", currentOrder.address , product.name);
- sizeArr--;
- }
- }
- }
- printf("Choose: Product/Order: ");
- scanf("%s", option);
- }
- printf("------------\n");
- for (int i = 0; i < sizeArr; i++)
- {
- printf("Name: %s\n", products[i].name);
- printf("Price: %.2f\n", products[i].price);
- printf("Product number: %d\n", products[i].uniqueProductNum);
- }
- return 0;
- }
- Product* remove(Product* products, int size, int index)
- {
- Product* newPorducts = (Product*)malloc(size * sizeof(Product));
- for (int i = 0; i < size; i++)
- {
- if (i < index)
- {
- newPorducts[i] = products[i];
- }
- else if (i > index)
- {
- newPorducts[i - 1] = products[i];
- }
- }
- return newPorducts;
- }
- //3
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- FILE* fp = fopen("input.txt", "rt");
- FILE* fpout = fopen("output.bin", "wb");
- char word[50];
- int resultsize = 0;
- char *result = malloc(0);
- char lastChar = 0;
- for(;;)
- {
- if(fscanf(fp, "%s", word) == EOF)
- break;
- if(lastChar == 0 || lastChar == word[0])
- {
- lastChar = word[strlen(word)-1];
- result = realloc(result, (++resultsize) * 50);
- memcpy(&result[(resultsize-1)*50], &word, strlen(word)+1);
- printf("%s - %d - %c - %c\n", word, strlen(word), word[strlen(word)-1], word[0]);
- }
- }
- fwrite(result, 1, resultsize * 50, fpout);
- return 0;
- }
- input.txt -
- apple car price elephant head tire cool soft eleven output night tent story movies pleasure memory time search shortcut river compare evening
- //4
- #include <stdio.h>
- #include <stdlib.h>
- int main() {
- int key;
- char input_file_name[100], output_file_name[100];
- FILE *input_file, *output_file;
- printf("Enter key: ");
- scanf("%d", &key);
- if (key < 2 || key > 10) {
- printf("Invalid key. Please try again!\n");
- return 1;
- }
- printf("Enter input file: ");
- scanf("%s", input_file_name);
- printf("Enter output file: ");
- scanf("%s", output_file_name);
- input_file = fopen(input_file_name, "r");
- if (input_file == NULL) {
- printf("Error while opening the input file!\n");
- return 1;
- }
- output_file = fopen(output_file_name, "w");
- if (output_file == NULL) {
- printf("Error while opening the output file!\n");
- return 1;
- }
- char c;
- while ((c = fgetc(input_file)) != EOF) {
- if (c >= 'a' && c <= 'z') {
- c = 'a' + (c - 'a' + key) % 26;
- } else if (c >= 'A' && c <= 'Z') {
- c = 'A' + (c - 'A' + key) % 26;
- }
- fputc(c, output_file);
- }
- fclose(input_file);
- fclose(output_file);
- printf("Success!\n");
- return 0;
- }
- //5
- #include <stdio.h>
- #include <string.h>
- int main() {
- char word[50], dashes[50], letter;
- int length, i, j, guesses, maxGuesses, found = 0;
- printf("Enter word: ");
- scanf("%s", word);
- length = strlen(word);
- for (i = 0; i < length; i++) {
- dashes[i] = '_';
- }
- dashes[length] = '\0';
- maxGuesses = length + 2;
- while (guesses < maxGuesses) {
- printf("\n%s\n", dashes);
- printf("Enter letter: ");
- scanf(" %c", &letter);
- found = 0;
- for (i = 0; i < length; i++) {
- if (word[i] == letter) {
- dashes[i] = letter;
- found = 1;
- }
- }
- if (!found) {
- printf("No match with that letter!\n");
- guesses++;
- }
- if (strcmp(word, dashes) == 0) {
- printf("You have guessed the \"%s\" word!\n", word);
- printf("Attempts: %d\n", guesses);
- return 0;
- }
- }
- printf("You lost! The word is \"%s\".\n", word);
- printf("Attempts: %d\n", guesses);
- return 0;
- }
- //6
- #include <stdio.h>
- #include <string.h>
- int findAnagram(char* str1, char* str2) {
- int len1 = strlen(str1);
- int len2 = strlen(str2);
- if (len1 != len2) {
- return 0;
- }
- int char_count[256] = {0};
- for (int i = 0; i < len1; i++) {
- char_count[str1[i]]++;
- }
- for (int i = 0; i < len2; i++) {
- char_count[str2[i]]--;
- }
- for (int i = 0; i < 256; i++) {
- if (char_count[i] != 0) {
- return 0;
- }
- }
- return 1;
- }
- int main() {
- char word1[50], word2[50];
- printf("Enter word 1: ");
- scanf("%s", word1);
- printf("Enter word 2: ");
- scanf("%s", word2);
- printf("%d\n", findAnagram(word1, word2));
- return 0;
- }
- //7
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct {
- int id;
- float hourlyPrice;
- float hoursPerWeek;
- float salaryPerWeek;
- } Employee;
- void addEmployee(Employee* employees, int* employees);
- void getSalary(Employee* employee);
- void print(Employee* employees, int employees);
- int main() {
- Employee employees[30];
- int employees, choice = 0;
- while (choice != 3) {
- printf("Menu:\n");
- printf("1. Add employee\n");
- printf("2. Print employees\n");
- printf("3. Exit\n");
- printf("Enter choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- addEmployee(&employees[employees], &employees);
- break;
- case 2:
- print(employees, employees);
- break;
- case 3:
- printf("Goodbye!\n");
- break;
- default:
- printf("Invalid choice\n");
- break;
- }
- }
- FILE* fp = fopen("employees.bin", "wb");
- if (fp == NULL) {
- printf("Error opening file\n");
- return 1;
- }
- fwrite(employees, sizeof(Employee), employees, fp);
- fclose(fp);
- return 0;
- }
- void addEmployee(Employee* employees, int* employees) {
- if (*employees >= MAX_EMPLOYEES) {
- printf("Maximum number of employees reached\n");
- return;
- }
- Employee new_employee;
- printf("Enter ID: ");
- scanf("%d", &new_employee.id);
- printf("Enter hourly pay: ");
- scanf("%f", &new_employee.hourlyPrice);
- printf("Enter weekly hours: ");
- scanf("%f", &new_employee.hoursPerWeek);
- getSalary(&new_employee);
- employees[*employees] = new_employee;
- (*employees)++;
- }
- void getSalary(Employee* employee) {
- if (employee->hoursPerWeek > 40) {
- employee->salaryPerWeek = 40 * employee->hourlyPrice
- + (employee->hoursPerWeek - 40) * employee->hourlyPrice * 1.5;
- } else {
- employee->salaryPerWeek = employee->hoursPerWeek * employee->hourlyPrice;
- }
- float taxes = employee->salaryPerWeek * 0.0365;
- employee->salaryPerWeek -= taxes;
- }
- void print(Employee* employees, int employees) {
- printf("ID\tHourly Pay\tWeekly Hours\tWeekly Salary\n");
- printf("-------------------------------------------------\n");
- for (int i = 0; i < employees; i++) {
- printf("%d\t%.2f\t\t%.2f\t\t%.2f\n",
- employees[i].id, employees[i].hourlyPrice,
- employees[i].hoursPerWeek, employees[i].salaryPerWeek);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement