c语言分支语句编程,实验 3 C语言分支语句、循环语句、函数综合应用编程-1

#include

#include

#include

#define N 5

int main()

{

int x, n;

srand(time(0));

for(n=1; n<=N; n++)

{

x = rand() % 100;

printf("%3d", x);

}

printf("\n");

return 0;

}

#define _CRT_SECURE_NO_DEPRECATE

#include

#include

#include

#define N 1

int main()

{

int x, n, a;

srand(time(0));

printf("猜猜2021年5月的哪一天是你的幸运日!\n");

printf("你有三次机会哦,快来猜猜看吧。(1~31):");

x = 1 + rand() % 30;

for(n=1; n<=3;)

{

scanf("%d", &a);

if (a == x)

{

printf("芜湖~猜对了!!\n");

break;

}

else if (a > x)

{

printf("幸运日在前面。\n");

}

else

{

printf("幸运日在后面。\n");

}

n++;

if (n > 3)

break;

printf("再试一次~(1~31):");

}

if (n > 3)

{

printf("可惜可惜,机会耗尽了。\n");

printf("其实幸运日在%d号啦", x);

}

return 0;

}

#define _CRT_SECURE_NO_DEPRECATE

#include

int main()

{

long a,b,m,n;

while(printf("Enter your number:"),scanf("%ld", &a)!=EOF)

{

m = a;

n = 1;

while (m /= 10)

n *= 10;

while (a)

{

if ((b = a / n) % 2 != 0)

(m *= 10) += b;

a %= n;

n /= 10;

}

printf("New number is:%ld\n", m);

}

return 0;

}

#define _CRT_SECURE_NO_DEPRECATE

#include

#include

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;

}

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);

}

}

}

.

否,因为当Δ不等于0时有两个根,但函数的返回值至多一个。

#define _CRT_SECURE_NO_DEPRECATE

#include

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)

{

if(n==1)

return 1;

else if(n==2)

return 0.5;

else

return fun(n-1)-(fun(n-1))/n+(fun(n-2))/n;

}

#include

#include

int isPrime(int n);

int main()

{

int m=0,n,col=1;

for(n=101;n<200;n++)

{

if(isPrime(n)==0)

{

printf("%d ",n);

col++;

m++;

while(col>5)

{

printf("\n");

col=1;

}

}

}

printf("\n");

printf("100~200之间共有%d个素数",m);

return 0;

}

int isPrime(int n)

{

int i;

for (i = 2; i <= sqrt(n); i++)

{

if (n%i == 0)

return 1;

}

return 0;

}

#include

#include

#include

void printCharMan(int line, int col);

void printSpaces(int n);

int main()

{

int line, col;

for(line=1,col=7; col<=60;col++ )

{

if(line<=10)

line+=1;

else

do

{

line-=1;

printCharMan(line, col);

Sleep(120);

system("cls");

col++;

}while(line>1);

printCharMan(line, col);

Sleep(120);

system("cls");

}

}

void printSpaces(int n)

{

int i;

for(i=1; i<=n; i++)

printf(" ");

}

void printCharMan(int line, int col)

{

int i, j;

for(i=1; i<=line-1; i++)

printf("\n");

printSpaces(col-1);

printf(" O \n");

printSpaces(col-1);

printf("\n");

printSpaces(col-1);

printf("I I\n");

}

实验任务7添加了几行代码,让小人像上下楼梯一样运动(单看可能没什么意义),等学了更多内容再回过头来添加修改。

b739ec46bb5c46d9c0aa4ce35ba1ea56.png

关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。

本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。

[实验 3 C语言分支语句、循环语句、函数综合应用编程-1]http://www.zyiz.net/tech/detail-155376.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值