字符串数组的总和如何?

时间:2016-11-30 15:04:43

标签: c arrays string sum

我需要编写一个可以计算字符串的程序,该字符串由权重及其单位组成,例如:" 10,kg,500,g,0.5,t"到权重的总和。不过,它必须首先将不同的重量转换成千克。这是我到目前为止转换克数但它不起作用。我正在使用C.

double total_weight_kg(String s) {         
    Array a = sa_of_string(s);
    Array w = da_of_string(s);
    double gram = 0;
    int n = a_length(a);     
    for (int i = 0; i < n; i++) {       
        if (i == 'g') {
            gram += get(double, w, i = i - 1);
        }
    }
    return gram * 0.001;
 }

2 个答案:

答案 0 :(得分:1)

如果要修复字符串格式,可以使用sscanf()从中读取值。

double total_weight_kg(String s) {
    double        kg = 0.0;
    double        g  = 0.0;
    double        t  = 0.0;

    //Assuming "<kg_val>, kg, <g_val>, g, <t_val>, t" format.
    sscanf("%lf, kg, %lf, g, %lf, t", &kg, &g, &t);

    kg += (g/1000.0) + (t*1000.0);

    return kg;
}

<强> UPDATE1: 我看到了this conversion。在我的答案中要清楚我假设“tonne”而不是“ton”。对于“ton”,您可以使用此程序。

double total_weight_kg(String s) {
    double        kg = 0.0;
    double        g  = 0.0;
    double        t  = 0.0;

    //Assuming "<kg_val>, kg, <g_val>, g, <t_val>, t" format.
    sscanf("%lf, kg, %lf, g, %lf, t", &kg, &g, &t);

    kg += (g/1000.0) + (t*907.185);

    return kg;
}

<强> UPDATE2 如果我必须在“9,kg,8,g,0.2,t,733,g,1,t,17.5,kg”或“0.1,g,9.7,kg”等字符串中添加重量,我会使用以下计划。

#include <stdio.h>
#include <string.h>

double total_weight_kg(char* s) 
{
    double        kg             = 0.0;
    double        wieght_val     = 0.0; //If you want to use array, make this an array, store value in diff indices and add them at the end.
    char          wieght_unit[4] = ""; //Max string is "kg,".
    char          *str           = s;


    while(0 < strlen(str))
    {
        sscanf(str, "%lf, %s,", &wieght_val, wieght_unit);
        printf("Read: %lf, %s\n", wieght_val, wieght_unit);

        if(!strcmp(wieght_unit, "kg,") || !strcmp(wieght_unit, "kg"))
            kg += wieght_val;
        else if(!strcmp(wieght_unit, "g,") || !strcmp(wieght_unit, "g"))
            kg += wieght_val / 1000.0;
        else if(!strcmp(wieght_unit, "t,") || !strcmp(wieght_unit, "t"))
            kg += wieght_val * 1000.0; //OR 907.185

        //Update string pointer.
        str = strstr(str, wieght_unit);
        str += strlen(wieght_unit);

        //Resetting values.
        strcpy( wieght_unit, "");
        wieght_val = 0.0;
    }

    return kg;
}

int main() {

    printf("Weight is %lf kg\n", total_weight_kg("9, kg, 8, g, 0.2, t, 733, g, 1, t, 17.5, kg"));

    return 1;
} 

输出:

Read: 9.000000, kg,                                                                                                                             
Read: 8.000000, g,                                                                                                                              
Read: 0.200000, t,                                                                                                                              
Read: 733.000000, g,                                                                                                                            
Read: 1.000000, t,                                                                                                                              
Read: 17.500000, kg                                                                                                                             
Weight is 1227.241000 kg 

答案 1 :(得分:0)

查看the man of used library,类似以下代码的内容应该有效。

for (int i = 0; i < n; i++) 
{   
   if ((i%2) != 0)
   {
      if (strcmp(sa_get(w,i), "g") == 0) 
      {
         gram += get(double, w, i - 1);
      }
   } 
}

代码未经过测试