Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   subscript and superscript (http://www.velocityreviews.com/forums/t668824-subscript-and-superscript.html)

shahid 02-04-2009 07:12 PM

subscript and superscript
 
hello,
i want to write subscipts and superscripts in c++.is there
any compiler which do this.also tell me the latest compiler .currently
iam using turbo cv3.0.

Juha Nieminen 02-04-2009 09:49 PM

Re: subscript and superscript
 
shahid wrote:
> i want to write subscipts and superscripts in c++.is there
> any compiler which do this.


Here you go:

#include <iostream>

int main()
{
std::cout << "subscipts and superscripts" << std::endl;
}

Pascal J. Bourguignon 02-05-2009 09:44 AM

Re: subscript and superscript
 
shahid <shahidali.shahid@gmail.com> writes:
> i want to write subscipts and superscripts in c++.is there
> any compiler which do this.also tell me the latest compiler .currently
> iam using turbo cv3.0.


Well we don't know what you mean.

In most current implementations of Common Lisp, we can use any unicode
character in symbols, so we can write subscript and superscript
characters in Common Lisp. For example:

C/USER[6]> (defun d¹ (f dx) (lambda (x) (/ (- (funcall f (+ x dx)) (funcall f x)) dx)))

C/USER[7]> (defun d² (f dx) (d¹ (d¹ f dx) dx))

C/USER[8]> (funcall (d² (lambda (x) (+ (* x x) (* 2 x) 1)) 0.001) 1)
1.9073485
C/USER[9]>


But ISTR that the C++ standard doesn't allow random unicode characters
(not even accented letters) in identifiers, only
[A-Za-z_][A-Za-z0-9_]*.

Instead of writting d¹₂, you can write d_sup1_sub2.

--
__Pascal Bourguignon__

James Kanze 02-05-2009 09:58 AM

Re: subscript and superscript
 
On Feb 4, 10:49 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> shahid wrote:
> > i want to write subscipts and superscripts in c++.is there
> > any compiler which do this.


> Here you go:


> #include <iostream>


> int main()
> {
> std::cout << "subscipts and superscripts" << std::endl;
>
> }


Maybe:
std::cout << "$x_i + a^2$" ;
is closer to what he is looking for:-). Or maybe not; as Victor
says (and your ironic response is meant to point out), it's not
really clear what he's looking for. However:

The compiler reads a linear sequence of printable characters;
there is no concept of subscript or superscript at the compiler
level (but a compiler which read from a source which did have
subscripts and superscripts could map somthing like $x^2$ to
"x[2]").

The library only supports reading and writing from linear
sequences of bytes---or characters, if the file is opened in
text mode. (Literally, char's, regardless of the mode.) The
exact meaning of those bytes is more or less implementation
defined---on some of my machines (but not all), writing a
two byte sequence with the values 0x78, 0xB2 will result in "x"
(if that doesn't display correctly, it's a small letter x
followed by a superscript 2). More generally, however, the
output will have to be either directly in the PDL understood by
the output support (printer, etc.), or in some intermedate
language interpreted by a special program which knows how to
drive various devices. In other words, he should output
Postscript, LaTeX, or whatever. In which case, of course, he'll
have to conform to whatever that language requires. (My
suggestion, "$x^2$", is of course TeX or LaTeX.)

And if he's outputting directly to a GUI terminal, he will have
to use the primitives of the GUI library; these usually provide
some means of positionning graphic text in the lower level
containers. (The only GUI library I'm familiar with is Swing,
and that's in Java, not C++. But I presume the principles are
pretty universal: you have a couple of text components, which
provide various ways of displaying pure text, but you can also
do your own graphics on a "raw" component, with graphic
primitives for drawing text in various sizes and orientations at
a specific place in the component.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Pascal J. Bourguignon 02-05-2009 04:01 PM

Re: subscript and superscript
 
James Kanze <james.kanze@gmail.com> writes:

> On Feb 4, 10:49 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>> shahid wrote:
>> > i want to write subscipts and superscripts in c++.is there
>> > any compiler which do this.

>
>> Here you go:

>
>> #include <iostream>

>
>> int main()
>> {
>> std::cout << "subscipts and superscripts" << std::endl;
>>
>> }

>
> Maybe:
> std::cout << "$x_i + a^2$" ;
> is closer to what he is looking for:-). Or maybe not; as Victor
> says (and your ironic response is meant to point out), it's not
> really clear what he's looking for. However:
>
> The compiler reads a linear sequence of printable characters;
> there is no concept of subscript or superscript at the compiler
> level (but a compiler which read from a source which did have
> subscripts and superscripts could map somthing like $x^2$ to
> "x[2]").


Another way to map, would be, like in the NASA programming language HAL/S
http://history.nasa.gov/computers/Appendix-II.html
to write expressions over several lines:

E: 2
M: std::cout << x + a ;
S: i

it would be 'trivial' to write a preprocessor to scan sources and
convert them to "pure" C++

std::cout << x[i]+pow(a,2);




E: -2i
E: e
M: std::cout << x + a ;
E 2
S: n
S: i

--
__Pascal Bourguignon__

shahid 02-05-2009 07:50 PM

Re: subscript and superscript
 
well i want to write the words just like the power of anything for
example .A's power is 3 but we write in c++ as A3 .is there anyway to
write it as power of A or below the A3.

Pascal J. Bourguignon wrote:
> James Kanze <james.kanze@gmail.com> writes:
>
> > On Feb 4, 10:49 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> >> shahid wrote:
> >> > i want to write subscipts and superscripts in c++.is there
> >> > any compiler which do this.

> >
> >> Here you go:

> >
> >> #include <iostream>

> >
> >> int main()
> >> {
> >> std::cout << "subscipts and superscripts" << std::endl;
> >>
> >> }

> >
> > Maybe:
> > std::cout << "$x_i + a^2$" ;
> > is closer to what he is looking for:-). Or maybe not; as Victor
> > says (and your ironic response is meant to point out), it's not
> > really clear what he's looking for. However:
> >
> > The compiler reads a linear sequence of printable characters;
> > there is no concept of subscript or superscript at the compiler
> > level (but a compiler which read from a source which did have
> > subscripts and superscripts could map somthing like $x^2$ to
> > "x[2]").

