首页 > 其他分享 >实验三

实验三

时间:2023-04-01 21:33:42浏览次数:39  
标签:return int long char 实验 func include

实验三

task.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中字符串
}

 

 

讨论:程序实现的功能

在第line行第col列打印一段文本,即打印(line-1)行空行,打印(col-1)列空格,在第line行、col列输出text中字符串,重复此操作10次。(line为1-25中随机的一行,col为1-80中随机的一列)

 

 

task.2

 

程序源代码

// 利用局部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));//此处像1的是小写的l,长长整型输出时用%lld 

    return 0;
}

// 函数定义
long long fac(int n) {
    static long long p = 1;
    printf ("p = %lld\n",p);
    p = p * n;

    return p;
}

程序运行截图

程序源代码

// 练习:局部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) {    //a = 4,b = 1           a = k = 4,b = m = 1
    static int m = 0, i = 2;

    i += m + 1;             //i = 2 + 0 + 1 = 3     i = 3 + 8 + 1 = 12  
    m = i + a + b;          //m = 3 + 4 + 1 = 8     m = 12 + 4 + 1 =17

    return m;
}

程序运行截图

讨论:

(理论分析见代码注释)

static变量在运行时数值保持不变,在对static变量执行下一步操作时,该变量保持其上一步计算出的值不变。

 

 

task.3

程序源代码

#include <stdio.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);
    }
    return 0;
 } 
 
 long long func(int n)//f(n) + 1 =2 *(f(n-1) + 1) 
 {
     if (n == 1) 
        return 1;
     else
        return 2 *(func(n-1) + 1) - 1;
 
 }

 程序运行截图

.4

 

 

task.4

程序源代码

#include <stdio.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));
    
    return 0;
    
}

int func (int n,int m)
{
    if(m == n|| m == 0) 
    return 1;
    else if(m > n)    
    return 0;
    else if(m == 1)
    return n;
    else
    return func (n - 1,m) + func (n - 1,m - 1);
}

程序运行截图

 

 

task.5

程序源代码

编写递归函数 mypow() 实现计算x的y次方
#include <stdio.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\n", x, y, ans); } return 0; } double mypow (int x,int y) {
if (y == 0) return 1; else if(y > 0) return x * mypow (x, y-1); else return 1/(x * mypow (x, -y-1)); }

程序运行截图

 

程序源代码

不使用递归函数,使用迭代算法实现计算x的y次方
#include <stdio.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\n", x, y, ans);//%g也为double型,但更灵活,可省略小数点后多余的0 
}
return 0;
}

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

 

程序运行截图

 

 

task.6

程序源代码

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

    unsigned int n;
    while(scanf("%u", &n) != EOF)
    {
    i = 0;
    hanoi(n, 'A', 'B', 'C');
    printf("一共移动了%d次\n\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);
       moveplate(n, from, to);
       hanoi(n-1, temp, from, to);
     }
 }
 
 void moveplate(unsigned int n,char from,char to)
 {
     static int cishu = 0;
     cishu = cishu + 1;
     printf("%u: %c-->%c\n",n,from,to);
    ++i;
  } 

程序运行截图

 

 

task.7

程序源代码

#include <stdio.h>
#include <math.h>
int is_prime (int n);

int main()
{
    int s, i;
    while (scanf("%d",&s) != EOF)
{
    for (i = 2;i <= s/2 ;++i)
    {
        if(is_prime(i)&&is_prime(s-i)) 
        {
            printf("%d = %d + %d\n", s, i, s-i);
            break;
        }
    }
}
     return 0;
}
int is_prime (int n)
{
    int k;
    for (k = 2;k <= sqrt(1.0*n); ++k)
     {  
    if(n%k==0)
    return 0;
    }
    return 1;
}

程序运行截图

 

 

task.8

程序源代码

#include<stdio.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: ");
    }
    
 } 
 
 long func(long s)
 {
     int k, j;
     int count = 0;
     long n = 0;
     while(s != 0)
     {
         j = s % 10;         //分离出个位 
         s = s/10;           //去掉个位的数字,得到一个新的数字,以便继续分离 
         if (j % 2 != 0)     //判断是否为奇数 
         {
             n = j * pow(10,count) + n;  //重点 
             count++;
         }
     }
     
     
     return n;
 }

程序运行截图

 

标签:return,int,long,char,实验,func,include
From: https://www.cnblogs.com/csy426/p/17273730.html

相关文章

  • 实验三 函数
    实验1.#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intma......
  • 三月三十日第三次实验
    实验一:#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,c......
  • 202031607327-杨辽辽 实验一 软件工程准备 初步了解软件工程
    202031607327-杨辽辽实验一软件工程准备初步了解软件工程项目内容班级博客链接20级卓越班本次作业要求链接实验一软件工程准备我的课程学习目标1.学习博客园软件开发者学习社区使用技巧和经验;2.了解Github的基本操作;本次作业在哪些方面帮我实现学习......
  • 实验2
    实验目的1.知道Python中字符串的表示,熟练使用索引、切片、常见字符串操作2.知道Python中列表的表示,熟练使用索引、切片、常见列表操作,能正确使用列表推导式3.针对具体问题场景,能够灵活、组合使用字符串、列表、控制语句编程解决实际问题实验准备实验前,请练习/复习以下内容:4......
  • 实验2
    实验任务一源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(R2......
  • 202031607323-后涌- 实验一 软件工程准备—什么是软件?什么是工程?
    项目内容班级博客链接班级链接本次作业要求链接作业要求我的课程学习目标了解掌握软件在开发过程中的过程、方法和工具本次作业在哪些方面帮我实现学习目标准备学习软件工程的工具任务1:调查问卷在以下网址提交课程调查问卷完成情况:已认真填写并提......
  • 实验三
    task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){i......
  • 实验3
    实验任务1源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){......
  • 实验三
    试验任务一源码程序#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);/......
  • 202031607224-邓思超 实验一 软件工程准备—认识软件工程
    实验一软件工程准备项目内容班级博客链接班级博客本次作业要求链接本次作业要求链接我的课程学习目标(1)学习博客园软件开发者学习社区使用技巧和经验。(2)了解Github的基本操作。本次作业在哪些方面帮我实现学习目标(1)通过博客园阅读了专业相关的一些博客内容......