Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.0 kB
3
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include "function.h"

int solver(int **ptr, int *sum, char *s){
    
    int len = strlen(s);
    int neg[len];
    int j = 0, d = 0;//用tmp判斷讀到下一個數字了沒
                       //j為index,用來儲存每個數字的值
    for(int i = 0; i <= len; i++) neg[i] = 1;
    for(int i = 0; i <= len; i++){

        if(d == 1 && !isdigit(s[i])){
            *sum += *ptr[j];
            d = 0;
            j++;
        }
        if(s[i] == '-') neg[j] = -1;//如果是負數,先初始化成INT_MAX方便判斷
        if(isdigit(s[i])){
            d = 1;
            if(neg[j] >= 0){
                *ptr[j] = (*ptr[j])*10 + (s[i] - '0');
            }
            else{
                // if(*ptr[j] == -1) *ptr[j] = -(s[i]-'0');
                // else *ptr[j] = (*ptr[j])*10 - (s[i]-'0');//如果是負數,後面的數字用-的來加上去
                *ptr[j] = (*ptr[j])*10 - (s[i]-'0');
            }