实验三
// 生成N个0~99之间的随机整数,并打印输出
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main() {
int x, n;
srand(time(0)); // 以当前系统时间作为随机种子
for(n=1; n<=N; n++) {
x = rand() % 100; // 生成一个0~99之间的随机整数
printf("%3d", x);
}
printf("\n");
return 0;
}

// 生成N个0~99之间的随机整数,并打印输出
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main() {
int x, n;
srand(time(0)); // 以当前系统时间作为随机种子
for(n=1; n<=N; n++) {
x = rand() % 32; // 生成一个0~99之间的随机整数
printf("%3d", x);
}
printf("\n");
return 0;
}

实验2
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 1
int main(){
int x, n, m;
n = 0;
printf("猜猜2021年5月哪一天会是你的luck day?\n");
printf("开始喽,你有三次机会,猜吧(1~31):");
srand(time(0));
x = rand()%32;
while(n<3)
{
scanf("%d",&m);
if (m>x)
{
if(n<2)
printf("你猜的日期晚了,luck day悄悄溜到前面啦\n再猜(1~31):");
else
printf("你猜的日期晚了,luck day已经过去啦\n") ;
}
else if(m<x)
{
if(n<2)
printf("你猜的日期早啦,luck day悄悄溜到后面啦\n再猜(1~31):");
else
printf("你猜的日期晚了,luck day已经过去啦\n");
}
else
{
printf("猜对啦") ;
return 0 ;
}
n++;
}
printf("次数用完啦。偷偷告诉你:5月,你的luck day 是5号");
return 0;
}

实验3
#include<stdio.h>
#include<math.h>
int main(){
long n,x,s,b,c;
while(printf("enter a number:"),scanf("%ld",&n)!=EOF){
b=0;
s=0;
x=n;
while(x!=0){
c=x%10;
x=x/10;
if(c%2!=0)
{
s=s+pow(10,b)*c;
b++;
}
}
printf("new number:%ld\n",s);
}
return 0;
}

实验4
// 一元二次方程求解(函数实现方式)
// 重复执行, 直到按下Ctrl+Z结束
#include <math.h>
#include <stdio.h>
// 函数声明
void solve(double a, double b, double c);
// 主函数
int main() {
double a, b, c;
printf("Enter a, b, c: ");
while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
solve(a, b, c); // 函数调用
printf("Enter a, b, c: ");
}
return 0;
}
// 函数定义
// 功能:求解一元二次方程,打印输出结果
// 形式参数:a,b,c为一元二次方程系数
void solve(double a, double b, double c) {
double x1, x2;
double delta, real, imag;
if(a == 0)
printf("not quadratic equation.\n");
else {
delta = b*b - 4*a*c;
if(delta >= 0) {
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
printf("x1 = %.2f, x2 = %.2f\n", x1, x2);
}
else {
real = -b/(2*a);
imag = sqrt(-delta) / (2*a);
printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n", real, imag, real, imag);
}
}
}

实验5
#include <stdio.h>
double fun(int n);
int main() {
int n;
double s;
printf("Enter n(1~10): ");
while(scanf("%d", &n) != EOF) {
s = fun(n);
printf("n = %d, s= %f\n\n", n, s);
printf("Enter n(1~10): ");
}
return 0;
}
double fun(int n) {
double p,s;
int k;
k=1;
p=-1;
s=0;
while(k<=n){
p=-1*p/k;
s=s+p;
k++;
}
return(s);
}

实验6
#include<stdio.h>
#include<math.h>
int isprime(int a);
int main(){
int p,b;
p=101;
b=0;
while(p<=200){
if(isprime(p)==1){
printf("%4d",p);
b=b+1;
if(b%5==0){
printf("\n");
}
}
p=p+1;
}
printf("\n100~200素数个数为:%d",b);
}
int isprime(int a){
int i;
double m;
m=sqrt(a);
for(i=2;i<=m;i++){
if(a%i!=0){
}
else if(i>m){
return 1;
}
else return 0;
}
return 1;
}


浙公网安备 33010602011771号