如何在c ++中输入并检查文本文件,然后输出特定信息

时间:2018-01-23 13:00:49

标签: c++ project

我正在研究我的学校项目,该项目主要关注周期表和碳氢化合物。我试着做一些不同的事情,所以我为每个函数制作了自定义头文件,并将它们包含在主程序中。我现在也想使用文件处理概念。所以我决定将所有元素名称和符号及其原子序数放在文本文件中。我尝试在用户输入atomic no的帮助下访问它们,并决定在文本文件中为它设置模式,前3个空格将是数字或空格,然后是空格,然后是元素名称,如:

1(space)(space)(space)Hydrogen(H)

:

20(space)(space)Calcium(Ca)

:



118(space)Oganesson(Og)

这是我的头文件与此特定程序有关

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<math.h>
#include<fstream.h>
#include<string.h>

void ptablefunc()
{int a_no, s1, s2, p2, s3, p3, s4, d3, p4, s5, d4, p5, s6, f4, d5, p6, s7, f5, d6, p7;
int no[30];
char name[15];
textcolor(GREEN);
textbackground(BLACK);
clrscr();
randomize();

int k,l,m,n,o,p,q;

cout<<"\t\t\t       PERIODIC TABLE\n";
cout<<"\n     1___2___3___4___5___6___7___8___9__10__11__12__13__14__15__16__17__18_____\n";
cout<<" 1 | H |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   | He|\n";
cout<<"   ---------------------------------------------------------------------------\n";
cout<<" 2 | li| Be|   |   |   |   |   |   |   |   |   |   |  B| C | N | O | F | Ne|\n";
cout<<"   ---------------------------------------------------------------------------\n";
cout<<" 3 | Na| Mg|   |   |   |   |   |   |   |   |   |   | Al| Si| P | S | Cl| Ar|\n";
cout<<"   --------------------------------------------------------------------------\n";
cout<<" 4 | K | Ca| Sc| Ti| V | Cr| Mn| Fe| Co| Ni| Cu| Zn| Ga| Ge| As| Se| Br| Kr|\n";
cout<<"   ---------------------------------------------------------------------------\n";
cout<<" 5 | Rb| Sr| Y | Zr| Nb| Mo| Tc| Ru| Rh| Pd| Ag| Cd| In| Sn| Sb| Te| I | Xe|\n";
cout<<"   ---------------------------------------------------------------------------\n";
cout<<" 6 | Cs| Ba| * | Hf| Ta| W | Re| Os| Ir| Pt| Au| Hg| Tl| Pb| Bi| Po| At| Rn|\n";
cout<<"   ---------------------------------------------------------------------------\n";
cout<<" 7 | Fr| Ra| * | Rf| Db| Sg| Bh| Hs| Mt| Ds| Rg| Cn| Nh| Fl| Mc| Lv| Ts| Og|\n";
cout<<"   ---------------------------------------------------------------------------";
cout<<" \n * (Lanthanoid series) La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu\n";
cout<<" ---------------------------------------------------------------------------\n";
cout<<" *  (Actinoid series)  Ac Th Pa  U Np Pu Am Cm Bk Cf Es Fm Md No Lr\n";
cout<<"\n";
cout<<"\tEnter the atomic number of the element you want to search for:";
cout<<"\n\n\t\t\t\t    ->";
 cin>>a_no;
 clrscr();
 textbackground(BLACK);
 textcolor(RED+BLINK);
 clrscr();

  for(int i=0;i<12;i++)
    cout<<endl;

   cout<<"\t\t\t\t   LOADING...";

     delay(2000);
      textcolor(GREEN);
      clrscr();
      cout<<"\n\n";
      cout<<"\t\t\t\t   ";
//----------------------------------------------------------------------------
ifstream element;
element.open("table.txt",ios::in);

while(!element.eof())
{
cout<<no<<endl;
}
//--------------------------------------------------------------------------------

if(a_no>=1&&a_no<=118) //to print the atomic number of the given element
{
if (a_no<119)
cout<<endl<<"\n\nAtomic Number of element is "<<a_no<<endl<<endl;
}
delay(500);
//-----------------------------------------------------------------------------------

if(a_no==1)//to find the atomic mass of the given element
 cout<<"Atomic mass of element is "<<a_no<<endl<<endl;
if (a_no>1 && a_no<22)
 cout<<"Atomic mass of element is "<<2*a_no+random(2)<<endl<<endl;
else if (a_no>22 && a_no<32)
 cout<<"Atomic mass of element is "<<2*a_no+random(3)+2<<endl<<endl;
else if (a_no>31 && a_no<38)
 cout<<"Atomic mass of element is "<<2*a_no+random(5)+5<<endl<<endl;
else if (a_no>37 && a_no<46)
 cout<<"Atomic mass of element is "<<2*a_no+random(5)+8<<endl<<endl;
else if (a_no>45 && a_no<58)
 cout<<"Atomic mass of element is "<<2*a_no+random(8)+12<<endl<<endl;
else if (a_no>57 && a_no<72)
 cout<<"Atomic mass of element is "<<(245*a_no)/100<<endl<<endl;
else if (a_no>71 && a_no<81)
 cout<<"Atomic mass of element is "<<(25*a_no)/10<<endl<<endl;
else if (a_no>80 && a_no<103)
 cout<<"Atomic mass of element is "<<(256*a_no)/100<<endl<<endl;
else if (a_no>102 && a_no<113)
 cout<<"Atomic mass of element is "<<(254*a_no)/100<<endl<<endl;
else if (a_no>112 && a_no<119)
 cout<<"Atomic mass of element is "<<(251*a_no)/100<<endl<<endl;
delay(500);
//---------------------------------------------------------------------------------------------------------

if (a_no==1)  //the shell configuration of the given element
 {k=1;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<endl<<endl;}
else if (a_no==2)
 {k=2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<endl<<endl; }
else if (a_no>2 && a_no<11)
 {k=2;
 l=a_no-2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<endl<<endl;}
else if (a_no>=11 && a_no<19)
 {k=2;
 l=8;
 m=a_no-10;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<endl<<endl;}
else if (a_no>=19 && a_no<21)
 {k=2;
 l=8;
 m=8;
 n=a_no-18;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<endl<<endl;}
else if (a_no>=21 && a_no<29)
 {k=2;
 l=8;
 n=2;
 m=a_no-12;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<endl<<endl;}
else if ((a_no>=29 && a_no<37) ||(a_no==46))
 {k=2;
 l=8;
 m=18;
 n=a_no-28;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<endl<<endl;}
else if (a_no==37 || a_no==38)
 {k=2;
 l=8;
 m=18;
 n=8;
 o=a_no-36;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<endl<<endl;}
else if (a_no==39 || a_no==40 || a_no==43)
 {k=2;
 l=8;
 m=18;
 o=2;
 n=a_no-30;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<endl<<endl;}
else if(a_no==41 || a_no==42 || a_no==44 || a_no==45)
 {k=2;
 l=8;
 m=18;
 o=1;
 n=a_no-29;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<endl<<endl;}
else if(a_no>=47 && a_no<55)
 {k=2;
 l=8;
 m=18;
 n=18;
 o=a_no-46;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<endl<<endl;}
else if(a_no==55 || a_no==56)
 {k=2;
 l=8;
 m=18;
 n=18;
 o=8;
 p=a_no-54;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<endl<<endl;}
else if(a_no==57 || a_no==58 || a_no==64 || a_no==71)
 {k=2;
 l=8;
 m=18;
 n=a_no-39;
 o=9;
 p=2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<endl<<endl;}
else if((a_no>=59 && a_no<64) || (a_no>=65 && a_no<71))
 {k=2;
 l=8;
 m=18;
 n=a_no-38;
 o=8;
 p=2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<endl<<endl;}
else if(a_no>=72 && a_no<81)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=a_no-62;
 p=2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<endl<<endl;}
else if(a_no>=81 && a_no<87)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=18;
 p=a_no-78;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<endl<<endl;}
else if(a_no==87 || a_no==88)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=18;
 p=8;
 q=a_no-86;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<" Q-"<<q<<endl<<endl;}
else if((a_no>=97 && a_no<104) || a_no==94 || a_no==95)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=a_no-70;
 p=8;
 q=2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<" Q-"<<q<<endl<<endl;}
else if(a_no==90)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=18;
 p=10;
 q=2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<" Q-"<<q<<endl<<endl;}
else if((a_no>=91 && a_no<94) || a_no==89)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=a_no-71;
 p=9;
 q=2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<" Q-"<<q<<endl<<endl;}
else if((a_no>=104 && a_no<110) || a_no==112)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=32;
 p=a_no-94;
 q=2;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<" Q-"<<q<<endl<<endl;}
else if(a_no==110 || a_no==111)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=32;
 p=a_no-93;
 q=1;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<" Q-"<<q<<endl<<endl;}
else if(a_no>=113 && a_no<119)
 {k=2;
 l=8;
 m=18;
 n=32;
 o=32;
 p=18;
 q=a_no-110;
 cout<<"The shell configuration of the element is -> "<<"K-"<<k<<" L-"<<l<<" M-"<<m<<" N-"<<n<<" O-"<<o<<" P-"<<p<<" Q-"<<q<<endl<<endl;}
