简体   繁体   English

有人可以帮我用我的C代码吗

[英]can someone help me with my c code

Hello so i am creating a program that reads from a file and outputs each category of the things in the text in sorted for example i want it to output like this : 您好,所以我正在创建一个程序,该程序从文件中读取并按排序输出文本中各个类别的东西,例如,我希望它输出如下:

Company name: air france Date of creation: 06281957 Flight number: AT6801 Incoming city: london Arrival city: paris Amount of fuel liters left: 380 Plane category: B777

this is the input : 这是输入:

air qatar06281957AT680londonmadrid380B777 turkish airlines05201933TK1298istanbulmadrid250A380 lufthansa01061953LH29frankfurtmadrid75B747 air canada06281957AT7245ammanmadrid120A320 turkish airlines05201933TK1266dohamadrid522A320 air france10071933AF123parismadrid105B777 -1

The code: 编码:

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

int main()
{
    FILE *inp,*outp;
    int i,j,c=0,l,c2=0,c3=0;int c4=0;int c5=0,c6=0,k,m,c7=0,flag=0;int c8=0,c9=0,c10=0,flag2=0,n,c11=0,c12=0,c13=0,c14=0,p=0,c15=0,c16=0,t,t1,t2,t3,t4,t5,t6,t7,s;
    char ultimate_array[600];char plane[100][6];char date[600][6];char nflight[600][6];char  destination[100][6];char fuel [100][6];char planetype [100][6];

    inp=fopen("input.txt","r");

    for(i=0;!feof(inp);i++)
    {
        fscanf(inp,"%c",&ultimate_array[i]);
    }

    s=strlen(ultimate_array);

    for(i=0;i<s;i++)
    {
        printf("%c",ultimate_array[i]);
    }

    printf("\n\n\n\n\n\n\n\n\n\n\n\n");
    for(t=0;t<6;t++)
    {
        for(j=0;j<200;j++)
        {
            c++;
            if(isdigit(ultimate_array[j]))
            {
                c3=c;
                while(c>=0)
                {
                    plane[t][c]=ultimate_array[j];
                    c--; j--;
                }
                break;
            }
        }

        for(k=0;k<200;k++)
        {
            c2++;
            if(isupper(ultimate_array[k]))
            {
                c5=c2; c2=c2-c3; c9=c2;
                while(c2>=0)
                {
                    date[t][c2]=ultimate_array[k];
                    k--; c2--;
                }
                break;
            }
        }

        for(l=c3;l<200;l++)
        {
            c4++;
            if(islower(ultimate_array[l]))
            {
                c4=c4+c3-c5;
                while(c4>=0)
                {
                    while(flag==0)
                    {
                        c8=c4; c6=c4+c5; flag=1;
                    }

                    nflight[t][c4]=ultimate_array[l];
                    c4--; l--;
                }
                break;
            }
        }

        c10=c9+c8;

        for(m=c6;m<200;m++)
        {
            c7++;
            if(isdigit(ultimate_array[m]))
            {
                c7--;m--;

                while(c7>=0)
                {
                    while(flag2==0)
                    {
                        c13=c7; flag2=1;
                    }
                    destination[t][c7]=ultimate_array[m];
                    c7--; m--;
                }
                break;
            }
        }

        for(n=c14;n<200;n++)
        {
            c11++; c14=c3+c13+c10;
            if(isupper(ultimate_array[n]))
            {
                c12=c11; c11=c11-1;
                while(c11>=0)
                {
                    fuel[t][c11]=ultimate_array[n];
                    c11--; n--;
                }
                break;
            }
        }

        c15=c14+c12;

        for(p=c15;p<200;p++)
        {
            c16++;
            if(ultimate_array[p]=='\n')
            {
                while(c16>=0)
                {
                    planetype[t][c16]=ultimate_array[p];
                    c16--; p--;
                }
                break;
            }
        }

        for(t1=0;t1<20;t1++)
        {
            printf(" %c",plane[t1][t]);
        }

        printf("\n");

        for(t2=0;t2<100;t2++)
        {
            printf("%c",destination[t2][t]);
        }

        printf("\n");

        for(t3=0;t3<100;t3++)
        {
            printf("%c",date[t3][t]);
        }

        printf("\n");

        for(t4=0;t4<100;t4++)
        {
            printf(" %c",fuel[t4][t]);
        }

        printf("\n");

        for(t5=0;t5<100;t5++)
        {
            printf(" %c",nflight[t5][t]);
        }
        printf("\n");
        for(t6=0;t6<5;t6++)
        {
            printf(" %c",planetype[t][t6]);
        }
    }

    return 0;
}

