-不重复。计划目标是相同的,但需要不同的方法来实现,如下所述。问题有两个部分,如果我没有完全按照需要的方式完成第一部分,那么我就无法完成第二部分。-
我需要编写一个 C 程序,然后编写一个基于该 C 程序在功能上相同的汇编程序。我发现我的 C 程序:它读取包含数字的用户输入,然后用一个单 (') 或双 (") 来表示英尺和英寸,并不断提示用户直到他们输入 0,然后汇总所有输入并打印总长度以英寸为单位,不适用于以下 assembly 函数:
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;
}
显然我无法“将数字和引号解析成多个部分”。我需要使用与上面列出的汇编函数类似的函数。我不知道该使用什么,因为我觉得我已经尝试过一些与它们类似的东西。有人告诉我 strtol() 与 readUInt() 类似,我想这很好?无论如何,我不需要将字符串解析为来自标准输入的部分,而是需要“将值中的值读入两个单独的部分”。所以我想区别在于,我需要立即将输入读取为两个不同的值,而不是读取一部分然后进行解析。我怎样才能用我目前拥有的东西做到这一点?请注意 assembly 函数可以做什么。当我开始编写汇编版本时,我必须能够使用这些函数来代替 C 函数。
最佳答案
鉴于您仅使用 encrypted.o
中提供的函数的要求,您必须做出一些假设。 (1) encrypted.o
包含函数定义。 (2) 它有一个可以包含的头文件,以便您可以使用提供的函数。 (3) readUInt
将在到达第一个非数字字符后停止读取并将其放回 stdin
中。
根据这些标准,并且仅使用提供的函数,您的代码将如下所示:
#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
,我们可以进行测试。
关于C 程序将值读入两个单独的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29462390/