كود لتطبيق C

Mahmood_18 • منذ 5 سنوات

السلام عليكم ورحمة الله وبركاته 

 

لدي كود برمجي تم عمله بواسطة لغة C الأم

 

السؤال هو task 4 باقي الأمور تم عملها والحمد لله 

 

 

كلمات دليلية: c

ساعد بالإجابة

"إن في قضاء حوائج الناس لذة لا يَعرفها إلا من جربها، فافعل الخير مهما استصغرته فإنك لا تدري أي حسنة تدخلك الجنة."

الإجابات (1)

Mahmood_18 • منذ 5 سنوات
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_ROUTE_SIZE 15

char* find_route_common_country(char* route1[MAX_ROUTE_SIZE], char* route2[MAX_ROUTE_SIZE], int route_len1, int route_len2)
{
   int i,j;

   for(i = 0; i < route_len1; i++)
   {
      for(j = 0; j < route_len2; j++)
      {
         if(strcmp(route1[i], route2[j]) == 0)
            return route1[i];
      }
   }
   return NULL;
}

int count_chars(char* str)
{
   int count = 0;
   while(*str != '\0')
   {
      count++;
      str++;
   }
   return count;
}


int main()
{
   int flightNo_1;
   int flightNo_2;
   char destination[15];
   char input_route_1[256];
   char input_route_2[256];
   int route_len_1 = 0;
   int route_len_2 = 0;
   char* route1_countries[MAX_ROUTE_SIZE];
   char* route2_countries[MAX_ROUTE_SIZE];
   char* country;
   char* common_country;


   printf("Enter first flight number: ");
   scanf("%d", &flightNo_1);
   printf("Enter second flight number: ");
   scanf("%d", &flightNo_2);
   getchar();

   printf("Enter destination country name: ");
   fgets(destination, sizeof(destination), stdin);
   destination[strlen(destination) - 1] = '\0';

   printf("Enter route (country names separate by space) of flight %d:\n", flightNo_1);
   fgets(input_route_1, sizeof(input_route_1), stdin);
   input_route_1[strlen(input_route_1) - 1] = '\0';

   printf("Enter route (country names separate by space) of flight %d:\n", flightNo_2);
   fgets(input_route_2, sizeof(input_route_2), stdin);
   input_route_2[strlen(input_route_2) - 1] = '\0';

   country = strtok(input_route_1, " ");
   while(country != NULL)
   {
      route1_countries[route_len_1++] = country;
      country = strtok(NULL, " ");
   }

   country = strtok(input_route_2, " ");
   while(country != NULL)
   {
      route2_countries[route_len_2++] = country;
      country = strtok(NULL, " ");
   }


   common_country = find_route_common_country(route1_countries, route2_countries, route_len_1, route_len_2);

   if(common_country != NULL)
      printf("\nBoth flights route via common country '%s'.\n", common_country);
   else
      printf("The flights route do not have any common country.\n");


   printf("Number of characters in destination '%s': %d\n", destination, count_chars(destination));

   return 0;
}

 

لايوجد لديك حساب في عالم البرمجة؟

تحب تنضم لعالم البرمجة؟ وتنشئ عالمك الخاص، تنشر المقالات، الدورات، تشارك المبرمجين وتساعد الآخرين، اشترك الآن بخطوات يسيرة !