《C和指针》学习笔记[第七章 函数]

7.10 问题

1.

可以在函数中做一些信息输出的工作,可以清晰的看到函数的调用

2.

我在本地使用测试中,如果没有函数原型,入参检测默认都会int。

3.

编译无法通过报错

4.

会转换成整形

5.

会转换成整形

6.

会转换成整形

7.

经过测试,该函数参数int array[10] 与int array[]效果一样,这样的话,这个函数无法

处理大于或者小于10个元素的整形数组,一个会可能越界,一个可能会没有处理全部元素

8.

都是重复做一件事情,然后达到一定的条件停止.

9.

便于管理,方便维护

10

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
#include <time.h>

long count;

long
fiboncci(int i)
{
    count++;
    if (i <= 2) {
        return 1;
    }
    else
        return fiboncci(i - 1) + fiboncci( i - 2);
}


int main(void)
{
    long f_res;
    int i = 2;
    time_t start, end;
    start = time(NULL);
    do {
        count = 0;
        f_res = fiboncci(i);
        printf("i = %d, res = %ld, count = %ld\n", i, f_res, count);
        i++;
        
    } while (i<=50);
    
    end = time(NULL);
    
    printf("运行时间 = %ld\n", end - start);
    
    return EXIT_SUCCESS;
}
复制代码

 

7.11

1.

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>


int hermite(int n, int x)
{
    if (n <= 0 ) {
        return 1;
    }
    else if (n == 1)
        return 2 * x;
    else
        return 2 * x * hermite(n - 1, x) - 2 * (n -1) * hermite(n -2, x);
}

int main(void)
{
    
    int i;
    i = hermite(3, 2);
    printf("result = %d\n", i);

    
    return EXIT_SUCCESS;
}
复制代码

2.

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>


int gcd(int m , int n){
    if (m <= 0 || n <= 0) {
        return 0;
    }
    else{
        if (m % n == 0) {
            return n;
        }
        else{
            return gcd(n, m % n);
        }
    }
}

int main(void)
{
    
    int i;
    i = gcd(77, 35);
    printf("result = %d\n", i);

    
    return EXIT_SUCCESS;
}
复制代码

 3.

 这是用递归写的不完善版本。

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>


int ascii_to_integer(char *string){
    if (string[0] == '\0') {
        return 0;
    }
    else if (string[0] >= '0' && string[0] <= '9'){
        return (string[0] - '0') * pow(10, strlen(string)-1)  + ascii_to_integer(++string);
    }
}

int main(void)
{
    
    char string[1000];
    
    gets(string);
    
    printf("result = %d\n", ascii_to_integer(string));

    return EXIT_SUCCESS;
}
复制代码

这是用循环写

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>


int ascii_to_integer(char *string){
    char ch = *string;
    int r_num = 0;
    while (ch) {
        if (ch >= '0' && ch <= '9') {
            r_num = r_num * 10 + ch - '0';
        }
        else
            return 0;
        ch = *(++string);
    }
    return r_num;
}

int main(void)
{
    
    char string[1000];
    
    gets(string);
    
    printf("result = %d\n", ascii_to_integer(string));

    return EXIT_SUCCESS;
}
复制代码

4.

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>


int max_list(int f_num, ...){
    int max_num = f_num;
    int num;
    
    va_list vararg;
    
    va_start(vararg, f_num);
    
    while (1) {
        num = va_arg(vararg, int);
        if (num >= 0 ) {
            if (num > max_num) {
                max_num = num;
            }
        }
        else{
            break;;
        }
    }
    
    return max_num;
}

int main(void)
{
    
    printf("result = %d\n", max_list(1,2,3,4,99,6,-1));

    return EXIT_SUCCESS;
}
复制代码

5.

 

复制代码
//
//  t_c.c
//  pointers_on_c
//
//  Created by sidian on 2022/5/27.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>


void print_integer(int n)
{
    printf("%d", n);
}

void print_float(double n)
{
    printf("%f", n);
}

int my_print(char *string, ...)
{
    va_list var_arg;
    
    va_start(var_arg, string);
    
    int r_num = 0;
    char * t_string;
    while (*string++ != '\0') {
        if (*(string-1) == '%') {
            switch (*string) {
                case 'd':
                    print_integer(va_arg(var_arg, int));
                    string++;
                    r_num++;
                    break;
                case 'f':
                    print_float(va_arg(var_arg, double));
                    string++;
                    r_num++;
                    break;
                case 's':
                    t_string = va_arg(var_arg, char *);
                    while (*t_string++ != '\0') {
                        putchar(*(t_string - 1));
                    }
                    string++;
                    r_num++;
                    break;
                case 'c':
                    putchar(va_arg(var_arg, int));
                    string++;
                    r_num++;
                    break;
                default:
                    putchar(*string);
                    break;
            }
        }else
        {
           putchar(*(string-1));
        }
    }
    putchar('\n');
    
    return r_num;;
}



