- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
请多多指教!我正在完成“C 语言编程”一书中的练习。
我必须编写一个程序,它接受一个整数,然后提取并用英语显示整数的每一位。
因此,如果我输入 1234,它应该打印回“一二三四”。
由于这个练习接近本书的开头,它还没有教授数组、函数、指针或字符串。我认为这意味着我不允许使用它们中的任何一个。所以我必须用非常有限的选项以某种方式解决它。
我写的内容或多或少有效,但数字以相反的顺序打印回来。我真的很难找到一个替代方案,并且一直在研究过去几个人的代码。
我意识到几年前发布了另一个非常相似的问题,但我在这个问题上能做的更有限,更不用说,他/她的问题要复杂得多。
如果您能看一看并提供一些建议,我将不胜感激。
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int digit;
//Accept integer
printf("Choose a number.\n");
int num = GetInt();
// adding minus to the beginning if int is negative
if (num < 0)
{
num = -num;
printf("minus ");
}
// isolate each digit from integer and then print in english
do
{
digit = num % 10;
switch(digit)
{
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
case 10:
printf("Ten ");
break;
default:
break;
}
num /= 10;
} while(num != 0);
printf("\n");
}
最佳答案
假设你不能使用递归,因为你不能定义你自己的函数,你需要一个初始循环来找到最左边的数字位置。
以下代码使用 place
变量将 10 提高到“位数减 1”,将零视为单个数字。主循环将(剩余部分)数字除以 place
,打印出该数字,然后减少数字模 place
并除以 place
下一次迭代增加 10,直到打印完所有数字(当 place
为零时)。
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int digit;
//Accept integer
printf("Choose a number.\n");
int num = GetInt();
// adding minus to the beginning if int is negative
if (num < 0)
{
num = -num;
printf("minus ");
}
// get 10 to the power of 'number of digits minus 1'
int place;
for (place = 1; place <= num / 10; place *= 10)
;
// isolate each digit from integer and then print in english
do
{
digit = num / place;
switch(digit)
{
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
default:
break;
}
num %= place;
place /= 10;
} while (place != 0);
printf("\n");
}
关于C. 我的数字以相反的顺序打印(英文)?我怎样才能解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378946/
在 .m 文件中: UIActionSheet *someSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"TI
常用HTML正则表达式 1.只能输入数字和英文的: 复制代码 代码如下: <input onkeyup="value=value.replace(/[/W]/g,'
前言 本文主要介绍使用UITextField限制只可输入中,英文,数字,我们可以使用NSPredicate正则表达式可以过滤,下面看看详细的步骤方法 首先设置UItextField的代理 实现
本文实例讲述了php判断输入是否是纯数字,英文,汉字的方法。分享给大家供大家参考。具体分析如下: 这里利用php的mb_strlen和strlen函数就可以轻松得知字符串的构成是全英文、英汉混合、
给定一个文本字符串 var string1 = 'IAmNotFoo'; 如何只提取大写字母? 'IANF' 下面是每个链接的一些方法: function isUpperCase1(aCharacte
如何在 PHP tcpdf 中生成连字? 最佳答案 tcpdf 使用 FreeType 来栅格化字体,或者只是将它们嵌入到 PDF 中并在查看器中栅格化。连字实际上是在字体中定义的。如果您使用的是 T
请多多指教!我正在完成“C 语言编程”一书中的练习。 我必须编写一个程序,它接受一个整数,然后提取并用英语显示整数的每一位。 因此,如果我输入 1234,它应该打印回“一二三四”。 由于这个练习接近本
只能输入中文:<input id="input1" type="text" onkeyup="value=value.replace(/[^\
复制代码 代码如下: $str='asb天水市12'; if (preg_match("/^[\x7f-\xff]+$/", $str)){ echo '全部是汉字'; }
我发现这个公式可以将金额转换为单词,但它不能 100% 起作用。每当金额以 20,30, 40 等结尾时,都会出现错误。例如 1020 会出错,但 1019 和 1021 可以正常工作。我试图自己解决
某些 Windows 计算机安装了多种显示语言。 我可以使用以下函数以该语言获取当前用户的 UI 语言: function GetUsersWindowsLanguage: string; var
引言需求:(输入框限制输入多少字符) 1、一个字母、符号、数字相当于一个字符 2、一个汉字相当于两个字符 3、不能输入特殊字符 4、不能输入emoji表情 直接上代码 1、注册通知
我目前正在使用 Emacs + Magit,但我想我的问题比这些软件的具体使用更笼统。我读过 How does one change the language of the command line
有谁知道包含示例英语词典单词脚本的 Web 引用链接,可用于填充 SQL Server 中的词典表? 我可以处理 .txt 或 .csv 文件,或类似的文件。 或者,我正在向我的网络应用程序添加自定义
Oracle(多语言环境)如何判断给定字符串(表列值)的语言(英文,中文...)? 最佳答案 应该可以使用像 Language Dectection for Java 这样的库并将它与您的 PL/SQ
当您现在在 Xcode 5 上本地化一个文件时,您将拥有这个“基本”文件,据我所知,如果用户使用的是另一种未本地化的语言,则会使用该文件。在旧版本的 Xcode 上,您没有。 我希望英语成为默认使用的
我有如下文字: 아니다 bukan 싫다 tidak suka 훌륭하다 bagus 我正在尝试删除英文行(英文字母)并将其附加到上行(韩文字母)的末尾,如下所示: 아니다bukan 싫다tidak
这个问题在这里已经有了答案: Unicode string literals in VBA (3 个回答) How to type Unicode currency character in Visu
我可以知道是否有任何 SQL 脚本可以满足我的以下场景? 如果column1包含中文字符,更新值column2为'Chinese' 如果 column1 包含英文字符,则将值 column2 更新为“
我正在使用一个图像处理 API 来读取图像上的文本,并且从我返回的字符串数据中,我需要提取英语或字典中的单词以及常见的名字和姓氏。换句话说,我在字符串中得到了我需要的文本,但在结果中也得到了一些我需要
我是一名优秀的程序员,十分优秀!