-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW54.cpp
More file actions
50 lines (48 loc) · 992 Bytes
/
Copy pathHW54.cpp
File metadata and controls
50 lines (48 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<stdio.h>
#include<string.h>
#pragma warning (disable : 4996)
void inputString(const char*, char*);
int cal_number(char*);
int numPlace(int place); // 10의 제곱수 구하는 함수(자릿수)
int main()
{
char str[100];
int result;
while (1) {
inputString("# 문장을 입력하시오 : ", str);
if (strcmp(str,"end") == 0) { break; }
result = cal_number(str);
printf("\"%s\" 내의 총 숫자는 [%d]입니다.\n\n",str,result);
}
return 0;
}
void inputString(const char* msg, char* str)
{
printf("%s", msg);
scanf("%s",str);
}
int cal_number(char* str)
{
int number[10] = {0,}, res = 0, i = -1;
int len = strlen(str);
while(i != len){
int numberlen = 0;
i++;
while ((str[i] >= 48) && (str[i] <= 57)) {
number[numberlen] = str[i] - '0';
numberlen++;
i++;
}
for (int j = numberlen-1; j >= 0; j--) {
res += number[numberlen-1-j]* numPlace(j);
}
}
return res;
}
int numPlace(int place) {
int i, res = 1;
for (i = 0; i < place; i++) {
res *= 10;
}
return res;
}