Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A functional C++ program that doesn't use these characters: ';'. '{', '}', '\'

This is a problem that was given at a regional contest last year. Basically, you need to make a simple program in C++ that can print the result of several strings of the format(digit, operator, digit), until EOF is reached. The operators are the usual +, -, *, \.

For example

3+5
2+2
4*3
9/3

Results in

8 4 12 3

The catch is obviously that I can't use the characters ; { } \

I tried something like this (it just adds 2 integers but that's not the focus)

#include<bits/stdc++.h>
#define dummy ;
#define slashDummy '\\'
#define curlDummy {
#define curlDummy2 }
using namespace std dummy


int main() curlDummy
    ios_base::sync_with_stdio(false) dummy
    cin.tie(0) dummy
    int x = 4 dummy
    int y = 5 dummy
    int c = x + y dummy
    cout << c dummy
    return 0 dummy
curlDummy2

But by using the characters in the #define I fail the task. However, if I try to replace { with char(123), the compiler won't run it (I figured it out in the end). This means that this approach is completely wrong.

I also tried to #define everything, but I soon realized that I'm not that good of a programmer and I don't know what I am doing.

#include <iostream>
#define bAcc char(123)
#define rAcc char(125)
#define div char(92)
#define smcolon char(59)
#define unite2(a, b) a##b
#define unite3(a, b, c) a##b##c
#define Acc unite2(bAcc, rAcc)
#define program "while(cin.get()) if(int a = 'a') if(char b = 'b') if(int c = 'c') while(std::cin >> a && a != EOF && std::cin >> b && std::cin >> c) if(b == '+' && std::cout << a + c << std::endl) Acc else if(b == '-' && std::cout << a - c << std::endl) Acc else if(b == '*' && std::cout << a * c << std::endl) Acc else if(b == char(47) && std::cout << a char(47) c << std::endl) Acc"
#define run unite3(bAcc, program, rAcc)

int main() run

The main function won't let me progress with this task, I tried to circumvent it but it always require { }.

How can I solve this problem? Thank you for your time.

like image 602
Teo Oprescu Avatar asked Jan 31 '26 06:01

Teo Oprescu


2 Answers

Trigraphs and if statements could achieve this.

#include <bits/stdc++.h>

int main(void)
??<
        if (std::ios_base::sync_with_stdio(false))
        if (std::cin.tie(0)) ??< ??>
        if (int x = 4) ??<
                if (int y = 5) ??<
                        if (int c = x + y) ??<
                                if (std::cout << c << std::endl) ??< ??>
                        ??>
                ??>
        ??>
??>

Compile with -trigraphs flag.

like image 79
nraiha Avatar answered Feb 01 '26 22:02

nraiha


Simply use the digraphs <% and %> instead of { and }.

like image 30
FlatAssembler Avatar answered Feb 01 '26 21:02

FlatAssembler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!