用数组排序结构,输出错误

时间:2018-01-30 12:56:28

标签: c arrays structure qsort

  1. 程序的输出应该是这样的,但每个项目按“nummer”
  2. 排序
      

    nummer:30871
      namn:永恒的黑暗
      pris:39.799999
      volym:330.000000
      typ:Porter och stout
      stil:帝国搬运工och stout
      forpackning:Flaska
      土地:Sverige
      产品:Beerbliotek AB
      alkoholhalt:11.000000(这是一个项目,有10个元素)

    1. 我现在得到的输出有点乱和错误

        

      nummer:1966644114
        namn:£   pris:0.000000
        volym:0.000000
        典型值:
        STIL:
        forpackning:
        土地:
        PRODUCENT:
        alkoholhalt:0.000000

    2. 我的猜测是class URLValidator { public static boolean urlValidator(String url) { try { new URL(url).toURI(); return true; } catch (URISyntaxException exception) { return false; } catch (MalformedURLException exception) { return false; } } public static void main(String[] args) { String url = "https://media-cdn.tripadvisor.com/media/po/03/d8/a8/70/zdel-s.jpg"; if (urlValidator(url)) System.out.print("The given URL: " + url + " , contain image."); else System.out.print("The given URL: " + url + " , is not contain image."); } } 错了

    3. “varor.csv”行中的一个示例每个元素都以逗号分隔:

    4. 30854,Stigbergets West Coast India Pale Ale,32.90,330.00,Ale brittisk-amerikansk stil,India pale ale(IPA),Flaska,Sverige,Stigbergets Bryggeri,6.50

      NUM_ITEMS

      ...

      #define max(a,b) ((a)>(b)?(a):(b))
      #define min(a,b) ((b)>(a)?(a):(b))
      #define NUM_ITEMS (sizeof(items) / sizeof(items[0])) 
      
      char *oneline, *tok;
          char envara[512];
          char delim[] = ",";
          FILE *fp;
          int i;
      
      struct vara
      {
          int nummer;
          char namn[100];
          float pris;
          float volym;
          char typ[100];
          char stil[100];
          char forpackning[20];
          char land[20];
          char producent[50];
          float alkoholhalt;
      } items[100];
      
      
          if ((fp = fopen("varor.csv", "r")) == NULL)
          {
              fprintf(stderr, "Filen varor.csv gick inte att öppna\n");
              exit(-1);
          }
          for (i = 0; i < 100 && fgets(envara, 512, fp); i++) {
              envara[strcspn(envara, "\n")] = '\0';
              oneline = strdup(envara);
      

      ...

              tok = strtok(oneline, delim);
              items[i].nummer = atoi(tok);
              tok = strtok(NULL, delim);
              strncpy(items[i].namn, tok, sizeof items[i].namn); items[i].namn[(sizeof items[i].namn) - 1] = 0;
              tok = strtok(NULL, delim);
              items[i].pris = atof(tok);
              tok = strtok(NULL, delim);
              items[i].volym = atof(tok);
              tok = strtok(NULL, delim);
              strncpy(items[i].typ, tok, sizeof items[i].typ); items[i].typ[(sizeof items[i].typ) - 1] = 0;
              tok = strtok(NULL, delim);
              strncpy(items[i].stil, tok, sizeof items[i].stil); items[i].stil[(sizeof items[i].stil) - 1] = 0;
              tok = strtok(NULL, delim);
              strncpy(items[i].forpackning, tok, sizeof items[i].forpackning); items[i].forpackning[(sizeof items[i].forpackning) - 1] = 0;
              tok = strtok(NULL, delim);
              strncpy(items[i].land, tok, sizeof items[i].land); items[i].land[(sizeof items[i].land) - 1] = 0;
              tok = strtok(NULL, delim);
              strncpy(items[i].producent, tok, sizeof items[i].producent); items[i].producent[(sizeof items[i].producent) - 1] = 0;
              tok = strtok(NULL, delim);
              items[i].alkoholhalt = atof(tok);
      
      int nummer_sortering(const void* n1, const void* n2)
      {
          const struct vara *item1 = n1;
          const struct vara *item2 = n2;
      
          return item1->nummer - item2->nummer;
      }
      

0 个答案:

没有答案