我试图复制这个例子来实现像 C++ 这样的运算符优先规则(我从一个子集开始,但我最终计划添加其他的)。
尽我所能,我无法获得解析单个二进制操作的语法。它会解析文字 (44, 3.42, "stackoverflow") 就好了,但会失败像3 + 4.
我没有看这个问题,而这一次,试图让我的解决方案的工作,但得到了同样的结果。
(为了保持简短,我将仅在此处发布相关部分,完整代码在此处)
AST 的相关数据结构:
enum class BinaryOperator
{
ADD, SUBTRACT, MULTIPLY, DIVIDE, MODULO, LEFT_SHIFT, RIGHT_SHIFT, EQUAL, NOT_EQUAL, LOWER, LOWER_EQUAL, GREATER, GREATER_EQUAL,
};
typedef boost::variant<double, int, std::string> Litteral;
struct Identifier { std::string name; };
typedef boost::variant<
Litteral,
Identifier,
boost::recursive_wrapper<UnaryOperation>,
boost::recursive_wrapper<BinaryOperation>,
boost::recursive_wrapper<FunctionCall>
> Expression;
struct BinaryOperation
{
Expression rhs, lhs;
BinaryOperator op;
BinaryOperation() {}
BinaryOperation(Expression rhs, BinaryOperator op, Expression lhs) : rhs(rhs), …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <set>
using namespace std;
int main() {
// your code goes here
set<char[10]> rno;
rno.insert("208/CO/11");
rno.insert("206/CO/11");
rno.insert("209/CO/11");
rno.insert("208/CO/11");
set<char[10]>::iterator it;
for(it=rno.begin();rno.end()!=it;it++)
cout<<*it<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码不起作用?如果我用字符串替换char [10]它可以正常工作,但如果c ++是c的超集,为什么这个关联容器与c字符串不兼容?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int count = 0;
c=fgetc(file);
while (c != '\n' )
{
instruction_file[count] = atoi(c);
c = fgetc(file);
count++;
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息是
warning: passing argument 1 of 'atoi' makes pointer from integer without a cast
/usr/include/stdlib.h 147, expected const char* but argument of type char
Run Code Online (Sandbox Code Playgroud) 我的代码将文件读入 unsigned char* 数组。该文件实际上是一个文本文件,其“正常”行结尾为 0x0D、0x0A,长度为 64 字节。
FILE * inputFile = fopen(sInfile.c_str(), "r+");
unsigned char * readArray = (unsigned char *)malloc(sizeof(unsigned char)*readSize); //readSize=64
int bytes_read = fread(readArray, sizeof(unsigned char), readSize, inputFile);
Run Code Online (Sandbox Code Playgroud)
但 bytes_read 是 59,当我检查 readArray 时,它没有任何 \r (0x0D),但文件有。为什么 fread 会跳过所有这些值?有没有一个设置可以控制这个?
我想声明一个类的一些属性.我正在考虑为类中我想要的属性创建私有变量.
然后通过引用公开私有变量.但是我通过指针也可以传递私有变量的地址.因此,类的用户可以修改变量.
那么哪种方式通过引用或指针会更好,如下例所示?
class ExampleClass
{
private:
int age;
public:
//This function allows access via reference
int& GetAgeByReference()
{
int& refAge= age;
return refAge;
}
//This function allows access via pointer
int* GetAgeByPointer()
{
int* pointAge= &age;
return pointAge;
}
}
Run Code Online (Sandbox Code Playgroud) 我不明白指针如何指定char数组或字符串:
char* Carr = new char[5];
Carr = "Hello";
Run Code Online (Sandbox Code Playgroud)
但是下面的代码不起作用:
int* Iarr = new int[5];
Iarr = { 1,2,3,4 };
Run Code Online (Sandbox Code Playgroud)
我知道指针可以赋值而不是值,所以编译器如何接受字符串?
这是一个非常简单的,我无法找到答案.
我正在尝试编写在传递两个字符时返回bool的函数,其中左边的优先级更高.
我可以通过以下方式实现所需的功能:
bool Precedence(char lhs, char rhs) {
if ((lhs == '*' || lhs == '/') && (rhs == '+' || rhs == '-'))
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
这是好的和花花公子,但为什么不是以下完全相同:
bool Precedence(char lhs, char rhs) {
if ((lhs == ('*' || '/')) && (rhs == ('+' || '-')))
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
我知道这是错的,但为什么呢?对我来说,他们都读得很凹凸.对不起,我知道这很愚蠢,但这让我很生气.
我正在尝试从我读取的文件中获取 int 值。诀窍是我不知道这个值有多少字节,所以我首先读取长度八位字节,然后尝试读取长度八位字节告诉我的尽可能多的数据字节。当我尝试将数据八位字节放入 int 变量并最终打印它时,问题就出现了 - 如果第一个数据八位字节为 0,则仅复制后面的一个,因此我尝试读取的 int 是错误的,如 0x00A2与 0xA200 不同。如果我使用 ntohs 或 ntohl,那么 0xA200 会被错误地解码为 0x00A2,因此它不能解决空洞问题。我正在像这样使用 memcpy:
memcpy(&dst, (const *)src, bytes2read)
Run Code Online (Sandbox Code Playgroud)
其中 dst 是 int,src 是 unsigned char *,bytes2read 是 size_t。
那么我做错了什么?谢谢!
尝试读取文件时,我有一个无限循环.文件从用户输入中保存,然后使用单独的功能进行读取,最后显示.
这是我的阅读功能.循环没有找到eof(); 从文件.我看不出是什么问题.没有编译器错误.
void read(HouseholdItems items[AMOUNT], fstream& myFile, fstream& yourFile)
{
myFile.open("insured.dat", ios::in | ios::binary); // Open myFile
yourFile.open("uninsured.dat", ios::in | ios::binary); // Open yourFile
for(int i = 0; i < AMOUNT; i++)
{
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
for(i = 0; i < AMOUNT; i++)
{
while (myFile)
{
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: "; …Run Code Online (Sandbox Code Playgroud) 我知道对于无序映射,"find"返回迭代器,而"at"返回映射值.我只是好奇哪一个更快.
当我输入0到9之间的任何数字时,我希望我的代码打印“您的汽车停在A区”。它正在接受输入,但是程序终止而没有进入if条件(我认为)
{
int value=0;
cout<<"Enter car serial number"<<endl;
cin>>value;
if(value >= 48 && value <= 57)
{
cout<<"your car is parked in section Z"<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)