Steefmaester

Test adresse ip

Sep 21st, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. //Verification de la validité d'une adresse IP by steef
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. #include "fonction.h"
  7. using namespace std ;
  8.  
  9. vector<string> split(string str, char delimiter) { //fonction permettant de découper un string
  10.   vector<string> internal;
  11.   stringstream ss(str);
  12.   string tok;
  13.  
  14.   while(getline(ss, tok, delimiter)) {
  15.     internal.push_back(tok);
  16.   }
  17.  
  18.   return internal;
  19. }
  20.  
  21.  
  22. int test_membre_IP(string IP) //teste un membres de l'adresseIP
  23. {
  24.         if(IP.size() > 3 || IP.size() == 0) return -1; //le membre de l'adresse ne doit pas dépasser 3 caractère et ne doit pas etre vide
  25.        
  26.         for (int i=0; i<IP.size(); i++)
  27.         if (IP[i] < '0' || IP[i] > '9') return -1; //Il ne doit contenir que des chiffres
  28.        
  29.         if(atoi(IP.c_str()) > 255) return -1;//Il ne doit pas dépasser 255
  30.         return 0;
  31. }
  32.  
  33. int main()
  34. {
  35.         string adresseip ="122.15.266.10";
  36. cout<<"adresse ip : "<<adresseip<<endl;
  37.        
  38. //début
  39.         int i =0;
  40.         int point =0;
  41.         while( (i != adresseip.size()) && point < 4 ){
  42.             if( adresseip.at(i) == '.' )                                //calcule du nombre de points
  43.                 point ++;
  44.             i ++;
  45.      }
  46.  
  47.         if(point != 3)
  48.         {
  49.                 cout<<"adresse ip invalide "<<endl; //verification du nombre de points
  50.                 return -1;
  51.         }
  52.  
  53. vector<string> sep;
  54. for (int i = 0; i<4 ;i++)
  55. {
  56.          sep = split(adresseip, '.'); //découpage de l'adresse ip
  57. }
  58.  
  59.  
  60. for(int i = 0; i < 4; ++i){
  61.         if (test_membre_IP(sep[i]) < 0) //vérification de chaque membre de l'adresse
  62.         {
  63.                 cout<<"Mauvaise adresse ip !"<<endl;
  64.                 return -1;
  65.         }
  66. }
  67.  
  68. cout<<"Adresse ip valide ! "<<endl;
  69.  
  70.         return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment