因此,这个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;
}
我建议你可以探索*数组*。汇总输入数据时,您的代码是正确的,但不记得为打印输入的每个值。你在注释掉的'/ * totalCycles [i],totalInst [i])中有一些提示:* /' –
你必须在这里使用数组。如果你提示什么是你想要打印的数据,那么我可以解决它。 –
输入的选择:1 输入指令类的数量:(百万)2 输入1类的指令数:3 输入机器(兆赫)的频率:200 输入类的CPI 1 3 输入CPI 4 输入等级2(百万)的指令计数:5 输入等级3的CPI:6 输入等级3(百万)的指令计数:7 – doctrshoe