葛才璟

实验2 C语言分支语句、循环语句应用编程

实验2 C语言分支语句、循环语句应用编程

1. 实验任务1

#include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
printf("2049%04d\n", num);
scanf("%d", &num);
printf("2049%04d\n", num);
scanf("%d", &num);
printf("2049%04d\n", num);
return 0;
}

  

 %04d的作用:使输出的数据宽度为四,右对齐,左边填充0.

#include <stdio.h>
int main()
{
int num,i=1;

while(i<4){
scanf("%d", &num);
printf("2049%04d\n", num);
i++;
}

return 0;
}

2. 实验任务2

#include <stdio.h>
int main(){
char ans1, ans2;
printf("复习了没? (输入y或Y表示复习了,输入n或N表示没复习) : ");
ans1 = getchar(); 
getchar(); 
printf("\n动手敲代码了没? (输入y或Y表示敲了,输入n或N表示木有敲) : ");
ans2 = getchar();
if (ans1=='y'&&ans2=='y'||ans1=='y'&&ans2=='Y'||ans1=='Y'&&ans2=='y'||ans1=='Y'&&ans2=='Y') 
printf("\n罗马不是一天建成的:)\n");
else
printf("\n罗马不是一天毁灭的。。。\n");
return 0;
}

 3. 实验任务3 

#include <stdio.h>
int main(){
char x;
x = getchar();
if (x>='0'&&x<='9')
printf("%c是数字字符\n", x);
else if (x>='a'&&x<='z'||x>='A'&&x<='Z')
printf("%c是英文字母\n", x);
else
printf("%c是其它字符\n", x);
return 0;
}

 

 

 

 4.实验任务4

#include <stdio.h>
int main(){
int a,n;
int top,bottom;
double SUM,item;

while(scanf("%d %d",&n,&bottom)!=EOF){
int top=1;
double SUM=0;
a=bottom;
while(top<=n){
item=1.0*top/bottom;
SUM+=item;
top++;
bottom=a+bottom*10;
}printf("n=%d,a=%d,sum=%lf\n",n,a,SUM);
}
return 0;
}

 

 

 5.实验任务5

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const int N = 5;
int main(){
    int x, n;
    srand((unsigned)time(NULL)); 
    for (n = 1; n <= N; n++){
        x = rand() %(31-1+1)+1; 
        printf("%3d", x);
    }printf("\n");
    return 0;
}

 

 6.实验任务6

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
    int x,y,y2,i;
    
    printf("猜猜十二月哪一天是你的luck day\n开始喽,你有三次机会(1~31):"); 
    srand((unsigned)time(NULL));
    x=rand()%(31-1+1)+1; 
    
    for(i=0;i<2;i++){
        scanf("%d",&y);
        if(x<y){
             printf("你猜的日期晚了,try again:");
        }else if(x>y){
            printf("你猜的日期早了,再试次:");
        }else{
            printf("恭喜猜中啦\n");break;
        }
    }
    
    scanf("%d",&y2);
    if(x>y2){
        printf("猜早啦,luck day还早呢,前面次数用完了\n"); 
    }else if(x<y2) {
        printf("猜晚啦,luck day早过了,前面次数用完了\n");
    }else{
        printf("猜对啦\n");
    }
    printf("你的luck day是%d",x);
    
    return 0;
}

 

 7.实验任务7

#include<stdio.h>
int main(){
    int n,i;
    scanf("%d",&n);
    
    for(i=1;i<=n;i++){
        printf(" o \n");
        printf("<H>\n");
        printf("I I\n");
        printf("\n");
    }
    return 0;
} 

 

 

#include<stdio.h>
int main(){
    int n,i;
    scanf("%d",&n);
    
    for(i=1;i<=n;i++){
        printf(" o \t");
    }printf("\n");
    for(i=1;i<=n;i++){
        printf("<H>\t");
    }printf("\n");
    for(i=1;i<=n;i++){
        printf("I I\t");
    }printf("\n");
    
    return 0;
} 

 

 

#include<stdio.h>
int main(){
    int n,i,a,k;
    scanf("%d",&n);
    
    for(k=0;k<n;k++){
        for(a=0;a<k;a++){
            printf("\t");
        } 
        for(i=0;i<2*n-1-2*k;i++){
            printf(" o \t");
        }printf("\n");    
        for(a=0;a<k;a++){
            printf("\t");
        }    
        for(i=0;i<2*n-1-2*k;i++){
            printf("<H>\t");
        }printf("\n");
        for(a=0;a<k;a++){
            printf("\t");
        }
        for(i=0;i<2*n-1-2*k;i++){
            printf("I I\t");
        }printf("\n");
    }
    return 0;
} 

 

 

  

 

posted on 2021-11-15 22:43  葛才璟  阅读(84)  评论(1编辑  收藏  举报

导航