2017-02-13 52 views
0

因此,这个C程序显示一个菜单,并要求用户输入一个1-4的数字。如果选择了一个,程序会要求用户输入参数。然后再次显示菜单。如果选择2,程序应该在格式化的表格中显示用户的输入。这就是我遇到麻烦的地方,我不知道如何让用户输入到for循环中,以格式化的表格打印出来。这里是我目前的代码:C编程,需要帮助获取用户输入到for循环

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

int numOfInst, totalCycles, totalInst, clockRate; 

void parameters(){ 
totalInst = 0; 
totalCycles = 0; 
int counter, cpiClass, instCount; 

printf("Enter the number of instruction classes: \n"); 
scanf(" %d", &numOfInst); 
printf("Enter the frequency of the machine (MHz): \n"); 
scanf(" %d", &clockRate); 

for (counter = 0; counter < numOfInst; counter++){ 
    printf("Enter CPI of class %d: ", counter + 1); 
    scanf(" %d", &cpiClass); 
    printf("Enter instruction count of class %d (millions): ", counter + 1); 
    scanf(" %d", &instCount); 

    totalInst += instCount; 
    totalCycles += cpiClass * instCount; 
} 
    return; 

} 

float avgCPI(){ 
float avg = totalCycles/((float)totalInst); 
return (avg); 
} 

float execTime(){ 
float time = (totalCycles/((float)clockRate)) * 1000; 
return (time); 
} 

float calcMips(){ 
float mips = totalInst/(totalCycles/((float)clockRate)); 
return (mips); 

} 
void printParam(){ 
int i; 
printf("-------------------------\n"); 
printf("| Class\t | CPI\t |Count\t |\n"); 
for (i = 0; i < numOfInst; i++); 
printf(" %d\t", (i)); 
/*totalCycles[i], totalInst[i]);*/ 
return; 
} 

/*void printPerformance{ 
}*/ 
int main() 
{ 
int option; 
do { 
    printf("Performance assessment: "); 
    printf("\n-----------------------"); 
    printf("\n1) Enter parameters: "); 
    printf("\n2) Print table of parameters: "); 
    printf("\n3) Print table of performance "); 
    printf("\n4) Quit \n"); 
    scanf(" %d", &option); 

    switch (option){ 
    case 1: parameters(); 
     break; 
    case 2: printParam(); 
     break; 
    case 3: 
     break; 
    case 4: 
     break; 
    default: printf("Invalid input, please enter a number from 1-4 "); 


    } 


}while(option != 4); 
return 0; 
} 
+0

我建议你可以探索*数组*。汇总输入数据时,您的代码是正确的,但不记得为打印输入的每个值。你在注释掉的'/ * totalCycles [i],totalInst [i])中有一些提示:* /' –

+0

你必须在这里使用数组。如果你提示什么是你想要打印的数据,那么我可以解决它。 –

+0

输入的选择:1 输入指令类的数量:(百万)2 输入1类的指令数:3 输入机器(兆赫)的频率:200 输入类的CPI 1 3 输入CPI 4 输入等级2(百万)的指令计数:5 输入等级3的CPI:6 输入等级3(百万)的指令计数:7 – doctrshoe

回答

0

代码:

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

int cpiClass[10000], instCount[10000], numOfInst; 
void parameters(){ 
    int counter; 
    printf("Enter the number of instruction classes: \n"); 
    scanf(" %d", &numOfInst); 
    for (counter = 0; counter < numOfInst; counter++){ 
     printf("Enter CPI of class %d: ", counter + 1); 
     scanf(" %d", &cpiClass[counter]); 
     printf("Enter instruction count of class %d (millions): ", counter + 1); 
     scanf(" %d", &instCount[counter]); 
    } 
    return; 
} 
void printParam(){ 
    int i; 
    printf("-------------------------\n"); 
    printf("| Class\t | CPI\t |Count\t |\n"); 
    for (i = 0; i < numOfInst; i++){ 
     printf(" %d\t%d\t%d\t\n", i+1,cpiClass[i],instCount[i]); 
    } 
    return; 
} 
int main() 
{ 
    int option; 
    do { 
     printf("\nPerformance assessment: "); 
     printf("\n-----------------------"); 
     printf("\n1) Enter parameters: "); 
     printf("\n2) Print table of parameters: "); 
     printf("\n3) Quit \n"); 
     scanf(" %d", &option); 
     switch (option){ 
     case 1: parameters(); 
      break; 
     case 2: printParam(); 
      break; 
     case 3: 
      break; 
     default: printf("Invalid input, please enter a number from 1-3 "); 
     } 
    }while(option != 3); 
    return 0; 
} 

此代码工作细如您接受输出。

我这里编辑什么:

在第5行,我宣布2阵列,并予以公布。

int cpiClass[1000], instCount[10000], numOfInst; 

参数()函数线12和14采取输入阵列。

scanf(" %d", &cpiClass[counter]); 
scanf(" %d", &instCount[counter]); 

现在我所有的输入数据都存储在这两个数组中。

在选择选项2时,主函数显示来自这些数组的数据。

printParam()函数编辑

printf(" %d\t%d\t%d\t\n", i+1,cpiClass[i],instCount[i]); 

此打印功能会显示您的输出,你接受。

注:

在这里,如果你与你的代码比较一些功能缺失。我已将它们删除,因为显示此输出不需要这些。

如果你需要所有的代码,然后去here

我已经把所有的代码包括其他功能。

0

请注意,您只有int变量来存储numOfInst,totalCycles,totalInst和clockRate。这意味着无论您为这些变量赋值,以前的值都会被覆盖并丢失。为了打印出所有以前的输入,你必须将它们存储在一些数据结构中(提示:看看列表或hashmaps)。使用数组来存储输入也是可行的,但这会将可以存储的输入数量限制为数组大小。