簡體   English   中英

C 程序上的奇怪 output

[英]Weird output on C program

我的任務是創建一個從 CL 中提取輸入並與它們一起工作的程序

它應該執行以下操作

Input./name 測試 + 最佳 + Rest Output TestBestRest

輸入./名稱 600 + 500 - 100 Output 1000

單詞只會連接到字符串,但數字必須能夠添加或減去。 在混合字符串中,所有內容都被視為字符串而不是 integer。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


int main(int argc, char *argv[])
{
    char arr1[15][25]={'\0'};
    int nums=0;
    int temp;
    char tempstring[15]={'\0'};
    char symbol[15]={'\0'};
    int tempnum=0;
    int endnum=0;
    char strcat[100]={'\0'};

    printf("This program only recognizes 10 command line entries\n");

    for(int indx=1;indx<argc;indx+=1)
    {
        char *input = argv[indx];
//      if(isalnum(input[indx]))
            strncpy(tempstring,argv[indx],14);
        if (indx>1)
            strncpy(symbol,argv[indx-1],14);
        printf("index is %d\n",indx);
            printf("Tempstring is %s\n\n",tempstring);
        tempnum=atoi(tempstring);
            printf("Tempnum is %d\n",tempnum);
        if(tempnum!=0)
        {
            printf("inside %d\n", indx);

            printf("Temp num %d and End num is %d\n", tempnum,endnum);
//          printf("is num\n");
            printf("Tempstring is %s\n\n",tempstring);
            printf("Tempnum is %d\n",tempnum);
            printf("symbol is %c\n\n",symbol[0]);

            if (symbol[0]==45)
            {
                endnum=endnum-tempnum;
                printf("minus\n");
            }
            else if(symbol[0]==43)
            {
                printf("plus\n");
                endnum=endnum+tempnum;
            }else if (indx==1)
            {
                printf("no sym\n");
                endnum=endnum+tempnum;
            }
        }

        if (isalpha(input[indx])!=0)
        {
            printf("argv[%d] = %s\n",indx, tempstring);
            //strncpy(arr1[indx],argv[indx],10);
            if (tempstring[0]!=43)
                strncat(strcat,tempstring,14);
        }

    }
        printf("End number %d\n",endnum);
    printf("Concatenated %s\n", strcat);

    return 0;
}

代碼按要求執行(我很抱歉檢查 printfs 的錯誤),但有一個例外

如果輸入./name Test + Rest + Best + 600 則輸出TestRest600。

這就是 printfs 的代碼結果

─╼ $./a6p1 Test + Rest + Best + 600
This program only recognizes 10 command line entries
index is 1
Tempstring is Test

Tempnum is 0
argv[1] = Test
index is 2
Tempstring is +

Tempnum is 0
argv[2] = +
index is 3
Tempstring is Rest

Tempnum is 0
argv[3] = Rest
index is 4
Tempstring is +

Tempnum is 0
argv[4] = +
index is 5
Tempstring is Best

Tempnum is 0
index is 6
Tempstring is +

Tempnum is 0
argv[6] = +
index is 7
Tempstring is 600

Tempnum is 600
inside 7
Temp num 600 and End num is 0
Tempstring is 600

Tempnum is 600
symbol is +

plus
argv[7] = 600
End number 600
Concatenated TestRest600

任何幫助都會受到歡迎,我知道它有一些垃圾代碼,我需要在第二次通過時清理它們,但我希望先解決這個問題。

你的問題是:

if (isalpha(input[indx])!=0)

變量indx用於將 arguments 索引到程序中,在這里您使用它來索引參數中的字符,這是一個非常糟糕的主意。 對於參數Best index 等於5並且訪問 "Best"[5] 將給您一個 '\0',它是一個字符串終止符,而不是一個字母,這就是Best沒有連接的原因。

您可能應該改用它:

if (isalpha(input[0])!=0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM