鱼C论坛

 找回密码
 立即注册
查看: 2133|回复: 2

[已解决]这个找了2个小时,找不出错误在哪里,很烦

[复制链接]
发表于 2022-11-21 22:46:02 | 显示全部楼层 |阅读模式
50鱼币

在最前面

在最前面

在中间

在中间

就是遇到字符串就出错

就是遇到字符串就出错

#include <stdio.h>
#include <stdarg.h>

int int_print(int d);
int str_print(char *s);
int wdpritnf(char *s, ...);

int int_print(int d){
        int i = 0, j = 1, ls, count = 0;

        if(d < 0){
                putchar('-');
                d = -d;
                count++;
        }

        ls = d;
        while (ls > 9){
                j *= 10;
                ls /= 10;
        }

        while (d > 0){
                putchar(d / j + '0');
                d = d % j;
                j = j / 10;
                count++;
        }

        return count;
}

int str_print(char *s){
        int i = 0;

        while (s[i] != '\0'){
               putchar(s[i]);
               i++;
        }

        return i;
}

int wdprintf(char *s, ...){
        int d, i = 0, count = 0;
        char c;
        va_list in;

        va_start(in, s);

        while (s[i] != '\0'){
                if (s[i] == '%'){
                        switch (s[++i]){
                                case 'd':{
                                                 d = va_arg(in, int);
                                                 count += int_print(d);
                                                 break;
                                        }
                                case 'c':{
                                                 c = va_arg(in, int);
                                                 putchar(c);
                                                 count++;
                                                 break;
                                        }
                                case 's':{
                                                 s = va_arg(in, char *);
                                                 count += str_print(s);
                                                 break;
                                        }
                        }
                        i++;
                }
                else{
                        putchar(s[i]);
                        i++;
                        count++;
                }
        }
        return count;
        va_end(in);
}

int main(){
        int d, i;
        char c;
        char *s;

        wdprintf("请输入一个字符串:");
        scanf("%s",s);
        i = wdprintf("这个字符串是%s\n",s);

        wdprintf("请输入一个字符:");
        scanf("%c",&c);
        i = wdprintf("这个字符是%c\n",c);

        wdprintf("请输入一个数字:");
        scanf("%d",&d);
        i = wdprintf("这个数字是%d\n",d);

        return 0;
}

不知道为什么。
最佳答案
2022-11-21 22:46:03
本帖最后由 jackz007 于 2022-11-22 02:05 编辑

        我也折腾了 3+ 个小时,不过,可变参数函数,我是从 0 开始学的,现炒现卖。
  1. #include <stdio.h>
  2. #include <stdarg.h>

  3. int int_print(int d)
  4. {
  5.         int i = 0, j = 1, ls, count = 0;
  6.         if(d < 0){
  7.                 putchar('-');
  8.                 d = -d;
  9.                 count++;
  10.         }
  11.         ls = d;
  12.         while (ls > 9){
  13.                 j *= 10;
  14.                 ls /= 10;
  15.         }
  16.         while (d > 0){
  17.                 putchar(d / j + '0');
  18.                 d = d % j;
  19.                 j = j / 10;
  20.                 count++;
  21.         }
  22.         return count;
  23. }

  24. int str_print(const char * s)
  25. {
  26.         int i = 0            ;
  27.         while (s[i] != '\0'){
  28.                putchar(s[i]) ;
  29.                i ++          ;
  30.         }
  31.         return i             ;
  32. }

  33. int wdprintf(const char * s , ...)
  34. {
  35.         int d , i = 0 , count = 0                                  ;
  36.         char c , * cp                                              ;
  37.         va_list in                                                 ;
  38.         va_start(in , s)                                           ;
  39.         while (s[i]) {
  40.                 if (s[i] == '%') {
  41.                         switch (s[++ i]) {
  42.                                 case 'd': d = va_arg(in , int)     ;
  43.                                           count += int_print(d)    ;
  44.                                           break                    ;
  45.                                 case 'c': c = va_arg(in , int)     ;
  46.                                           putchar(c)               ;
  47.                                           count ++                 ;
  48.                                           break                    ;
  49.                                 case 's': cp = va_arg(in , char *) ;  // 指针 cp 不可以用 s
  50.                                           count += str_print(cp)   ;  // 指针是 cp
  51.                                           break                    ;
  52.                         }
  53.                 } else {
  54.                         putchar(s[i])                              ;
  55.                         count ++                                   ;
  56.                 }
  57.                 i ++                                               ;
  58.         }
  59.         va_end(in)                                                 ;
  60.         return count                                               ;
  61. }

  62. int main()
  63. {
  64.         int d, i                               ;
  65.         char c                                 ;
  66.         char s[512]                            ;

  67.         wdprintf("请输入一个字符串:")         ;
  68.         scanf("%s%*c" , s)                     ;
  69.         i = wdprintf("这个字符串是:%s\n" , s) ;

  70.         wdprintf("请输入一个字符:")           ;
  71.         scanf("%c", & c)                       ;
  72.         i = wdprintf("这个字符是:%c\n" , c)   ;

  73.         wdprintf("请输入一个数字:")           ;
  74.         scanf("%d", & d)                       ;
  75.         i = wdprintf("这个数字是: %d\n",d)    ;
  76.         return 0                               ;
  77. }
