은/

2011-02-27 2 views
0
/* 
    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\n", index); 
     printf("Number of legs %d\n", leg_num); 

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

     for (n = 0; n < leg_num; n ++) 
    { 
     printf("At leg Number%d\n", 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("\n"); 

    } 

    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).\n"); /*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).\n"); 
     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; 
    } 
    } 
} 

변수 덮어 쓰기 (읽기가 쉬웠 그래서 내가 잠시 다시이 질문을하지만, 내 코드가 너무 지저분 그래서 함수에서 다시 작성) 내가 루프에서 빠져 나올 때마다 내 leg_num이 쓰여지는 것이므로 printf에서 인쇄하려고 할 때 마지막으로 입력 한 leg_num은 다음에서 사용중인 숫자입니다.은/

for (n = 0; n < leg_num; n ++) 인쇄 루프에서 두 번째 것

EDITED

그래서 2 번 여행을 할 때 # 1은 3 번 다리가 있고 # 2는 2 번 다리가 있고 인쇄 루프를 통과 할 때마다 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 문을 입력했기 때문에 다른 모든 기능이 제대로 작동합니다. leg_num을 배열에 저장하고이를 사용하는 방법을 잘 모르지만, 이것이 숙제의 일부이고 교수님이 거의 모든 것을 제한하고 있지만 기본 루프 단순 배열을 제한한다는 사실을 생각하고있었습니다. (N ++; N

+0

에? 첫 번째 또는 두 번째? – BlackBear

+0

printf 루프에있는 문자 –

+0

죄송합니다. 이해가되지 않습니다. "[...] 마지막 leg_num을 인쇄하려고 할 때". leg_num은 input_leg 함수에서 읽은 다음 일정한 상태로 유지되는 것 같습니다. – CygnusX1

답변

1
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++) 
+0

아를 재실행 할 것 같아. 그냥 배열 ._을 사용해야했다. –