首页 > 其他分享 >实验2

实验2

时间:2023-03-18 21:47:16浏览次数:35  
标签:lf include int scanf 实验 printf main

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;//在【586,701】随机取一个数 
        printf("202283300%04d\n",number);
     } 
     return 0;
}

line18实现的功能:在586~701之间随机取一个数

这个程序的功能:把随机取的五个数带入到后三位

 

 

 

 

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);  //error
    printf("x = %lf,y = %lf\n",x,y);  //error
    
    return 0;
}

 

 

 

 

task3

#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;
}

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

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

 

 

 

 

task4

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

int main()
{
    char color;
    while(scanf("%c",&color) != EOF){
    switch(color){
        case 'y':printf("wait a minute\n");break;
        case 'g':printf("go go go\n");break;
        case 'r':printf("stop!\n");break;
        default:printf("something must be wrong...\n");break;
        }
    getchar();
    }
    system("pause");
    return 0;
}

 

 

 

 

task5

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int num;
    printf("猜猜2023年4月哪一天会是你的lucky day\n\n");
    printf("开始喽,你有三次机会,猜吧(1~30):");

    while(num!=17){
        scanf("%d",&num);
        if(num<17){
            printf("\n");
            printf("你猜的日期早了,你的lucky day还没到呢\n\n");
            }
        
        if(num>17){
            printf("\n");
            printf("你猜的日期晚了,你的lucky day已经过啦\n\n");
        }
        if(num==17)break;
        printf("再猜(1~30):");
    } 
    printf("\n哇,猜中了:-)\n");
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int num,i;
    printf("猜猜2023年4月哪一天会是你的lucky day\n\n");
    printf("开始喽,你有三次机会,猜吧(1~30):");

    for(i=1;i<4;i++){
        scanf("%d",&num);
        if(num<20){
            printf("\n");
            printf("你猜的日期早了,你的lucky day还没到呢\n\n");
            printf("再猜(1~30):");
            }
        
        if(num>20){
            printf("\n");
            printf("你猜的日期晚了,你的lucky day已经过啦\n\n");
        }
    } 
    if(i==4)printf("\n\n次数用完啦~偷偷告诉你:4月,你的lucky day是20号\n");
    return 0;
}

 

 

 

 

task6

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int column,line,value;
    
    for(column=1;column<=9;column++){
        for(line=1;line<=column;line++){
            value = column*line;
            printf("%d x %d = %d   ",line,column,value);
        }
        printf("\n");
    }
    return 0;
}

 

 

 

 

task7

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i,j,n;
    printf("input:"); 
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i-1;j++)
            printf("\t");
        
        for(j=1;j<=(n-i)*2+1;j++)
            printf(" O \t");
           
        printf("\n");
        
        for(j=1;j<=i-1;j++)
            printf("\t");
        
        for(j=1;j<=(n-i)*2+1;j++)
            printf("<H>\t");
        
        printf("\n");
        
        for(j=1;j<=i-1;j++)
            printf("\t");
        
        
        for(j=1;j<=(n-i)*2+1;j++)
            printf("I I\t");
        
        printf("\n\n");
        
    } 
}

第i行,需要打印(2*i-1)个字符小人,前面需要打印(i-1)个空白

 

 

 

收获:能掌握各种循环,选择结构语句的形式

          输入的格式要与scanf里的格式相同

         在字符型,空格也会输出

新发现:while(..... != EOF)能连续输入,直至同时按crtl+z     (注意:!=   是连着写的 )

待提高:对复杂的循环结构的嵌套尚不熟练,需要试很多次才能运行正确

            区分break和continue的用法

 

标签:lf,include,int,scanf,实验,printf,main
From: https://www.cnblogs.com/apeach/p/17231758.html

相关文章

  • 实验2
    实验任务1:代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;......
  • 实验二
    实验任务一源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;......
  • SYNU PTA C++ 第三章实验题
    题目详细内容见PTA,仅提供答案参考。7-7冒泡1#include<iostream>2usingnamespacestd;34voidmerge(int*arr,intleft,intmid,intright)5{6......
  • 实验二
    task1.c#defineR1586#defineR2701intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number......
  • 实验2 C语言输入输出和控制语句应用编程
    实验任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineR15866#defineR27017intmain()8{9intn......
  • 202113020023李湘楠实验2实验报告
    //test1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sr......
  • STM32 HAL库学习 (2) USART实验
    使用STM32F407串口:PA9、PA10(利用CH340G驱动)1.stm32f4xx_hal_uart.c函数说明HAL_UART_Init函数要使用一个外设首先要对它进行初始化,所以先看串口的初始化函数,其声明......
  • 实验2
    实验任务1实验代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti......
  • 实验2
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;......
  • 实验2
    实验任务一运行结果  程序源码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumbe......