函数:isdigit()、atoi()、c_str()

来源:互联网 发布:网络灰色项目 编辑:程序博客网 时间:2024/05/17 07:58

题目名称 : [Exception] NumberParser

时间限制 : 1000 ms

空间限制 : 32 MB

Your job is to finish two function int parseNumber(const char *str) & bool isNumber(const char* str) and create a class named NumberParseException.

Most of you had learned it from class, so there will be no more information for u.Just see some examples.

Sample Input1

01 2

Sample Output1

Not a number.Not a number.sum is 3

Sample Input2

01.1 1.2

Sample Output2

Not a number.Not a number.Not a number.

代码:

judge.h:

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
class NumberParseException {};
bool isNumber(const char * str) {
    if (str == NULL) {
        return false;
    }
    int len = strlen(str);


    if (len == 0)
        return false;
    bool isNumber = false;
    char ch;
    for (int i = 0; i < len; i++) {
        if (i == 0 && (str[i] == '-' || str[i] == '+'))
            continue;
        if (isdigit(str[i])) {
            isNumber = true;
        } else {
            isNumber = false;
            break;
        }
    }
    return isNumber;
}


int parseNumber(const char * str) throw (NumberParseException) {
    if (!isNumber(str))
        throw NumberParseException();
    return atoi(str);
}

main.cpp:

#include <iostream>
#include <string>
#include <stdio.h>
#include "judge.h"
using namespace std;




void BasicTest() {
    string str1 = "", str2 = "1";
    try {
        int num1 = parseNumber(str1.c_str());
        int num2 = parseNumber(str2.c_str());
        printf("sum is %d\n", num1+num2);
    } catch (NumberParseException) {
        printf("Not a number.\n");
    }
    str1 = "1";
    try {
        int num1 = parseNumber(str1.c_str());
        int num2 = parseNumber(NULL);
        printf("sum is %d\n", num1+num2);
    } catch (NumberParseException) {
        printf("Not a number.\n");
    }
    cin >> str1 >> str2;
    try {
        int num1 = parseNumber(str1.c_str());
        int num2 = parseNumber(str2.c_str());
        printf("sum is %d\n", num1+num2);
    } catch (NumberParseException) {
        printf("Not a number.\n");
    }
}


void HardTest() {
    int Ttime;
    cin >> Ttime;
    while (Ttime--) {
        string str1, str2;
        cin >> str1 >> str2;
        try {
            int num1 = parseNumber(str1.c_str());
            int num2 = parseNumber(str2.c_str());
            printf("sum is %d\n", num1+num2);
        } catch (NumberParseException) {
            printf("Not a number.\n");
        }
    }
}


int main() {
    int hard;
    cin >> hard;
    if (hard)
        HardTest();
    else
        BasicTest();
}


注:

知识点1:

函数isdigit(),头文件<ctype.h>(c语言),<cctype>(c++)。作用是检验参数是否为数字0到9.若参数为数字,返回非0值,否则返回NULL(0)。

知识点2:

函数atoi(表示ascii to integer)是把字符串转换成整型数的一个函数,头文件<stdlib.h>或<cstdlib>。atoi()函数会会扫描参数字符串,跳过前面的空白字符(例如空格,tab缩进等),直到遇到数字或者正负符号才开始做转换,遇到非数字或字符串结束时(‘\0’)才结束转换,并将结果返回。如果参数不能转换成int或者参数为空字符串,则将返回0.

eg:

1、转换类型为char*

#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
    int n;
    char *str = "12345.67";
    n = atoi(str);
    printf("n=%d\n",n);
    return 0;
}
输出:
n = 12345
2、
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
    char* temp = " 2013";
    char temp1[] = "2014";
    string temp2 = "2015";
    int num1,num2,num3;
    num1 = atoi(temp);
    num2 = atoi(temp1);
    //num3 = atoi(temp2);//显示string类型不能直接进行转换。
    num3 = atoi(temp2.c_str());
    cout<<num1<<" "<<num2<<" "<<num3<<endl;
    return 0;
}
输出:
2013 2014 2015
知识点3、c_str()
c_str 是c++ 中 string类 (class) 的 函数,它能把 string类 的对象里的字符串 转换成 C 中 char 型变量 的 字符串



编辑 

原创粉丝点击