9aaeb4bece
Add validation to prevent input starting with 0.
147 lines
2.9 KiB
C
147 lines
2.9 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int scan(void);
|
|
unsigned long func(int n);
|
|
void output(unsigned long sum);
|
|
void encr(unsigned long sum, char *cpum);
|
|
int cpumodel(char cpum[]);
|
|
|
|
|
|
int main() {
|
|
int n;
|
|
unsigned long isum;
|
|
char cpum[64];
|
|
|
|
cpumodel(cpum);
|
|
|
|
printf("Buff: ");
|
|
if (getchar() == '1') {
|
|
setbuf(stdin, NULL);
|
|
printf("buff is off\n");
|
|
} else {
|
|
printf("buff is on\n");
|
|
}
|
|
while(getchar()!='\n');
|
|
|
|
n = scan();
|
|
if(n == 0) return 0;
|
|
isum = func(n);
|
|
|
|
output(isum);
|
|
encr(isum,cpum);
|
|
return 0;
|
|
}
|
|
|
|
int scan(void){
|
|
int x, n;
|
|
n = 0;
|
|
|
|
printf("Enter num: 1-100 000\nNum: ");
|
|
|
|
while(((x = getchar())!='\n') && x != EOF){
|
|
if((x >= '0') && (x <= '9')){
|
|
n = (n*10) + (x-'0');
|
|
}
|
|
|
|
else{
|
|
if(x == '\n');
|
|
else(printf("Err \"%c\" is not a num.\n",x));
|
|
while((x = getchar())!='\n')
|
|
if(x == EOF) return 0;
|
|
printf("Num: ");
|
|
n = 0;
|
|
continue;
|
|
}
|
|
|
|
if(n > 100000){
|
|
while((x = getchar())!='\n')
|
|
if(x == EOF) return 0;
|
|
n = 0;
|
|
printf("You enter n>100 000 Try again\nNum: ");
|
|
continue;
|
|
}
|
|
|
|
if(x=='0'&&n<=0){
|
|
while((x = getchar())!='\n')
|
|
if(x == EOF) return 0;
|
|
printf("You num cannot starts with 0. Try again\nNum: ");
|
|
continue;
|
|
}
|
|
|
|
}
|
|
if (x == EOF) return 0;
|
|
return(n);
|
|
}
|
|
|
|
|
|
unsigned long func(int n){
|
|
int i;
|
|
unsigned long sum = 0;
|
|
unsigned char mem[65536];
|
|
char data[256 * 1024];
|
|
|
|
for(i = 1; i <= n; i++){
|
|
sum += i;
|
|
mem[i%65536] = (unsigned char)(i % 256);
|
|
data[i%(256*1024)] = (unsigned char)((sum + i) % 256U);
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
|
|
void output(unsigned long sum){
|
|
int i = 0;
|
|
char buf[20];
|
|
|
|
printf("\nresult: ");
|
|
while(sum > 0){
|
|
buf[i++] = (char)('0' + (sum % 10));
|
|
sum /= 10;
|
|
}
|
|
while (i-- > 0)
|
|
putchar(buf[i]);
|
|
putchar('\n');
|
|
}
|
|
|
|
|
|
int cpumodel(char cpum[]) {
|
|
int c, i = 0;
|
|
printf("\nEnter proc name: ");
|
|
while ((c = getchar()) != '\n' && c != EOF && i < 64 - 1) {
|
|
cpum[i++] = (char)c;
|
|
}
|
|
cpum[i] = '\0';
|
|
return i;
|
|
}
|
|
|
|
|
|
void encr(unsigned long sum, char *cpum) {
|
|
char buf[20];
|
|
int i = 0;
|
|
int cpu_len = strlen(cpum);
|
|
|
|
if (cpu_len == 0) {
|
|
printf("CPU model is empty!\n");
|
|
return;
|
|
}
|
|
|
|
if (sum == 0) {
|
|
buf[i++] = '0';
|
|
} else {
|
|
while (sum > 0) {
|
|
buf[i++] = '0' + (sum % 10);
|
|
sum /= 10;
|
|
}
|
|
}
|
|
|
|
printf("enc result: ");
|
|
for (int j = i - 1; j >= 0; j--) {
|
|
int digit = buf[j] - '0';
|
|
char cpu_char = cpum[(i - 1 - j) % cpu_len];
|
|
int enc = (digit + (int)cpu_char) % 10;
|
|
putchar('0' + enc);
|
|
}
|
|
putchar('\n');
|
|
}
|