简体   繁体   中英

string consisting of positive integer or positive decimal, convert toI number format

I was given the following assignment, but I do not understand exactly what is the problem exactly...

Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the numbers to the number format.

from my perspective what is saying is that I need to read a string consisting of x integer/decimal and convert it to an integer or a double...is that right? it seems so easy because I can just use strtol() and strtod().

It depends on whether you are allowed to use the standard library functions or not. If so, then, yes, it is just as easy as you described. If not, you will have to parse the string looking for decimal points, minus signs, etc. and convert by your own algorithms.

I'm not writing an answer for you, but you basically your assignment asks to convert strings of form say :

  • "123"
  • "-321"
  • "+3219871230"
  • "0123"

to corresponding numbers.

Also, since this is an assignment your professor shouldn't be interested in seeing solutions using library functions strtod , strtol etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM