c - 在他离开“输入”选项后,我如何保持用户输入的值并让他从他完成的开始?
问题描述
我有一项必须做的运动。希望有人能理解我没有做的事情。
每当用户按下回车,输入少于 10 个整数,按下 q 退出选项然后返回,他所有的输入值都将消失。我想输入要留在那里的值,直到存储的总元素为 10。我该怎么做?
笔记!用户不应该退出应用程序(在这种情况下是 CMD),而是他们应该只能离开“进入”选项并仍然“存储”他们的输入值。
#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 values\n");
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++)
推荐阅读
- python-3.x - 如何使用 Matplotlib 更改饼图中的颜色
- visual-studio-code - 如何在 VS 代码格式 .vue 文件中制作 Veture 扩展,以便将 html 属性保留在一行中?
- javascript - jQuery | 选中关联复选框时如何显示变量
- azure-web-app-service - 部署应用服务 CDN 端点时出现 Terraform 脚本错误
- node.js - 有什么方法可以制作我的 npm 包的图像并在我的 react 项目中使用它?
- python - 我需要在 Docker 容器中使用 django 运行 Shell Script
- amazon-web-services - 如何获取所有地区名称,即亚太地区(孟买)
- java - javax.net.ssl.SSLHandshakeException:javax.net.ssl.SSLProtocolException:SSL 握手中止:
- openam - 我们可以在 AMConfig.properties 中为 com.iplanet.am.server.host 设置两个条目吗?
- django - 如何在不在 Django 中创建新模型的情况下显示来自两个模型的数据?