How to display C++ code and math beautifully

Joined
5/2/06
Messages
12,165
Points
273
Just a note for the members who currently taking our online C++ course but it would be useful for many people as well.
  • Code highlighter
Many people are used to [code]your code[/code] but you can use [code=csharp]your code here[/code] and paste the text code from some editor onto Quantnet. Here is how it will look using C++ language.
Code:
// Hello.cpp
//
// Simple example of function bodies.
//

// Preprocessor for include files
#include <stdio.h> // C style I/O
#include <iostream> // C++ style I/O using operator overloading
using namespace std; // The C++ logical collection of functions

void hello_C()
{
printf("Hello Quantnet, C style\n");
}

void hello_CPP()
{
// Using (binary) operator overloading
cout << "Hello Quantnet, C++ style \n";
}
You can specify up to 201 languages such as csharp, java, sql, php, etc
  • Latex
To display mathematical formula nicely on Quantnet, you can use \(latex-code\)
\(\large e^x=\sum_{n=0}^\infty\frac{x^n}{n!}\) will give me \(\large e^x=\sum_{n=0}^\infty\frac{x^n}{n!}\)
 
Just want to update that we now have full GeSHi support (201 programming languages) for coding posting on Quantnet. The default language will be C++.
To post code snippet and use a specific language, you can use code=csharp in this format

[code=csharp]your code goes here[/code]

The full list of supported languages is here

SQL:
DROP TABLE IF EXISTS foo;
CREATE TABLE foo (
  id INT(11) NOT NULL AUTO_INCREMENT,
  characters INT(11) DEFAULT '0',
  time_taken DOUBLE DEFAULT NULL,
  language_used VARCHAR(100) DEFAULT NULL,
  SOURCE text,
  time_highlighted datetime DEFAULT NULL,
  PRIMARY KEY  (id)
) TYPE=MyISAM;

Code:
/******************************************************
 
** Name:
 
** Filename: multiples.cpp
 
** Project #: Deitel & Deitel 1.32
 
** Project Description: Write a program that reads in two integers
  and determines an prints if the first is a multiple of the second.
 
** Output:
First integer is a multiple of second integer
No
Yes
If Yes show that first number is  multiple of second
by displaying that first number divided by second number
is a whole number with no remainder
 
** Input:
Two integers
 
** Algorithm:
Explain function to be performed
direct user to input two integers
take the two integers that are input and
determine if the first integer divided by
the second integer will result in a whole
number with no remainder. If no remainder,
print Yes, first number is a multiple of second
number and explain. Then end program. If there
is a remainder print No, first number is not a
multiple of second number and explain.
Then end program.
 
******************************************************/
 
// Include files
#include <iostream>  // used for cin, cout
#include <conio.h>
using namespace std;
 
// Global Type Declarations
 
// Function Prototypes
void instruct (void);
void pause ();
 
//Global Variables - should not be used without good reason.
 
int main ()
{
// Declaration section
  int integer1, integer2, answer, quotient, result;
 
// Executable section
instruct ();
  cout << "Enter first integer: ";
  cin >> integer1;
  cout << "Enter second integer: ";
  cin >> integer2;
  answer = integer1 % integer2;
  quotient = integer1 / integer2;
  result = quotient * integer2;
  if ( answer == 0 )
  cout << "\n**** Yes "<< integer1 << " is a multiple of "
  << integer2 << " ****" << endl;
  if ( result == integer1 )
  cout << "\nWe know "<< integer1 << " is a multiple of "
  << integer2 << " because " << integer1 << " divided by "
  << integer2 << " equals\na whole number which is "
  << quotient << " and " << quotient << " multiplied by "
  << integer2 << " equals " << result << endl;
  if ( answer != 0 )
  cout << "\n**** No "<< integer1 << " is not a multiple of "
  << integer2 << " **** "<< "\n\nWe know this because "
  << integer1 << " divided by "
  << integer2 << " equals " << quotient
  << " with a remainder of " << answer << endl;
 
    pause ();
return 0;
 
}
 
void instruct (void)
{
  // Declaration section
 
  // Executable section
cout << "Enter two integers below. The program will then determine \n"
  << "if the first number entered is a multiple of the second \n"
  << "number entered.\n "<< endl;
}
 
void pause ()
{
    // Declaration section
 
    // Executable section
    cout << "\nPress any key to continue...";
    getch();
    cout << "\r";
    cout << "                            ";
    cout << "\r";
}
 
The syntax highlighting looks great, Andy.

For the LaTeX, why not use MathJax (http://www.mathjax.org/) instead? It might be a little lower-maintenance than running your own LaTeX image server, and they already have a MediaWiki theme available. Just a thought :)
 
Someone mentioned mathjax the other day which I checked out but didn't get it to work on Quantnet. Nonetheless, I like to have total control of essential services we use here, Latex is one of them. I have our mathtex server running for several years without issues.
 
I've suggested this before... Writing math here would be a lot more fluid if it required typing only one character, like '$'.
 
Love the cpp and Latex support!

I have a suggestion. Would it be possible to have line numbers visible? Maybe as a parameter option, like
Code:
[code=cpp, numbering=true]...
or maybe
Code:
[numbered_code=cpp]...
I could imagine it would be a nice feature to have for the C++ course. Then we could say things like, "Comment out line 4 and see if it compiles" or "delete lines 8-14, they aren't needed".[/code]
 
write a book.webp

C++:
    1 //C++ Code
    2 int main(){
    3   unsigned short i;   
    4   printf("Hello World A\n");
    5   i = 0;
    6   for(; i < 3; i++)
    7     printf( "i -> %d\n", i);   
    8   //--------------------   
    9   printf("Hello World B\n");
   10   i = 0;
   11   for(; i < 3; i++);
   12     printf( "i -> %d\n", i);   
   13   return 0;
   14 }
   15
   16 OUTPUT>
   17 Hello World A
   18 i -> 0
   19 i -> 1
   20 i -> 2
   21 Hello World B
   22 i -> 3
 
Back
Top Bottom