我必须做一定的练习。希望有人了解我未能做的事情。
每当用户按ENTER时,以少于10个整数输入时,按Q退出选项,然后返回,他的所有输入值都将消失。我想输入值留在那里,直到存储的总元素为10。我该怎么做?
注意!用户不应该退出应用程序(在这种情况下为CMD),而应该只能留下" Enter"选项并仍然具有"存储"的输入值。
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define L 10
void view(int a[])
{
int i;
printf("n Here are the currently stored valuesn");
printf("[");
for(i=0; i<L; i++)
{
printf(" %d ",a[i]);
}
printf("]");
}
int enter(int a[], int n)
{
printf("n Note! You can press any letter if you want to return to the main menu. n");
for(int i=0; i<L; i++)
{
int mesa;
printf(" Enter measurement #%d (or q to quit): ", n+1);
int check = scanf("%d",&mesa);
if(check)
{
a[i] = mesa;
n++;
}
else
{
char temp;
scanf(" %c",&temp);
return n;
}
}
return n;
}
void compute(int a[], int n)
{
printf(" Computing...n");
//Min value
{
int i;
int min = 10000;
for (i = 0; i < L; i++)
if(a[i] < min)
min = a[i];
printf("n The min value is: %d n", min);
}
//Max value
{
int i;
int max = a[0];
for (i = 0; i < L; i++)
if (a[i] > max)
max = a[i];
printf(" The max value is: %d n", max);
}
// Average & Normalization
{
float average;
int i;
int norm;
int sum;
for(i = 0; i < L; ++i)
{
sum = sum + a[i];
average = (float)sum / 10; //typecast - Ge en interger en tillfällig roll. På så sätt kan du säga åt programmet att du faktiskt vill ha float som svar och inte ett heltal som svar.
}
printf(" Average value: %.2f n", average);
printf(" Normalized array: [");
for(i = 0; i < L; i++)
{
norm = a[i] - average; //average - every a[]
printf(" %d", (int)round(norm));
}
printf(" ]");
}
}
void reset(int a[])
{
printf(" You have chosen to reset the array. All the elements in the array will now be deleted. n");
//memset( a, 0, 10*sizeof(a)); <--- Kan ej användas då sizeof() funktionen läser av en variabel och inte hela arrayens storlek.
memset( a, 0, 10*sizeof(int*));
}
int main()
{
char command;
int a[L];
printf(" Hello and welcome to this measurement tool. In this program you will be able to type in and analyze data.n");
printf(" In the section below, you can choose a letter v,e,c,r or q to do certain things. More information will be provided down below.n");
printf("n v(View) - Displays currently stored values.");
printf("n e(Enter) - Allows you to store values. ");
printf("n c(Compute) - Displays the maxiumum, minimum, normalized and average value of those which are stored.");
printf("n r(Reset) - Deletes all stored values.");
printf("n q(Quit) - Exits the program. n");
printf("n Please choose one of the commands above: ");
do
{
int n = 0;
scanf(" %c",&command);
switch(command)
{
case 'v' : view(a);break;
case 'e' : enter(a,n);break;
case 'c' : compute(a,n);break;
case 'r' : reset(a);break;
default : printf("n Please choose one of the options given.n");break;
case 'q' :
{
printf(" Want to exit? Confirmation needed. Press q again to exit.");
scanf(" %c",&command);
}
}
printf("n VECRQ?: ");
} while(command != 'q');
return 0;
}
在main()
中,将n
的声明移到循环外:
int n = 0;
do
{
...
更新n
时,您致电enter()
:
n = enter(a,n);
在enter
功能中,从n
启动循环:
for(int i=n; i<L; i++)