Quiz 02
===
# [A] StrReplace
## Notes
## Code
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SHORT 11
#define LONG 256
int find_str(char find[SHORT], char str[LONG]);
int main(){
char find[SHORT], replace[SHORT], str[LONG], result[LONG];
int idx, result_idx;
while(1){
memset(find, 0, SHORT);
memset(replace, 0, SHORT);
memset(str, 0, LONG);
gets(find);
if(find[0] == '*'){
break;
}
gets(replace);
gets(str);
idx = find_str(find, str);
while(idx != -1){
memset(result, 0, LONG);
result_idx = 0;
for(size_t i=0; i<idx; i++){
result[result_idx++] = str[i];
}
for(size_t i=0; i<strlen(replace); i++){
result[result_idx++] = replace[i];
}
for(size_t i=result_idx-strlen(replace)+strlen(find); i<strlen(str); i++){
result[result_idx++] = str[i];
}
strncpy(str, result, LONG);
idx = find_str(find, str);
}
printf("%s\n", str);
}
return 0;
}
int find_str(char str1[SHORT], char str2[LONG]){
int found;
for(size_t i=0; i<strlen(str2); i++){
if(str2[i] == str1[0]){
found = 1;
for(size_t j=1; j<strlen(str1); j++){
if(str2[i+j] != str1[j]){
found = 0;
break;
}
}
if(found){
return i;
}
}
}
return -1;
}
```
# [B] Cat
## Notes
## Code
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct cat{
char *breed, *origin, *type, *body, *coat;
int value, price;
};
int main(){
char input[150];
char *c_value;
struct cat Cat;
printf("Breed Origin Type Body Coat Value Price\n");
for(int i=0; i<93; i++){
printf("-");
}
puts("");
while(gets(input)){
if(strlen(input) > 10){
Cat.breed = strtok(input, ",");
Cat.origin = strtok(NULL, ",");
Cat.type = strtok(NULL, ",");
Cat.body = strtok(NULL, ",");
Cat.coat = strtok(NULL, ",");
c_value = strtok(NULL, ",");
Cat.value = 0;
for(size_t i=0; i<strlen(c_value); i++){
Cat.value *= 10;
Cat.value += (int)c_value[i] - (int)'0';
}
Cat.price = Cat.value;
switch(Cat.type[0]){
case 'M':
Cat.price *= 3;
break;
case 'N':
Cat.price *= 2;
break;
case 'C': default:
break;
}
switch(Cat.coat[0]){
case 'A': case 'R':
Cat.price *= 4;
break;
case 'L':
Cat.price *= 3;
break;
case 'S':
Cat.price *= 2;
break;
case 'H': default:
break;
}
printf("%-25s%-22s %c %-12s%-12s %-7d %5d\n", Cat.breed, Cat.origin, Cat.type[0], Cat.body, Cat.coat, Cat.value, Cat.price);
}
memset(input, 0, 150);
}
for(int i=0; i<93; i++){
printf("-");
}
puts("");
return 0;
}
```
# [C] Tablelize
## Notes
## Code
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int att[10];
int min, max, avg;
}data;
int get_att_count(char *str);
int str_to_int(char *str);
void print_lines(char c, int att_count);
int main(){
char input[100];
data Data, Avg;
int turn=0, att_count, printed = 0;
while(gets(input)){
if(turn == 0){
if(printed){
puts("");
}
else{
printed = 1;
}
att_count = get_att_count(input);
memset(Avg.att, 0, 10*sizeof(int));
print_lines('=', att_count);
puts("");
printf("Trn.");
for(int i=1; i<=att_count; i++){
printf("%6s%d", "Att", i);
}
printf("%5s%5s%6s\n", "Min", "Max", "Avg.");
print_lines('-', att_count);
puts("");
}
if(input[0] == 0){
print_lines('-', att_count);
puts("");
printf("Avg.");
Avg.att[0] /= turn;
printf("%7d", Avg.att[0]);
Avg.avg = Avg.att[0];
Avg.max = Avg.att[0];
Avg.min = Avg.att[0];
for(size_t i=1; i<att_count; i++){
Avg.att[i] /= turn;
if(i == 9){
printf("%8d", Avg.att[i]);
}
else{
printf("%7d", Avg.att[i]);
}
Avg.avg += Avg.att[i];
if(Avg.att[i] > Avg.max){
Avg.max = Avg.att[i];
}
else if(Avg.att[i] < Avg.min){
Avg.min = Avg.att[i];
}
}
Avg.avg /= att_count;
printf("%5d%5d%6d\n", Avg.min, Avg.max, Avg.avg);
print_lines('=', att_count);
puts("");
turn = 0;
}
else{
printf("%04d", turn);
memset(Data.att, 0, 10*sizeof(int));
Data.att[0] = str_to_int(strtok(input, ","));
printf("%7d", Data.att[0]);
Avg.att[0] += Data.att[0];
Data.avg = Data.att[0];
Data.max = Data.att[0];
Data.min = Data.att[0];
for(size_t i=1; i<att_count; i++){
Data.att[i] = str_to_int(strtok(NULL, ","));
if(i == 9){
printf("%8d", Data.att[i]);
}
else{
printf("%7d", Data.att[i]);
}
Avg.att[i] += Data.att[i];
Data.avg += Data.att[i];
if(Data.att[i] > Data.max){
Data.max = Data.att[i];
}
else if(Data.att[i] < Data.min){
Data.min = Data.att[i];
}
}
Data.avg /= att_count;
printf("%5d%5d%6d\n", Data.min, Data.max, Data.avg);
turn++;
}
}
}
int get_att_count(char *str){
int counter = 0;
for(size_t i=0; i<strlen(str); i++){
if(str[i] == ','){
counter++;
}
}
return counter+1;
}
int str_to_int(char *str){
int result = 0;
for(size_t i=0; i<strlen(str); i++){
result *= 10;
result += (int)str[i] - (int)'0';
}
return result;
}
void print_lines(char c, int att_count){
for(int i=0; i<20+7*att_count; i++){
printf("%c", c);
}
if(att_count == 10){
printf("%c", c);
}
}
```
###### tags: `程設二quiz`