码迷,mamicode.com
首页 > 编程语言 > 详细

【C++学习·几段代码】Day 1

时间:2015-10-26 07:03:36      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

——《CPP》第17章 输入输出与文件

 

01

#include<iostream>
#include<cstring>

int main()
{
    using std::cout;
    using std::endl;
    const char *state1="Florida";
    const char *state2="Kansas";
    const char *state3="Euphoria";
    int len=std::strlen(state2);
    cout<<"Increasing loop index:\n";
    int i;
    for(i=1;i<=len;i++)
    {
        cout.write(state2,i);
        cout<<endl;
    }
    
    cout<<"Decreasing loop index:\n";
    for(i=len;i>0;i--)
    {
        cout.write(state2,i)<<endl;
    }
    
    cout<<"Exceeding string length:\n";
    cout.write(state2,len+3)<<endl;
    
    return 0;
    
} 

技术分享

 

02

#include<iostream>

int main()
{
    using namespace std;
    cout<<"Input a integer:";
    int n;
    cin>>n;
    
    cout<<"n    n*n\n";
    
    cout<<n<<"    "<<n*n<<"(decimal)\n";
    
    cout<<hex;
    cout<<n<<"    "<<n*n<<"(hexadecimal)\n";

    cout<<oct<<n<<"    "<<n*n<<"(octal)\n";
    
    dec(cout);//恢复原始输出格式    
    cout<<n<<"    "<<n*n<<"(decimal)\n";
    
    return 0;
    
} 

技术分享

 

03

#include<iostream>
//自动填充
int main()
{
    using std::cout;
    cout.fill(*);
    const char *number[5]={"No.1","No.2","No.3","No.4","No.5"};
    long bonus[5]={1000,1350,820,2000,980};
    
    for(int i=0;i<5;i++)
    {
        cout<<number[i]<<":$";
        cout.width(7);
        cout<<bonus[i]<<"\n";
    }
    
    return 0;
    
} 

技术分享

 

04

#include<fstream>  
#include<iostream>  
#include<cmath>  
#include<cstdlib>
using namespace std;  
//////////////从键盘上读取字符的函数  
void read_save(){  
      char c[80];  
      ofstream outfile("data1.txt");//以输出方工打开文件  
      if(!outfile){  
                   cerr<<"open error!"<<endl;//注意是用的是cerr   
                   exit(1);  
                   }  
          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]);//将字母字符存入磁盘文件   
                   cout<<c[i]<<"";  
                   }  
                   cout<<endl;  
                   outfile.close();  
                   }  
void creat_data(){  
      char ch;  
      ifstream infile("data1.txt",ios::in);//以输入的方式打开文件   
      if(!infile){  
                  cerr<<"open error!"<<endl;  
                  exit(1);  
                  }  
    ofstream outfile("after.txt");//定义输出流f3.dat文件  
    if(!outfile){  
                 cerr<<"open error!"<<endl;  
                 exit(1);  
                 }  
     while(infile.get(ch)){//当读取字符成功时   
     if(ch<=122&&ch>=97)  
     ch=ch-32;  
     outfile.put(ch);  
     cout<<ch;  
     }  
     cout<<endl;  
     infile.close();  
     outfile.close();  
     }  
     int main(){  
         read_save();  
         creat_data();  
        system("pause");  
         return 0;  
         } 

技术分享

 

心累、、、

【C++学习·几段代码】Day 1

标签:

原文地址:http://www.cnblogs.com/suzyc/p/4910166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!