int main(void)
{
    int num;
    char *string = "ab%dcd, %s,%f,!!%c!!";
    
    num = my_print(string, 15,"hjkl",1.234,'p');
    
    printf("r_num = %d\n", num);
    

    return EXIT_SUCCESS;
}
复制代码

 

6.

复制代码
//
//  t_c.c
//  pointers_on_c
//
//  Created by sidian on 2022/5/27.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>


static char *number[] = {"", "ONE ", "TWO ", "THREE ", "FOUR ", "FIVE ",
                            "SIX ", "SEVEN ", "EIGHT ", "NINE ","TEN ",
                            "ELEVEN ", "TWELVE ", "THIRTEEN ", "FOURTEEN ", "FIFTEEN ",
                            "SIXTEEN ", "SEVENTEEN ", "EIGHTEEN ", "NINETEEN "
                           };
static char *degree[] = {"HUNDERS ", "THOUSAND ", "MILLION ", "BILLION " };
static char *tens[] = {"", "", "TWENTY ", "THIRTY ", "FORTY ", "FIFTY ",
                               "SIXTY ", "SEVENTY ", "EIGHTY ", "NINETY "
                            };


char* string_copy(char *des, const char *sou){
    while (*sou != '\0'){
        *des++ = *sou++;
    }
    return des;
}

// 输出大单位数字前面的数值
char* cal_quotient(int quotient, char *buffer)
{
    char hunders_quotient,hunders_remainer, ten_quotient, ten_remainer;
    hunders_quotient = quotient / 100;
    hunders_remainer = quotient % 100;
    
    if (hunders_quotient) {
        buffer = string_copy(buffer, number[hunders_quotient]);
        buffer = string_copy(buffer, degree[0]);
    }

    if (hunders_remainer) {
        ten_quotient = hunders_remainer / 10;
        ten_remainer = hunders_remainer % 10;
        if (ten_quotient >= 2) {
            buffer = string_copy(buffer, tens[ten_quotient]);
            if (ten_remainer) {
                buffer =  string_copy(buffer, number[ten_remainer]);
            }
        }
        else if (ten_quotient == 1){
            buffer = string_copy(buffer, number[ten_remainer + 10]);
        }
        else {
            buffer = string_copy(buffer, number[ten_remainer]);
        }

    }
    
    
    return buffer;
    
}

// 计算billion的输出,并输出指针位置
char *cal_billion(unsigned int amount, char *buffer)
{
    char *r_string = buffer;
    int quotient;
    quotient = amount / 1000000000;
    if (quotient) {
        r_string = cal_quotient(quotient, buffer);
        r_string = string_copy(r_string, degree[3]);
    }
    return r_string;
}

// 计算million的输出
char *cal_million(unsigned int amount, char *buffer)
{
    char *r_string = buffer;
    int quotient;
    amount %= 1000000000;
    quotient = amount / 1000000;
    if (quotient) {
        r_string = cal_quotient(quotient, buffer);
        r_string = string_copy(r_string, degree[2]);
    }
    return r_string;
}

// 计算thousand的输出
char *cal_thousand(unsigned int amount, char *buffer)
{
    char *r_string = buffer;
    int quotient;
    amount %= 1000000;
    quotient = amount / 1000;
    if (quotient) {
        r_string = cal_quotient(quotient, buffer);
        r_string = string_copy(r_string, degree[1]);
    }
    return r_string;
}

char *cal_digit(unsigned int amount, char *buffer)
{
    char *r_string = buffer;
    int quotient;
    quotient = amount % 1000;
    if (quotient) {
        r_string = cal_quotient(quotient, buffer);
    }
    return r_string;
}

void written_amount( unsigned int amount, char *buffer){
    buffer = cal_billion(amount, buffer);
    buffer = cal_million(amount, buffer);
    buffer = cal_thousand(amount, buffer);
    buffer = cal_digit(amount, buffer);
    
    buffer[0] = '\0';
    
}

int main(void){
    
//    unsigned int num =4017483648;
    unsigned int num = 16312;

    char string[200];
    
    written_amount(num, string);
    
    printf("%u\n%s\n",num, string);
    
    return EXIT_SUCCESS;
}
复制代码

 

posted @   就是想学习  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示