zoukankan      html  css  js  c++  java
  • 简单I/O和string编程

    作业2-3:文件in.txt中有多个整数,各个数字之间以空白字符分隔。请读出所有的数字,并转成16进制数据输出到另一个文件out.txt中。只需提交源文件 NumberConvert.cpp。

    作业2-4:输入一段文字,其中包含一些非法的单词,请将这些非法单词删除或替换,然后输出过滤后的文字。只需提交源文件 Word.cpp。要求:程序格式规范,变量、函数命名合理,程序中应有必要的注释。

    View Code
    #include <iostream>
    #include <cstdio>
    #include <fstream>
    using namespace std;
    
    int main(){
        ifstream a;
        ofstream b;
        a.open("in.txt");
        b.open("out.txt");
        if(!a){
            cerr<<"NO!"<<endl;
            exit(0);
        }
        if(!b){
            cerr<<"NO!"<<endl;
            exit(0);
        }
        int n;
        b << hex;
        while(a>>n){
            b<< uppercase << n << '\n';
        }
        b<<flush;
        a.close();
        b.close();
        return 0;
    }
    View Code
    #include <iostream>
    #include <string>
    using namespace std;
    
    const int Max = 109;
    string ou[Max];
    
    int main() {
        cout << "请输入不合法的单词库,输入“#”结束" << endl;
        string ss;
        int size = 0;
        while(cin >> ou[size]) {
            if(ou[size] == "#")
                break;
            size++;
        }
        cout << "请输入一段文字,其中包含一些非法的单词,请将这些非法单词删除或替换,输入“#”结束" << endl;
        string ans;
        while(cin >> ss) {
            if(ss == "#")
                break;
            ans += ss;
            ans += " ";
        }
        int len = ans.size();
        for(int i = 0;i < size; i++){
            while(ans.find(ou[i]) < len){
                int pos = ans.find(ou[i]);
                ans.erase(pos,ou[i].size());
                len = ans.size();
            }
            ou[i].clear();
        }
        //以下为整理格式;防止两个单词之间有两个空格和去掉最后的结尾空格
        while(ans.find("  ") < len){
            int pos = ans.find("  ");
            ans.erase(pos,1);
            len = ans.size();
        }
        ans.resize(ans.size()-1);
    
        cout << ans << endl;
        ans.clear();
        return 0;
    }
  • 相关阅读:
    SQL Server连接Oracle详细步骤
    SQLServer2012连接mysql5.5
    SQL Server的链接服务器技术
    2键盘录入Scanner
    1标识符
    电脑从新分盘(软件)
    Tomcat安装配置
    windows下安装和配置多个版本的JDK
    Myeclipse2014的安装
    Could not find acceptable representation
  • 原文地址:https://www.cnblogs.com/gray035/p/2962489.html
Copyright © 2011-2022 走看看