首页 > 编程语言 >实验3 函数应用编程

实验3 函数应用编程

时间:2023-04-05 14:35:02浏览次数:40  
标签:函数 int 编程 long char 实验 func ans 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

task2_1

 1 // 利用局部static变量的特性,计算阶乘
 2 #include <stdio.h>
 3 long long fac(int n); // 函数声明
 4 int main() 
 5 {
 6     int i, n;
 7     printf("Enter n: ");
 8     scanf("%d", &n);
 9     for (i = 1; i <= n; ++i)
10     printf("%d! = %lld\n", i, fac(i));
11     return 0;
12 }
13 // 函数定义
14 long long fac(int n)
15  {
16     static long long p = 1;
17     //printf("p = %lld\n", p);
18     p = p * n;
19     return p;
20 }

task2_2

 1 // 利用局部static变量的特性,计算阶乘
 2 #include <stdio.h>
 3 long long fac(int n); // 函数声明
 4 int main() 
 5 {
 6     int i, n;
 7     printf("Enter n: ");
 8     scanf("%d", &n);
 9     for (i = 1; i <= n; ++i)
10     printf("%d! = %lld\n", i, fac(i));
11     return 0;
12 }
13 // 函数定义
14 long long fac(int n)
15  {
16     static long long p = 1;
17     printf("p = %lld\n", p);
18     p = p * n;
19     return p;
20 }

 

 总结:

static变量的特性为:只赋初值一次,,之后每次调用函数时保留上次函数调用时的值

实验任务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)
{
    long long int f;
    if(n==1)
    f=1;
    else
    f=2*func(n-1)+1;
    return f;
}

 

 实验任务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)
{
    int result;
    if(m==0)
    result=0;
    else
    {
        if(m>n)
        result=0;
        else
        {
            if(m==n)
            result=1;
            else
            {
                if(m==1)
                result=n;
                else
                result=func(n-1,m)+func(n-1,m-1);
            }
        }
    }
    return result;
}

 

 

 

 实验任务5

task5_1

 

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

task5_2

#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)
{
    double ans=1;
    if(y==0)
    ans=1;
    if(y>0)
        ans=ans*x*mypow(x,y-1);
    if(y<0)
        ans=ans/x*mypow(x,y+1);
    return ans;
}

 

 实验任务6

 

#include<stdio.h>
#include<math.h>
void hanoi(int n,char from,char temp,char to);
void move(int n,char from,char to);
int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        hanoi(n,'A','B','C');
        printf("total degree:%g\n",pow(2,n)-1);
    }
    return 0;
}

void hanoi(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(int n,char from,char to)
{
    printf("%d:%c-->%c\n",n,from,to);
}

 

 

 实验任务7

 

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

 

 实验任务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: ");
    }
    return 0;
}

long func(long s)
{
    long ans=0;
    long x,i=1;
    while(s!=0)
    {
        x=s%10,s=s/10;
        if(x%2!=0)
        {
            ans=ans+x*i;
            i=i*10;
        }    
    }
    return ans;
}

 

 

 

 

 

 

 

 

 

 

 

 

标签:函数,int,编程,long,char,实验,func,ans,include
From: https://www.cnblogs.com/dl123/p/17273728.html

相关文章

  • 实验3
    task1实验源码#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......
  • PTA基础编程——6-8简单阶乘计算
    本题要求实现一个计算非负整数阶乘的简单函数。函数接口定义:intFactorial(constintN);其中N是用户传入的参数,其值不超过12。如果N是非负整数,则该函数必须返回N的阶乘,否则返回0。裁判测试程序样例:#include<stdio.h>intFactorial(constintN);intmain(){   int......
  • Javascript模块化编程(三):require.js的用法
    这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战。我采用的是一个非常流行的库require.js。一、为什么要用require.js?最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了。后来,代码越来越多,一个文件不够了,必须分......
  • yaml-cpp YAML格式处理库的介绍和使用(面向业务编程-文件格式处理)
    yaml-cppYAML格式处理库的介绍和使用(面向业务编程-文件格式处理)YAML格式介绍YAML的格式介绍,有关ini、json和xml或许很多人已经很了解了,但是关于YAML,还有许多人不了解。YAML被设计成更适合人类阅读(我想正因为如此,所以相对来说更灵活,就导致到使用的时候很多人会觉得它看起来并不......
  • 面向对象、设计原则、设计模式、编程规范、重构,这五者的关系(三)
    要写出满足这些评价标准的高质量代码,我们需要掌握一些更加细化、更加能落地的编程方法论,包括面向对象设计思想、设计原则、设计模式、编码规范、重构技巧等。比如:面向对象中的继承、多态能让我们写出可复用的代码;编码规范能让我们写出可读性好的代码;设计原则中的单一职责、D......
  • 实验三
    任务一代码#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......
  • 实验一 华为路由交换技术基本配置
    实验一华为路由交换技术基本配置实验目的:掌握进入进入路由器、交换机各种模式配置登陆console口密码配置系统视图密码保存、删除配置等基本命令实验拓扑:实验步骤:一、路由器的基本配置1.双击PC1,打开串口,连接路由器AR1左侧为刚进入路由器时的默认模式--用户模式(用......
  • Java | 一分钟掌握异步编程 | 3 - 线程异步
     作者:Mars酱 声明:本文章由Mars酱编写,部分内容来源于网络,如有疑问请联系本人。 转载:欢迎转载,转载前先请联系我!前言前两篇介绍了理论,这篇讲具体的实现方式。前言都是废话,直接上车~简单粗暴创建一个对象,继承Thread类,实现run函数,这个线程异步就做完了:/***@authormars酱*/publ......
  • 实验二
    实验任务一实验源码:1#字符串的基础操作23x='nbaFIFA'4print(x.upper())5print(x.lower())6print(x.swapcase())7print()89x='abc'10print(x.center(10,'*'))11print(x.ljust(10,'*'))12print(x.rjust(10......
  • VLOOKUP函数16种经典用法
    说起Excel中的数据查找,VLOOKUP可真是大名鼎鼎。属于Excel“查找王者”!VLOOKUP函数果真所向披靡吗?今天就和大家一起说说Excel中的数据查询那些事儿。深入了解一下VLOOKUP函数的各种用法,看看这位大众情人还藏着多少不为人知的秘密!功能:在表格的首列查找指定的数值,并返回表格当前行中指......