delay(500);
//------------------------------------------------------------------------------------------------------------------------------------------
     //to find the electronic configuration of the element
cout<<"The electronic configuration of the element is : \n";
         if (a_no<=2)
      s1=a_no;
     else if (a_no>2)
        s1=2;

     if (a_no<=4 && a_no>=2)
      s2=a_no-2;
     else if (a_no<2)
      s2=0;
     else
      if (a_no>4)
        s2=2;

     if (a_no<=10 && a_no>=4)
        p2=(a_no-4);
     else if (a_no<4)
        p2=0;
     else
        if (a_no>10)
        p2=6 ;

     if (a_no<=12 && a_no>=10)
        s3=a_no-10;
     else if (a_no<10)
        s3=0;
     else
        if (a_no>12)
        s3=2;

     if (a_no<=18 && a_no>=12)
        p3=a_no-12;
     else if (a_no<12)
        p3=0;
     else
        if (a_no>18)
        p3=6;

     if (a_no<=20 && a_no>=18)
        s4=a_no-18;
     else if (a_no<18)
         s4=0;
     else
        if(a_no>20)
        s4=2;

     if (a_no<=30 && a_no>=20)
        d3=a_no-20;
     else if (a_no<20)
        d3=0;
     else
        if (a_no>30)
        d3=10;

     if (a_no<=36 && a_no>=30)
        p4=a_no-30;
     else if (a_no<30)
        p4=0;
     else
        if(a_no>36)
        p4=6;

     if (a_no<=38 && a_no>=36)
        s5=a_no-36;
     else if (a_no<36 || a_no==46 )
        s5=0;
     else
        if((a_no>38 && a_no<41)|| a_no>47 || a_no==43)
        s5=2;
     else
        if((a_no>40 && a_no<46)|| a_no==48)
        s5=1;

     if ((a_no<=40 && a_no>=38)||a_no==43)
        d4=a_no-38;
     else if ((a_no>=41 && a_no<=45)||a_no==47)
        d4=a_no-37;
     else if (a_no<38)
        d4=0;
     else
        d4=10;

     if (a_no<=54 && a_no>=48)
        p5=a_no-48;
     else if (a_no<48)
        p5=0;
     else
        if (a_no>54)
        p5=6;

     if (a_no<=56 && a_no>=54)
        s6=a_no-54;
     else if (a_no<54)
        s6=0;
     else
        if (a_no>56)
        s6=2;
     if (a_no<=70 &&  a_no>=56)
        f4=a_no-56;
     else if (a_no<56)
        f4=0;
     else
        if(a_no>70)
        f4=14;

     if (a_no<=80 && a_no>=70)
        d5=a_no-70;
     else if (a_no<70)
        d5=0;
     else
        if (a_no>80)
        d5=10;

     if (a_no<=86 && a_no>=80)
        p6=a_no-80;
     else if (a_no<80)
        p6=0;
     else
        p6=6;

     if (a_no<=88 && a_no>=86)
        s7=a_no-86;
     else if (a_no<86)
        s7=0;
     else
        if(a_no>88)
        s7=2;

     if(a_no<=102 && a_no>=88)
        f5=a_no-88;
     else if (a_no<88)
        f5=0;
     else
        if (a_no>102)
        f5=14;

     if (a_no<=112 && a_no>=102)
        d6=a_no-102;
     else if (a_no<102)
        d6=0;
     else
         if(a_no>112)
         d6=10;

     if (a_no<=118 && a_no>=112)
        p7=a_no-112;

     else if (a_no<112)
        p7=0;
     else
        if (a_no>118)
        p7=6;

        cout<<"\n 1s: "<<s1<<"\n 2s: "<<s2<<" 2p: "<<p2<<"\n 3s: "<<s3<<" 3p: "<<p3<<" 3d: "<<d3;
        cout<<"\n 4s: "<<s4<<" 4p: "<<p4<<" 4d: "<<d4<<" 4f: "<<f4<<"\n 5s: "<<s5<<" 5p: "<<p5<<" 5d: "<<d5<<" 5f: "<<f5;
        cout<<"\n 6s: "<<s6<<" 6p: "<<p6<<" 6d: "<<d6<<"\n 7s: "<<s7<<"\n 7p: "<<p7; 
delay(500);
//---------------------------------------------------------------------------------------------------------------------------------------

