作业帮 > 综合 > 作业

想问你曾经做过的一道题 用c++做 要满足以下要求

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/07/30 17:13:57
想问你曾经做过的一道题 用c++做 要满足以下要求
从一个文本文件读取正文,将其中的小写字母转化成大写字母,大写字母转换成小写字母,其他字符不变,然后输出到另一个文本文件中保存.要求:(1)用一个子函数完成转换功能(2)用文件实现(3)交作业时,文本文件与程序文件都要有.
要和上次做的那个答案不一样
我是初学者
想问你曾经做过的一道题 用c++做 要满足以下要求
// 下面的代码已经是做到最简单的,不能再简单的了:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void chang(char a[],char letters,int index)
{
   if(letters>=65&&letters<=90) // 大写
   {
     a[index]=letters+32; // 转换小写
   }
   else if(letters>=97&&letters<=122) // 小写
   {
   a[index]=letters-32; // 转换大写
   }
   else{ // 非字母(原文读入)
      a[index]=letters;
   }
}
int main()
{
ifstream file; // 读取
ofstream fileOut; // 写入
string filename;
string line;
char le[1000],c;

int len,index=0;
   
cout<<"Enter the name of the .txt file to be analyzed:";
cin>>filename; // 读入目标文件 (假设是target.txt)
cout<<endl;

// 打开文件
file.open(filename.c_str());
if(file.fail())// 打开失败
{
cout<<"Unable to find file."<<endl;
file.close();
return 1;
}
while (!file.eof()) // 没有到文件末尾
{
getline(file,line);
len=line.length();
if(len==0) continue; // 跳过空白行
        for (int i=0;i<len;i++)
        {
c=line.at(i);
chang(le,c,index);
++index;
}
}
    file.close(); // 关闭文件
    
cout<<"Conversion is completed, Which you want to save the file?"<<endl;

    cout<<"Enter the name of the .txt file to be save:";
cin>>filename; // 读入目标文件 (假设是new.txt)
cout<<endl;

// 打开文件
fileOut.open(filename.c_str());
if(file.fail())// 打开失败
{
cout<<"Unable to find file."<<endl;
file.close();
return 1;
}

for (int j=0;j<index;j++) fileOut.put(le[j]); // 写入另指定的文件
cout<<endl;
return 0;
}
再问: ��������Ϊʲôһֱ�Ҳ�������Ҫ���ļ� �ļ������������� �ܲ����鷳��ѽ�ͼ��һ�� �鷳�� лл