首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用C中的函数接受数据类型并将其存储在定义的结构中?

如何使用C中的函数接受数据类型并将其存储在定义的结构中?
EN

Stack Overflow用户
提问于 2013-08-09 21:26:38
回答 3查看 184关注 0票数 0

I有问题,返回我定义的结构,函数scan_sci应该从输入源获取一个字符串,表示一个正数的科学表示法,并将其分解为组件,以便存储在scinot_t结构中。示例输入为0.25000e4

代码语言:javascript
复制
  #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    typedef struct{
        double mantissa;
        int exp;
    }sci_not_t;

    int scan_sci (sci_not_t *c);

    int main(void){

        sci_not_t inp1, inp2;

        printf("Enter the mantissa in the range [0.1, 1.0) then its exponent: ");
        scan_sci(&inp1);
        printf("Enter second number with same specifications as above: ");
        scan_sci(&inp2);






      system("PAUSE");  
      return 0;
    }

    int scan_sci (sci_not_t *c){

        int status;
        double a;
        int b;

        status = scanf("%lf %d", &c->mantissa, &c->exp);

        a = c->mantissa;
        b = c->exp;
        *c = pow(a,b);

        if (status == 2 && (c->mantissa >= 0.1 && c->mantissa < 1.0) ){
            status = 1;
        }else{
            printf("You did not enter the mantissa in the correct range \n");
            status = 0;
        }


        return (status);
    }

    sci_not_t sum(sci_not_t c1, sci_not_t c2){

        return c1 + c2;
    }

    sci_not_t product(sci_not_t c1, sci_not_t c2){

        return c1 * c2;
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-09 22:32:37

下面是一些从控制台读取并将值放入scinot_t的代码,没有任何验证。

它使用scanf将整个字符串读取为两部分。第一部分%[^e]读取任何不是e的char。然后,它读取'e‘,最后是指数。

尾数首先被读取为字符串,然后使用sscanf对其进行重解析。

代码语言:javascript
复制
#include <stdlib.h>

typedef struct {
    double mantissa;
    int exp;
}scinot_t;

void scan_sci(scinot_t* s);

int main(void){
  scinot_t d;

  printf("Enter a value: ");
  scan_sci(&d);

  printf("Mantissa is %lf Exponent is %d\n", d.mantissa, d.exp);
  return 0;
}

void scan_sci (scinot_t *sn){
  char inp1[20];
  scanf("%[^e]e%d", inp1, &sn->exp);
  sscanf(inp1, "%lf", &sn->mantissa);
  return;
}

要测试尾数范围更新main,如下所示:

代码语言:javascript
复制
int main(void){
  scinot_t d;

  printf("Enter a value: ");
  for (;;) {
    scan_sci(&d);
    if (d.mantissa>=0.1 && d.mantissa<=1.0)
      break;
    printf("You did not enter the mantissa in the correct range\nEnter a value: ");
  }

  printf("Mantissa is %lf Exponent is %d\n", d.mantissa, d.exp);
  return 0;
}
票数 0
EN

Stack Overflow用户

发布于 2013-08-09 21:33:38

这里有很多问题。首先,您的参数对于scan_sci是错误的。您不是要传递指向您声明的结构的指针,而是传递一个字符数组。你的声明如下:

代码语言:javascript
复制
scinot_t scan_sci( scinot_t *collection );

若要符合传递指向结构的指针的要求,您需要将声明更改为以下内容。注意,非常糟糕的实践和错误倾向于返回指向在堆栈上声明的变量的指针,这就是为什么我们要在scan_sci中改变结构。

代码语言:javascript
复制
void scan_sci( scinot_t *collection );

现在,您需要在调用该函数并使用&运算符传递其内存地址之前创建该结构。

票数 1
EN

Stack Overflow用户

发布于 2013-08-09 22:32:30

你有几个问题,这应该能解决它们。首先,scan_sci现在接受一个指针来返回值,在成功时返回0,在失败时返回非0(没有捕获所有的失败模式)。除此之外,scanf是贪婪的,而e是浮点字符串表示的合法部分,因此,您的原始版本只解析了一个参数,而该双正在吃掉整个输入字符串--例如"123e123“--它们都进入了double。为了解决这个问题,基于e上的拆分,将解析分为两个独立的分析。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    double mantissa;
    int exp;
}scinot_t;

int scan_sci(scinot_t * tl){
    char buf[80]; 
    fgets(buf, 80, stdin);
    char * e = strchr(buf, 'e');
    if (!e) { return -1; }
    // Split buf up to act like 2 strings
    // e.g. 123.01e321 becomes:
    // 123.01 and 321 with e pointing to 321
    *e = '\0';
    e++;
    // Not checking any errors
    tl->mantissa = strtod(buf, NULL);
    tl->exp = strtol(e, NULL, 10);
    return 0;
}

int main(void){
    printf("Enter a value\n");
    // Init to 0 
    scinot_t sav = {0};
    int ret = scan_sci(&sav);
    if (ret) {
        printf("error, 'e' expected\n");
    }
    else {
        printf("%lfe%d", sav.mantissa, sav.exp);
    }

    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18155902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档