C程序求总位数
C program to find total number of digits
我写了这个程序来计算用户输入的一行文本的总位数。我在使用 getchar()
时遇到错误。我似乎无法弄清楚我做错了什么?
#include <stdio.h>
#define MAX_SIZE 100
void main() {
char c[MAX_SIZE];
int digit, sum, i;
digit, i = 0;
printf("Enter a line of characters>");
c = getchar();
while (c[i] != '\n') {
digit = 0;
if (c [i] >= '0' && c[i] <= '9') {
digit++;
}
}
printf("%d\n", digit);
}
我将使用 sum 变量添加我找到的所有数字。但我在 getchar()
行收到错误。帮助??
您可以在不使用数组的情况下输入 "line of text"。
#include <stdio.h>
#include <ctype.h>
int main(void) { // notice this signature
int c, digits = 0, sum = 0;
while((c = getchar()) != '\n' && c != EOF) {
if(isdigit(c)) {
digits++;
sum += c - '0';
}
}
printf("%d digits with sum %d\n", digits, sum);
return 0;
}
请注意 c
是 int
类型。大多数库的字符函数不使用 char
类型。
编辑: 添加了数字总和。
Weather Vane 的回答是最好最简单的回答。但是,为了将来参考,如果您想迭代(循环)一个数组,使用 for 循环会更容易。此外,您的主要功能应该 return 一个 int 并且应该如下所示:int main()
。您需要在 main 函数的末尾放置一个 return 0;
。这是您的程序的修改版本,它使用 for 循环遍历字符数组。我使用 gets 函数从控制台读取一行字符。它会等待用户输入字符串。
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char c[MAX_SIZE];
int digit = 0;
printf("Enter a line of characters>");
gets(c);
for (int i = 0; i < MAX_SIZE; i++)
{
if (c[i] == '\n') break; // this line checks to see if we have reached the end of the line. If so, exit the for loop (thats what the "break" statment does.)
//if (isdigit(c[i])) // uncomment this line and comment or delete the one below to use a much easier method to check if a character is a digit.
if (c [i]>= '0' && c[i] <= '9')
{
digit++;
}
}
printf("%d\n", digit);
return 0;
}
使用 math.h header 中定义的 log10() 函数可以轻松获取整数的位数。考虑这个程序
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, const char *argv[]) {
system("clear");
unsigned int i, sum = 0, numberOfDigits;
puts("Enter a number");
scanf("%u", &i);
numberOfDigits = (int) (log10(x) + 1);
system("clear");
while(i != 0) {
sum += (i % 10);
i /= 10;
}
fprintf(stdout, "The sum is %i\n", sum);
fflush(stdin);
return 0;
}
我写了这个程序来计算用户输入的一行文本的总位数。我在使用 getchar()
时遇到错误。我似乎无法弄清楚我做错了什么?
#include <stdio.h>
#define MAX_SIZE 100
void main() {
char c[MAX_SIZE];
int digit, sum, i;
digit, i = 0;
printf("Enter a line of characters>");
c = getchar();
while (c[i] != '\n') {
digit = 0;
if (c [i] >= '0' && c[i] <= '9') {
digit++;
}
}
printf("%d\n", digit);
}
我将使用 sum 变量添加我找到的所有数字。但我在 getchar()
行收到错误。帮助??
您可以在不使用数组的情况下输入 "line of text"。
#include <stdio.h>
#include <ctype.h>
int main(void) { // notice this signature
int c, digits = 0, sum = 0;
while((c = getchar()) != '\n' && c != EOF) {
if(isdigit(c)) {
digits++;
sum += c - '0';
}
}
printf("%d digits with sum %d\n", digits, sum);
return 0;
}
请注意 c
是 int
类型。大多数库的字符函数不使用 char
类型。
编辑: 添加了数字总和。
Weather Vane 的回答是最好最简单的回答。但是,为了将来参考,如果您想迭代(循环)一个数组,使用 for 循环会更容易。此外,您的主要功能应该 return 一个 int 并且应该如下所示:int main()
。您需要在 main 函数的末尾放置一个 return 0;
。这是您的程序的修改版本,它使用 for 循环遍历字符数组。我使用 gets 函数从控制台读取一行字符。它会等待用户输入字符串。
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char c[MAX_SIZE];
int digit = 0;
printf("Enter a line of characters>");
gets(c);
for (int i = 0; i < MAX_SIZE; i++)
{
if (c[i] == '\n') break; // this line checks to see if we have reached the end of the line. If so, exit the for loop (thats what the "break" statment does.)
//if (isdigit(c[i])) // uncomment this line and comment or delete the one below to use a much easier method to check if a character is a digit.
if (c [i]>= '0' && c[i] <= '9')
{
digit++;
}
}
printf("%d\n", digit);
return 0;
}
使用 math.h header 中定义的 log10() 函数可以轻松获取整数的位数。考虑这个程序
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, const char *argv[]) {
system("clear");
unsigned int i, sum = 0, numberOfDigits;
puts("Enter a number");
scanf("%u", &i);
numberOfDigits = (int) (log10(x) + 1);
system("clear");
while(i != 0) {
sum += (i % 10);
i /= 10;
}
fprintf(stdout, "The sum is %i\n", sum);
fflush(stdin);
return 0;
}