实验3

实验任务1

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define N 80

void print_text(int line, int col, char text[]);//函数声明
void print_spaces(int n);//函数声明
void print_blank_lines(int n);//函数声明

int main(){
    int line, col, i;
    char text[N] = "hi,April~";

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

    for(i = 1;i<+ 10; ++i){
        line = rand()%25;
        col  = rand()%80;
        print_text(line, col, text);
        Sleep(1000);//暂停1000ms
    }

    return 0;
}
// 打印n个空格
void print_spaces(int n) {
    int i;
    
    for(i = 1; i <= n; ++i)
        printf(" ");
}
// 打印n行空白行
void print_blank_lines(int n) {
    int i;
    
    for(i = 1; i <= n; ++i)
        printf("\n");
}
// 在第line行第col列打印一段文本
void print_text(int line, int col, char text[]) {
    print_blank_lines(line-1); // 打印(line-1)行空行
    print_spaces(col-1); // 打印(col-1)列空格
    printf("%s", text); // 在第line行、col列输出text中字符串
}

该代码在屏幕上间隔1s生成10个“hi,April~”

实验任务2

task2_1.c

//利用局部static变量的特性,计算阶乘

#include<stdio.h>
#include<stdlib.h>
long long fac(int n);//函数声明

int main(){
    int i, n;

    printf("Enter n:");
    scanf("%d", &n);

    for(i = 1;i <= n; ++i)
        printf("%d! = %lld\n", i, fac(i));

    system("pause");
    return 0;
}
//函数定义
long long fac(int n){
    static long long p = 1;

    p = p*n;

    return p;
}

增加一行源代码后,测试结果截图:

task2_2.c

//练习:局部static变量特性

#include<stdio.h>
#include<stdlib.h>
int func(int, int);//函数声明

int main(){
    int k = 4, m = 1, p1, p2;

    p1 = func(k,m);//函数调用
    p2 = func(k,m);//函数调用
    printf("%d, %d\n",p1,p2);

    system("pause");
    return 0;
}
//函数定义
int func(int a, int b){
    static int m = 0, i = 2;

    i+=m + 1;
    m = i + a + b;

    return m;
}

运行截图

总结局部static变量的特性:

局部变量只会在最开始初始化,运行过程中不会

函数执行完后,数据不会丢失会保留变量的值,在下一次调用函数时,变量仍会记得原来的值

 

实验任务3

#include<stdio.h>
#include<stdlib.h>
long long func(int n);//函数声明

int main(){
    int n;
    long long f;

    while (scanf("%d",&n) !=EOF){
        f = func(n);//函数调用
        printf("n = %d,f = %lld\n", n, f);
    }
    system("pause");
    return 0;
}
long long func(int n){
    long long ans;
    if(n==1)
       ans=1;
    else
       ans = 2*func(n-1)+1;
    
    return ans;
}

运行结果

 

实验任务4

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

int func(int n, int m);

int main(){
    int n, m;

    while(scanf("%d%d", &n, &m) !=EOF)
        printf("n = %d, m = %d, ans = %d\n", n, m, func(n,m));

    system("pause");
    return 0;
}
int func(int n, int m){
    int ans;
    if(n==m||m==0)
        ans=1;
    else if(n<m)
        ans=0;
    else
        ans=func((n-1),m)+func((n-1),(m-1));
    return ans;
}

运行截图

 

实验任务5

task5_1.c

程序源码

#include<stdio.h>
#include<stdlib.h>
double mypow(int x, int y);//函数声明

int main(){
    int x, y;
    double ans;

    while(scanf("%d%d", &x,&y) !=EOF){
        ans = mypow(x, y);//函数调用
        printf("%d的%d次方: %g\n", x, y, ans);//两个n什么用捏
    }

    system("pause");
    return 0;
}
double mypow(int x, int y){
    int i=1;
    double ans=1;
    if(y>=0){
        for(i=1;i<=y;i++)
            ans=1*x*ans;}
    else{
        for(i=1;i<=y;i++)
            ans=ans*1/((double)x);}
    return ans;
}

运行截图

 

task5_2.c

程序源码

#include<stdio.h>
#include<stdlib.h>
double mypow(int x, int y);//函数声明

int main(){
    int x, y;
    double ans;

    while(scanf("%d%d", &x,&y) !=EOF){
        ans = mypow(x, y);//函数调用
        printf("%d的%d次方: %g\n", x, y, ans);
    }

    system("pause");
    return 0;
}
double mypow(int x, int y){
    double ans;
    if(y==0)
        ans=1;
    else if(y>0)
        ans=x*mypow(x, y-1);
    else
        ans=1/mypow(x, -y);
    
    return ans;
}

运行截图

 实验任务6

程序源码

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void hanoi(unsigned int n, char from, char temp, char to);
void moveplate(unsigned int n,char from,char to);
int main()
{
    unsigned int n;
    int i;
    while(scanf("%u",&n)!=EOF){//输入盘子数目
        hanoi(n,'A','B','C');
        i=pow(2,n)-1;//熟悉熟悉熟悉熟悉熟悉熟悉熟悉熟悉熟悉
        printf("\n一共移动了:%d次\n",i);}
    system("pause");
    return 0;
}
void hanoi(unsigned int n,char from,char temp,char to)
{
    if(n==1)
        moveplate(n,from,to);
    else
    {
        hanoi(n-1,from,to,temp);//n-1个盘子从A以C为中转移到B上
        moveplate(n,from,to);//将盘子n从A转移到C上
        hanoi(n-1,temp,from,to);//将n-1个盘子从B以A为中转移到C上
    }
}
void moveplate(unsigned int n,char from,char to)
{
    printf("%u:%c-->%c\n",n,from,to);
}

运行截图

实验任务7

程序源码

#include<stdio.h>
#include<stdlib.h>
int is_prime(int n)
{
    int i;
    int k=1;
    for(i = 2;i < n;i++)
    {
        if(n%i==0)
        {
            k=0;
            break;
        }
    }
    return k;
}
int main()
{
    int n;
    int i;
    while(scanf("%d",&n)!=EOF){
    if(n>2 && n%2==0)
    {
        for(i=2;i<n;i++)
        {
            if(is_prime(i)&&is_prime(n-i))
            {
                break;
            }
        }
        printf("%d=%d+%d\n",n,n-i,i);
    }
    else
    {
        printf("请重新输入!\n");
    }}
    system("pause");
    }

运行截图

 哈哈哈哈哈

实验任务8

程序源码

#include <stdio.h>
#include<stdlib.h>
#include <math.h>
long func(long s);
int main(){
    long s,t;
    printf("Enter a number:");
    while(scanf("%ld",&s)!=EOF){
        t=func(s);
        printf("new number is:%ld\n\n",t);
        printf("Enter a number:");
    }
    system("pause");
    return 0;
}
long func(long s)
{
    int t, num=0, i=0;
    while(s!=0)
    {
        t=s%10;
        if(t%2==1){
            num=num+t * pow(10, i);
            i++;
        }
        s=s/10;
    }
    return num;
}

运行截图

 

posted @ 2023-04-02 23:37  zlzliz  阅读(36)  评论(0)    收藏  举报