简体   繁体   English

如何在不使用 stdlib.h 或 string.h 的情况下将数字转换为字符串?

[英]How do I convert a number to a string without using stdlib.h or string.h?

Here is my exercise:这是我的练习:

Write a function that receives from the user two strings that show two real numbers (possibly negative), And produces a string that contains the integer part of most of the numbers represented by the string.编写一个函数,从用户那里接收两个显示两个实数(可能为负数)的字符串,并生成一个字符串,该字符串包含该字符串表示的大多数数字的整数部分。 For example, Given the string 2356.12 and the string 243.5 the program must create the string 2112.例如,给定字符串2356.12和字符串243.5 ,程序必须创建字符串2112.

void addstrings(char *snum1, char *snum2, char *res);

I need to create this function, and two more helper functions我需要创建这个函数,以及另外两个辅助函数

//  a function that receives a string that represents an actual number and returns the above number (that i succeed)
double stod(char *snum);
// A function that receives an integer and produces a string representing the number
void itos(long int num, char *snum);

that are used in the addstrings function.addstrings函数中使用。


Here is my attempt so far:到目前为止,这是我的尝试:

void addstring(char *snum1, char *snum2, char *res) {
    stod(snum1);
    stod(snum2);

    *res = (long int)(*snum1 + *snum2);
}

double stod(char *snum) {
    int res = 0;
    for (int i = 0; snum[i] != '\0'; i++)
        res = res * 10 + snum[i] - '0';

    // return result.
    return res;
}

double itos(long int num, char *snum) {
    int i = 0;
    while (num) {
        snum[i++] = (num % 10) + '0';
        num = num / 10;
    }

    return (double)num;
}

int main() {
    char arr1[SIZE + 1];
    char arr2[SIZE + 1];
    char *res = NULL;
    int temp;

    gets(arr1);
    gets(arr2);

    addstring(arr1, arr2, res);

    printf("%d", addstring);

    return 0;
}

What am I missing in the addstring function?我在addstring函数中缺少什么?

There are many problems in your question and your code:您的问题和代码中有很多问题:

  • Write a function that receives from the user two strings that show two real numbers (possibly negative), this part is unclear: do you mean the function should read user input and convert it to double values?编写一个函数,从用户接收两个显示两个实数(可能为负数)的字符串,这部分不清楚:您的意思是该函数应该读取用户输入并将其转换为double值吗?

  • And produces a string that contains the integer part of most of the numbers represented by the string This is also unclear.并产生一个字符串,其中包含该字符串表示的大多数数字的整数部分。这也不清楚。 From the next phrase, it appears you should output the integral part of the difference of the double values.从下一个短语看来,您应该输出double值差的整数部分。

  • double stod(char *snum); could be implemented using sscanf() .可以使用sscanf()来实现。 Your version only works for positive integral values.您的版本仅适用于正整数值。

  • void itos(long int num, char *snum); could be implemented simply using sprintf() .可以简单地使用sprintf()来实现。

  • in addstrings you do not store the return values of stod() and instead you add the values of the first characters of the strings and you store that as the first character of the output.addstrings中,您不存储stod()的返回值,而是添加字符串的第一个字符的值,并将其存储为输出的第一个字符。

  • you should never use gets()你不应该使用gets()

  • printf("%d", addstring); is incorrect: you pass the address of function addstring instead of its return value.不正确:您传递函数addstring的地址而不是其返回值。

Here is a corrected version:这是一个更正的版本:

#include <math.h>
#include <stdio.h>

double stod(char *snum) {
    double d;
    if (sscanf(snum, "%lf", &d) == 1)
        return d;
    else
        return 0;
}

void itos(long int num, char *snum) {
    sprintf(snum, "%ld.", num);
}

void addstring(char *snum1, char *snum2, char *res) {
    double d1 = stod(snum1);
    double d2 = stod(snum2);
    long int diff = (long int)fabs(d1 - d2);
    itos(diff, res);
}

int main() {
    char arr1[32];
    char arr2[32];
    char res[32];

    if (scanf("%31s%31s", arr1, arr2) == 2) {
        addstring(arr1, arr2, res);
        printf("%s\n", res);
    }
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法排序没有。 字符 arrays 按字母顺序排列,不使用#include<string.h> 或#include<stdlib.h> ?</stdlib.h></string.h> - Is there a way to sort a no. of character arrays in alphabetical order without using the #include<string.h> or #include<stdlib.h>? string.h和stdlib.h函数给我错误:未定义引用“…” - string.h and stdlib.h functions give me the error: undefined reference to '…' 为什么memset,memchr等内存函数在string.h中,而在stdlib.h中却没有其他mem函数? - Why memory functions such as memset, memchr… are in string.h, but not in stdlib.h with another mem functions? 如何在不使用string.h的情况下连接无限数量的字符串 - how to concatenate an indefinite number of strings without using string.h 为什么stdio.h、stdlib.h、string.h声明size_t、NULL等而不是#include<stddef.h> ?</stddef.h> - Why stdio.h, stdlib.h, string.h declare size_t, NULL, etc. instead of #include <stddef.h>? 如何在不使用stdlib.h的情况下执行此代码 - How would I get this code to be executed without using stdlib.h 我如何使用string.h将字符合并到C中的字符串? - How do I merge chars to string in C using string.h? 不使用string.h库从字符串中删除字符 - remove characters from string without using string.h library 在不使用string.h库的情况下在字符串中搜索“好” - Searching a string for 'good' without using the string.h library 为什么我们要包含 stdlib.h? - Why do we include stdlib.h?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM