Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "nd.h"
  3. #include "nsd.h"
  4.  
  5. int jeCislo(char ch) {
  6.     if (ch >= '0' && ch <= '9') {
  7.         return (1);
  8.     }
  9.     if (ch == ' ' || ch =='-') {
  10.     // if (ch == ' ') {
  11.         return (1);
  12.     }
  13.     return (0);
  14. }
  15.  
  16. int main(int argc, char** argv) {
  17.  
  18.    
  19.    
  20.     char buf[256];
  21.  
  22.     while (fgets(buf, sizeof (buf), stdin)) {
  23.  
  24.        
  25.         char cislo1[16];
  26.         char cislo2[16];
  27.  
  28.         int mIndex = 0;
  29.         int index = 0;
  30.         int error =0;
  31.         if(buf[0] == ' '){
  32.             fprintf(stderr,"Wrong input.");
  33.             continue;
  34.         }
  35.         while (buf[mIndex] != ' ') {
  36.           //  printf("prislo mi cislo: -%c-\n", buf[mIndex]);
  37.            
  38.             if (jeCislo(buf[mIndex]) == 0) {
  39.               //  printf("Wrong input : -%c-\n", buf[mIndex]);
  40.                 error++;
  41.                 break;
  42.             }
  43.             cislo1[index] = buf[mIndex];
  44.         if(cislo1[index] == '-' && index!=0){
  45.            error++;
  46.         break;
  47.         }
  48.             index++;
  49.             mIndex++;
  50.         }
  51.         cislo1[index] = '\0';
  52.       //  printf("%s\n", cislo1);
  53.        
  54.         mIndex++;
  55.         int index2 =0;
  56.          if(buf[mIndex] == ' ' || error>0){
  57.             fprintf(stderr,"Wrong input.");
  58.             continue;
  59.         }
  60.     if((buf[0] =='-' && buf[1] ==' ') || (buf[mIndex] == '-' && jeCislo(buf[mIndex+1])==0)){ //minus na zacatku
  61.              fprintf(stderr,"Wrong input.");
  62.             continue;
  63.         }
  64.         while(buf[mIndex] != '\n'){
  65.           if(buf[mIndex]==' '){
  66.                 error++;
  67.                 break;
  68.             }
  69.               if (jeCislo(buf[mIndex]) == 0) {
  70.                // printf("Wrong input2 : -%c-\n", buf[mIndex]);
  71.                 error++;
  72.                 break;
  73.             }
  74.             cislo2[index2] = buf[mIndex];
  75.          if(cislo2[index2] == '-' && index2!=0){
  76.            error++;
  77.         break;
  78.         }
  79.             index2++;
  80.             mIndex++;
  81.         }
  82.        
  83.         cislo2[index2] = '\0';
  84.       //  printf("%s\n", cislo2);
  85.        
  86.         if(error >0 || (cislo2[0] - '0') == -48){
  87.              fprintf(stderr,"Wrong input.");
  88.             continue;
  89.         }
  90.        
  91.         int number1;
  92.         int number2;
  93.         sscanf(cislo1, "%d", &number1);
  94.         sscanf(cislo2, "%d", &number2);
  95.        // printf("cisla jsou: %d a %d\n", number1, number2);
  96.     if(number1 != 1 && number2 !=1 && nd(number1)==1 && nd(number2)==1){
  97.        printf("prime\n");  
  98.        continue;
  99.     }
  100.    
  101.     printf("%d\n", nsd(number1, number2));
  102.  
  103.     }
  104.  
  105.    fprintf(stderr,"DONE");
  106.     return (0);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement