2013-08-20 147 views
0

我試圖將下列公式轉換爲C,但未能獲得正確的語法。基本上,我使用今天的日期減去一組定義的日期來查看mod的剩餘部分是否匹配28天的輪轉,如果是,則提供所需的答案。下面是我試圖轉換爲C的Excel工作公式...將Excel公式轉換爲C

=IF(OR(
MOD(TODAY()-DATE(2013,1,7),28)=0, 
MOD(TODAY()-DATE(2013,1,16),28)=0, 
MOD(TODAY()-DATE(2013,1,25),28)=0, 
MOD(TODAY()-DATE(2013,2,4),28)=0, 
MOD(TODAY()-DATE(2013,2,13),28)=0, 
MOD(TODAY()-DATE(2013,2,22),28)=0)=TRUE,"Option1",IF(
OR(
MOD(TODAY()-DATE(2013,1,9),28)=0, 
MOD(TODAY()-DATE(2013,1,18),28)=0, 
MOD(TODAY()-DATE(2013,1,28),28)=0, 
MOD(TODAY()-DATE(2013,2,6),28)=0, 
MOD(TODAY()-DATE(2013,2,15),28)=0, 
MOD(TODAY()-DATE(2013,2,25),28)=0)=TRUE,"Option2",IF(
OR(
MOD(TODAY()-DATE(2013,1,11),28)=0, 
MOD(TODAY()-DATE(2013,1,21),28)=0, 
MOD(TODAY()-DATE(2013,1,30),28)=0, 
MOD(TODAY()-DATE(2013,2,8),28)=0, 
MOD(TODAY()-DATE(2013,2,18),28)=0, 
MOD(TODAY()-DATE(2013,2,27),28)=0)=TRUE,"Option3",IF(
OR(
MOD(TODAY()-DATE(2013,1,14),28)=0, 
MOD(TODAY()-DATE(2013,1,23),28)=0, 
MOD(TODAY()-DATE(2013,2,1),28)=0, 
MOD(TODAY()-DATE(2013,2,11),28)=0, 
MOD(TODAY()-DATE(2013,2,20),28)=0, 
MOD(TODAY()-DATE(2013,3,1),28)=0)=TRUE,"Option4","Not Required")))) 

某種靈魂能幫助我控制正確的方向嗎?

+0

如果您想在C中使用解決方案,爲什麼要添加C#標記? – Save

+0

你可以發佈你的C代碼不能正常工作嗎? – Macattack

+0

道歉我刪除了以前添加的C#標籤錯誤。 – user2701654

回答

1

我認爲這可能是相關的:

http://www.codeproject.com/Articles/2750/Excel-serial-date-to-Day-Month-Year-and-vise-versa

轉換後的代碼如下

#include <time.h> 
#include <stdio.h> 

int DMYToExcelSerialDate(int nYear, int nMonth, int nDay) 
{ 
// Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a 
// leap year, but Excel/Lotus 123 think it is... 
if (nDay == 29 && nMonth == 02 && nYear==1900) 
    return 60; 

// DMY to Modified Julian calculatie with an extra substraction of 2415019. 
long nSerialDate = 
     (int)((1461 * (nYear + 4800 + (int)((nMonth - 14)/12)))/4) + 
     (int)((367 * (nMonth - 2 - 12 * ((nMonth - 14)/12)))/12) - 
     (int)((3 * ((int)((nYear + 4900 + (int)((nMonth - 14)/12))/100)))/4) + 
     nDay - 2415019 - 32075; 

if (nSerialDate < 60) 
{ 
    // Because of the 29-02-1900 bug, any serial date 
    // under 60 is one off... Compensate. 
    nSerialDate--; 
} 

return (int)nSerialDate; 
} 

int main (void) 
{ 
int serial_date; 
time_t now; 
struct tm *date_time; 

time (&now); 

date_time = localtime(&now); 
serial_date = DMYToExcelSerialDate((date_time->tm_year+1900), (date_time->tm_mon + 1) ,date_time->tm_mday); 


if (((serial_date - DMYToExcelSerialDate(2013,1,7))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,1,16))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,1,25))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,4))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,13))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,22))%28 == 0)) 
{ 
    printf("option1\n"); 
    return 1; 
} 
else if (((serial_date - DMYToExcelSerialDate(2013,1,9))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,1,18))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,1,28))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,6))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,15))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,25))%28 == 0)) 
{ 
    printf("option2\n"); 
    return 2; 
} 
else if (((serial_date - DMYToExcelSerialDate(2013,1,11))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,1,21))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,1,30))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,8))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,18))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,27))%28 == 0)) 
{ 
    printf("option3\n"); 
    return 3; 
} 
else if (((serial_date - DMYToExcelSerialDate(2013,1,14))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,1,23))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,1))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,11))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,2,20))%28 == 0) || 
    ((serial_date - DMYToExcelSerialDate(2013,3,1))%28 == 0)) 
{ 
    printf("option4\n"); 
    return 4; 
} 
else 
{ 
    printf("not required\n"); 
    return 0; 
} 

} 

請注意轉換後的代碼利用了源代碼根據共同開發和分發許可證(CDDL)許可,如所述在上面的文章中編輯。

0

我不是一個Excel函數嚮導,但我敢肯定,以下三個功能 可以幫助:時間(),本地時間()和mktime()。每個都是ANSI C並有詳細記錄。通過獲取當前時間,然後在函數之間來回傳遞(通過指向結構或time_t值的指針),您可以操縱或檢查不同的時間值。 簡單的例子:

#include <windows.h> 
#include <ansi_c.h> 

void timeFunc(void); 

void main(void) 
{ 
    timeFunc(); 
} 

void timeFunc(void) 
{ 
    struct tm *today; 
    struct tm *modified_time; 
    time_t secCntGMT1900 = 0; 
    time_t modifiedTime; 
    // Get GMT time in seconds since 1900 
    time(&secCntGMT1900); 

    // Convert GMT time to local time to local time format 
    today = localtime(&secCntGMT1900); 

    //modify 'today' members: 
    today->tm_hour = 0; 
    today->tm_min = 0; 
    today->tm_sec = 0; 
    today->tm_mon = 2; 
    today->tm_mday =1; 
    today->tm_year; 

    //modified struct passed to mktime() 
    modifiedTime = mktime(today); 
}