Advertisement
jbn6972

Tws progs

Nov 29th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define vi vector<int>
  6. #define vll vector<long long int>
  7.  
  8. void BellmanFord(int network[3][3], int routers, int connections, int source, int arr[3][3])
  9. {
  10.     int dist[routers];
  11.     for (int i = 0; i < routers; i++)
  12.     {
  13.         dist[i] = INT_MAX;
  14.     }
  15.  
  16.     dist[source] = 0;
  17.  
  18.     for (int i = 0; i < routers - 1; i++)
  19.     {
  20.         for (int j = 0; j < connections; j++)
  21.         {
  22.             if (dist[network[j][0]] != INT_MAX && dist[network[j][0]] + network[j][2] < dist[network[j][1]])
  23.                 dist[network[j][1]] = dist[network[j][0]] + network[j][2];
  24.         }
  25.     }
  26.  
  27.     // cout << "Router Distance from Source-router " << source + 1 << endl;
  28.     for (int i = 0; i < routers; i++)
  29.     {
  30.         arr[source][i] = min(arr[source][i], dist[i]);
  31.         arr[i][source] = min(arr[source][i], dist[i]);
  32.     }
  33. }
  34.  
  35. int main()
  36. {
  37.  
  38.     std::ios::sync_with_stdio(false);
  39.     int routers;
  40.     cout << "Enter the number of routers :: ";
  41.     cin >> routers;
  42.     int connections;
  43.     cout << "Enter the number of connections :: ";
  44.     cin >> connections;
  45.     int s, d, hc;
  46.     int network[3][3];
  47.     int arr[3][3];
  48.     for (int i = 0; i < routers; i++)
  49.     {
  50.         for (int j = 0; j < routers; j++)
  51.         {
  52.             network[i][j] = INT_MAX;
  53.         }
  54.     }
  55.     cout << "Enter " << connections << " details in (source_router,destination_router,hop_cost) format " << endl;
  56.     for (int i = 0; i < connections; i++)
  57.     {
  58.         cin >> s >> d >> hc;
  59.         network[s - 1][d - 1] = hc;
  60.         network[d - 1][s - 1] = hc;
  61.     }
  62.     for (int i = 0; i < 3; i++)
  63.     {
  64.         for (int j = 0; j < 3; j++)
  65.         {
  66.             arr[i][j] = INT_MAX;
  67.         }
  68.     }
  69.     int choice;
  70.     while (1)
  71.     {
  72.         cout << "Do you want to compute the shortest hops ? press 1 to compute ,any other character to exit :: ";
  73.         cin >> choice;
  74.         if (choice == 1)
  75.         {
  76.             int src;
  77.             cout << "Enter the source router to compute the shortest hops to other routers :: ";
  78.             cin>>src;
  79.             BellmanFord(network, routers, connections, src-1, arr);
  80.             for (int i = 0; i < 3; i++)
  81.             {
  82.                 for (int j = 0; j < 3; j++)
  83.                 {
  84.                     cout << arr[i][j] << " ";
  85.                 }
  86.                 cout << endl;
  87.             }
  88.         }
  89.         else
  90.         {
  91.             break;
  92.         }
  93.     }
  94.     return 0;
  95. }
  96.  
  97. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  98. #include <bits/stdc++.h>
  99. using namespace std;
  100. #define ll long long
  101. #define vi vector<int>
  102. #define vll vector<long long int>
  103.  
  104. char t[128], cs[128], g[] = "10001000000100001";
  105. int a, e, c;
  106. int N = strlen(g);
  107.  
  108. void xor_fun()
  109. {
  110.     for (c = 1; c < N; c++)
  111.     {
  112.         cs[c] = ((cs[c] == g[c]) ? '0' : '1');
  113.     }
  114. }
  115.  
  116. void crc()
  117. {
  118.     for (e = 0; e < N; e++)
  119.     {
  120.         cs[c] = cs[c + 1];
  121.     }
  122.  
  123.     do
  124.     {
  125.         if (cs[0] == '1')
  126.         {
  127.             xor_fun();
  128.         }
  129.         for (c = 0; c < N; c++)
  130.         {
  131.             cs[c] = cs[c + 1];
  132.             cs[c] == t[e++];
  133.         }
  134.     } while (e <= a + N - 1);
  135. }
  136.  
  137. int main()
  138. {
  139.     std::ios::sync_with_stdio(false);
  140.     cout<<"Enter the Polynomial :: ";
  141.     cin>>t;
  142.     cout<<"\nGenerating Polynomial is :: "<<g;
  143.     a = strlen(t);
  144.  
  145.     for(e = a;e<a+N-1;e++){
  146.         t[e] = '0';
  147.     }
  148.     cout<<"\nModified t[u] is :: "<<t;
  149.     crc();
  150.     getchar;
  151.     cout<<"\nChecksum is :: "<<cs;
  152.     for(e=a;e<a+N-1;e++){
  153.         t[e] = cs[e-a];
  154.     }
  155.     cout<<"\nFinal Codeword is :: "<<t;
  156.     cout<<"\nTest Error detection 0(yes) 1(no) ? :: ";
  157.     cin>>e;
  158.  
  159.     if(e==0){
  160.         cout<<"\nEnter the position where error is to be inserted :: ";
  161.         cin>>e;
  162.         t[e] = (t[e] == '0')?'1':'0';
  163.         cout<<"\nErrorneous data :: "<<t;
  164.     }
  165.     crc();
  166.     for(e=0;(e<N-1) && (cs[e]!='1');e++);
  167.  
  168.     if(e<N-1){
  169.         cout<<"\nError Detected";
  170.     }
  171.     else{
  172.         cout<<"\nNo Error Detected";
  173.     }
  174. }
  175. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  176. #include <stdio.h>
  177. #include <stdlib.h>
  178. #include <math.h>
  179.  
  180. int gcd(int a, int b)
  181. {
  182.     int c;
  183.     while(a != b)
  184.     {
  185.         if(a < b)
  186.         {
  187.             c = a;
  188.             a = b;
  189.             b = c;
  190.         }
  191.         a -= b;
  192.     }
  193.     return a;
  194. }
  195.  
  196. int mod(int x,int y, int p)
  197. {
  198.     int res = 1;
  199.  
  200.     x = x % p;
  201.     if (x == 0) return 0;
  202.  
  203.     while (y > 0)
  204.     {
  205.         // If y is odd, multiply x with result
  206.         if (y & 1)
  207.             res = (res*x) % p;
  208.         y = y>>1; // y = y/2
  209.         x = (x*x) % p;
  210.     }
  211.     return res;
  212. }
  213.  
  214. int main()
  215. {
  216.     int p,q,n,e,m,c,d,x,z;
  217.     int en[100],de[100], j = 0,fact_z[100];
  218.     printf("\nEnter the value of p and q: ");
  219.     scanf("%d%d", &p, &q);
  220.     n = p*q;
  221.     int cnt;
  222.     cnt = 0;
  223.     z = (p-1)*(q-1);
  224.     for(int i = 1;i<z;i++){
  225.         if(z%i == 0){
  226.             printf("%d ",i);
  227.             fact_z[cnt] = i;
  228.             cnt++;  
  229.         }
  230.     }
  231.     printf("Choose a value for e from a number which is not in list :: ");
  232.     scanf("%d",&e);
  233.     for(int i=0;i<cnt;i++){
  234.         if(fact_z[i] == e|| gcd(e,z) != 1){
  235.             printf("e belongs to the factors list get lost!!");
  236.             return 0;
  237.         }
  238.     }
  239.     printf("Enter the message(int) to be encrypted: ");
  240.     scanf("%d", &m);
  241.     printf("Before encryption: %d\n", m);
  242.     c = mod(m, e, n);
  243.     printf("After encryption: %d\n", c);
  244.     d = z;
  245.  
  246.     cnt = 2;
  247.     while (1){
  248.         if(d%e == 0){
  249.             break;
  250.         }else{
  251.             d = (cnt*z)+1;
  252.             cnt++;
  253.         }
  254.     }
  255.     d = d/e;
  256.     printf("\n%d\n",d);
  257.     x = mod(c, d, n);
  258.     printf("After decryption: %d\n", x);
  259.  
  260.     return 0;
  261. }
  262. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  263. #include <iostream>
  264. #include <dos.h>
  265. #include <stdlib.h>
  266. #include <stdio.h>
  267. #include<windows.h>
  268.  
  269. using namespace std;
  270.  
  271. #define bucketSize 512
  272.  
  273. int random(int min, int max){
  274.    return min + rand() / (RAND_MAX / (max - min + 1) + 1);
  275. }
  276.  
  277. void bktInput(int a, int b)
  278. {
  279.     if (a > bucketSize)
  280.         cout << "\n\t\tBucket overflow";
  281.     else
  282.     {
  283.         Sleep(500);
  284.         while (a > b)
  285.         {
  286.             cout << "\n\t\t" << b << " bytes outputted."<<"\t Remaining bytes "<<a;
  287.             a -= b;
  288.             Sleep(500);
  289.         }
  290.         if (a > 0)
  291.             cout << "\n\t\tLast " << a << " bytes sent\t";
  292.         cout << "\n\t\tBucket output successful";
  293.     }
  294. }
  295.  
  296. int main()
  297. {
  298.     int op, pktSize;
  299.     // randomize();
  300.     cout << "Enter output rate : ";
  301.     cin >> op;
  302.     for (int i = 1; i <= 5; i++)
  303.     {
  304.         Sleep(random(100,1000));
  305.         pktSize = random(100,700);
  306.         cout << "\nPacket no " << i << "\tPacket size = " << pktSize;
  307.         bktInput(pktSize, op);
  308.     }
  309.     return 0;
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement