Can this program be shortened any further?

I basicallly need to write a program that reads in two numbers and a char.

eg. 10 + 10

then outputs its value.... The char can either be + or - or *. The number will always be less than 100. and you cannot use semicolons... :(

The input consists of int n which tells me how many test cases and then n number of a, b and c. Output should be on a new line. (ie. I need that endl there)

#include<list.h>
int main(char c,int a,int b,int n)
{if(cin>>n)
		while(n!=0)
			if(cin>>a>>c>>b){
				if(c==42)
					if(cout<<a*b<<endl){}
				if (c==43)
					if(cout<<a+b<<endl){}
				if(c==45)
					if(cout<<a-b<<endl){}
			if(n--){}
}}

that is what I have so far... any help is appreciated =)


185 characters. :P

Recommended Answers

All 5 Replies

That's just so weird lol been a while since I saw something like that :P

It's ridiculous. And why do you include list (and the deprecated list at that)? You need to include iostream. And your main signature is wrong. Presumably you're supposed to be reading the input on stdin.

It's ridiculous

That's just so weird

You're absolutely right !!

185 characters. :P

Actually there are 186 characters ...

I used the following program to count it (sure it can be written in a shorter way ;))

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	char chr;
	int c = 0;
	ifstream file;
	file.open("test4.cpp"); /* Replace test.cpp with the name of the file where you want to count the characters of */
	while(!file.eof())
	{
		file >> chr;
		c++;
	}
	cout << c << endl;
	return 0;
}

BTW, why do you use 'list.h' ? It doesn't compile with that ...
You can maybe create your own header file called 'h' and put the instruction #include <iostream.h> in it (if it's allowed to use a seperate header file) ...
After that you can replace #include <list.h> with #include "h"

Can this program be shortened any further?

Ah, a shortest code/code obfuscation contest. They're kind of fun sometimes and they've produced some of the worst coding practices known to man. We had a professor hold one for her 1st semester programming class and the department head read her the riot act for it. Never seen one that didn't allow semicolons though. Maybe I'll give it a whirl this evening.

Okay, I get it now. He uses list.h simply because it compiles with that (but why?) and is shorter than saying iostream.h. He declares variables in the main parameter list to avoid semicolons. He uses 42 instead of '*', etc., because it is one character shorter.

Here's something a little shorter:

#include <list.h>
int main(char c, int a, int b, int n) {
    if(cin>>n)
        while(cin>>a>>c>>b,
              cout<<(c==42?a*b:c==43?a+b:c==45?a-b:0)<<endl,
              --n) {}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.