簡體   English   中英

C程序將值讀入兩個單獨的部分中?

[英]C program read in values in to two separate parts?

-不可重復。 程序目標是相同的,但是需要不同的實現方式,下面將對此進行說明。 這個問題有兩個部分,如果我沒有完全按照需要做的第一部分,那么我就不能做第二部分。

我需要編寫一個C程序,然后基於該C程序編寫一個在功能上相同的Assembly程序。 我發現我的C程序:讀取包含數字的用戶輸入,然后輸入單個(')或雙(“)來表示英尺和英寸,並不斷提示用戶直到他們輸入0,然后對所有輸入進行匯總並打印總長度(以英寸為單位)不適用於以下裝配功能:

void printStr(char *)
 Arguments:
 edi = address of null-terminated string to print
 Returns:
 Nothing

void printUInt(unsigned)
Arguments:
 edi = Unsigned integer to print
Returns:
 Nothing

char getchar()
Arguments:
 None
Returns:
 eax = the next character

uinsigned readUInt()
Arguments:
 None
Returns:
 eax = an unsigned int read from stdin.
       (eax is 0 on error)

這是C程序:

#include <stdio.h>
#include <string.h>
int main(void)
{
    char value[50];
    char *end;
    int sum = 0;
    int conv;
    do
    {
            printf("Enter a measurement and unit(Ex: 4' or 3\"; 0' or 0\"    when done): ");
            fgets(value, 50, stdin);
            conv = strtol(value, &end, 10);
            if(strstr(value, "\'") != NULL)
            {
                    conv = strtol(value, &end, 10);
                    sum = sum + (conv*12);
            }
            else if(strstr(value, "\"") != NULL)
            {
                    conv = strtol(value, &end, 10);
                    sum = sum + conv;
            }
    }
    while (conv != 0);

    printf("Total: %d, %s\n", sum, "inches" );
    return 0;
}

顯然,我不能“將數字和引號分成多個部分”。 我需要使用與上面列出的Assembly函數相似的函數。 我不確定該使用什么,因為我覺得我已經嘗試過一些類似的方法。 有人告訴我strtol()與readUInt()類似,我猜那很好嗎? 無論如何,不​​是將字符串解析為來自stdin的部分,而是需要“將值中的值讀入兩個單獨的部分”。 所以我想區別是不是先讀一部分然后解析,我需要立即將輸入讀取為兩個不同的值。 我該如何使用目前擁有的東西進行操作? 請注意匯編功能可以執行的操作。 當我去編寫Assembly版本時,我必須能夠使用那些函數代替C函數。

由於只使用所提供的功能的要求encrypted.o ,也有一些假設,你必須作出。 (1) encrypted.o包含函數定義。 (2)它具有可包含的頭文件,因此您可以使用提供的功能。 (3)在到達第一個非數字字符並將其放回stdin后, readUInt將停止讀取。

根據這些條件,僅使用提供的功能,您的代碼將如下所示:

#include <stdio.h>

/* encrypted.h must contain declarations for the functions below, and
   must contain all required headers beyond `stdio.h` required here */
#include "encrypted.h"

/* function prototypes */
void printStr(char *);
void printUInt(unsigned);
unsigned readUInt();

int main(void)
{
    int c = 0;                 /* always initialize ALL variables */
    unsigned measurement = 0;
    unsigned sum = 0;

    while (1)
    {
        printStr ("Enter a measurement and unit(Ex: 4' or 3\"; 0' or 0\"    when done): ");

        /* read/validate measurement with readUInt() */
        if ((measurement = readUInt()) == 0|| measurement == EOF) {
            printStr ("notice: 0 read as measurement, or [CTRL+D] pressed, exiting.\n");
            break;
        }

        /* read/validate unit with getchar() */
        if (!(c = getchar()) || c == EOF) {
            printStr ("error: 0 unable to read unit.\n");

            /* empty the input buffer */
            while ((c = getchar()) != '\n' && c != EOF);

            /* get next input */
            continue;
        }

        if ( c == '\'')
        {
            sum += measurement * 12;        /* update sum as inches */
        }
        else if (c == '\"')
        {
            sum += measurement;             /* update sum as feet   */
        }
        else
            printStr ("error: invalid or no unit read.\n");

        /* empty the input buffer */
        while ((c = getchar()) != '\n' && c != EOF);
    }

    /* print final result using only printStr & printUInt */

    printStr ("\nTotal: ");
    printUInt (sum);
    printStr (" inches.\n\n");

    return 0;
}

請注意,沒有encrypted.o/encrypted.h ,除了您之外,其他任何人都無法測試。 編譯類似於以下內容:

gcc -Wall -Wextra -c -o yourfile.o yourfile.c

鏈接類似的內容:

ld -o yourprog yourfile.o encrypted.o

如果您可以提供encrypted.o我們可以進行測試。

#include <stdio.h>
int main(void)
{
    int value;
    int unit;
    int sum =0;
    do
    {
            printf("Enter a measurement and unit(Ex: 4' or 3\"; 0' or 0\" when done): ");
            scanf("%d,%d", &value, &unit);
            unit = getchar();
            if(unit == '\'')
            {
                    sum = sum + (value*12);
            }
            else if(unit == '\"')
            {
                    sum = sum + value;
            }
    }
    while (value != 0);

    printf("Total: %d, %s\n", sum, "inches" );
    return 0;
}

暫無
暫無

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

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