Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4.  
  5. int main(){
  6.     char inp[512];
  7.     int i, j, len, is_constant, point, expo, plus, minus, after_e;
  8.  
  9. start:
  10.  
  11.  
  12.     i = 0;
  13.     j = 0;
  14.     len = 0;
  15.     is_constant = 1;
  16.     point = 0;
  17.     expo = 0;
  18.     plus = 0;
  19.     minus = 0;
  20.     after_e = 0;
  21.  
  22.     printf("Enter Constant: ");
  23.     scanf("%[^\n]s", inp);
  24.  
  25.     len = strlen(inp);
  26.  
  27.     for(i = 0; i < len; i++){
  28.  
  29.         // Check for digits and .
  30.         if(!((inp[i] >= '0' && inp[i] <= '9') || inp[i] == '.' || inp[i] == '-' || inp[i] == '+' || inp[i] == 'E' || inp[i] == 'e')){
  31.             is_constant = 0;
  32.             break;
  33.         }
  34.  
  35.         // Check for Multiple .
  36.         if(inp[i] == '.'){
  37.             point++;
  38.             if(point > 1){
  39.                 is_constant = 0;
  40.                 break;
  41.             }
  42.         }
  43.  
  44.         if(inp[i] == 'E' || inp[i] == 'e'){
  45.             expo++;
  46.             if(expo > 1){
  47.                 is_constant = 0;
  48.                 break;
  49.             }
  50.         }
  51.  
  52.         if(inp[i] == '+'){
  53.             plus++;
  54.             if(plus > 1){
  55.                 is_constant = 0;
  56.                 break;
  57.             }
  58.         }
  59.  
  60.         if(inp[i] == '-'){
  61.             minus++;
  62.             if(minus > 1){
  63.                 is_constant = 0;
  64.                 break;
  65.             }
  66.         }
  67.     }
  68.  
  69.     if(is_constant == 1 && expo == 1){
  70.         for(i = 0; i < len; i++){
  71.             // Plus Minus out of E
  72.             if(inp[i] == '-' || inp[i] == '+'){
  73.                 is_constant = 0;
  74.                 break;
  75.             }
  76.  
  77.             if(inp[i] == 'E' || inp[i] == 'e'){
  78.                 // More Checks
  79.                 for(j = i; j < len; j++){
  80.                     after_e++;
  81.                 }
  82.             }
  83.         }
  84.  
  85.         if(expo > 0 && after_e == 1){
  86.             is_constant = 0;
  87.         }
  88.     }
  89.  
  90.  
  91.     if(is_constant == 1 && len > 0){
  92.         printf("Constant\n\n");
  93.     }else{
  94.         printf("Not a Constant\n\n");
  95.     }
  96.  
  97.     fflush(stdin);
  98.     goto start;
  99.  
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement