Answer the question
In order to leave comments, you need to log in
How to improve the code in C (make "prettier")?
Task:
Главный вождь племени Абба не умеет считать. В обмен на одну из его земель вождь другого племени предложил ему выбрать одну из трех куч с золотыми монетами. Но вождю племени Абба хочется получить наибольшее количество золотых монет. Помогите вождю сделать правильный выбор!
INPUT.TXT
В первой строке входного файла INPUT.TXT записаны три натуральных числа через пробел. Каждое из чисел не превышает 10100. Числа записаны без ведущих нулей.
OUTPUT.TXT
В выходной файл OUTPUT.TXT нужно вывести одно целое число — максимальное количество монет, которые может взять вождь.
#include <stdio.h>
#define N 103
char* maxNum(char *n1, char *n2);
int length(char s[]);
int main()
{
FILE *in;
FILE *out;
int i,j;
char c;
char num1[N];
char num2[N];
char num3[N];
char *nums[3] = {num1,num2,num3};
in = fopen("input.txt","r");
for (i=0;i<3; ++i){
j = 0;
while(c=fgetc(in)){
if ( c == '\n' || c == ' ' ){
(*(nums+i))[j] = '\0';
break;
}
(*(nums+i))[j] = c;
j++;
}
}
fclose(in);
out = fopen("output.txt","w");
fprintf(out, "%s",maxNum(*nums, maxNum(*(nums+1),*(nums+2))));
fclose(out);
//printf("%s", maxNum(*nums, maxNum(*(nums+1),*(nums+2))));
return 0;
}
int length(char s[]){
int i;
for(i=0; i<N,s[i]!='\0';i++)
;
return i;
}
char* maxNum(char *n1, char *n2)
{
int i;
if (length(n1)>length(n2))
return n1;
if (length(n2)>length(n1))
return n2;
for (i=0; i<N;i++){
if (n1[i]>n2[i])
return n1;
if (n2[i]>n1[i])
return n2;
}
}
Answer the question
In order to leave comments, you need to log in
char num1[N]; char num2[N]; char num3[N]; char *nums[3] = {num1,num2,num3};
nums[3][N]
?if ( c == '\n' || c == ' ' || c == EOF)
nums[i][j]
?strnlen
from string.h You can improve in the following ways:
#include <stdio.h>
#include <string.h>
#define MAX_NUM_LENGTH 100
const char* chose(const char* left, const char* right)
{
const size_t len_left = strlen(left);
const size_t len_right = strlen(right);
if (len_left > len_right)
return left;
if (len_right > len_left)
return right;
if (strcmp(left, right) < 0)
return right;
return left;
}
const char* get_max(const char* n1, const char* n2, const char* n3)
{
return
chose(
chose(n1, n2),
chose(n2, n3));
}
int main()
{
FILE* in = NULL;
FILE* out = NULL;
char n1[MAX_NUM_LENGTH + 1];
char n2[MAX_NUM_LENGTH + 1];
char n3[MAX_NUM_LENGTH + 1];
in = fopen("input.txt", "r");
if (!in) {
perror("Cannot open file 'input.txt'");
return 1;
}
if (3 != fscanf(in, "%100[0-9] %100[0-9] %100[0-9]", n1, n2, n3)) {
perror("Wrong input format");
fclose(in);
return 1;
}
const char* max_num = get_max(n1, n2, n3);
out = fopen("output.txt", "w");
if (!out) {
perror("Cannot open file 'output.txt'");
return 1;
}
fprintf(out, "%s", max_num);
fclose(in);
fclose(out);
return 0;
}
You need to find your style of code design, and try to stick to
it, I recommend for the future S. McConnell "Perfect Code"
----------------------- -------------------------------------------------- --------------
indents after mathematical signs ( +, -, *, /, =), after a comma, after checks ( ==, !=, <=, <, >, > =), logical indents - after each task, variables must be declared as close as possible to the place of use
#include <stdio.h>
#include <stdlib.h> /* atoi */
int main()
{
char nums[3][N];
// читаем
FILE *in = fopen("input.txt", "r");
for (int i = 0; i < 3; ++i)
{
int j = 0;
char c;
while(c = fgetc(in))
{
nums[i][j++] = c;
if (c == '\n' || c == ' ')
{
nums[i][j] = '\0';
break;
}
}
}
fclose(in);
// находим
int maxNums = 0;
for (int i = 0; i != sizeof(nums); ++i)
{
const int curNum = atoi(nums[i]);
if (curNum <= maxNums)
continue;
maxNums = curNum;
}
// выводим
FILE *out = fopen("output.txt", "w");
fprintf(out, "%s", maxNums);
fclose(out);
// printf("%s", maxNum(*nums, maxNum(*(nums+1),*(nums+2))));
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question