资源简介
网易公开课 哈佛大学 CS50的必要文件
代码片段和文件信息
/***************************************************************************
*CS50 Library 3.0
*
* https://manual.cs50.net/Library
*
* Glenn Holloway
* David J. Malan
*
* based on Eric Roberts‘ genlib.c and simpio.c.
*
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
* http://creativecommons.org/licenses/by-nc-sa/3.0/
***************************************************************************/
#include
#include
#include
#include “cs50.h“
/*
* Reads a line of text from standard input and returns the equivalent
* char; if text does not represent a char user is prompted to retry.
* Leading and trailing whitespace is ignored. If line can‘t be read
* returns CHAR_MAX.
*/
char
GetChar(void)
{
// try to get a char from user
while (true)
{
// get line of text returning CHAR_MAX on failure
string line = GetString();
if (line == NULL)
return CHAR_MAX;
// return a char if only a char (possibly with
// leading and/or trailing whitespace) was provided
char c1 c2;
if (sscanf(line “ %c %c“ &c1 &c2) == 1)
{
free(line);
return c1;
}
else
{
free(line);
printf(“Retry: “);
}
}
}
/*
* Reads a line of text from standard input and returns the equivalent
* double as precisely as possible; if text does not represent a
* double user is prompted to retry. Leading and trailing whitespace
* is ignored. For simplicity overflow and underflow are not detected.
* If line can‘t be read returns DBL_MAX.
*/
double
GetDouble(void)
{
// try to get a double from user
while (true)
{
// get line of text returning DBL_MAX on failure
string line = GetString();
if (line == NULL)
return DBL_MAX;
// return a double if only a double (possibly with
// leading and/or trailing whitespace) was provided
double d; char c;
if (sscanf(line “ %lf %c“ &d &c) == 1)
{
free(line);
return d;
}
else
{
free(line);
printf(“Retry: “);
}
}
}
/*
* Reads a line of text from standard input and returns the equivalent
* float as precisely as possible; if text does not represent a float
* user is prompted to retry. Leading and trailing whitespace is ignored.
* For simplicity overflow and underflow are not detected. If line can‘t
* be read returns FLT_MAX.
*/
float
GetFloat(void)
{
// try to get a float from user
while (true)
{
// get line of text returning FLT_MAX on failure
string line = GetString();
if (line == NULL)
return FLT_MAX;
// return a float if only a float (possibly with
// leading and/or trailing whitespace) was provided
char
- 上一篇:IT绩效考核KPI模板
- 下一篇:js打印有设置和预览功能
评论
共有 条评论