Advertisement
utroz

The Equation Paint

Sep 18th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.33 KB | None | 0 0
  1. /*  The Equation Paint (v 0.01) on Linux Console!
  2.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  3.  *  File: global.h
  4.  *  Blog: http://OAKcoders.com/ (Access it)!
  5. */
  6.  
  7. /* Equation Size */
  8. #define MAX 30
  9. typedef char string[MAX];
  10.  
  11. /* Software Version */
  12. #define SOFTWARE_VERSION 0.01
  13.  
  14. /* Errors ID's */
  15. #define ERROR_X -1 // Don't find x.
  16.  
  17. /* Graphics ID */
  18. #define ARROW_NORTH '^'
  19. #define ARROW_RIGHT '>'
  20. #define ARROW_LEFT '<'
  21. #define ARROW_SOUTH 'v'
  22.  
  23. #define V '|' /* Vertical */
  24. #define H '-' /* Horizontal */
  25. #define POINT '*'
  26.  
  27. /* Signal Status */
  28. #define POS_NUM 0 /* Positive Number */
  29. #define NEG_NUM 1 /* Negative Number */
  30.  
  31. /* Graphic Paint Size */
  32. #define GS 10
  33.  
  34. //------------
  35. /*  The Equation Paint (v 0.01) on Linux Console!
  36.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  37.  *  File: main.c
  38.  *  Blog: http://OAKcoders.com/ (Access it)!
  39. */
  40.  
  41. #include "global.h"
  42. #include <stdio.h>
  43. #include <string.h>
  44.  
  45. int main(void)
  46. {
  47.   string str;
  48.   int equ_status, value_x;
  49.   float cord[3];
  50.   size_t equation;
  51.  
  52.   printf("\tWelcome to Equation Paint (v %.2f) by Utroz!\n\tPlease access http://Oakcoders.com/ to more informations.\n", SOFTWARE_VERSION);
  53.  
  54.   do
  55.   {    
  56.     printf("\n\tPlease input a First Degree Equation: ");
  57.     fgets(str, MAX, stdin);
  58.     puts("\n");
  59.  
  60.     /* Receive POS_X or error */
  61.     equ_status = check_syntax(str, strlen(str));
  62.   } while(equ_status == ERROR_X);
  63.  
  64.   /* Catch previous value of x */
  65.   value_x = catch_pre_x(str, equ_status);  
  66.  
  67.   /* Send previous value of x to
  68.    * cordenate calculate */
  69.   calc_cordenates(cord, value_x);
  70.  
  71.   /* Catch array size and put on
  72.    * equation variable and
  73.    * show results on log */
  74.   equation = sizeof(cord)/sizeof(cord[0]);
  75.   log_msg(value_x, cord, equation, str);
  76.  
  77.   /* Reorganize array removing
  78.    * x and previous value of x
  79.    * after print this value */
  80.   sort_array(str, strlen(str), equ_status);
  81.  
  82.   /* Starting calculus and
  83.    * after show this on log */
  84.   start_calc(str, cord);
  85.   log_msg(value_x, cord, sizeof(cord)/sizeof(cord[0]), str);
  86.  
  87.   /* Show results on cordenates graphic */
  88.   show_cordenates(cord, sizeof(cord)/sizeof(cord[0]));
  89.   show_graphics(cord, sizeof(cord)/sizeof(cord[0]));
  90.  
  91.   return 0;
  92. }
  93.  
  94. //------------
  95. /*  The Equation Paint (v 0.01) on Linux Console!
  96.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  97.  *  File: first_degree.h
  98.  *  Blog: http://OAKcoders.com/ (Access it)!
  99. */
  100.  
  101. #include <stdlib.h>
  102. #include <stdio.h>
  103. #include "global.h"
  104.  
  105. int check_syntax(char *args, int argc);
  106. int catch_pre_x(char *args, int pos_x);
  107. void calc_cordenates(float *args, int arg_x);
  108. void sort_array(char *args, int argc, int pos_x);
  109. void start_calc(char *args, float *arg_f);
  110. int check_signal(char * args);
  111.  
  112. /* This function Search by X */
  113. int check_syntax(char *args, int argc)
  114. {
  115.   int pos_x;  
  116.   for(pos_x = 0; pos_x < argc; pos_x++)
  117.   {
  118.     /* Searching by X */
  119.     if(args[pos_x] == 'x' || args[pos_x] == 'X')
  120.       return pos_x;    
  121.   }
  122.  
  123.   /* If don't find to letter 'X', ERROR_X return! */
  124.   return ERROR_X;
  125. }
  126.  
  127. /* This function converts a previous value of x. */
  128. int catch_pre_x(char *args, int pos_x)
  129. {
  130.   char *tmp; /* Temporary Pointer */
  131.   int previous_x = 0, i;
  132.  
  133.   tmp = (char *) malloc(pos_x-1);  
  134.  
  135.   /* Catching number value */
  136.   for(i = 0; i < pos_x; i++)
  137.     tmp[i] = args[i];  
  138.  
  139.   /* Converting to integer */
  140.   previous_x = atoi(tmp);  
  141.   free(tmp);
  142.  
  143.   return previous_x;
  144. }
  145.  
  146. /* Calculate cordenates with X(-1,0,1) */
  147. void calc_cordenates(float *args, int arg_x)
  148. {  
  149.   args[0] = arg_x * -1;
  150.   args[1] = arg_x * 0;
  151.   args[2] = arg_x * 1;
  152. }
  153.  
  154. /* Define array only with values after of x */
  155. void sort_array(char *args, int argc, int pos_x)
  156. {
  157.   int i, aux = 0;
  158.  
  159.   for(i = pos_x+1; i < argc; i++)
  160.   {
  161.     /* Resorting... */
  162.     args[aux++] = args[i];
  163.     args[i] = 0;
  164.   }
  165. }
  166.  
  167. /* Starting Calculations */
  168. void start_calc(char *args, float *arg_f)
  169. {
  170.   int signal, number_;
  171.  
  172.   /* Check equation signal */
  173.   signal = check_signal(args);
  174.   /* Converting number to integer */
  175.   number_ = atoi(args);
  176.  
  177.   /* Calculate coordinate Y
  178.      with base on signal */
  179.   if(signal == POS_NUM)
  180.   {    
  181.     arg_f[0] += number_;
  182.     arg_f[1] += number_;
  183.     arg_f[2] += number_;
  184.   }
  185.   else
  186.   {
  187.     arg_f[0] -= number_;
  188.     arg_f[1] -= number_;
  189.     arg_f[2] -= number_;
  190.   }
  191.  
  192.   printf("\tSignal: [%d] / Value: [%d]\n", signal, number_);
  193. }  
  194.  
  195. /* This function check the equation signal */
  196. int check_signal(char *args)
  197. {
  198.   if(args[0] == '+') return POS_NUM;
  199.   return NEG_NUM;
  200. }  
  201.  
  202. //------------
  203. /*  The Equation Paint (v 0.01) on Linux Console!
  204.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  205.  *  File: paint_graphics.h
  206.  *  Blog: http://OAKcoders.com/ (Access it)!
  207. */
  208.  
  209. #include <stdio.h>
  210. #include <string.h>
  211. #include "global.h"
  212.  
  213. void log_msg(int arg_x, float *arg_f, size_t argc, char *args);
  214. void show_cordenates(float *arg_f, size_t argc);
  215. void show_graphics(float *arg_f, size_t argc);
  216. void create_map(char args[][GS], int argc);
  217.  
  218. /* This function is responsible to
  219.  * showing calculus record */
  220. void log_msg(int arg_x, float *arg_f, size_t argc, char *args)
  221. {
  222.   int i;
  223.  
  224.   /* Previous value of x */
  225.   printf("\tPrevious: %d\n", arg_x);  
  226.  
  227.   /* Show cordenates result */
  228.   for(i = 0; i < argc; i++)
  229.     printf("\tFloat [%d]: %.1f\n", i-1, arg_f[i]);
  230.  
  231.   printf("\tCurrent Equation: %s\n", args);
  232. }
  233.  
  234. /* Showing cordenates of x,y point */
  235. void show_cordenates(float *arg_f, size_t argc)
  236. {
  237.    int i;
  238.    
  239.    printf("\t--------------------\n");    
  240.    printf("\t- Cordenates:\n");
  241.    printf("\tx\t|\ty\n");
  242.    printf("\t--------------------\n");    
  243.    
  244.    for(i = 0; i < argc; i++)
  245.       printf("\t%d\t|\t%2.1f\n", i-1, arg_f[i]);
  246.    
  247.    printf("\t--------------------\n");
  248.    getchar();
  249. }
  250.  
  251. /* Showing graphic cordenates
  252.  * of x,y point */
  253. void show_graphics(float *arg_f, size_t argc)
  254. {
  255.     /* (GS) is a graphic size constant defined
  256.      * on global.h */
  257.  
  258.     int i, j; // Line / Column
  259.     char tmp[GS][GS];
  260.    
  261.     create_map(tmp, GS);
  262.    
  263.     printf("\n\t--------------------\n");    
  264.     printf("\t- Equation Graphic:\n");
  265.     printf("\t--------------------\n");
  266.    
  267.     /* Put arrow details on
  268.      * graphic vertex */
  269.     tmp[0][ (GS-1)/2 ] = ARROW_NORTH;
  270.     tmp[GS-1][ (GS-1)/2 ] = ARROW_SOUTH;
  271.     tmp[ (GS-1)/2 ][0] = ARROW_LEFT;
  272.     tmp[ (GS-1)/2 ][GS-1] = ARROW_RIGHT;
  273.    
  274.     /* Creating x vertex(line) */    
  275.     for(i = 1; i < GS-1; i++)
  276.       tmp[i][ (GS-1)/2 ] = V;
  277.    
  278.     tmp[( (GS-1)/2) -1 ][(GS-1)] = 'x';
  279.     /*---------------------*/
  280.    
  281.     /* Creating y vertex(line) */    
  282.     for(i = 1; i < GS-1; i++)
  283.       tmp[ (GS-1)/2 ][i] = H;
  284.  
  285.     tmp[0][ ((GS-1)/2) -1 ] = 'y';
  286.     /*---------------------*/
  287.    
  288.     /* Put Default X Cordenates (-1,0,1) */
  289.     tmp[ ((GS-1)/2) +1 ] [ ((GS-1)/2) -1 ] = '1';
  290.     tmp[ ((GS-1)/2) +1 ] [ ((GS-1)/2) +1 ] = '1';
  291.    
  292.     /* Print Graphic */
  293.     for(i = 0; i < GS; i++)
  294.     {
  295.       printf("\n\n");
  296.       for(j = 0; j < GS; j++)
  297.       printf("  %c  ", tmp[i][j]);      
  298.     }
  299.    
  300.     puts("\n");
  301.     getchar();
  302.    
  303. }  
  304.  
  305. /* Creating graphic map */
  306. void create_map(char args[][GS], int argc)
  307. {
  308.     int i, j;
  309.    
  310.     for(i = 0; i < argc; i++)
  311.     {
  312.       for(j = 0; j < argc; j++)
  313.       args[i][j] = ' ';      
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement