Advertisement
Beyrin

Tree

May 17th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.80 KB | None | 0 0
  1. ???
  2. MAAIN C
  3. ???
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #include "lab_2_5.h"
  8.  
  9.  
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     char* str = (char*)malloc(sizeof(char)*40);
  14.     int argcheck = 0;
  15.  
  16.     for (int count = 1; count < argc; count++){
  17.             if (!strcmp(argv[count], "-fin") && (count+1 < argc)){
  18.                 if (load(str, argv[count+1]) == 0) printf("---Wrong arguments---\n");
  19.                 else argcheck = 1;
  20.             }
  21.     }
  22.     if (argcheck == 0){
  23.         printf("---Enter tree---\n");
  24.         free(str);
  25.         str = dscan();
  26.  
  27.     }
  28.     while(!check(str)){
  29.         printf("---Enter tree---\n");
  30.         free(str);
  31.         str = dscan();
  32.     }
  33.     int* dig = turnnum(toint(turn(str)));
  34.     STree *root = NULL;
  35.     root = (STree*)malloc(sizeof(STree));
  36.     root -> prev = (STree*)malloc(sizeof(STree));
  37.     root -> value = dig[0];
  38.     root -> right = NULL;
  39.     root -> left = NULL;
  40.     root -> prev -> right = NULL;
  41.     root -> prev -> left = NULL;
  42.     for (int count = 1; dig[count] >=0; count++){
  43.         Insert(root, dig[count]);
  44.     }
  45.     argcheck = 0;
  46.     char *filename;
  47.     for (int count = 1; count < argc; count++){
  48.         if (!strcmp(argv[count], "-fout") && (count+1 < argc)){
  49.             if (count+1 >= argc) printf("---Wrong arguments---\n");
  50.             else {
  51.                 argcheck = 1;
  52.                 filename = argv[count+1];
  53.             }
  54.         }
  55.     }
  56.     if (argcheck == 1){
  57.         FILE *file = fopen(filename, "r");
  58.         if (file == NULL) {
  59.             system("Cls");
  60.             printf("Cant open file\n");
  61.         }
  62.         else
  63.             fPrintTree(root, filename);
  64.             fclose(file);
  65.     }
  66.     else
  67.         PrintTree(root);
  68.     FreeTree(root);
  69.     free(str);
  70.     free(dig);
  71.     return 0;
  72. }
  73.  
  74.  
  75. ???
  76. BRAACKET C
  77. ???
  78. #include "bracket.h"
  79.  
  80. int br_check(char st[]){
  81.     int i, k = 0;
  82.     char* br = (char*)malloc(sizeof(char)*strlen(st));
  83.     for (i = 0; i < strlen(st); i++){
  84.             switch (st[i]){
  85.                 case '(':
  86.                         br[k] = '(';
  87.                         k++;
  88.                         break;
  89.                 case '[':
  90.                         br[k] = '[';
  91.                         k++;
  92.                         break;
  93.                 case '{':
  94.                         br[k] = '{';
  95.                         k++;
  96.                         break;
  97.                 case ')':
  98.                     if ((br[k-1] == '('))
  99.                         k--;
  100.                     else{
  101.                         free(br);
  102.                         return 0;
  103.                     }
  104.                     break;
  105.                 case ']':
  106.                     if ((br[k-1] == '['))
  107.                         k--;
  108.                     else{
  109.                         free(br);
  110.                         return 0;
  111.                     }
  112.                     break;
  113.                 case '}':
  114.                     if ((br[k-1] == '{'))
  115.                         k--;
  116.                     else{
  117.                         free(br);
  118.                         return 0;
  119.                     }
  120.                     break;
  121.  
  122.             }
  123.     }
  124.     if (k == 0){
  125.         free(br);
  126.         return 1;
  127.     }
  128.     else{
  129.         free(br);
  130.         return 0;
  131.     }
  132. }
  133.  
  134. int bracket(char* a)
  135. {
  136.     int n = 0;
  137.     int i = 0;
  138.     for (i = 0; i < strlen(a); i++)
  139.         if ((a[i] == '(') || (a[i] == '[') || (a[i] == '{') || (a[i] == ')') || (a[i] == ']') || (a[i] == '}'))
  140.             n++;
  141.  
  142.     if (n == 0){
  143.         printf("----------no bracket----------\n");
  144.         return 0;
  145.     }
  146.     if (br_check(a) == 0){
  147.         printf("----------wrong bracket----------\n");
  148.         return 0;
  149.     }
  150.     return 1;
  151. }
  152.  
  153.  
  154. ???
  155. FILEWORK C
  156. ???
  157. #include "filework.h"
  158.  
  159. int save(char *str, char *filename){
  160.     int check = 1;
  161.     FILE *filej = fopen(filename, "w");
  162.     if (filej == NULL) {
  163.         system("Cls");
  164.         printf("Cant open file\n");
  165.         check = 0;
  166.  
  167.     }
  168.     else{
  169.         fputs(str, filej);
  170.     }
  171.     fclose(filej);
  172.     return check;
  173. }
  174.  
  175. int load(char *str, char *filename){
  176.     int check = 1;
  177.     FILE *file = fopen(filename, "r");
  178.     if (file == NULL) {
  179.         system("Cls");
  180.         printf("Cant open file\n");
  181.         check = 0;
  182.     }
  183.     else{
  184.         fgets(str, 40, file);
  185.     }
  186.     fclose(file);
  187.     return check;
  188. }
  189.  
  190. char* dscan()
  191. {
  192.     char* str = (char*)malloc(sizeof(char));
  193.     char sym = 0;
  194.     int n = 1;
  195.     while ((sym = getc(stdin)) != '\n'){
  196.         str[n-1] =  sym;
  197.         str = (char*)realloc(str, ++n);
  198.         str[n-1] = '\0';
  199.     }
  200.     return str;
  201. }
  202.  
  203.  
  204. ???
  205. LAB 2 5 C
  206. ???
  207. #include "lab_2_5.h"
  208.  
  209. int to10 (char dig)
  210. {
  211.     int k = 0;
  212.     char kod[] = "0123456789";
  213.     for (k = 0; k < 10; k++)
  214.         if (kod[k] == dig)
  215.             return k;
  216.         return -1;
  217. }
  218.  
  219. char* turn(char *str){
  220.     int j = strlen(str)-1;
  221.     char save;
  222.     for (int i = 0; i < j; i++, j--){
  223.         save = str[j];
  224.         str[j] = str[i];
  225.         str[i] = save;
  226.     }
  227.     return str;
  228. }
  229.  
  230. int* turnnum(int *num){
  231.     int j;
  232.     for (j = 0; num[j] != -1; j++);
  233.     j--;
  234.     int save;
  235.     for (int i = 0; i < j; i++, j--){
  236.         save = num[j];
  237.         num[j] = num[i];
  238.         num[i] = save;
  239.     }
  240.     return num;
  241. }
  242.  
  243. int* toint(char* str){
  244.     int* nums = (int*)malloc(sizeof(int));
  245.     int count = 1;
  246.     int ncount = 0;
  247.     int n = 0;
  248.     nums[0] = 0;
  249.     while ((str[n]) != '\0'){
  250.         if (to10(str[n]) > 0){
  251.             nums[count-1] += to10(str[n])*pow(10, ncount++);
  252.         }
  253.         else
  254.         if ((n != 0) && (to10(str[n-1]) >= 0)){
  255.             nums = (int*)realloc(nums, sizeof(int)*(++count));
  256.             nums[count-1] = 0;
  257.             ncount = 0;
  258.         }
  259.         n++;
  260.     }
  261.     nums = (int*)realloc(nums, sizeof(int)*(++count));
  262.     nums[count-1] = -1;
  263.     return nums;
  264. }
  265.  
  266. void Insert(STree *root, int v){
  267.     if (v > root->value)
  268.             root->right == NULL ?
  269.             (root->right = (STree*)malloc(sizeof(STree)),root->right->value = v, root->right->prev = root, root->right->right = root->right->left = NULL)
  270.             : Insert(root->right, v);
  271.     else
  272.             root->left == NULL ?
  273.             (root->left = (STree*)malloc(sizeof(STree)),root->left->value = v, root->left->prev = root, root->left->right = root->left->left = NULL)
  274.             : Insert(root->left, v);
  275. }
  276. void PrintTree(STree *root){
  277.     if (root){
  278.         if (root->prev->right || root->prev->left)
  279.         if (!(root->prev->right == root) || !root->prev->left) printf("(");
  280.         printf("%d", root->value);
  281.         PrintTree(root->left);
  282.         if (root->right && root->left) printf(",");
  283.         PrintTree(root->right);
  284.         if (root->prev->right || root->prev->left)
  285.         if (!root->prev->right  || root->prev->right==root)
  286.         printf(")");
  287.     }
  288. }
  289.  
  290. void fPrintTree(STree *root, char* filename){
  291.     FILE *file = fopen(filename, "w");
  292.     if (file == NULL) {
  293.         system("Cls");
  294.         printf("Cant open file\n");
  295.     }
  296.     else{
  297.         if (root){
  298.             if (root->prev->right || root->prev->left)
  299.             if (!(root->prev->right == root) || !root->prev->left) fprintf(file, "(");
  300.             fprintf(file, "%d", root->value);
  301.             fPrintTree(root->left, filename);
  302.             if (root->right && root->left) fprintf(file, ",");
  303.             fPrintTree(root->right, filename);
  304.             if (root->prev->right || root->prev->left)
  305.             if (!root->prev->right  || root->prev->right==root)
  306.             fprintf(file, ")");
  307.         }
  308.     }
  309.     fclose(file);
  310. }
  311.  
  312. void FreeTree(STree *root){
  313.     if (root){
  314.         FreeTree(root->left);
  315.         FreeTree(root->right);
  316.         free(root);
  317.     }
  318. }
  319.  
  320. int check(char *str){
  321.     int check = 0;
  322.     check = bracket(str);
  323.     if (to10(str[0]) < 0){
  324.         printf("---No Head---\n");
  325.         check = 0;
  326.     }
  327.     if (str[1] != '('){
  328.             printf("---No first bracket---");
  329.             check = 0;
  330.         }
  331.     for(int count = 0; count < strlen(str)-1; count++){
  332.         if ((str[count] == ',' && str[count+1] == ',') ||
  333.             (str[count] == ',' && str[count+1] == '(') ||
  334.             (str[count] == ',' && str[count+1] == ')') ||
  335.             (str[count] == '(' && str[count+1] == ',') ||
  336.             (str[count] == '(' && str[count+1] == '(') ||
  337.             (str[count] == '(' && str[count+1] == ')') ||
  338.             (str[count] == ')' && str[count+1] == '(') ){
  339.             printf("---Wrong punctuation---\n");
  340.             check = 0;
  341.         }
  342.     }
  343.     for(int count = 0; count < strlen(str); count++){
  344.         if ((str[count] != ',') &&
  345.             (str[count] != '(') &&
  346.             (str[count] != ')') &&
  347.             (to10(str[count]) < 0)){
  348.             printf("---Wrong symbol # %d---\n", count);
  349.             check = 0;
  350.         }
  351.     }
  352.     return check;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement