Untitled

 avatar
unknown
c_cpp
2 years ago
1.0 kB
3
Indexable
#ifndef FUNCTION_H
#define FUNCTION_H
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

int solver(int **ptr,int *sum, char *s){
    
    int len = strlen(s);
    int j = 0, tmp = 0;//用tmp判斷讀到下一個數字了沒
                       //j為index,用來儲存每個數字的值
    for(int i = 0; i <= len; i++){
        if(tmp == 1 && isdigit(s[i]) == 0){
            *sum += *ptr[j];
            tmp = 0;
            j++;
        }
        if(s[i] == '-') *ptr[j] = -INT_MAX;//如果是負數,先初始化成INT_MAX方便判斷
        if(isdigit(s[i]) == 1){
            tmp = 1;
            if(*ptr[j] >= 0){
                *ptr[j] = (*ptr[j])*10 + s[i] - '0';
            }
            else{
                if(*ptr[j] == -INT_MAX) *ptr[j] = -(s[i]-'0');
                else *ptr[j] = (*ptr[j])*10 - (s[i]-'0');//如果是負數,後面的數字用-的來加上去
            }
        }
    }
    return j;
}
#endif
Editor is loading...