cout<<"\n\n Valency : ";  //finding the valency of the given element
int v;
if(a_no==1 || a_no==2)
{ v=2-a_no;
 cout<<v;}
else if(a_no>2 && a_no<7)
{ v=a_no-2;
 cout<<v;}
else if(a_no>6 && a_no<11)
{ v=6-p2;
 cout<<v;}
else if(a_no>10 && a_no<17)
{ v=a_no-10;
 cout<<v;}
else if(a_no>16 && a_no<25)
{ v=fabs(a_no-18);
 cout<<v;}
else if(a_no==28||a_no==29||a_no==36)
{ v=2;
 cout<<v;}
else if(a_no>=30 && a_no<=35)
{ v=a_no-28;
 cout<<v;}
else if(a_no>=37 && a_no<=43)
{ v=a_no-36;
 cout<<v;}
else if(a_no>=47 && a_no<=53)
{ v=a_no-46;
 cout<<v;}
else if(a_no>=55 && a_no<=58)
{ v=a_no-54;
 cout<<v;}
else if((a_no>=60 && a_no<=70) || a_no==26 ||(a_no>=100 && a_no<=102))
{ v=3;
 cout<<v;}
else if(a_no>=71 && a_no<=75)
{ v=a_no-68;
 cout<<v;}
else if((a_no>=76&&a_no<=79)||a_no==86||a_no==93||a_no==94||a_no==44||a_no==45||a_no==54)
{ v=6;
 cout<<v;}
else if(a_no>=80 && a_no<=85)
{ v=a_no-78;
 cout<<v;}
else if(a_no>=87 && a_no<=92)
{ v=a_no-86;
 cout<<v;}
else if((a_no>=95 && a_no<=99)||a_no==59||a_no==46||a_no==25||a_no==27)
{ v=4;
 cout<<v;}
else if(a_no>=103 && a_no<=107)
{ v=a_no-100;
 cout<<v;}
else if(a_no>=108 && a_no<=118)
{cout<<"not found yet";}
delay(500);
//---------------------------------------------------------------------------------------

cout<<"\n\nRadioactivity status: ";  //to find out the radioactive element
if (a_no==43 || a_no==61 || (a_no>=84 && a_no<119))
 cout<<"The element is radioactive. "<<endl<<endl;
else
 cout<<"The element is not radioactive. "<<endl<<endl;
 getch();     
}

我只需要知道如何检查a_no是否与文件中的原子序号匹配,如果匹配,我如何只输出它们匹配的行中的名称?

2 个答案:

答案 0 :(得分:0)

您正试图将其标记为&#39;一个字符串,有一个函数在名为strtok()的C库中执行此操作。它接受一个输入字符串(你的行)并返回下一个字符串,直到它看到你指定的字符是分隔符,在这种情况下是空格。

char* token = strtok(line, delimiter);
while (token) // is not null
{
    printf("The next token is %s\n", token);
    token = strtok(NULL, delimiter);
}

strtok将忽略连续的分隔符,因此3,2或1个空格之间没有区别。

(空格不是一个很好的字符,可以用作分隔符,因为它们可以用单词出现。你应该对这段代码没问题,但是当你来到人们的名字时,请使用星号或其他东西。)

答案 1 :(得分:0)

我建议你做的是将代码运行时手动检查的所有信息写入f.e中的正确可序列化文件中。 JSON或XML格式,然后只使用一些C ++库将格式读入您的程序。

这是一个JSON示例,说明此文件的结构如何:

{
    "19" : {
        "name" : "Blahium",
        "shortName" : "BL",
        "mass" : 2,
        "shellConfig" : {
            "K" : 2,
            "L" : 8,
            "M" : 8,
            "N" : 1
        },
        "electronicConfig" : {
            "s1" : 12,
            "p4" : 25
        }
    },
    "20" : {
       // ...
    },
    // ...
}

您可以用来读取此JSON数据的库可能是nlohmann::json,这很容易使用。

然后在您的代码中,您只需加载JSON,读取数据并打印即可。因此,例如,对于shell配置部分,你可能会有这样的东西,而不是拥有这300条if else语句和重复代码的行:

// nlohmann::json data is the above file serialized by the json library
void printElectronicConfiguration(nlohman::json data, int atomicNumber) {
    auto item = data.find(atomicNumber);
    if (item == data.end()) 
        return; // return if element with that number wasn't found

    cout << "The shell configuration of the element is -> ";
    if (item["K"] != 0)
        cout << "K-" << item["K"];
    // etc
    cout << endl;
}

如果示例不够清楚,我很抱歉,但是从您提供的600多行代码中,很难提取重要信息以及此数据方案的外观。