Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAXVAL 100
- int to_int(char array) {
- return ((int)array - '0');
- }
- typedef struct Node {
- unsigned char name[MAXVAL];
- unsigned char address[MAXVAL];
- int number[MAXVAL];
- struct Node *next;
- }Node;
- typedef struct {
- Node *head;
- } list;
- void add_new(Node **head);
- void list_foreach(list *list, void(*function)(Node** head));
- int list_find(Node* list, int(*cmp_func)(char*, char*), unsigned char* value);
- int cmp(const unsigned char* a, const unsigned char* b) {
- return strcmp(a, b);
- }
- void print_all(Node** head);
- Node* getNth(Node* head, int n);
- void deleteNth(Node **head, int n);
- void save(Node* head);
- void help(void);
- int input(Node **head,list* tmp, int count);
- int main(int argc, char** argv) {
- int count;
- Node* head = malloc(sizeof(Node));
- head = NULL;
- list* tmp = malloc(sizeof(list));
- if (argc > 1 && strcmp(argv[1], "-i") != 0) {
- printf("neizvestnyi kluch");
- return 0;
- }
- if (argc == 2 && !strcmp(argv[1], "-i")) { //если введён ключ -i выполнять программу до ввода exit
- help();
- while (1) {
- count = 0;
- if (input(&head, tmp, count) == 1)
- break;
- }
- return 0;
- }
- }
- int input(Node **head, list *tmp, int count){
- char buffer[MAXVAL];
- gets_s(buffer, MAXVAL);
- if (strcmp(buffer, "exit") == 0)
- return 1;
- if (strcmp(buffer, "add") == 0){
- count = 1;
- add_new(head);
- }
- if (strcmp(buffer, "printall") == 0) {
- count = 1;
- tmp->head = *head;
- list_foreach(tmp, &print_all);
- }
- if (strcmp(buffer, "find") == 0) {
- count = 1;
- char example[MAXVAL];
- gets_s(example, MAXVAL);
- list_find(*head, &cmp, example);
- }
- if (strcmp(buffer, "delete") == 0) {
- count = 1;
- tmp->head = *head;
- char example[MAXVAL];
- gets_s(example, MAXVAL);
- int position = list_find(*head, &cmp, example);
- deleteNth(head, position);
- }
- if (strcmp(buffer, "save") == 0){
- count = 1;
- save(*head);
- }
- if (count == 0) printf("neizvestnay komanda\n");
- return 0;
- }
- void deleteNth(Node **head, int n) {
- if (n == 0) {
- Node *tmp = *head;
- (*head) = (*head)->next;
- free(tmp);
- }
- else {
- Node *prev = getNth(*head, n - 1);
- Node *elm = prev->next;
- prev->next = elm->next;
- free(elm);
- }
- printf("deleted");
- }
- Node* getNth(Node* head, int n) {
- int counter = 1;
- while (counter < n && head) {
- head = head->next;
- counter++;
- }
- return head;
- }
- void print_all(Node** head) {
- int i = 0;
- printf("\n");
- printf("name:");
- puts((*head)->name);
- printf("address:");
- puts((*head)->address);
- printf("number:");
- while ((*head)->number[i] >= 0) {
- printf("%d", (*head)->number[i++]);
- }
- printf("\n");
- }
- int list_find(Node* list, int(*cmp_func)(char*, char*), unsigned char* value) { // возвращает тот узел, для которого результат функции сравнения равен нулю
- int i = 0, counter = 0;
- while (list != NULL) {
- if ((*cmp_func)(value, list->name) == 0) {
- printf("name:");
- puts(list->name);
- printf("address:");
- puts(list->address);
- printf("number:");
- while (list->number[i] >= 0) {
- printf("%d", list->number[i++]);
- }
- printf("\n");
- return counter;
- }
- else {
- list = list->next;
- counter++;
- }
- }
- return -1;
- }
- void list_foreach(list *list, void(*function)(Node** head)) //применаем функцию function к каждому узлу списка
- {
- Node *tmp = list->head;
- while (tmp != NULL) {
- function(&tmp);
- tmp = tmp->next;
- }
- }
- void add_new(Node **head) {
- Node *tmp = (Node*)malloc(sizeof(Node));
- unsigned char name[MAXVAL];
- unsigned char address[MAXVAL];
- char number[MAXVAL];
- printf("name:");
- gets_s(name, MAXVAL);
- strcpy(tmp->name, name);
- printf("address:");
- gets_s(address, MAXVAL);
- strcpy(tmp->address, address);
- printf("number:");
- gets_s(number, MAXVAL);
- int i = 0;
- while (1) {
- if (number[i] == '\0') break;
- tmp->number[i] = to_int(number[i]);
- i++;
- }
- tmp->next = (*head);
- (*head) = tmp;
- }
- void save(Node* head){
- FILE *S1;
- int i=0;
- S1 = fopen("S1.txt", "ab");
- if (S1 == NULL)
- exit(1);
- while (head != NULL){
- fprintf(S1, "name:");
- fputs(head->name, S1);
- fprintf(S1, "\naddress:");
- fputs(head->address, S1);
- printf("\n");
- fprintf(S1, "number");
- while (head->number[i] >= 0) {
- fprintf(S1,"%d", head->number[i++]);
- }
- fprintf(S1,"\n");
- head=head->next;
- }
- fclose(S1);
- printf("saved");
- }
- void help(void){
- printf("Available commands:\n");
- printf("add ");
- printf("printall ");
- printf("find ");
- printf("delete ");
- printf("save ");
- printf("exit\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement