实验三
2021/4/15 10:56:05
本文主要是介绍实验三,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
实验三
实验结论
1、实验任务1
#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; printf("%3d", x); } printf("\n"); return 0; }
2、实验任务2
#include<stdio.h> #include<stdlib.h> #include<time.h> #define N 3 int main(){ int x,n,ans; srand(time(0)); x=rand()%32; printf("猜猜2021年哪一天会是你的luck day\n开始喽,你有三次机会,猜吧(1~31):"); for(n=1;n<=N;n++){ scanf("%d",&ans); if(ans==x) { printf("猜中了"); break; } else if(ans>x) { printf("你猜的日期晚了,luck day悄悄溜到前面啦\n再猜(1~31):"); } else if(ans<x) { printf("你猜的日期早了,luck day还没到呢\n再猜(1~31):"); } } if(n==4) printf("\n次数用完了,偷偷告诉你,你的luck day是: %d号",x); return 0; }
4、实验任务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 quadretic 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 - %.2f\n",real,imag,real,imag); } } }
5、实验任务5
#include<stdio.h> double fun(int n); //函数声明 int main(){ int n; double s; printf("Enter m(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 x,sign=1,s1=0; for (int i = 1; i<n; i++){ x=1; for(int y = 1; y<=i; y++){ x*=y; } s1+=sign/x; sign*=(-1); } return s1; }
7、实验任务7
#include <stdio.h> #include <stdlib.h> #include <windows.h> void printCharMan(int line, int col); void printSpaces(int n); int main() { int line,col; for(line = 5, col = 5; col<=60; col++){ printCharMan(line, col); Sleep(50); system("cls"); } } //打印n个空格 void printSapces(int n){ int i; for(i = 1; i<=n; i++) printf(" "); } //在第line行第col列打印一个字符小人 void printCharMan(int line, int col) { int i,j; //打印line-1行空格 for(i=1; i<=line-1; i++) printf("\n"); //打印col-1个空格 printSpaces(col-1); //在第line行、第col列打印字符小人的头 printf(" O \n"); //打印col-1个空格 printSpaces(col-1); //在第line行、第col列打印字符小人的身体 printf("<H>\n"); //打印col-1个空格 printSpaces(col-1); //在第line行、第col列打印字符小人的腿 printf("I I\n"); }
实验任务3、6不会。2、5勉强参考着会。
这篇关于实验三的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?