C++读了一会儿
C++ reading from a while
我有一个正在从中读取数据的文件,数据的格式为
[function] [number1] [number2] 其中 [number2] 是可选的!
例如。
+ 88 19
- 29 28
! 4
+ 2 2
上面的输出:
107
1
当行的格式为 [function] [number1] [number2] 时,我的代码工作正常,但当行中不存在 [number2] 时,我的代码失败。
到目前为止我的代码:
我正在使用的库:
iostream
fstream
cstdlib
字符串
...
while (infile >> function >> number1>> number2)
{
switch (function)
{
case '+':
addition(number1, number2);
break;
case '-':
subtraction(number1, number2);
break;
case '!':
factorial(number1);
break;
...
如果 [number2] 不存在,我如何才能只读取 [number1] 并在下次读取时跳回到 [function]。
感谢任何帮助,
谢谢!
您应该逐行阅读文件。
然后,你应该按' '
解析每一行。您可以使用布尔变量并在到达第二个 space 时将其设置为 true
。如果你到达字符串的末尾并且布尔值仍然为假,那么你没有任何数字 2.
这将是适应您的方法的一种方式,但对输入的假设较少。请记住,此代码非常特定于您选择的格式,并且 100% 相信您正在读取的文件是正确的。正如人们在评论中提到的那样,阅读该行并对其进行解析会更可靠,并且可能是您未来应该考虑的事情。然而,这确实遵循您当前的方法,并进行了一些修改。
const bool isUnary(char function)
{
if (function == '!') return true;
// Add further unary functions here as if/switch
return false
}
int main()
{
// Only read function here
while (infile >> function)
{
// Prepare your numbers
int number1{}, number2{};
// If the function is Unary (IE: Takes one parameter)
if (isUnary(function))
{
infile >> number1; // Read the one number
}
// Otherwise read both numbers
else
{
infile >> number1;
infile >> number2;
}
// Then switch on function as you otherwise would
switch (function)
{
case '+':
addition(number1, number2);
break;
case '-':
subtraction(number1, number2);
break;
case '!':
factorial(number1);
break;
}
}
return 0;
}
你可以使用一些技巧;您至少在每一行的开头有一个算术运算符字符 eg( "+ - *") 并且它后面至少有一个参数。因此,您可以使用字符类型的变量来获取运算符,并在循环内读取它之后读取剩余的整行,然后我们使用 API strtok
传递给它 space (' ').
在读取运算符和行后的循环中,我们根据运算符切换我们调用 strtok
来解析行,并 atoi
将结果转换为整数。
这是我为您精心设计的一个简单程序,我认为它运行良好:
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
//using namespace std;
// some forward declarations
int Add(int, int = 0); // as long as you said the second parameter is optional
int Sub(int, int = 0);
int Mult(int, int = 0);
int Div(int, int = 0);
int Fact(int);
int main(){
std::ifstream in("data.txt");
std::string sLine;
char op;
int param1, param2;
while(in >> op){ // getting the first character from the file which is considered to be the operator
std::getline(in, sLine); // after reading the operator above we read the remaining whole line containing 1 or 2 integer values.
switch(op){
case '+':{
// some parsing and converting here
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
// calling relevant functions and printing
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Add(param1, param2) << std::endl;
}
}
break;
case '-':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Sub(param1, param2) << std::endl;
}
}
break;
case '*':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Mult(param1, param2) << std::endl;
}
}
break;
case '/':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Div(param1, param2) << std::endl;
}
}
break;
case '!':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << op << " = " <<
Fact(param1) << std::endl;
}
}
break;
}
}
in.close(); // graceful close
std::cout << std::endl;
std::cin.get();
return 0;
}
int Add (int a, int b) { return a + b;}
int Sub (int a, int b) { return a - b;}
int Mult(int a, int b) { return a * b;}
int Div (int a, int b) { return a / b;}
int Fact(int a) {
int tmp(a - 1);
while( tmp)
a *= tmp--;
return a;
}
文件data.txt
内容:
+ 88 19
- 29 28
! 4
+ 2 2
输出:
88 + 19 = 107
29 - 28 = 1
4! = 24
2 + 2 = 4
我有一个正在从中读取数据的文件,数据的格式为
[function] [number1] [number2] 其中 [number2] 是可选的!
例如。
+ 88 19
- 29 28
! 4
+ 2 2
上面的输出:
107
1
当行的格式为 [function] [number1] [number2] 时,我的代码工作正常,但当行中不存在 [number2] 时,我的代码失败。
到目前为止我的代码:
我正在使用的库:
iostream
fstream
cstdlib
字符串
...
while (infile >> function >> number1>> number2)
{
switch (function)
{
case '+':
addition(number1, number2);
break;
case '-':
subtraction(number1, number2);
break;
case '!':
factorial(number1);
break;
...
如果 [number2] 不存在,我如何才能只读取 [number1] 并在下次读取时跳回到 [function]。
感谢任何帮助,
谢谢!
您应该逐行阅读文件。
然后,你应该按' '
解析每一行。您可以使用布尔变量并在到达第二个 space 时将其设置为 true
。如果你到达字符串的末尾并且布尔值仍然为假,那么你没有任何数字 2.
这将是适应您的方法的一种方式,但对输入的假设较少。请记住,此代码非常特定于您选择的格式,并且 100% 相信您正在读取的文件是正确的。正如人们在评论中提到的那样,阅读该行并对其进行解析会更可靠,并且可能是您未来应该考虑的事情。然而,这确实遵循您当前的方法,并进行了一些修改。
const bool isUnary(char function)
{
if (function == '!') return true;
// Add further unary functions here as if/switch
return false
}
int main()
{
// Only read function here
while (infile >> function)
{
// Prepare your numbers
int number1{}, number2{};
// If the function is Unary (IE: Takes one parameter)
if (isUnary(function))
{
infile >> number1; // Read the one number
}
// Otherwise read both numbers
else
{
infile >> number1;
infile >> number2;
}
// Then switch on function as you otherwise would
switch (function)
{
case '+':
addition(number1, number2);
break;
case '-':
subtraction(number1, number2);
break;
case '!':
factorial(number1);
break;
}
}
return 0;
}
你可以使用一些技巧;您至少在每一行的开头有一个算术运算符字符 eg( "+ - *") 并且它后面至少有一个参数。因此,您可以使用字符类型的变量来获取运算符,并在循环内读取它之后读取剩余的整行,然后我们使用 API strtok
传递给它 space (' ').
在读取运算符和行后的循环中,我们根据运算符切换我们调用 strtok
来解析行,并 atoi
将结果转换为整数。
这是我为您精心设计的一个简单程序,我认为它运行良好:
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
//using namespace std;
// some forward declarations
int Add(int, int = 0); // as long as you said the second parameter is optional
int Sub(int, int = 0);
int Mult(int, int = 0);
int Div(int, int = 0);
int Fact(int);
int main(){
std::ifstream in("data.txt");
std::string sLine;
char op;
int param1, param2;
while(in >> op){ // getting the first character from the file which is considered to be the operator
std::getline(in, sLine); // after reading the operator above we read the remaining whole line containing 1 or 2 integer values.
switch(op){
case '+':{
// some parsing and converting here
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
// calling relevant functions and printing
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Add(param1, param2) << std::endl;
}
}
break;
case '-':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Sub(param1, param2) << std::endl;
}
}
break;
case '*':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Mult(param1, param2) << std::endl;
}
}
break;
case '/':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Div(param1, param2) << std::endl;
}
}
break;
case '!':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << op << " = " <<
Fact(param1) << std::endl;
}
}
break;
}
}
in.close(); // graceful close
std::cout << std::endl;
std::cin.get();
return 0;
}
int Add (int a, int b) { return a + b;}
int Sub (int a, int b) { return a - b;}
int Mult(int a, int b) { return a * b;}
int Div (int a, int b) { return a / b;}
int Fact(int a) {
int tmp(a - 1);
while( tmp)
a *= tmp--;
return a;
}
文件data.txt
内容:
+ 88 19
- 29 28
! 4
+ 2 2
输出:
88 + 19 = 107
29 - 28 = 1
4! = 24
2 + 2 = 4