首页 > 其他分享 >实验2

实验2

时间:2023-03-19 22:14:31浏览次数:39  
标签:int scanf inp flag 实验 printf include

实验任务2


task1

代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 5
#define R1 586
#define R2 701

int main()
{
int number;
int i;

srand( time(0) ); // 以当前系统时间作为随机种子

for(i = 0; i < N; ++i)
{
number = rand() % (R2 - R1 + 1) + R1;
printf("20228330%04d\n", number);
}

return 0;
}

运行结果:

 讨论:

1. 生成586~701之间的随机整数

2. 随机生成5个本班学号

 

task2

代码:

#include<stdio.h>

int main()
{
double x, y;
char c1, c2, c3;
int a1, a2, a3;

scanf("%d,%d,%d", &a1, &a2, &a3); //第一处
printf("a1 = %d, a2 = %d, a3 = %d\n", a1,a2,a3);

scanf("%c%c%c", &c1, &c2, &c3);
printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3);

scanf("%lf,%lf", &x, &y); //第二处
printf("x = %lf, y = %lf\n",x, y);

return 0;
}

运行结果:

  task3_2 代码:

#include <stdio.h>
#include <math.h>

int main()
{
double x, ans;

while(scanf("%lf", &x) != EOF)
{
ans = pow(x, 365);
printf("%.2f的365次方: %.2f\n", x, ans);
printf("\n");
}

return 0;
}

运行结果:

 

task3_3

代码:

#include <stdio.h>
#include <math.h>

int main()
{
    double x, ans;
    
    while(scanf("%lf", &x) != EOF)
    {
        ans = 9*x/5+32;
        printf("摄氏度c = %.2f时,华氏度f = %.2f\n", x, ans);
        printf("\n");
    }
    
    return 0;
}

运行结果:

  task4 代码:
#include <stdio.h>

int main()
{
    char inp;
    //int flag;
    
    //flag=0;
    while(scanf("%c", &inp) != EOF)
    {
        getchar();
        if (inp == 'y') {
            printf("wait a minute\n");
            //flag = 1;
        }
        else if (inp == 'g') {
            printf("go go go\n");
            //flag = 1;
        }
        else if (inp == 'r') {
            printf("stop!\n");
            //flag = 1;
        }
        else if (inp!='y'&&inp!='g'&&inp!='r') {
            printf("something must be wrong...\n");
        }
    }
    
    return 0;
}

运行结果:

 

task5

代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    
    int lday,inp,flag;
    int i=3;
    srand(time(0));
    
    lday = rand() % (30 - 1 + 1) + 1;
    flag=0;
    
    printf("猜猜2023年4月那一天会是你的lucky day\n\n");
    printf("开始喽,你有三次机会,猜吧(1-30):");
    scanf("%d",&inp);
    
    if(inp<lday) {
            printf("\n");
            printf("你猜的日期早了,你的lucky day还没到呢\n\n");
            i--;
            //printf("再猜(1-30):");
            //scanf("%d",&inp);
        }
        else if(inp>lday) {
            printf("\n");
            printf("你猜的日期晚了,你的lucky day已经过啦\n\n");
            i--;
            //printf("再猜(1-30):");
            //scanf("%d",&inp);
        }
        else if(inp==lday) {
            printf("\n");
            printf("哇,猜中了:-)\n");
            flag=1;
        }
    
    while(i>=1&&i<3&&flag==0) {
        printf("再猜(1-30):");
        scanf("%d",&inp);
        if(inp<lday) {
            printf("\n");
            printf("你猜的日期早了,你的lucky day还没到呢\n\n");
            i--;
            //printf("再猜(1-30):");
            //scanf("%d",&inp);
        }
        else if(inp>lday) {
            printf("\n");
            printf("你猜的日期晚了,你的lucky day已经过啦\n\n");
            i--;
            //printf("再猜(1-30):");
            //scanf("%d",&inp);
        }
        else if(inp==lday) {
            printf("\n");
            printf("哇,猜中了:-)\n");
            flag=1;
            break;
        }
    }
    
    if(i<1&&flag==0) {
        printf("次数用完啦。偷偷告诉你:4月,你的lucky day是%d号\n",lday);
    }
    
    return 0;
}

运行结果:

 

 

task6

代码:

#include <stdio.h>

int main() {
    
    int i,j=1;
    
    while(i<=9) {
        j=1;
        while(j<=i) {
            printf("%d×%d=%d\t",j,i,i*j);
            j++;
        }
        printf("\n");
        i++;
    }
    
    return 0;
}

运行结果:

 

 

task7

代码:

#include <stdio.h>

int main() {
    
    int n,i,j1,j2,j3,flag,k;
    
    printf("input n: ");
    scanf("%d",&n);
    i=n;
    j1=2*n-1,j2=2*n-1,j3=2*n-1;
    flag=0,k=0;
    
    while(i>=1) {
        k=0;
        while(k<flag) {
            printf("\t");
            k++;
        }
        while(j1>=1) {
            printf(" O \t");
            j1--;
        }
        printf("\n");
        
        k=0;
        while(k<flag) {
            printf("\t");
            k++;
        }
        while(j2>=1) {
            printf("<H>\t");
            j2--;
        }
        printf("\n");
        
        k=0;
        while(k<flag) {
            printf("\t");
            k++;
        }
        while(j3>=1) {
            printf("I I\t");
            j3--;
        }
        
        printf("\n");
        i--;
        printf("\n");
        flag++;
        j1=2*i-1;
        j2=2*i-1;
        j3=2*i-1;
    }
     
    
    return 0;
}

运行结果:

 

 

标签:int,scanf,inp,flag,实验,printf,include
From: https://www.cnblogs.com/lolol954321/p/17232586.html

相关文章

  • 实验2
    1、实验任务1源代码及运行结果截图:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumb......
  • 结对编程--保龄球实验
    1.实验目的通过结对编程完成保龄球计分程序2.实验过程:(1):首先我们两个一起商讨编写保龄球积分程序需要什么方法,经过十几分钟的商讨我们一致决定一共需要publicstaticint......
  • 实验二
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;srand(time(0))......
  • 实验二
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sr......
  • 实验一 密码引擎-1-OpenEuler-OpenSSL编译
    任务详情安装Ubuntu和OpenEuler虚拟机下载最新的OpenSSL源码(1.1版本)用自己的8位学号建立一个文件夹,cd你的学号,用pwd获得绝对路径参考https://www.cnblogs.com/roc......
  • 实验2
    1代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;s......
  • 实验2
    实验任务11#include<stdio.h.>2#include<stdlib.h>3#include<time.h>45#defineN56#defineR15867#defineR270189intmain()10{11......
  • 实验2
    实验任务1程序源码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;......
  • 实验报告1
     ......
  • 实验二
    test1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sran......