How to display C++ code and math beautifully

Andy Nguyen

Member
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 [tex]your latex code[/tex]
[tex]\large e^x=\sum_{n=0}^\infty\frac{x^n}{n!}[/tex] will give me [tex]\large e^x=\sum_{n=0}^\infty\frac{x^n}{n!}[/tex]
 

Andy Nguyen

Member
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";
}
 

JKPappas

GPUs-R-US
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 :)
 

pruse

Active Member
I've suggested this before... Writing math here would be a lot more fluid if it required typing only one character, like '$'.
 

Prashant.Kumar

Member
C++ Student
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]
 
Top