如何在C编程中打印一定数量的星? [英] How do I print a certain number of stars in C programming?
问题描述
我希望计算机分析我写的内容并打印与输入的数字对应的星号数。我只能管理一段可以读取我输入内容的代码。我不能使用数组,但可以使用循环指令,如jnz和条件指令,如cmp。
我尝试过:
这是我到目前为止尝试的代码:
#includestdafx.h
#include< stdlib.h>
void printChar(char c);
void printStr(char * strAddr) ;
void printInt(int someInt);
// **************** ************************************************** ******** //
//定义了变量。 //
// ************************************** ************************************ //
int numItems;
int * items; //指向项目的指针
int anItem;
int wmain(int argc,_TCHAR * argv [])
{
items =(int *)malloc(1000);
numItems = 0;
do
{
printf(Enter item%d(0表示数据结束):,numItems + 1);
scanf(%d,& anItem);
items [numItems] = anItem;
numItems ++;
} while(anItem!= 0);
// ******************************************** ************************** //
//进入汇编程序。 //
// ************************************** ******************************** //
__asm
{
jmp完成
printNewLine:
push'\ r'//两行打印字符。
调用printChar
push'\ n'//两行打印另一行char。
致电printChar
ret
完成://做什么都没有
}
// ********************** ************ //
//汇编程序。 //
// ************************************** ******************************** //
printf(按回车键退出\\ n);
char dummy [10]; //以防万一缓冲区中的几个键
scanf(%c,dummy); //暂停。
scanf(%c,虚拟); //暂停。再一次。奇怪的事情发生了。
}
// ** ************************************************** ****************** //
//打印单个字符。 //
//将要打印的字符推入堆栈;先进先出//
//数据结构。请记住,与C#不同,这里的char大小为1个字节。 //
// //
//参数输入:将单个字符串推入堆栈,如上所示。 //
//返回:没什么。 //
//其他问题:它是否保留CPU寄存器eax,ebx,ecx,edx,//
// esi,edi等?不知道(它将我们带入C,所以//
//假设没有。//
// *********** ************************************************** ********* //
void printChar(char c)
{
printf(%c,c ); //%c表示作为字符
} //我们不会看到ret指令,除非您查看.cod列表
//在debug文件夹中。
// * ************************************************** ******************* //
//打印整个字符串,必须以零字节结束.//
//它需要一个参数,这是字符串的起始地址.//
// ****************** ************************************************** ** //
void printStr(char * strAddr)
{
printf(%s,strAddr);
} //除非你在debug文件夹中查看.cod列表
//,否则我们不会看到ret指令。
// ************** ************************************************** ****** //
//打印一个整数。 //
//将要打印的整数推入堆栈;先进先出//
//数据结构。请记住,这里的整数大小为4个字节。 //
// //
//参数in:将一个整数推入堆栈,如上所述。 //
//返回:没什么。 //
//其他问题:它是否保留CPU寄存器eax,ebx,ecx,edx,//
// esi,edi等?不知道(它将我们带入C,所以//
//假设没有。//
// *********** ************************************************** ********* //
void printInt(int someInt)
{
printf(%d,someInt );
}
首先,我认为没有理由在那里使用asm。你所做的一切都可以在C.对于你的问题,你有一个do-while循环获取数据并计算你有多少。为什么你不能再做一个do-while循环输出数据?
还有一件事 - 你把它标记为unicode但是你到处都在使用char。我看不出与unicode有关的任何内容。
for (i = 0 ; items [i]!= 0 ; i ++) // for ea数组中的ch条目
{
int j;
for (j = 0 ; j< items [i]; j ++) // 此项目条目中的计数
printChar(' *'); // 打印星数
printChar(' \ n'); // 明星后的新行
}
I want the computer to analyse what I have written and print the number of asterisks corresponding to the number inputted. I can only manage a piece of code that can read what I have inputted. I can't use arrays but can use loop instructions like jnz and conditional instructions like cmp.
What I have tried:
This is the code I have tried so far:
#include "stdafx.h"
#include <stdlib.h>
void printChar(char c);
void printStr(char *strAddr);
void printInt(int someInt);
//**************************************************************************//
// Variables defined. //
//**************************************************************************//
int numItems;
int *items; // Pointer to the items
int anItem;
int wmain(int argc, _TCHAR* argv[])
{
items = (int *)malloc(1000);
numItems = 0;
do
{
printf("Enter item %d (0 means end of data): ", numItems + 1);
scanf("%d", &anItem);
items[numItems] = anItem;
numItems++;
} while (anItem != 0);
//**********************************************************************//
// Into assembler. //
//**********************************************************************//
__asm
{
jmp finish
printNewLine:
push '\r' // Two lines to print a char.
call printChar
push '\n' // Two lines to print another char.
call printChar
ret
finish: // Do nothing
}
//**********************************************************************//
// Out of assembler. //
//**********************************************************************//
printf("press enter to quit\n");
char dummy[10]; //Just in case several keys in buffer
scanf("%c", dummy); //pause.
scanf("%c", dummy); //pause. And once more. Something weird going on.
}
//**********************************************************************//
// Prints a single character. //
// Push the char to be printed onto the stack; a First In Last Out //
// data structure. Remember, unlike C#, a char here is 1 byte in size. //
// //
// Parameters in: Push a single char onto the stack, as above. //
// Returns: Nothing. //
// Other issues: Does it preserve CPU registers eax, ebx, ecx, edx, //
// esi, edi etc.? No idea (it takes us into "C", so //
// assume not. //
//**********************************************************************//
void printChar(char c)
{
printf("%c", c); //%c means as a char
} // we don't seee the "ret" instruction unless you view the ".cod" listing
// in the "debug" folder.
//**********************************************************************//
// Print a whole string, which must end with a zero byte. //
// it takes one parameter, which is the start address of the string. //
//**********************************************************************//
void printStr(char *strAddr)
{
printf("%s", strAddr);
} // we don't seee the "ret" instruction unless you view the ".cod" listing
// in the "debug" folder.
//**********************************************************************//
// Prints a single integer . //
// Push the integer to be printed onto the stack; a First In Last Out //
// data structure. Remember, an integer here is 4 bytes in size. //
// //
// Parameters in: push a single integer onto the stack, as above. //
// Returns: Nothing. //
// Other issues: Does it preserve CPU registers eax, ebx, ecx, edx, //
// esi, edi etc.? No idea (it takes us into "C", so //
// assume not. //
//**********************************************************************//
void printInt(int someInt)
{
printf("%d", someInt);
}
Firstly, I see no reason to use asm in there at all. Everything you are doing can be done in C. To your question, you have a do-while loop there getting the data and counting how much you have. Why can't you make another do-while loop to output the data?
One more thing - you have this tagged as unicode but you are using char everywhere. I see nothing about this that has anything to do with unicode.
for (i = 0; items[i] != 0; i++) // for each entry in the array { int j; for (j = 0; j < items[i]; j++) // for the count in this item entry printChar('*'); // print the number of stars printChar('\n'); // new line after the stars }
这篇关于如何在C编程中打印一定数量的星?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!