English 中文(简体)
书面变量
原标题:For loop / over written variable
/*
  Program to calculate trip and plan flights
*/
#define TRIP 6
#define NUMLEG 10 
#include <stdio.h>
#include <string.h>

int input_trip(void);
int input_leg(int travel_leg[NUMLEG], int index);
char input_travel_type(char leg_type[TRIP][NUMLEG], int n, int index, int leg_num);
int main(void)
{ 
  int  row, col, trip_num, index, travel_leg[NUMLEG], leg_num, n; 
  char leg_type[TRIP][NUMLEG];
  trip_num = input_trip();
  for (index =0; index < trip_num; index++)
    { 
      leg_num = input_leg(travel_leg,index);

      printf("At Trip Number:%d
", index);
      printf("Number of legs %d
", leg_num );

      printf("A Airplane
");
      printf("R Train and rail travel
");
      printf("B Bus
");
      printf("C Car
");
      printf("F Ferry
");
      printf("S Cruise ship
");
      printf("M Motorcycle
");
      printf("Y Bicycle
");
      printf("T Boat other than a ferry or cruise ship
");
      printf("D Dirigible
");
      printf("O Other
");
      printf("NOTE!!:Please using capital letters (case sensitive).
");

      for (n = 0; n < leg_num; n ++)
    {
      printf("At leg Number%d
", n);
      input_travel_type(leg_type, n, index, leg_num);
    }
    }

  for (index = 0; index < trip_num; index++)
    {
      printf("Trip#:%d Num_leg:%d ", index+1, travel_leg[index]);
      for (n = 0;  n < leg_num ; n++)
    printf("Leg_type(#%d):%c ",n+1, leg_type[index][n]);
      printf("
");

    }

  return 0;
}

int input_trip(void)
{
  int trip_num;

  printf("Please enter the number of trips:");
  scanf("%d", &trip_num);
  // if( (trip_num <= TRIP) && (trip_num >= 3))
  if( (trip_num <= TRIP) && (trip_num >=1) ) 
    {
      return trip_num;
    }


  else 
    {
      while ((trip_num < 1) || ( trip_num > TRIP))
    {
      printf("Invalid number of trip. (Min of 3 trips and Max 6 trips).
");  /*input number of trips*/
      printf("Please enter the number of trips:");
      scanf("%d", &trip_num);
      if( (trip_num <= TRIP) && (trip_num >= 1))
        {
          return trip_num;
        }
    }

    } 
}
int input_leg(int travel_leg[NUMLEG], int index)
{
  int leg_num, i;
  char travel_type, checkA, A, R, B, C, F, S, M, Y, T, D, O;

  printf("Please enter the number of legs in your trip:");
  scanf("%d", &leg_num);
  if ( (leg_num <= NUMLEG) && (leg_num > 0) )
    {    
      travel_leg[index]=leg_num;
      return leg_num;
    }
  else 
    {
      while ( (leg_num < 0) || (leg_num > NUMLEG))
    {
      printf("Invalid number of legs(min 1 and max 10 legs).
");
      printf("Please enter the number of legs in your trip:");
      scanf("%d", &leg_num);
      if ( (leg_num <= NUMLEG) && (leg_num > 0) )
        {
          travel_leg[index]=leg_num;
          return leg_num;
        }
    }
    }
}

char input_travel_type(char leg_type[TRIP][NUMLEG], int n, int index, int leg_num)
{
  char travel_type, checkA;
  printf("Please enter the leg type for leg#%d:", n+1);
  scanf("%c", &travel_type);
  checkA = ( (travel_type ==  A ) || (travel_type ==  R ) || (travel_type ==  B ) || 
         (travel_type ==  C ) || (travel_type ==  F ) || (travel_type ==  S ) ||
         (travel_type ==  M ) || (travel_type ==  Y ) || (travel_type ==  T ) ||
         (travel_type ==  D ) || (travel_type ==  0 ) );

  if (checkA == 1)
    {
      leg_type[index][n]=travel_type;
    }
  else 
    {
      while (checkA != 1)
    {
      printf("Please enter the leg type for leg#%d:", n+1);
      scanf("%c", &travel_type);
      checkA = ( (travel_type ==  A ) || (travel_type ==  R ) || (travel_type ==  B ) || 
             (travel_type ==  C ) || (travel_type ==  F ) || (travel_type ==  S ) ||
             (travel_type ==  M ) || (travel_type ==  Y ) || (travel_type ==  T ) ||
             (travel_type ==  D ) || (travel_type ==  0 ) );

      if (checkA == 1)
        leg_type[index][n]=travel_type;
    }
    }
}

(我回过问这个问题,但我的法典太过错,所以我重新把它换成职能,以便更容易阅读)

问题Im在于,每当我走出坡道时,我腿部就翻了一番,因此,当我试图用印刷版印刷时,我所说的最后一段是:

(n = 0; n < leg_num ; n++) 页: 1

<><> >>>>

因此,当我乘坐2次旅行时,第1号有3次双程旅行,第2号有两条腿,通过印刷机进行,每次旅行只印两条腿。

Trip#:1 Num_leg:3 Leg_type(#1):C Leg_type(#2):B 
Trip#:2 Num_leg:2 Leg_type(#1):A Leg_type(#2):R 

Trip#:1 Num_leg:1 Leg_type(#1):S Leg_type(#2): 
Trip#:2 Num_leg:2 Leg_type(#1):F Leg_type(#2):S 

所有其他工作都很出色,因为我沿途做了印刷发言,以检查是否是问题,但却是问题。 我正想将“南”带入一个阵列,但并不肯定如何去做,此外,这是家务劳动的一部分,我们的教授正在限制几乎所有东西,而只是基本通道。

最佳回答
  printf("Trip#:%d Num_leg:%d ", index+1, travel_leg[index]);
  for (n = 0;  n < leg_num ; n++)

改变

  printf("Trip#:%d Num_leg:%d ", index+1, travel_leg[index]);
  for (n = 0;  n < travel_leg[index] ; n++)
问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签