日B视频 亚洲,啪啪啪网站一区二区,91色情精品久久,日日噜狠狠色综合久,超碰人妻少妇97在线,999青青视频,亚洲一区二卡,让本一区二区视频,日韩网站推荐

您好,歡迎來電子發(fā)燒友網(wǎng)! ,新用戶?[免費注冊]

當(dāng)前位置:電子發(fā)燒友網(wǎng) > 圖書頻道 > 電子 > 《C++程序設(shè)計實踐例題》 > 第5章 第五章

第1節(jié) 第一部分

例13.1 有一元二次方程ax2+bx+c=0,其一般解為

x1,2=-b±b2-4ac[]2a,但若a=0,或b2-4ac<0時,用此公式出錯。

編程序,從鍵盤輸入a,b,c的值,求x1和x2。如果a=0或b2-4ac<0,輸出出錯信息。

可寫出以下程序:

#include <iostream>

#include <cmath>

usingnamespacestd;

intmain( )

{floata,b,c,disc;

 cout<<"please input a,b,c:";

 cin>>a>>b>>c;

 if(a==0)

cerr<<"a is equal to zero,error!"<<endl; 

//將有關(guān)出錯信息插入cerr流,在屏幕輸出

 else

if((disc=b*b-4*a*c)<0)

cerr<<"disc=b*b-4*a*c<0"<<endl;   //將有關(guān)出錯信息插入cerr流,在屏幕輸出

else

{cout<<"x1="<<(-b+sqrt(disc))/(2*a)<<endl;

    cout<<"x2="<<(-b-sqrt(disc))/(2*a)<<endl;

}

 return0;

}

 

例13.10用ignore函數(shù)跳過輸入流中的字符。

先看不用ignore函數(shù)的情況:

#include <iostream>

usingnamespacestd;

intmain( )

{charch[20];

 cin.get(ch,20,'/');

 cout<<"The first part is:"<<ch<<endl;

cin.get(ch,20,'/');

 cout<<"The second part is:"<<ch<<endl;

return0;

}

 

將程序改為

#include <iostream>

usingnamespacestd;

intmain( )

{charch[20];

 cin.get(ch,20,'/');

 cout<<"The first part is:"<<ch<<endl;

 cin.ignore( );//跳過輸入流中一個字符

 cin.get(ch,20,'/');

 cout<<"The second part is:"<<ch<<endl;

 return0;

}

 

例13.11 有一個整型數(shù)組,含10個元素,從鍵盤輸入10個整數(shù)給數(shù)組,將此數(shù)組送到磁盤文件中存放。

#include <fstream>

usingnamespacestd;

intmain( )

{inta[10];

 ofstream outfile("f1.dat",ios::out);//定義文件流對象,打開磁盤文件"f1.dat"

 if(!outfile)                        //如果打開失敗,outfile返回0值

  {cerr<<"open error!"<<endl;

   exit(1);

  }

 cout<<"enter 10 integer numbers:"<<endl;

 for(inti=0;i<10;i++)

  {cin>>a[i];

   outfile<<a[i]<<" ";}            //向磁盤文件"f1.dat"輸出數(shù)據(jù)

 outfile.close();                   //關(guān)閉磁盤文件"f1.dat"

 return0;

}

 

例13.12 從例13.11建立的數(shù)據(jù)文件f1.dat中讀入10個整數(shù)放在數(shù)組中,找出并輸出10個數(shù)中的最大者和它在數(shù)組中的序號。

#include <fstream>

intmain( )

{inta[10],max,i,order;

 ifstream infile("f1.dat",ios::in|ios::nocreate);

//定義輸入文件流對象,以輸入方式打開磁盤文件f1.dat

 if(!infile)

  {cerr<<"open error!"<<endl;

   exit(1);

  }

 for(i=0;i<10;i++)

   {infile>>a[i];//從磁盤文件讀入10個整數(shù),順序存放在a數(shù)組中

    cout<<a[i]<<" ";}          //在顯示器上順序顯示10個數(shù)

 cout<<endl;

 max=a[0];

 order=0;

 for(i=1;i<10;i++)

   if(a[i]>max)

{max=a[i];                //將當(dāng)前最大值放在max中

order=i;                 //將當(dāng)前最大值的元素序號放在order中

     }

 cout<<"max="<<max<<endl<<"order="<<order<<endl;

 infile.close();

 return0;

}

 

例13.13 從鍵盤讀入一行字符,把其中的字母字符依次存放在磁盤文件f2.dat中。再把它從磁盤文件讀入程序,將其中的小寫字母改為大寫字母,再存入磁盤文件f3.dat。

#include <fstream>

usingnamespacestd;

// save_to_file函數(shù)從鍵盤讀入一行字符,并將其中的字母存入磁盤文件

voidsave_to_file( )

{ofstream outfile("f2.dat");  

//定義輸出文件流對象outfile,以輸出方式打開磁盤文件f2.dat

if(!outfile)

  {cerr<<"open f2.dat error!"<<endl;

exit(1);

  }

 charc[80];

 cin.getline(c,80);//從鍵盤讀入一行字符

 for(inti=0;c[i]!=0;i++)               //對字符逐個處理,直到遇'/0'為止

if(c[i]>=65&& c[i]<=90||c[i]>=97&& c[i]<=122)//如果是字母字符

{outfile.put(c[i]);                      //將字母字符存入磁盤文件f2.dat

cout<<c[i];}                            //同時送顯示器顯示

 cout<<endl;

 outfile.close();                         //關(guān)閉f2.dat

}

 

//從磁盤文件f2.dat讀入字母字符,將其中的小寫字母改為大寫字母,再存入f3.dat

voidget_from_file()

{charch;

 ifstream infile("f2.dat",ios::in|ios::nocreate);

//定義輸入文件流outfile,以輸入方式打開磁盤文件f2.dat

 if(!infile)

  {cerr<<"open f2.dat error!"<<endl;

   exit(1);

  }

ofstream outfile("f3.dat");

 //定義輸出文件流outfile,以輸出方式打開磁盤文件f3.dat

if(!outfile)

  {cerr<<"open f3.dat error!"<<endl;

   exit(1);

  }

while(infile.get(ch))//當(dāng)讀取字符成功時執(zhí)行下面的復(fù)合語句

  {if(ch>=97&& ch<=122)          //判斷ch是否為小寫字母

ch=ch-32;                    //將小寫字母變?yōu)榇髮懽帜?/p>

   outfile.put(ch);               //將該大寫字母存入磁盤文件f3.dat

   cout<<ch;                      //同時在顯示器輸出

  }

cout<<endl;

infile.close( );                  //關(guān)閉磁盤文件f2.dat

outfile.close();                 //關(guān)閉磁盤文件f3.dat

}

intmain( )

{save_to_file( );  

       //調(diào)用save_to_file( ),從鍵盤讀入一行字符并將其中的字母存入磁盤文件f2.dat

get_from_file( );  

   //調(diào)用get_from_file(),從f2.dat讀入字母字符,改為大寫字母,再存入f3.dat

 return0;

}

 

例13.14 將一批數(shù)據(jù)以二進(jìn)制形式存放在磁盤文件中。

#include <fstream>

usingnamespacestd;

structstudent

{charname[20];

 intnum;

 intage;

 charsex;

};

intmain( )

{student stud[3]={"Li",1001,18,'f',"Fun",1002,19,'m',"Wang",1004,17,'f'};

 ofstream outfile("stud.dat",ios::binary);

   if(!outfile)

    {cerr<<"open error!"<<endl;

     abort( );//退出程序

    }

   for(inti=0;i<3;i++)

    outfile.write((char*)&stud[i],sizeof(stud[i]));

   outfile.close( );

return0;

}

 

例13.15 將剛才以二進(jìn)制形式存放在磁盤文件中的數(shù)據(jù)讀入內(nèi)存并在顯示器上顯示。

#include <fstream>

usingnamespacestd;

structstudent

{string name;

 intnum;

 intage;

 charsex;

};

intmain( )

{student stud[3];

 inti;

 ifstream infile("stud.dat",ios::binary);

 if(!infile)

  {cerr<<"open error!"<<endl;

   abort( );

  }

  for(i=0;i<3;i++)

infile.read((char*)&stud[i],sizeof(stud[i]));

  infile.close( );

  for(i=0;i<3;i++)

   {cout<<"NO."<<i+1<<endl;

    cout<<"name:"<<stud[i].name<<endl;

    cout<<"num:"<<stud[i].num<<endl;;

    cout<<"age:"<<stud[i].age<<endl;

    cout<<"sex:"<<stud[i].sex<<endl<<endl;

   }

  return0;

 }

 

例13.16 有5個學(xué)生的數(shù)據(jù),要求:

(1)把它們存到磁盤文件中;

(2)將磁盤文件中的第1,3,5個學(xué)生數(shù)據(jù)讀入程序,并顯示出來;

(3)將第3個學(xué)生的數(shù)據(jù)修改后存回磁盤文件中的原有位置。

(4)從磁盤文件讀入修改后的5個學(xué)生的數(shù)據(jù)并顯示出來。

#include <fstream>

usingnamespacestd;

structstudent

{intnum;

 charname[20];

 floatscore;

};

intmain( )

{student stud[5]={1001,"Li",85,1002,"Fun",97.5,1004,"Wang",54,

                  1006,"Tan",76.5,1010,"ling",96};

 fstream iofile("stud.dat",ios::in|ios::out|ios::binary); 

//用fstream類定義輸入輸出二進(jìn)制文件流對象iofile

 if(!iofile)

  {cerr<<"open error!"<<endl;

   abort( );

  }

 for(inti=0;i<5;i++)//向磁盤文件輸出5個學(xué)生的數(shù)據(jù)

   iofile.write((char*)&stud[i],sizeof(stud[i])); 

 student stud1[5];                  //用來存放從磁盤文件讀入的數(shù)據(jù)

 for(inti=0;i<5;i=i+2)

   {iofile.seekg(i*sizeof(stud[i]),ios::beg);  //定位于第0,2,4學(xué)生數(shù)據(jù)開頭

    iofile.read((char*)&stud1[i/2],sizeof(stud1[0]));

//先后讀入3個學(xué)生的數(shù)據(jù),存放在stud1[0],stud[1]和stud[2]中

    cout<<stud1[i/2].num<<" "<<stud1[i/2].name<<" "<<stud1[i/2].score<<endl;

//輸出stud1[0],stud[1]和stud[2]各成員的值

   }

 cout<<endl;

 stud[2].num=1012;                         //修改第3個學(xué)生(序號為2)的數(shù)據(jù)

 strcpy(stud[2].name,"Wu");

 stud[2].score=60;

 iofile.seekp(2*sizeof(stud[0]),ios::beg);   //定位于第3個學(xué)生數(shù)據(jù)的開頭

 iofile.write((char*)&stud[2],sizeof(stud[2])); //更新第3個學(xué)生數(shù)據(jù)

 iofile.seekg(0,ios::beg);                       //重新定位于文件開頭

 for(inti=0;i<5;i++)

   {iofile.read((char*)&stud[i],sizeof(stud[i]));  //讀入5個學(xué)生的數(shù)據(jù)

    cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;

   }

 iofile.close( );

return0;

}

 

例13.17 將一組數(shù)據(jù)保存在字符數(shù)組中。

#include <strstream>

usingnamespacestd;

structstudent

{intnum;

 charname[20];

 floatscore;

}

intmain( )

{student stud[3]={1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90};

 charc[50];//用戶定義的字符數(shù)組

 ostrstream strout(c,30);   //建立輸出字符串流,與數(shù)組c建立關(guān)聯(lián),緩沖區(qū)長30

 for(inti=0;i<3;i++)       //向字符數(shù)組c寫3個學(xué)生的數(shù)據(jù)

   strout<<stud[i].num<<stud[i].name<<stud[i].score;

 strout<<ends;             //ends是C++的I/O操作符,插入一個'\\0'

 cout<<"array c:"<<c<<endl;    //顯示字符數(shù)組c中的字符

}

 

例13.18 在一個字符數(shù)組c中存放了10個整數(shù),以空格相間隔,要求將它們放到整型數(shù)組中,再按大小排序,然后再存放回字符數(shù)組c中。

#include <strstream>

usingnamespacestd;

intmain( )

{charc[50]="12 34 65 -23 -32 33 61 99 321 32";

 inta[10],i,j,t;

 cout<<"array c:"<<c<<endl;//顯示字符數(shù)組中的字符串

 istrstream strin(c,sizeof(c));    //建立輸入串流對象strin并與字符數(shù)組c關(guān)聯(lián)

 for(i=0;i<10;i++)

  strin>>a[i];                     //從字符數(shù)組c讀入10個整數(shù)賦給整型數(shù)組a

 cout<<"array a:";

 for(i=0;i<10;i++)

   cout<<a[i]<<" ";                 //顯示整型數(shù)組a各元素

 cout<<endl;

 for(i=0;i<9;i++)                   //用起泡法對數(shù)組a排序

for(j=0;j<9-i;j++)

      if(a[j]>a[j+1])

{t=a[j];a[j]=a[j+1];a[j+1]=t;}

 ostrstream strout(c,sizeof(c));    //建立輸出串流對象strout并與字符數(shù)組c關(guān)聯(lián)

 for(i=0;i<10;i++)

   strout<<a[i]<<" ";               //將10個整數(shù)存放在字符數(shù)組c

 strout<<ends;                      //加入'\\0'

 cout<<"array c:"<<c<<endl;         //顯示字符數(shù)組c

 return0;

}

 

例13.2 用控制符控制輸出格式。

#include <iostream>

#include <iomanip>//不要忘記包含此頭文件

usingnamespacestd;

intmain()

{inta;

 cout<<"input a:";

 cin>>a;

 cout<<"dec:"<<dec<<a<<endl;               //以十進(jìn)制形式輸出整數(shù)

 cout<<"hex:"<<hex<<a<<endl;              //以十六進(jìn)制形式輸出整數(shù)a

 cout<<"oct:"<<setbase(8)<<a<<endl;       //以八進(jìn)制形式輸出整數(shù)a

 char*pt="China";                        //pt指向字符串"China"

 cout<<setw(10)<<pt<<endl;                //指定域?qū)挒?0,輸出字符串

cout<<setfill('*')<<setw(10)<<pt<<endl; //指定域?qū)?0,輸出字符串,空白處以'*'填充

 doublepi=22.0/7.0;                     //計算pi值

 cout<<setiosflags(ios::scientific)<<setprecision(8);//按指數(shù)形式輸出,8位小數(shù)

 cout<<"pi="<<pi<<endl;                               //輸出pi值

 cout<<"pi="<<setprecision(4)<<pi<<endl;              //改為4位小數(shù)

 cout<<"pi="<<setiosflags(ios::fixed)<<pi<<endl;//改為小數(shù)形式輸出

 return0;

}

 

例13.3 用流控制成員函數(shù)輸出數(shù)據(jù)。

#include <iostream>

usingnamespacestd;

intmain( )

{inta=21

 cout.setf(ios::showbase);//顯示基數(shù)符號(0x或0)

 cout<<"dec:"<<a<<endl;         //默認(rèn)以十進(jìn)制形式輸出a

 cout.unsetf(ios::dec);         //終止十進(jìn)制的格式設(shè)置

 cout.setf(ios::hex);           //設(shè)置以十六進(jìn)制輸出的狀態(tài)

 cout<<"hex:"<<a<<endl;         //以十六進(jìn)制形式輸出a

cout.unsetf(ios::hex);         //終止十六進(jìn)制的格式設(shè)置

 cout.setf(ios::oct);           //設(shè)置以八進(jìn)制輸出的狀態(tài)

 cout<<"oct:"<<a<<endl;         //以八進(jìn)制形式輸出a

cout.unseft(ios::oct);

 char*pt="China";              //pt指向字符串"China"

 cout.width(10);                //指定域?qū)挒?0

 cout<<pt<<endl;                //輸出字符串

 cout.width(10);                //指定域?qū)挒?0

 cout.fill('*');                //指定空白處以'*'填充

 cout<<pt<<endl;                //輸出字符串

 doublepi=22.0/7.0;            //輸出pi值

cout.setf(ios::scientific);    //指定用科學(xué)記數(shù)法輸出

cout<<"pi=";                   //輸出"pi="

cout.width(14);                //指定域?qū)挒?4

cout<<pi<<endl;                //輸出pi值

cout.unsetf(ios::scientific); //終止科學(xué)記數(shù)法狀態(tài)

cout.setf(ios::fixed);        //指定用定點形式輸出

cout.width(12);               //指定域?qū)挒?2

cout.setf(ios::showpos);      //正數(shù)輸出“+”號

cout.setf(ios::internal);     //數(shù)符出現(xiàn)在左側(cè)

cout.precision(6);            //保留6位小數(shù)

cout<<pi<<endl;               //輸出pi,注意數(shù)符“+”的位置

return0;

}

 

例13.4 有一個字符串"BASIC",要求把它們按相反的順序輸出。

#include <iostream>

usingnamespacestd;

intmain( )

{char*a="BASIC";//字符指針指向'B'

 for(inti=4;i>=0;i--)

  cout.put(*(a+i));                  //從最后一個字符開始輸出

 cout.put('\\n');

 return0;

}

 

例13.5 通過測試cin的真值,判斷流對象是否處于正常狀態(tài)。

#include <iostream>

usingnamespacestd;

intmain( )

{floatgrade;

 cout<<"enter grade:";

 while(cin>>grade)//能從cin流讀取數(shù)據(jù)

{if(grade>=85) cout<<grade<<"GOOD!"<<endl;

if(grade<60) cout<<grade<<"fail!"<<endl;

cout<<"enter grade:";

}

 cout<<"The end."<<endl;

 return0;

}

 

例13.6 用get函數(shù)讀入字符。

#include <iostream>

intmain( )

{intc;

 cout<<"enter a sentence:"<<endl;

 while((c=cin.get())!=EOF)

  cout.put(c);

 return0;

}

 

例13.7 用getline函數(shù)讀入一行字符。

#include <iostream>

usingnamespacestd;

intmain( )

{charch[20];

 cout<<"enter a sentence:"<<endl;

 cin>>ch;

 cout<<"The string read with cin is:"<<ch<<endl;

 cin.getline(ch,20,'/');//讀19個字符或遇'/'結(jié)束

 cout<<"The second part is:"<<ch<<endl;

 cin.getline(ch,20);                              //讀19個字符或遇'/n'結(jié)束

 cout<<"The third part is:"<<ch<<endl;

 return0;

}

 

例13.8 逐個讀入一行字符,將其中的非空格字符輸出。

#include <iostream>

usingnamespacestd;

intmain( )

{charc;

 while(!cin.eof( ))//eof( )為假表示未遇到文件結(jié)束符

if((c=cin.get( ))!=' ')            //檢查讀入的字符是否為空格字符

     cout.put(c);

  return0;

}

 

例13.9 peek函數(shù)和putback函數(shù)的用法。

#include <iostream>

usingnamespacestd;

intmain( )

{charc[20];

 intch;

 cout<<"please enter a sentence:"<<endl;

 cin.getline(c,15,'/');

 cout<<"The first part is:"<<c<<endl;

 ch=cin.peek( );//觀看當(dāng)前字符

 cout<<"The next character(ASCII code) is:"<<ch<<endl;

 cin.putback(c[0]);             //將'I'插入到指針?biāo)柑?/p>

 cin.getline(c,15,'/');

 cout<<"The second part is:"<<c<<endl;

 return0;

}

?
班玛县| 名山县| 玛曲县| 成都市| 福鼎市| 纳雍县| 永吉县| 唐海县| 株洲县| 同江市| 南和县| 西华县| 盘山县| 阜南县| 沙田区| 鄂尔多斯市| 布拖县| 明溪县| 五台县| 苏尼特左旗| 珠海市| 德保县| 永善县| 万宁市| 威信县| 洛南县| 西和县| 武乡县| 分宜县| 广丰县| 青龙| 远安县| 英超| 上犹县| 黎平县| 游戏| 郯城县| 苗栗县| 金秀| 出国| 龙里县|