复制代码

            以后求助请把代码贴进代码框,否则,光*屁*股丢在外面有些代码会被列为页面描述指令而被过滤掉,比如,常见的 [ i ] 就停不住。

最佳答案

查看完整内容

我也折腾了 3+ 个小时,不过,可变参数函数,我是从 0 开始学的,现炒现卖。 以后求助请把代码贴进代码框,否则,光*屁*股丢在外面有些代码会被列为页面描述指令而被过滤掉,比如,常见的 [ i ] 就停不住。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-21 22:46:03 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-11-22 02:05 编辑

        我也折腾了 3+ 个小时,不过,可变参数函数,我是从 0 开始学的,现炒现卖。
  1. #include <stdio.h>
  2. #include <stdarg.h>

  3. int int_print(int d)
  4. {
  5.         int i = 0, j = 1, ls, count = 0;
  6.         if(d < 0){
  7.                 putchar('-');
  8.                 d = -d;
  9.                 count++;
  10.         }
  11.         ls = d;
  12.         while (ls > 9){
  13.                 j *= 10;
  14.                 ls /= 10;
  15.         }
  16.         while (d > 0){
  17.                 putchar(d / j + '0');
  18.                 d = d % j;
  19.                 j = j / 10;
  20.                 count++;
  21.         }
  22.         return count;
  23. }

  24. int str_print(const char * s)
  25. {
  26.         int i = 0            ;
  27.         while (s[i] != '\0'){
  28.                putchar(s[i]) ;
  29.                i ++          ;
  30.         }
  31.         return i             ;
  32. }

  33. int wdprintf(const char * s , ...)
  34. {
  35.         int d , i = 0 , count = 0                                  ;
  36.         char c , * cp                                              ;
  37.         va_list in                                                 ;
  38.         va_start(in , s)                                           ;
  39.         while (s[i]) {
  40.                 if (s[i] == '%') {
  41.                         switch (s[++ i]) {
  42.                                 case 'd': d = va_arg(in , int)     ;
  43.                                           count += int_print(d)    ;
  44.                                           break                    ;
  45.                                 case 'c': c = va_arg(in , int)     ;
  46.                                           putchar(c)               ;
  47.                                           count ++                 ;
  48.                                           break                    ;
  49.                                 case 's': cp = va_arg(in , char *) ;  // 指针 cp 不可以用 s
  50.                                           count += str_print(cp)   ;  // 指针是 cp
  51.                                           break                    ;
  52.                         }
  53.                 } else {
  54.                         putchar(s[i])                              ;
  55.                         count ++                                   ;
  56.                 }
  57.                 i ++                                               ;
  58.         }
  59.         va_end(in)                                                 ;
  60.         return count                                               ;
  61. }

  62. int main()
  63. {
  64.         int d, i                               ;
  65.         char c                                 ;
  66.         char s[512]                            ;

  67.         wdprintf("请输入一个字符串:")         ;
  68.         scanf("%s%*c" , s)                     ;
  69.         i = wdprintf("这个字符串是:%s\n" , s) ;

  70.         wdprintf("请输入一个字符:")           ;
  71.         scanf("%c", & c)                       ;
  72.         i = wdprintf("这个字符是:%c\n" , c)   ;

  73.         wdprintf("请输入一个数字:")           ;
  74.         scanf("%d", & d)                       ;
  75.         i = wdprintf("这个数字是: %d\n",d)    ;
  76.         return 0                               ;
  77. }
复制代码

            以后求助请把代码贴进代码框,否则,光*屁*股丢在外面有些代码会被列为页面描述指令而被过滤掉,比如,常见的 [ i ] 就停不住。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-11-22 10:30:39 | 显示全部楼层
谢谢看了一下,知道哪里有问题了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 04:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表