发布网友 发布时间:2022-04-23 01:35
共1个回答
热心网友 时间:2023-10-10 06:55
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
void isPrime();
void randSno();
void precOfPi();
int main()
{
int fun;
do
{
printf("1 判断一个数n是否是素数\n");
printf("2 随机选择某个班级某个学生\n");
printf("3 输出圆周率精度可选(2 - 10)\n");
printf("请选择功能(Q to quit):");
if (scanf("%d",&fun) == 0)
break;
system("cls");
switch(fun)
{
case 1:isPrime();break;
case 2:randSno();break;
case 3:precOfPi();break;
default:printf("请输入正确的功能编号\n");
}
}while (1);
return 0;
}
void isPrime()
{
unsigned int n,i,max;
printf("判断正整数数n是否是素数\n");
printf("请输入数n:");
while (scanf("%d",&n) == 1)
{
if ( n%2 == 0 || n < 2) printf("%d不是素数\n",n);
else if ( n == 2) printf("%d是素数\n",n);
else
{
max = sqrt((double)n)+0.5;
for (i = 3; i < max; i += 2)
{
if ( n%i == 0) break;
}
if ( i < max ) printf("%d不是素数\n",n);
else printf("%d是素数\n",n);
}
printf("任意键继续(E exit):");
if (getch() == 'e') break;
system("cls");
printf("判断正整数数n是否是素数\n");
printf("请输入数n: ");
}
system("cls");
while (getchar() != '\n')
continue;
}
void randSno()
{
int stuN[5]={34,35,30,31,29};
int c,s;
srand(time(0));
printf("任意键随机选择5个班级中某个学生:");
while (getch() != 'E')
{
c = rand() % 5;
s = rand() % stuN[c];
printf("本次随机%d班%d号学生\n",c + 1, s + 1);
printf("任意键继续 (E exit)\n");
if (getch() == 'E') break;
system("cls");
printf("随机5个班级中某个学生\n");
}
system("cls");
while (getchar() != '\n')
continue;
}
void precOfPi()
{
int n;
double pi = 3.1415926535;
printf("输出圆周率精度可选(2 - 10)\n");
printf("请输入精度要求(2-10):");
while (scanf("%d",&n) == 1 )
{
switch(n)
{
case 2:printf("精度2 pi = %.2lf\n",pi);break;
case 3:printf("精度3 pi = %.3lf\n",pi);break;
case 4:printf("精度4 pi = %.4lf\n",pi);break;
case 5:printf("精度5 pi = %.5lf\n",pi);break;
case 6:printf("精度6 pi = %.6lf\n",pi);break;
case 7:printf("精度7 pi = %.7lf\n",pi);break;
case 8:printf("精度8 pi = %.8lf\n",pi);break;
case 9:printf("精度9 pi = %.9lf\n",pi);break;
case 10:printf("精度10 pi = %.10lf\n",pi);break;
}
printf("任意键继续(E exit):");
if (getch() == 'E') break;
system("cls");
printf("输出圆周率精度可选(2 - 10)\n");
printf("请输入精度要求(2-10 E exit):");
}
system("cls");
while (getchar() != '\n')
continue;
}
追问就是选择后还要选择另一个菜单就不能实现了
追答大写 E 字符退出子菜单
来自:求助得到的回答