我将如何操作导入的 .text 文件以在屏幕上仅显示员工 SNN 的最后四个数字?

How would i manipulate an imported .text file to show only the last four numbers os an employee's SNN on the screen?

本文关键字:SNN 显示 最后 数字 四个 何操作 操作 导入 文件 text 屏幕      更新时间:2023-10-16

该程序从计算机上的位置检索文件并将其打印到屏幕上仅显示员工 SSN 的最后四位数字。

#include <iostream>
#include <fstream>
#include <cstdlib>   // needed for exit()
#include <string>
using namespace std;
int main()
{
    double Thanks_for_your_time;
    string filename = "C:\Emp\employee_info.txt";
    string line;
  ifstream inFile;
  inFile.open("C:\Emp\employee_info.txt");  // open the file with the  
                              // external name 
  if (inFile.fail())  // check for a successful open
  {
    cout << "nThe file was not successfully opened"
        << "n Please check that the file currently exists." 
         << endl;
    exit(1);
  }
  cout << "nThe file has been successfully opened for readingn"
       << endl;
  while (getline(inFile,line))
      cout << line << endl;

  // statements to read data from the file would be placed here
  do
{
   cout << "nThanks for your time(0 to quit):";
   cin >> Thanks_for_your_time;
   }
   /*

文件已成功打开读取

员工姓名:哈里·赫克员工 SSN:987-98-7987(除最后四个之外的所有内容都需要是"x"或空白)员工时薪:20.15 美元本周工作小时数: 40.25总工资: $811.04

员工姓名: 莎莉·斯莫斯员工 SSN:654-65-4654(除最后四个之外的所有内容都需要是"x"或空白)员工时薪:$ 50.25本周工作小时数: 40.35总工资: $2027.59

感谢您的时间(0退出):*/

使用标准库中的正则表达式。

#include <regex>
using namespace std::tr1;

我已经很多年没有玩过C++了,但它会是这样的(假设您将字符串存储在变量"str"中):

std::tr1::regex rx("[0-9]..-..-");
std::string replacement = "***-**-";
std::string str2 = std::tr1::regex_search(str, rx, replacement);

上面的代码是从这个网站引用的,你可以用这个很棒的工具测试你的正则表达式。我相当确定你想要regex_search而不是regex_replace,因为C++处理比赛的方式略有不同,但同样,我已经有一段时间没有使用C++了,所以我不能肯定地说。

请注意,"[0-9].-..-" 是一个正则表达式,它将匹配任何数字字符,后跟任何类型的两个字符(. 是通配符),然后是 -,然后是任何类型的另外两个字符,然后是另一个 -。因此,在您的文本中,它只会匹配两个 SSN 的前两个部分。然后,您将该匹配模式中的数字替换为星号。

另外,由于这是家庭作业,我想给你一些额外的资源,第一个是特定于语言的:

http://softwareramblings.com/2008/07/regular-expressions-in-c.html

http://www.regular-expressions.info/reference.html

http://www.zytrax.com/tech/web/regex.htm

此外,将来,如果您遵循社区指南来询问家庭作业问题,您可能会得到更有用的答案。