- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法获得解决此问题的功能程序(Kochan 的“C 语言编程”中的练习 5.6。目标是让程序接受输入数字,例如 123,并输出“一二三”。
现在,我有两个循环,结果是我想要输出的数字(2345 中的等式)2 将是所需的输出。我还希望跟踪多个并从初始表达式中减去。所以 2345 将输出 2,然后循环,并从 2345 中减去 2000。该程序应该计算 345,输出 3,并减去 300 等。然后我会担心将其转换为单词。
经过几个小时的工作后,我想知道是否存在我遗漏的根本缺陷。
作为引用,下一章是数组。这本书涵盖了循环(for、while、判断、if、else、switch)
到目前为止我得到的代码如下。我正在努力让它循环,并且已经为此奋斗了好几天。我所要展示的只是下面的代码,无可否认,它比我开始时要干净得多。下面显示的部分有效,但是当我尝试循环它时它失败了。到了这个地步,我只是尝试一切,但无济于事。再次,我希望下面的内容能够循环。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int number, counter, output;
printf ("Please input your number\n");
scanf (" %i", &number);
for (counter = 0; number > 9; counter = counter +1){
number = number /10;
output = number;
}
while (counter != 0){
number = number * 10;
counter = counter - 1;
}
printf ("%i %i\n", number, output);
return 0;
}
最佳答案
我尝试实现您在问题中陈述的概念,这是您的代码,并进行了一些更改以使其正常工作!希望它有帮助!!
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int number, counter, output;
//you need to create another variable to store the previous number
// in order to be able to make the substraction
int originalNumber;
printf ("Please input your number\n");
scanf (" %i", &number);
while(number!=0)
{
originalNumber=number; // preserve the number in every loop
for (counter = 0; number >= 10; counter = counter +1)
{
// we changed the condition in the for loop in order to keep
// only inside the number the left most digit
number = number /10;
}
// the output will contain the left most digit in every loop
output=number;
while (counter > 0)
{
number = number * 10;
counter = counter - 1;
}
// we get the new value by doing the substraction
// 345= 2345 - 2000
number = originalNumber - number;
// print the new number for the next iteration
// and the left most digit
printf ("%i %i ", number, output);
//print the output using letters
switch(output)
{
case 0:
printf("Zero \n");
break;
case 1:
printf("One \n");
break;
case 2:
printf("Two \n");
break;
case 3:
printf("Three \n");
break;
case 4:
printf("Four \n");
break;
case 5:
printf("Five \n");
break;
case 6:
printf("Six \n");
break;
case 7:
printf("Seven \n");
break;
case 8:
printf("Eight \n");
break;
case 9:
printf("Nine \n");
break;
default:
break;
}
}
return 0;
}
现在,通过 swich()
语句用字母打印每个数字变得很容易!
您可以更改printf()
的格式以获得所需的输出!!
关于c - 练习 5.6 Kochan 的 C 语言编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28030532/
K&R 前。 4.2 要求您修改给定的(非标准)atof 函数,该函数缺少处理指数的指数处理机制(如 123e6 或 456e-7)。我添加了一个最小的更改来处理正确输入的、无空格的个位数指数。为了检
我正在学习计算机科学入门类(class)的考试,我对“常规”算法和递归算法中的复杂性主题有疑问(通常我们将这些问题写成 C 代码). 我想知道 Internet 和/或书籍中是否有涵盖该主题的基础级别
console.log( ‘blah’.repeatMe( 3 ) ); 使用 Javascript 编写代码,使前面的函数打印: 输出:blahblahblah 最佳答案 噢,放弃函数式解决方案太有
我正在准备 Java SE 7 认证考试,并且正在做一些关于继承和访问修饰符的无聊练习。 但是现在我在应用继承时遇到了意外的行为。在我的基础包 com.testpkg 中,我有一个抽象类: packa
我刚刚开始了 C 语言队列的第一课,我得到了创建队列、向队列添加元素和删除元素的练习。但是,我在检查队列是满还是空时遇到了麻烦。 #include typedef struct FloatQueue
请问我从昨天开始就被困在下面这个问题中了。下面是问题: Write a program that uses console.log to print all the numbers from 1 to
我最近尝试了一些 Java,希望对我的风格进行一些评论。如果你喜欢看这个放在图像中的练习,并告诉我我的风格是否足够好?或者是做的还不够好,可以告诉我应该在哪方面多下工夫,帮我改进一下? exercis
我对手动编写 SQL 查询还很陌生,而且我有一个我似乎无法解决的练习。 我了解解决此问题所需的工具,但我就是想不出解决方案。 你能帮助我理解如何以一种能让我在未来解决类似练习的方式解决这个问题吗? 我
好吧,这就是练习: Define a class named student, containing three grades of students. The class will have a f
我是一个 JS 菜鸟,试图制作这个“你好,先生/小姐 你的名字!”干净的。我看不到在 if/else 中重构警报的方法,因为那样我就失去了 var b 的值。 JS: "use strict
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
反转二维数组的值,可以扩展 n 次。 [1, [2, [3, ... [n, null]]]] 给定: 所有数组的长度始终为 2 列表中的最后一个数组将包含一个 null 索引 1 示例: [1, [
我试图通过 Jason Hickey 笔记自学 OCaml,下面的练习让我难住了。 问题:编写一个函数 sum 给定两个整数边界 m,n 和函数 f 计算求和。 我正在尝试这个: let r
这是一个生成斐波那契数列的程序,这里是引用:http://sicp.org.ua/sicp/Exercise1-19 据说我们可以将程序视为“a <- bq + aq + ap and b <- bp
所以,我正在努力通过 SICP。 第 4 章的第一个练习是: Exercise 4.1. Notice that we cannot tell whether the metacircular eva
这个问题已经有答案了: Count the number of occurrences of a character in a string in Javascript (39 个回答) 已关闭 6
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我目前正在学习 JS,并且正在尝试进行编码练习。到目前为止,我已经成功地使用离线和在线部分代码的大量资源拼凑了以下代码。我已经非常接近了 - 只是结果中的数字无法正确。 一些背景:在函数中输入一个对象
我需要创建一个回收器 View 练习,这是一个带有简单的单个回收器的应用程序加载大小为 20 的页面,并且可以容纳无限数量的项目。 现在我不想做出重新加载越来越多的项目的幼稚解决方案,而是一个优雅的解
下面的实现正确吗? 输入:Oldrecords(GameRecord 对象数组)和 newRecords (GameRecord) 我将检查 oldRecords 数组中的 newRecord 值。如
我是一名优秀的程序员,十分优秀!