2011-02-27 39 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; 
    } 
    } 
} 

(我問這個問題而回,但我的代碼是太亂了,所以我重新寫了它的功能,因此更容易閱讀)For循環/過書面可變

我的問題因爲我每次走出循環都要重寫我的leg_num,所以當我嘗試在printf中打印輸出的最後一個leg_num時,我輸入的數字是:

for(n = 0; n < leg_num; n ++)第二個在印刷循環中

EDITED

所以,當我在2次旅行中旅行#1有3次旅行#2當它通過打印循環運行時,它有2個腿,每次旅行只打印2次旅行。因爲我把printf語句沿途檢查如果是這樣的問題,但它不是

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 

其他一切工作正常。我正在考慮將leg_num保存爲一個數組並使用它,但不知道如何去做,再加上這是作業的一部分,我們的教授幾乎限制了所有內容,但基本循環只是簡單的數組。

+0

這對於(N = 0; N BlackBear 2011-02-27 21:09:52

+0

printf循環中的一個 – 2011-02-27 21:11:21

+0

對不起,我不理解你。 「當我嘗試打印最後一個leg_num時」。 leg_num似乎是在input_leg函數中讀取的,然後保持不變? – CygnusX1 2011-02-27 21:14:41

回答

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

啊所以而不是使用另一個變量,我只需要使用數組._。 – 2011-02-27 21:30:22