wjy0305

View Post

实验3

task1

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

void printText(int line, int col, char text[]);  // 函数声明
void printSpaces(int n);  // 函数声明
void printBlankLines(int n); // 函数声明

int main()
{
    int line, col, i;
    char text[N] ="hi, May~";
    
    srand(time(0)); // 以当前系统时间作为随机种子
    
    for(i=1; i<=10; ++i)
    {
       line=rand()%25;
       col=rand()%80;
       printText(line, col, text);
       Sleep(1000);  // 暂停1000ms
    }
       return 0;
}

// 打印n个空格
void printSpaces(int n)
{
   int i;
   for(i=1; i<=n; ++i)
   printf(" ");
}

// 打印n行空白行
void printBlankLines(int n)
{
    int i;
    for(i=1; i<=n; ++i)
    printf("\n");
 }
 // 在第line行第col列打印一段文本
 void printText(int line, int col, char text[])
 {
     printBlankLines(line-1);  // 打印n-1行空行
     printSpaces(col-1);// 打印n-1列空格
     printf("%s", text);
}

     

 

程序实现功能:随机出现10个“hi,May~

 

 

task2-1

// 利用局部static变量的特性,计算阶乘
#include <stdio.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));
      return 0;
}
// 函数定义
long long fac(int n)
{
    static long long p=1;
    printf("p=%lld\n",p);//增加的一行 
    p=p*n;
    return p;
}

 

task2-2

// 练习:局部static变量特性
#include <stdio.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);
   return 0;
   }
// 函数定义
int func(int a, int b)
{
    static int m=0, i=2;
    i+=m+1;
    m=i+a+b;
    return m;
}

 

task3

#include <stdio.h>
long long fun(int n); // 函数声明
int main()
{
    int n;
    long long f;
    while (scanf("%d", &n) !=EOF)    
    {
        f=fun(n); // 函数调用
        printf("n = %d, f = %lld\n", n, f);   
     }
     return 0;
}
// 函数定义
// 待补足。。。
long long fun(int n)
{
    if(n==1)
    return 1;
    else 
    return fun(n-1)*2+1; 
}

 

task4

#include<stdio.h>
#include<stdlib.h>
void hanoi(unsigned n,char from,char to,char temp);
void moveplate(unsigned n,char from,char to);
int i;

int main()
{   
    unsigned n;
    while(scanf("%u",&n)!=EOF)
    {    i=0;
        hanoi(n,'A','C','B');
    printf("\n一共移动了%d次\n",i);}
    
    system("pause");
}
void hanoi(unsigned n,char from,char to,char temp)
{ if(n==1)
   moveplate(n,from,to);
else
    {
    hanoi(n-1,from,temp,to);
    moveplate(n,from,to);
    hanoi(n-1,temp,to,from);}


}
void moveplate (unsigned n,char from,char to)
{  
    i++;
    printf("第%u个盘子:%c-->%c\n",n,from,to);
}

 

task5

#include<stdio.h>
#include<math.h>
int is_prime(int n);
int main()
{    
    for(int j=4;j<=20;j+=2)
    {
        for(int k=2;k<=j/2;k++)
        {
            if(is_prime(k)==0&&is_prime(j-k)==0)
            {
                printf("%d=%d+%d\n",j,k,j-k);
                break;
            }
                
        }
    }
    return 0;
}
int is_prime(int n)
{    
    for(int i=2;i<=sqrt(n);i++) 
        if(n%i==0) return 1;
        return 0;
}

 

task6

#include<stdio.h>
#include<stdlib.h>
long fun(long s);
int main()
{    long s,t;
    printf("Enter a number:");
    while(scanf("%ld",&s) != EOF)
    {    t=fun(s);
        printf("new number is:%ld\n\n",t);
        printf("Enter a number:");
    
    }

     return 0;
}
//函数定义
long fun(long s)
{
    int i,j,k=0,m,n=0;
    i=s;
    while(i!=0)
    {
        j=i%10;
        if(j%2==1)
        k=k*10+j;
        
        i=i/10;
    }
    while(k!=0)
    {
        m=k%10;
        n=n*10+m;
        k=k/10;
    }
    return n;
    
    
}

实验总结:本次实验实践了局部变量static,掌握了一种求阶乘的方法。

            

分类:

技术点:

相关文章: