c++实训遇到的问题(1)

c++实训 人力资源管理系统

这个是个很小的系统,甚至称不上系统,只是具备一些基本的功能,用于练手的。

https://pan.baidu.com/disk/home?#list/path=%2F&vmode=list    这是云盘的地址,里面有这次实训项目的文档(很简单)。

内容

  基于这个小项目,这篇博文会列出我在写这个系统的时候遇到的问题,以及解决的方法

(ps:本人的c++只是学了皮毛,没有很深入,只是一个新手,如果您在浏览过程中发现错误,可以留言指出,我会及时更正,十分感谢

正文:

1:打开txt文件,读写数据

#include<iostream>
#include<stdio.h>
#include<string> 
#include<fstream>
using namespace std;

 int main()
 {  
    
    ifstream dire("pmtxt.txt");     
    // 打开文件 pm.txt  ,并将其命名为dire,读取文件内容 ,读操作(输入)的文件类 
    dire.fail() == 1;  //判断文件是否打开失败  1:打开失败  ;0:打开成功
    ofstream tofil("pm.txt");//  打开文件 pm.txt  ,并将其命名为tofil   ,写操作(输出)的文件类
    fstream records("pm.txt"); //   这样也可以打开  可读可    
    return 0; 
    
 }

 

2:读写文件内容

读取文件行内容 :getline ()  

#include<iostream>
#include<stdio.h>
#include<string> 
#include<fstream>
using namespace std;

 int main()
 {  
    string  line;
    ifstream dire("pmtxt.txt");     
    // 打开文件 pm.txt  ,并将其命名为dire,读取文件内容 ,读操作(输入)的文件类 
    dire.fail() == 1;  //判断文件是否打开失败  1:打开失败  ;0:打开成功
    ofstream tofil("pm.txt");//  打开文件 pm.txt  ,并将其命名为tofil   ,写操作(输出)的文件类
    fstream records("pm.txt"); //   这样也可以打开  可读可     
    
    getline(dire,line) ;   // 读取dire文件里的一行内容并保存在 line 里面    
    cout<<line;//显示这行内容    
    tofil<<line;//将line 字符串写入到tofil 文件中    
    tofil<<"新写入的一行"<<line;
    return 0;     
 }

 

3:关闭文件

  dire.close();

  tofil.close();

4:格式化输出

  头文件 #include<iomanip>

    固定长度:setw(int w)

    cout<<setw(8)<<"line"<<endl; //固定长度为8位 

  左对齐/右对齐
    cout<<std::right<<setw(10)<<"123";  //右对齐,固定长度10位

    cout<<setw(5)<<"123"<<endl;//右对齐
    cout<<std::left<<setw(10)<<"123";//左对齐,固定长度10位

    cout<<setw(6)<<"123"<<endl;//左对齐
     一旦设置左对齐,或者有右对齐之后,如果不改变其左右对其,则会保持之前设置

5:大小写转换

#include<iostream>
#include<stdio.h>
#include<string> 
#include <algorithm> 
using namespace std;

 int main()
 {  
    string str1,str2,str3;
    cin>>str1;    
    transform(str1.begin(),str1.end(),back_inserter(str2),::tolower);//全小写转换     
    transform(str1.begin(),str1.end(),back_inserter(str3),::toupper);
    //全大写转换 当str2,str3要重用时,要置空
    return 0;     
 }

 

6:字符数组转为字符串

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

using namespace std;

 int main()
 {  
    char str1[20]="aaaaaa";
    string str2=str1;//整个全转为字符串    
    string str3(&str1[0],&str1[5]);//(起始位置,结束位置)转为字符串    
    cout<<str2<<endl;
    cout<<str3<<endl;
    return 0;     
 }

 

7:string 类的一些方法
str.substr(起始位置,字符个数(不写表示到最后))

str.find(匹配字符串,起始位置,匹配字符串的前几位)

str.rfind(    匹配字符串        ,string::npos:表示从目标字符串某位开始, 匹配字符串的前几位  )
  返回首字符的位置,或者-1  
str.replace( 起始位置,删除个数,插入内容)
  返回替换后内容  会修改原字符串
 
#include<iostream>
#include<stdio.h>
#include<string> 
using namespace std;

 int main()
 {  
    string str1,str2,str3;
    int n1,n2;
    cin>>str1;
    cin>>str3;
    n1=str1.find('c');
    str2=str1.replace(n1,1,1,'o'); //(起始位置,删除个数,字符‘c’重复次数,‘字符’);
    cout<<str1<<endl;
    cout<<str2<<endl;
    n2=str3.find("ab");
    str2=str3.replace(n2,1,"ooo");//(起始位置,删除个数,插入字符串);
    cout<<str2<<endl;
    cout<<str3<<endl;
    
    
    return 0; 
    
 }

 

 
8:如何接收回车并判断
 
#include<iostream>
#include<stdio.h>
#include<string> 
using namespace std;

 int main()
 {  
    char str[20];
    int i=0,flag=1,n;
    getchar();//如果之前有回车,它可以吞掉回车 
    while(flag)
    {
        str[i]=getchar();//获取输入字符
        //或者:scanf("%c",&str[i]);
        if(str[i] == '\n')
        {
            cout<<"接收到回车"<<endl;
            flag=0;
            i++; 
        }//接收到回车退出
    }
    string str1(&str[0],&str[i-1]);//将字符数组转为字符串,其中不包含回车
    cout<<str1<<endl;
    return 0; 
    
 }

 

暂时,写到这里。 

 

posted @ 2017-09-16 11:29  森海书生  阅读(624)  评论(0编辑  收藏  举报