Advertisement
DarK_Camper

get_input.c SECS

Apr 14th, 2015
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include "string_me8.h"
  6. #include "get_input.h"
  7.  
  8. char input[60];//Remember to always reset
  9. int boolean = 1;//Never off.
  10.  
  11. int get_bool_input(){
  12.     int x = 0;
  13.     input[0] = '\0';
  14.     do{
  15.         fgets(input, 60, stdin);
  16.         clean_fgets(&input);
  17.         if(strcmp(input,"y")==0||strcmp(input,"Y")==0){
  18.             x = 1;
  19.             break;
  20.         }else if(strcmp(input,"n")==0||strcmp(input,"N")==0){
  21.             x = 0;
  22.             break;
  23.         }else{
  24.             input[0] = '\0';
  25.         }
  26.     }while(boolean==1);
  27.     return x;
  28. }
  29.  
  30. //Getting Input for Topology ==============================================================
  31. int get_topology(){
  32.     //Variable Declaration & Initialization
  33.     int topology;
  34.  
  35.     //Getting Input for Topology
  36.     input[0] = '\0';
  37.     do{
  38.         printf("Please select the topology of the circuit:\n\t1 - Series Circuit\n\t2 - Parallel Circuit\n\t3 - Ladder Circuit\n\n");
  39.         fgets(input, 60, stdin);
  40.         clean_fgets(&input);
  41.         if(strcmp(input,"1")==0||strcmp(input,"2")==0||strcmp(input,"3")==0){
  42.             break;
  43.         }else{
  44.             input[0] = '\0';
  45.         }
  46.     }while(boolean==1);
  47.     topology = atoi(input);
  48.     return topology;
  49. }
  50.  
  51. //Getting Input for Source Type ==============================================================
  52. int get_source_type(){
  53.     //Variable Declaration & Initialization
  54.     int source_type;
  55.  
  56.     //Getting Input for Source Type
  57.     input[0] = '\0';
  58.     do{
  59.         printf("Please select the type of Independent Source:\n\t1 - Voltage Source\n\t2 - Current Source\n\n");
  60.         fgets(input, 60, stdin);
  61.         clean_fgets(&input);
  62.         if(strcmp(input,"1")==0||strcmp(input,"2")==0){
  63.             break;
  64.         }else{
  65.             input[0] = '\0';
  66.         }
  67.     }while(boolean==1);
  68.     source_type = atoi(input);
  69.     return source_type;
  70. }
  71.  
  72. //Getting Input for Value of Source ==============================================================
  73. double get_source_value(){
  74.     //Variable Declaration & Initialization
  75.     double source_value;
  76.  
  77.     //Getting Input for Value of Source
  78.     input[0] = '\0';
  79.     do{
  80.         printf("Please input value of the source:\n\tNote:   The maximum value should be 1000000.00,\n\t\tThe minimum value should be -1000000.00,\n\t\tand it cannot be zero.\n\n");
  81.         fgets(input, 60, stdin);
  82.         clean_fgets(&input);
  83.         source_value = atof(input);
  84.         if (source_value == 0){
  85.             continue;
  86.         }else if(source_value<-1000000.00){
  87.             continue;
  88.         }else if(source_value>1000000.00){
  89.             continue;
  90.         }else{
  91.             break;
  92.         }
  93.     }while(boolean==1);
  94.     return source_value;
  95. }
  96.  
  97. //Getting Input for Resistor Number ==============================================================
  98. int get_resist_no(){
  99.     //Variable Declaration & Initialization
  100.     int resist_no;
  101.  
  102.     //Getting Input for Resistor Number
  103.     input[0] = '\0';
  104.     do{
  105.         printf("Please input the number of resistors:\n\tNote:   The maximum number of resistors is 100,\n\t\tThe minimum number of resistors is 1.\n\n");
  106.         fgets(input, 60, stdin);
  107.         clean_fgets(&input);
  108.         resist_no = atoi(input);
  109.         if (resist_no == 0){
  110.             continue;
  111.         }else if(resist_no<1){
  112.             continue;
  113.         }else if(resist_no>100){
  114.             continue;
  115.         }else{
  116.             break;
  117.         }
  118.     }while(boolean==1);
  119.     return resist_no;
  120. }
  121.  
  122. //Getting Resistor Values ==============================================================
  123. double get_resist_value(){
  124.         //Variable Declarations
  125.         double placeholder;
  126.         int i;
  127.  
  128.         //Geting Resistor Values
  129.         input[0] = '\0';
  130.  
  131.             do{
  132.                 fgets(input, 60, stdin);
  133.                 clean_fgets(&input);
  134.                 if (strcmp(input, "Infinity")==0||strcmp(input, "infinity")==0){
  135.                     placeholder = INFINITY;
  136.                     break;
  137.                 }else if (strcmp(input, "0")==0){
  138.                     placeholder = 0;
  139.                     break;
  140.                 }else{
  141.                     placeholder = atof(input);
  142.                     printf("DEBUG: Placeholder: %.6f\n", placeholder);
  143.  
  144.                     if(placeholder<=0.0){
  145.                         printf("E: Invalid Resistance!\n\tPlease re-input value of Resistor #%d: ",i+1);
  146.                         continue;
  147.                     }else{
  148.                         break;
  149.                     }
  150.                 }
  151.             }while(boolean==1);
  152.         return placeholder;
  153. }
  154.  
  155. // Getting Individual Resistor Values ==============================================================
  156. void get_individual_resistor_values(int integerArray[], double doubleArray[]){
  157.             /*List of indexes
  158.             intArray
  159.             0 - topology
  160.             1 - source_type
  161.             2 - resist_no
  162.             3+resist_no - resistor_values
  163.  
  164.             doubleArray
  165.             0 - effective_resistance
  166.             1 - effective_voltage
  167.             2 - effective_current
  168.             3 - source_value
  169.             */
  170.         //Placeholders
  171.         int indecs = 0;
  172.         double y = 0.0;
  173.  
  174.         // Getting Individual Resistor Values
  175.         input[0] = '\0';
  176.  
  177.         printf("Type \"exit\" to stop at any time.");
  178.         do{
  179.             printf("\n\tPlease input Resistor #: ");
  180.             fgets(input, 60, stdin);
  181.             //printf(input);//debug
  182.             clean_fgets(&input);
  183.             indecs = atoi(input);
  184.             if(strcmp(input, "exit"==0)||strcmp(input, "Exit"==0)||strcmp(input, "EXIT"==0)){
  185.                 break;
  186.             }else if(indecs<=0||indecs>integerArray[2]){
  187.                 printf("E: Invalid input. Please try again.");
  188.                 continue;
  189.             }else{
  190.                 print_indie_values(integerArray,doubleArray,indecs);
  191.             }
  192.         }while(boolean == 1);
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement