实验3 C语言分支语句和循环语句编程应用(友好猜日期^_^)
实验任务一
#include <stdio.h>
#include <math.h>
int main(){
float a, b, c, x1, x2;
float delta, real, imag;
printf("Enter a, b, c: ");
while(scanf("%f%f%f", &a, &b, &c) != EOF) {
if(a == 0) printf("not quadratic equation.\n\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\n", x1, x2);
}
else {
real = -b/(2*a);
imag = sqrt(-delta) / (2*a);
printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real, imag, real, imag);
}
}
printf("Enter a, b, c: ");
}
return 0;
}

实验任务二
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main (){
int x,n;
srand(time(0));
n=0;
do {
n++;x= rand()%10;
printf("%3d",x);
}while(n<N);
printf("\n");
return 0;
}

实验任务三
#include <stdio.h>
int main (){
int x=101;
int n=200;
int num=0;
for (;x<=200;x++){
int t,flag;
flag=1;
for (t=2;t<x;t++)
{
if (x%t==0)
{flag=0;
}
}
if (flag){
printf(" %d",x);
num++;
if (num%5==0) printf("\n");
}
}
printf("\n101~200之间共有%d个素数",num);
return 0;
}

实验任务四
#include <stdio.h>
int main(){
long s;
printf("Enter a number: ");
while (scanf("%ld",&s)!=EOF){
long sum,t;
sum=0;
int a,b;
b=0;
t=10;
long j=10;
if (s<1000000000){
while (s/t!=0){
a=(s%t-b)/(t/10);
b=b+a*(t/10);
if(a%2!=0){
sum=sum+a*(j/10);
j=j*10;
}
t=t*10;
}
a=(s%t-b)/(t/10);
b=b+a*(t/10);
if(a%2!=0){
sum=sum+a*(j/10);
}
}else{
int x;
for (x=1;x<=9;x++){
a=(s%t-b)/(t/10);
b=b+a*(t/10);
if(a%2!=0){
sum=sum+a*(j/10);
j=j*10;
}
t=t*10;
}
a=s/1000000000;
if(a%2!=0){
sum=sum+a*(j/10);
}
} printf("new number is: %ld\n\n",sum);
printf("Enter a number: ");
}
return 0;
}

实验任务五
#include <stdio.h>
int main(){
int n;
printf ("Enter n(1~10):");
while (scanf ("%d",&n)!=EOF){
int t,f,T;
f=1;
T=1;
double s=0.0;
for (t=1;t<=n;t++){
T=t*T;
s=s+f*(1.0/T);
f=f*(-1);
}
printf("n = %d, s = %f\n\n",n,s);
printf ("Enter n(1~10):");
}
}

实验任务六
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main (){
printf("猜猜2020年12月哪一天会是你的luck day\n\n");
int n= rand()%31+1;
int t=1;
printf ("开始喽,你有三次机会,猜吧(1~31):");
int a;
for (t=1;t<=3;t++){
scanf("%d",&a);
if(a<n){
printf("小伙子真着急,你猜的日期早了,luck day还妹到呢\n\n");
}
if (a>n){
printf("你猜的日期晚啦,luck已经莫得了\n\n");
}
if (a==n){
printf("这也能猜中??这么好的luck还需要luck day干嘛,速速去买彩票\n\n");
break;
}
}
if(a!=n){
printf("次数用光啦。。。偷偷告诉你:");
Sleep(1000);
printf("12月");
for (t=1;t<=3;t++){
Sleep(500);
printf("。");
}
Sleep(1000);
printf("每天都是luck day!!!");
}
}


随机推荐
- Mybatis【2.2】-- Mybatis关于创建SqlSession源码分析的几点疑问?
代码直接放在Github仓库[https://github.com/Damaer/Mybatis-Learning ],可直接运行,就不占篇幅了. 目录 1.为什么我们使用SQLSessionFact ...
- Java集合【4】-- iterable和Iterator的异同分析详解
目录 一.iterator介绍 二.iterable接口 三.为什么有Iterator还需要Iterable 一.iterator介绍 iterator接口,也是集合大家庭中的一员.和其他的Map和C ...
- 秒极啊!手把手带你进行shiro授权拦截器的重写,学到了学到了
shiro整合前后端分离的springboots,Vue项目真的是有很多大坑啊. 今天我的主题是:如何设置shiro过滤器. 遇到问题:我的项目是前后端分离的,shiro里面有一个shiroFilte ...
- Django匆匆一眼却解答了多年疑惑
Django 是 Python 的 一款 Web 开发框架,另外还有 Tornado,Flask,Twisted.为什么我要选择学 Django?原因很简单,上家公司来了个网易的测开,就是用 Djan ...
- PyQt(Python+Qt)学习随笔:Qt Designer中部件的是否接受鼠标拖放事件的acceptDrops属性及含义
acceptDrops属性表示当前部件是否接受鼠标拖放事件,鼠标拖放应该是与鼠标拖拽结合在一起的,在Qt Designer中可以通过属性acceptDrops设置部件是否接受鼠标拖放事件.如果部件接受 ...
- ip 子网掩码、网络地址、广播地址计算
例:已知ip 16.158.165.91/22子网掩码 根据22 得知子网掩码占22位 即:11111111.11111111.11111100.00000000 == 255.255.252. ...
- STL——容器(Map & multimap)的排序与遍历
1. Map & multimap 的排序与遍历 map<T1,T2,less<T1> > mapA; //该容器是按键的升序方式排列元素.如果未指定less< ...
- STL——容器(List)list 数据的存取
list.front(); //返回第一个元素 list.back(); //返回最后一个元素 1 #include <iostream> 2 #include <list> ...
- 图的遍历DFS
图的遍历DFS 与树的深度优先遍历之间的联系 树的深度优先遍历分为:先根,后根 //树的先根遍历 void PreOrder(TreeNode *R){ if(R!=NULL){ visit(R); ...
- SpringBoot + Mybatis-Plus 实现多数据源简单示例
1. 简介 在单体项目中,经常出现想要访问多个数据源的情况,或者因为某些性能瓶颈,将大数据量的业务表分离到另一个库等情况. 实现多数据源的方案有很多,Mybatis-Plus提供了非常简单的实 ...