I have been struggling with a lot of things but I finally managed to separate every category to a different array, however , when I tried to do it for 2D array I always get garbage can someone point out what the mistake I did ? 我一直在做很多事情,但是最终我设法将每个类别都分离到一个不同的数组中,但是,当我尝试对2D数组进行处理时,我总是很垃圾,有人可以指出我的错误吗?

we couldn't read in the input file and write it in a 2D array so the concept that i have used in this code i search in the array until i find something that would help me separate the word from the others such as 06281957 in the input i used isdigit to identify which index has it then i copy the previous characters into a new array this trick seemed to work for a 1d array however when i tried to scan it to 2d it stopped working and only random chars seemed to appeari wanted to print the whole 2d array of each category but i failed to do so, for the variables i used them as checkpoints for the next check to put the things i want in a new array. 我们无法读取输入文件并将其写入2D数组,因此我在该代码中使用的概念一直在数组中搜索,直到找到可以帮助我将单词与其他单词分开的东西(例如06281957)我使用isdigit输入来标识具有哪个索引,然后将先前的字符复制到新数组中,此技巧似乎适用于1d数组,但是当我尝试将其扫描到2d时,它停止工作,并且似乎只有随机字符出现了打印每个类别的整个2d数组,但是我没有这样做,因为变量将它们用作下一个检查的检查点,以将我想要的东西放到新数组中。 For minimizing the code i cant seem to find another solution to do so i know that my code is very big but if someone has a better idea it would be awesome. 为了最小化代码,我似乎找不到其他解决方案,因此我知道我的代码很大,但是如果有人有更好的主意,那就太好了。

One thing that will simplify your code is using scansets . 可以简化代码的一件事是使用扫描集 It will make it much easier to extract and separate digits from non-digits, lower from upper case. 这将使提取数字和从非数字中分离出数字变得更加容易,而大写的数字则要小得多。 Fixed width specifiers is also a concept you should read into. 固定宽度说明符也是您应该阅读的概念。 To see how many characters where extracted, use %n described in previous link. 要查看提取了多少个字符,请使用上一链接中所述的%n Examples: 例子:

  /* ... */
  inp=fopen("input.txt","r");
  /* your code... */
  for(i=0;!feof(inp);i++)
  {
    fscanf(inp,"%c",&ultimate_array[i]);
  }
  s=strlen(ultimate_array);
  for(i=0;i<s;i++)
  {
    printf("%c",ultimate_array[i]);
  }

  /* ...can be replaced by something similar to this (untested) */
  fscanf(inp, "%599s%n", ultimate_array, &s); /* read up to 599 characters, put number of read characters in s */
  ultimate_array[s]=0; /* set null terminator */
  char a[10]; sprintf(a, "%%%ds", s); /* create print format, e.g. "%123s" */
  printf(a, ultimate_array) /* print 's' number of characters */

Make sure to take it slow as the lines can be hard to read initially. 确保慢慢来,因为一开始可能很难阅读这些行。 As you become more familiar with it I'm sure you will find scanf does "exactly what is needed" using no loops or conditions at all. 随着您对它的熟悉,我相信您会发现scanf完全不使用循环或条件就可以“确实需要”。

Don't use feof . 不要使用feof Instead check the return value from scanf . 而是检查scanf的返回值。 If you try extract a single "something" from file using scanf and it returns 0 (zero arguments were filled), you are done reading the file. 如果您尝试使用scanf从文件中提取单个“内容”,并且返回0(填充了零个参数),则读取文件已完成。

Making 2D arrays is almost always wrong. 制作2D阵列几乎总是错误的。 Group your variables in structures: 将变量分组为结构:

typedef struct flights_t {
  char plane[100];
  char date[100];
  /* ... */
} flights[6]; unsigned int nrOfFlights = 0;

int c[16];
int t[7];

Make symbolic constants instead of litering the code with copies of 20 , 100 and 6 all over for improved readability and maintainance. 让符号常量,而不是litering与复制码201006遍提高可读性和维修。 You will also recieve much better help from forums such as SO. 您还将从诸如SO之类的论坛中获得更好的帮助。

Your posted code is sadly beyond saving without proper description of what each code section is supposed to do. 可悲的是,如果没有正确描述每个代码段的功能,那么所发布的代码将无法保存。 Hopefully this "answer" will get you going making better code. 希望这个“答案”能使您编写更好的代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM