《C++语言基础》程序阅读——标准输入输出对象及文本文件

时间:2021-08-15 10:06:58

返回:贺老师课程教学链接


1、阅读并运行下面的示例程序,掌握标准输入输出流的控制
例1
#include <iostream>#include <math.h>
using namespace std;
int main()
{
float a,b,c,disc;
cout<<"please input a,b,c:";
cin>>a>>b>>c;
if (a==0)
cerr<<"a is equal to zero,error!"<<endl;
else if ((disc=b*b-4*a*c)<0)
cerr<<"disc=b*b-4*a*c<0"<<endl;
else
{
cout<<"x1="<<(-b+sqrt(disc))/(2*a)<<endl;
cout<<"x2="<<(-b-sqrt(disc))/(2*a)<<endl;
}
return 0;
}

例2
#include <iostream>#include <iomanip>using namespace std;int main(){    int a;    cout<<"input a:";    cin>>a;    cout<<"dec:"<<dec<<a<<endl;    cout<<"hex:"<<hex<<a<<endl;    cout<<"oct:"<<setbase(8)<<a<<endl;    char *pt="China";    cout<<setw(10)<<pt<<endl;    cout<<setfill('*')<<setw(10)<<pt<<endl;    double pi=22.0/7.0;    cout<<setiosflags(ios::scientific)<<setprecision(8);    cout<<"pi="<<pi<<endl;    cout<<"pi="<<setprecision(4)<<pi<<endl;    cout<<"pi="<<setiosflags(ios::fixed)<<pi<<endl;    return 0;}

例3
#include <iostream>using namespace std;int main(){    int a=21;    cout.setf(ios::showbase);    cout<<"dec:"<<a<<endl;    cout.unsetf(ios::dec);    cout.setf(ios::hex);    cout<<"hex:"<<a<<endl;    cout.unsetf(ios::hex);    cout.setf(ios::oct);    cout<<"oct:"<<a<<endl;    char *pt="China";    cout.width(10);    cout<<pt<<endl;    cout.width(10);    cout.fill('*');    cout<<pt<<endl;    double pi=22.0/7.0;    cout.setf(ios::scientific);    cout<<"pi=";    cout.width(14);    cout<<pi<<endl;    cout.unsetf(ios::scientific);    cout.setf(ios::fixed);    cout.width(12);    cout.setf(ios::showpos);    cout.setf(ios::internal);    cout.precision(6);    cout<<pi<<endl;    return 0;}

例4-1
#include <iostream>using namespace std;int main( ){    char *a="BASIC";		//字符指针指向'B'    for(int i=4; i>=0; i--)        cout.put(*(a+i));   //从最后一个字符开始输出    cout.put('\n');    return 0;}

例4-2
#include<iostream>#include<cstdio>int main(){    char *a="BASIC";    for(int i=4; i>=0; i--)        putchar(*(a+i));    putchar('\n');    return 0;}

例5
#include <iostream>using namespace std;int main( ){    float grade;    cout<<"enter grade:";    while(cin>>grade)  //能从cin流读取数据    {        if(grade>=85)            cout<<grade<<" GOOD!"<<endl;        if(grade<60)            cout<<grade<<" fail!"<<endl;        cout<<"enter grade:";    }    cout<<"The end."<<endl;    return 0;}

例6
(1)不带参数的get函数
#include<iostream>#include<cstdio>using namespace std;int main( ){    int c;    cout<<"enter a sentence:"<<endl;    while((c=cin.get())!=EOF)        cout.put(c);    return 0;}

(2) 有一个参数的get函数
#include<iostream>#include<cstdio>using namespace std;int main( ){    char c;    cout<<"enter a sentence:"<<endl;    while(cin.get(c))  //读取一个字符赋给字符变量c,如果读取成功,cin.get(c)为真        cout.put(c);    cout<<"end"<<endl;    return 0;}

(3) 有3个参数的get函数
#include<iostream>using namespace std;int main( ){    char ch[20];    cout<<"enter a sentence:"<<endl;    cin.get(ch,10,'\n');//指定换行符为终止字符    cout<<ch<<endl;    return 0;}

例7
#include <iostream>using namespace std;int main( ){    char ch[20];    cout<<"enter a sentence:"<<endl;    cin>>ch;    cout<<"The string read with cin is:"<<ch<<endl;    cin.getline(ch,20,'/');//读19个字符或遇'/'结束    cout<<"The second part is:"<<ch<<endl;    cin.getline(ch,20);    //读19个字符或遇'/n'结束    cout<<"The third part is:"<<ch<<endl;    return 0;}

2、阅读并运行下面的示例程序,体会对文本文件的访问。
例11
#include<iostream>#include <fstream>#include<cstdlib>using namespace std;int main( ){    int a[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(int i=0; i<10; i++) //向磁盘文件"f1.dat"输出数据    {        cin>>a[i];        outfile<<a[i]<<" ";    }    cout<<"The numbers have been writen to file. "<<endl;    outfile.close();       //关闭磁盘文件"f1.dat"    return 0;}

例12
#include<iostream>#include <fstream>#include<cstdlib>using namespace std;int main( ){    int a[10],max,i,order;    ifstream infile("f1.dat",ios::in);    //定义输入文件流对象,以输入方式打开磁盘文件f1.dat    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(i=0; i<10; i++)    {        infile>>a[i];  //从磁盘文件读入10个整数,顺序存放在a数组中        cout<<a[i]<<" ";    }          //在显示器上顺序显示10个数    cout<<endl;    max=a[0];    order=0;    for(i=1; i<10; i++)        if(a[i]>max)        {            max=a[i];                //将当前最大值放在max中            order=i;                 //将当前最大值的元素序号放在order中        }    cout<<"max="<<max<<endl<<"order="<<order<<endl;    infile.close();    return 0;}

例13
#include<iostream>#include<fstream>#include<cstdlib>using namespace std;void save_to_file( );void get_from_file();int main( ){    save_to_file( );    //调用save_to_file( ),从键盘读入一行字符并将其中的字母存入磁盘文件f2.dat    get_from_file( );    //调用get_from_file(),从f2.dat读入字母字符,改为大写字母,再存入f3.dat    return 0;}// save_to_file函数从键盘读入一行字符,并将其中的字母存入磁盘文件void save_to_file( ){    ofstream outfile("f2.dat");    //定义输出文件流对象outfile,以输出方式打开磁盘文件f2.dat    if(!outfile)    {        cerr<<"open f2.dat error!"<<endl;        exit(1);    }    char c[80];    cin.getline(c,80);		   //从键盘读入一行字符    for(int i=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();                 //关闭f2.dat}//从磁盘文件f2.dat读入字母字符,将其中的小写字母改为大写字母,再存入f3.datvoid get_from_file(){    char ch;    ifstream infile("f2.dat",ios::in);    //定义输入文件流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))//当读取字符成功时执行下面的复合语句    {        if(ch>=97 && ch<=122)          //判断ch是否为小写字母            ch=ch-32;                  //将小写字母变为大写字母        outfile.put(ch);               //将该大写字母存入磁盘文件f3.dat        cout<<ch;                      //同时在显示器输出    }    cout<<endl;    infile.close( );                 //关闭磁盘文件f2.dat    outfile.close();                 //关闭磁盘文件f3.dat}

3、请写出下面程序的输出结果
(1)
#include <iostream> #include <fstream> using namespace std;int main() {         fstream outfile,infile;         outfile.open("data.dat",ios::out);         outfile<<"1111111111"<<endl;         outfile<<"aaaaaaaaaa"<<endl;         outfile<<"AAAAAAAAAA"<<endl;    outfile<<"**********"<<endl;    outfile.close();         infile.open("data.dat",ios::in);         char line[80];         int i=0;         while(!infile.eof())         {                 i++;                 infile.getline(line,sizeof(line));                 cout<<i<<": "<<line<<endl;         }         infile.close();     return 0;}

(2)说出程序的功能,并上机验证(请自建a.txt)
#include <iostream>#include <fstream>using namespace std;int main(){    ifstream readFile;    ofstream writeFile;    char ch;    readFile.open("a.txt", ios::in);    writeFile.open("b.txt", ios::out);    while (readFile.get(ch))        writeFile.put(ch);    readFile.close();    writeFile.close();    cout << "Finish!" << endl;    return 0;}


(3)
#include <iostream>#include <fstream>#include<cstring>using namespace std;int main(){    ifstream readFile;    ofstream writeFile;    char ch[100];    readFile.open("a.txt", ios::in);    writeFile.open("b.txt", ios::out);    while (!readFile.eof())    {        readFile.getline(ch,100,'\n');        writeFile.write(ch,strlen(ch));        writeFile.write("\n",1);    }    readFile.close();    writeFile.close();    cout << "Finish!" << endl;    return 0;}