2017-09-23 53 views
1

我支持不使用全局變量函數。我不知道哪裏有int測量[LENGTH],x,i;像它一樣工作。我還需要一個函數來計算用戶在getMeasurements中輸入的測量數量(nrOfMeasurements)。例如,如果他們輸入4而不是f 10,這是前所未有的。來計算平均值。如何將全局變量轉換爲函數以及如何計算數組的輸入?

#include <stdio.h> 
#include <stdbool.h> 
#include <stdlib.h> 
#include <limits.h> 

#define LENGTH 10 

const char *IntroMsg = "\n\t Hello To TEST THE PRGRAM!\n", 
      *MenuMsg = "\n\t Menu \n\t v (View) \n\t e (Enter) \n\t c (Compute) \n\t r (Reset) \n\t q (Quit) \n\n"; 

int measurements[LENGTH],//not here 
     x, i;//not here 

void getMeasurements(){ 
    for(x=0; x<LENGTH; x++){ 
     printf("Enter number #%d: ", x+1); 
     scanf("%d", &measurements[x]); 

     if(measurements[x]==0){ 
      break; 
    } 
    } 
    return; 
} 

void getView(){ 
    printf("\t\n"); 
    printf("Your measurements:\n"); 
    if(x>0){ 
     for (i = 0; i < x; i++){   
     printf("\b %d ", measurements[i]); 
     } 
    } 
    else if(x==0){ 
     printf(" [NO MEASUREMENTS]"); 
    } 
    printf("\n"); 
} 

void getCompute(){ 
    int min = INT_MAX; 
    int max = INT_MIN; 
    int calculus; 
    float sum; 
    for(x=0; x<LENGTH; x++){ 
      if (measurements[x]<min) min=measurements[x]; 
      if (measurements[x]>max) max=measurements[x]; 
      sum=sum+measurements[x]; 

      calculus= measurements[x] - (sum/LENGTH); 
      printf("[%d]", calculus); 
    } 
    printf("\nMax number: %d", max); 
    printf("\nMin number: %d", min); 
    printf("\nAverage: %.2f", sum/LENGTH); 
    printf("\n\n"); 
} 
int main(){ 

    while(1){ 
     char choice; 
     puts (MenuMsg); 
     scanf(" %c", &choice); 

     if(choice=='e') 
      getMeasurements(); 

     else if(choice=='v'){ 
      getView(); 
     } 

     else if(choice=='c'){ 
      getCompute(); 
     } 

     else if(choice=='q'){ 
      break; 
     } 
    } 
    return 0; 
} 
+2

傳遞變量作爲參數傳遞給相關的功能。 – dbush

回答

0

你可以做這樣的事情:

#include <stdio.h> 

#define MAX_LENGTH 10 

const char *IntroMsg = "\n\t Hello To TEST THE PROGRAM!\n"; 
const char *MenuMsg = "\n\t Menu \n" 
         "\t v (View)\n" 
         "\t e (Enter)\n" 
         "\t c (Compute)\n" 
         "\t r (Reset)\n" 
         "\t q (Quit)\n\n"; 

int getMeasurements(int *measurements, int length) { 
    int i; 
    for (i = 0; i < length; i++) { 
     printf("Enter number #%d: ", i+1); 
     scanf("%d", &measurements[i]); 
     if(measurements[i] == 0) { 
      break; 
     } 
    } 
    return i; 
} 

void getView(const int *measurements, int length){ 
    int i; 

    printf("\t\nYour measurements:\n"); 
    if (length == 0) { 
     printf(" [NO MEASUREMENTS]\n"); 
     return; 
    } 

    for (i = 0; i < length; i++) { 
     printf("\b %d ", measurements[i]); 
    } 
    printf("\n"); 
} 

void getCompute(const int *measurements, int length) { 
    int min, max, calculus, i; 
    float sum = 0; 

    if (length == 0) { 
     printf(" [NO MEASUREMENTS]\n"); 
     return; 
    } 

    min = max = measurements[0]; 
    for (i = 0; i < length; i++){ 
      if (measurements[i] < min) min = measurements[i]; 
      if (measurements[i] > max) max = measurements[i]; 
      sum += measurements[i]; 
      calculus = measurements[i] - (sum/length); 
      printf("[%d]", calculus); 
    } 
    printf("\nMax number: %d", max); 
    printf("\nMin number: %d", min); 
    printf("\nAverage: %.2f", sum/length); 
    printf("\n\n"); 
} 

int main() { 
    int measurements[MAX_LENGTH]; 
    int measurements_count = 0; 
    int run = 1; 

    while (run) { 
     char choice; 
     puts(MenuMsg); 
     scanf(" %c", &choice); 

     switch (choice) { 
     case 'e': 
      measurements_count = getMeasurements(measurements, MAX_LENGTH); 
      break; 
     case 'v': 
      getView(measurements, measurements_count); 
      break; 
     case 'c': 
      getCompute(measurements, measurements_count); 
      break; 
     case 'q': 
      run = 0; 
      break; 
     default: 
      printf("Wrong input\n"); 
      break; 
     } 
    } 
    return 0; 
} 
相關問題