首页 > 其他分享 >实验三 函数

实验三 函数

时间:2023-04-01 21:33:06浏览次数:38  
标签:函数 int long char 实验 func printf include

实验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中字符串 }

实现的功能是打印随机行数“hi,April”

实验2

#include <stdio.h>
long long fac(int n);
int main() {
    int i, n;
    printf("Enter n: ");
    scanf_s("% 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;
    
    p = p * n;
    return p;
}

实验2不知道为啥运行总是报错如上,但是代码应该没有问题,输入5之后按理应该得到5的阶乘即120。

加入一行printf("p = %lld\n", p)后应该分别打印出1,1,2,6,24

实验2.2:因为static作用于局部变量时可以改变生命周期,使得第二次执行fac的时候不进行m = 0, i = 2的赋值,而是延续上一步计算出的m=8,所以理论上应该输出8,17。实际输出也确实如此,如下图:

实验3:

#include <stdio.h>
long long func(int n); // 函数声明
int main() {
    int n;
    long long f;
    while (scanf_s("%d", &n) != EOF) {
        f = func(n); // 函数调用
        printf("n = %d, f = %lld\n", n, f);
    }
    return 0;
}

// 函数定义
long long func(int n){
    int i ;
    double f = 0.0;
    for (i = 0; i < n; i++)
        f = ((f + 1) * 2)-1;
    return f;
}

实验4:

#include <stdio.h>
int func(int n, int m);
int main() {
    int n, m;
    while (scanf_s("%d%d", &n, &m) != EOF)
        printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
    return 0;
}
int func(int x, int y) {
    int i = 1;
    double f = 1.0;
    for (i = 1; i <= y; i++)
        f *= (x - i + 1) / i;
    return f;
}

实验5:

 
#include <stdio.h>

double mypow(int x, int y);

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

    while (scanf_s("%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)
{
    int i;
    double m=1.0;
    if (y>= 0)
        for (i = 1; i <= y; i++)
            m = m * x;
    else
        for (i = 0; i > y; i--)
            m = m / x;
    return m;
    
}

实验6:

#include <stdio.h>
int a = 0;
void hanoi(unsigned int n, char from, char temp, char to);
void move(unsigned int n, char from, char to);
int main() {
    
    unsigned int n;
    while (scanf_s("%u", &n) != EOF) {

        hanoi(n, 'A', 'B', 'C');
        
        printf("共需要%d步", a);
    }
        return 0;
    
    

}
void hanoi(unsigned int n, char from, char temp, char to) {
    if (n == 1)
        move(n, from, to);
    else
    {
        hanoi(n - 1, from, to, temp);
        move(n, from, to);
        hanoi(n-1, temp,from, to);

    }
}

void move(unsigned int n, char from, char to) {
    
    a++;
    printf("%u:%c-->%c\n", n, from, to);
}

 

#include <stdio.h> 
#include <math.h>
int is_prime(int n);
int main()
{
    int i, k, a[15], j = 0, l;
    for (k = 2; k <= 20; k++)
    {
        if (is_prime(k))
            a[j++] = k;
    }
    for (i = 2; i <= 10; i++)
    {
        for (l = 1; l <= 10; l++)
        {

            if (is_prime(2 * i - a[l - 1]))
            {
                printf("%d = %d + %d\n", (2 * i), a[l - 1], (2 * i - a[l - 1])); break;
            }

        }
    }
    return 0;
}
int is_prime(int n)
{
    int i, flag = 1;
    for (i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0) { flag = 0; break; }
    }
    return flag;
}

实验8:

#include <stdio.h>
#include <math.h>
long func(long s); // 函数声明
int main() {
    long s, t;
    printf("Enter a number: ");
    while (scanf_s("%ld", &s) != EOF) {
        t = func(s); // 函数调用
        printf("new number is: %ld\n\n", t);
        printf("Enter a number: ");
    }
    return 0;
}
long func(long x) {
    int t, n = 0, i = 0;
    while (x != 0)
    {
        t = x % 10;
        if (t % 2 != 0) {
            n = n + pow(10, i) * t;
            i++;
        }
        x = x / 10;
    }
    return n;

}

 

标签:函数,int,long,char,实验,func,printf,include
From: https://www.cnblogs.com/zcyy123/p/17278717.html

相关文章

  • 三月三十日第三次实验
    实验一:#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:调查问卷在以下网址提交课程调查问卷完成情况:已认真填写并提......
  • 函数
    1.自定义函数 2.库函数库函数:IO函数  字符串操作函数 字符操作函数 内存操作函数         时间/日期操作函数  数学函数 其他库函数使用库函数时,必须使用包含#include对应的头文件https://cplusplus.com函数查询函数的参数: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......
  • 函数高级
    一、函数返回多个数据值1、return关键字的两个作用返回数据值结束函数运行2、函数中如果想要返回多个数据值,一般是组成元组进行返回deffunc(a,b):'''返回两个数的和及差'''returna+b,a-b#元组可加括号,也可......
  • 实验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);/......