수정버전 ¶
~cpp
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
const int MAX_SIZE_NAME=25;
const int BACK_SPACE=8;
const int ENTER=13;
void input_and_calculate(int*);
int input_and_return_cursur(char*, int);
void calculate(char*, int, int*, int);
int input_to_calculator(int);
void output(int*);
void main()
{
int name_score[2];
input_and_calculate(name_score);
output(name_score);
}
void input_and_calculate(int *name_score)
{
for (register int i=0; i<2; ++i)
{
char temp_save_name[MAX_SIZE_NAME]={0,};
calculate(temp_save_name, input_and_return_cursur(temp_save_name, i), name_score, i);
printf ("\n");
}
}
int input_and_return_cursur(char *temp_save_name, int i)
{
int cursur=0;
printf ("%d번째 사람의 이름을 입력해주세요.\n>>",i+1);
while (cursur<MAX_SIZE_NAME)
{
char temp_char;
temp_char=getche();
if (BACK_SPACE==temp_char)
{
printf(" \b");
--cursur;
}
else if(ENTER==temp_char)
break;
else
{
temp_save_name[cursur]=temp_char;
++cursur;
}
}
return cursur;
}
void calculate(char *temp_save_name, int cursur, int *name_score, int i )
{
for (register int j=0; j<cursur; ++j)
{
tolower(temp_save_name[j]);
if ('a'<=temp_save_name[j] && temp_save_name[j]<='z')
name_score[i]+=temp_save_name[j]-'a'+1;
}
name_score[i]=input_to_calculator(name_score[i]);
}
int input_to_calculator(int number)
{
if (number<10)
return number;
else
{
int temp_number=0;
while (number>0)
{
temp_number+=number%10;
number/=10;
}
return input_to_calculator(temp_number);
}
}
void output(int *name_score)
{
float result_percent;
if (name_score[0]>name_score[1])
result_percent=(float)name_score[1]*100/(float)name_score[0];
else
result_percent=(float)name_score[0]*100/(float)name_score[1];
printf("두사람의 사랑지수는 %.2f%%입니다.",result_percent);
}