#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//向数组中输入数据
int main() {
int arr[10] = {0};
int MaxSize = 10;//数组的最大可存储数
int len = 0;//数组长度
int x = 1;
int num = 0;
int flag1 = 1;
int flag = 1;
while(flag1)
{
printf("\n---------------------\n");
printf("---输入数据请按\"1\"\n");
printf("---查看数组请按\"2\"\n");
printf("---退出请按\"3\"\n");
printf("---------------------\n");
flag = 1;
scanf("%d", &x);
if (x == 3) {
return 0;
}
while(flag){
switch (x)
{
case 1:
if (len >= MaxSize)
{
printf("数据已满!\n");
flag = 0;
break;
}
else
{
printf("请输入一个数据:\n");
scanf("%d", &num);
arr[len] = num;
len++;
flag = 0;
break;
}
case 2:
printf("\n------------\n");
printf("数据如下:\n");
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n------------\n");
flag = 0;
break;
default:
printf("输入不合法!\n");
flag = 0;
break;
}
}
}
return 0;
}
C语言--向数组中存储数据
最新推荐文章于 2024-04-23 17:10:28 发布