我将如何操作导入的 .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?
该程序从计算机上的位置检索文件并将其打印到屏幕上仅显示员工 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
此外,将来,如果您遵循社区指南来询问家庭作业问题,您可能会得到更有用的答案。
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- 我将如何操作导入的 .text 文件以在屏幕上仅显示员工 SNN 的最后四个数字?