>
> Another way to map, would be, like in the NASA programming language HAL/S
> http://history.nasa.gov/computers/Appendix-II.html
> to write expressions over several lines:
>
> E: 2
> M: std::cout << x + a ;
> S: i
>
> it would be 'trivial' to write a preprocessor to scan sources and
> convert them to "pure" C++
>
> std::cout << x[i]+pow(a,2);
>
>
>
>
> E: -2i
> E: e
> M: std::cout << x + a ;
> E 2
> S: n
> S: i
>
> --
> __Pascal Bourguignon__


James Kanze 02-05-2009 10:59 PM

Re: subscript and superscript
 
On Feb 5, 8:50 pm, shahid <shahidali.sha...@gmail.com> wrote:
> well i want to write the words just like the power of
> anything for example .A's power is 3 but we write in c++ as A3
> .is there anyway to write it as power of A or below the A3.


In C++, we write "a to the power of 3" "std::pow( a, 3 )". The
same (modulo the std::) as in C, Fortran, Basic, Ada and most
other programming languages. (In Lisp, I think it's "(pow a
3)", and in Forth "a 3 pow", but those are more or less
exceptions.)

C++, like all other programming languages (or almost
all---Pascal's example would seem to be an exception) read a
linear sequence of characters. There's no subscript,
superscript, italics or different fonts (blackboard bold, any
one?). And only a limited number of characters. So much of
your mathematical notation gets rearranged: a[i], instead of

a
i
and pow( a, 2 ) instead of:
2
a

.. That corresponds to the technology that was available then,
and even today, the program editors I know don't support things
like subscripts and superscripts, so you couldn't enter them
into the code even if the compiler supported them.

Note that this latter point is general, and doesn't affect just
code editors. For this reason, most mathematical texts are
actually written in LaTex (or maybe even plain TeX), in which _
is the subscript operator, and ^ the superscript operator (and
there are tons of other operators).

If you're interested in using mathematical notation in your
code, and seeing it as mathematical notation when you print the
program, I'd suggest looking into cweb, a (La)TeX based
preprocessor for C and C++.

--
James Kanze

James Kanze 02-05-2009 11:11 PM

Re: subscript and superscript
 
On Feb 5, 10:44 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> shahid <shahidali.sha...@gmail.com> writes:
> > i want to write subscipts and superscripts in c++.is there
> > any compiler which do this.also tell me the latest compiler
> > .currently iam using turbo cv3.0.


> Well we don't know what you mean.


> In most current implementations of Common Lisp, we can use any
> unicode character in symbols, so we can write subscript and
> superscript characters in Common Lisp.


In standard C++, you can also use any Unicode character. But
only in strings or in user symbols, and in user symbols, it must
be a character considered "alphanumeric" (according to the
UnicodeData.txt file).

> For example:


> C/USER[6]> (defun d¹ (f dx) (lambda (x) (/ (- (funcall f (+ x dx)) (funcall f x)) dx)))
> D¹
> C/USER[7]> (defun d² (f dx) (d¹ (d¹ f dx) dx))
> D²
> C/USER[8]> (funcall (d² (lambda (x) (+ (* x x) (* 2 x) 1)) 0.001) 1)
> 1.9073485
> C/USER[9]>


> But ISTR that the C++ standard doesn't allow random unicode characters
> (not even accented letters) in identifiers, only
> [A-Za-z_][A-Za-z0-9_]*.


That's wrong. Regretfully, a lot of implementations don't
implement the standard in this regard, however.

> Instead of writting d¹₂, you can write d_sup1_sub2.


I'm not sure, but I think that d¹₂ would be legal. (I'd have to
verify whether superscript 1 and subscript 2 are classified by
Unicode as digits.)

There are two issues, however. The first is that the standard
doesn't impose any specific character encoding in input; only
that the characters in the basic character set are present (and
not even that if you're willing to use trigraphs). So Unicode
ends up being formally specified using "universal character
names" (\uxxxx and \Uxxxxxxxx). The input mapping is
implementation defined, however, and it is clearly the intent
that on a platform which supports Unicode (including UTF-8), the
compiler should map all non-ASCII characters to the
corresponding univeral character names (which it can represent
internally as a single four byte value, e.g. in Unicode). Not
very many compilers are conform in this respect, however, and
even less respect the intent.

--
James Kanze

osmium 02-06-2009 04:00 AM

Re: subscript and superscript
 
"James Kanze" wrote:

>In standard C++, you can also use any Unicode character. But
>only in strings or in user symbols, and in user symbols, it must
>be a character considered "alphanumeric" (according to the
>UnicodeData.txt file).


But the OP should note that the Turbo C++ compiler he is using is *not*
standard C++, it predates the standard by quite a bit..




Daniel Pitts 02-06-2009 04:34 AM

Re: subscript and superscript
 
osmium wrote:
> "James Kanze" wrote:
>
>> In standard C++, you can also use any Unicode character. But
>> only in strings or in user symbols, and in user symbols, it must
>> be a character considered "alphanumeric" (according to the
>> UnicodeData.txt file).

>
> But the OP should note that the Turbo C++ compiler he is using is *not*
> standard C++, it predates the standard by quite a bit..
>
>
>

If I recall, it doesn't even support templates.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


All times are GMT. The time now is 10:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.