Untitled
unknown
plain_text
3 years ago
5.3 kB
13
Indexable
#include <stdio.h>
#include <stdlib.h>
int mult_2(int num){
return num*2;
}
int exp_3(int num){
return num*num*num;
}
int abs(int num){
if(num < 0)
return -num;
else
return num;
}
void forEachElement(int* array, int n, int(*func)(int)){
for (int i = 0; i<n; i++){
array[i] = func(array[i]);
}
}
int main() {
int size = 500;
int arr[size];
int n;
for (int i = 0; i<size; i++){
scanf("%d", &arr[i]);
}
scanf("%d", &n);
switch (n) {
case 1: forEachElement(arr, size, mult_2); break;
case 2: forEachElement(arr, size, exp_3); break;
case 3: forEachElement(arr, size, abs); break;
default: printf("Error"); return 0;
}
for (int i = 0; i<size-1; i++){
printf("%d\n", arr[i]);
}
printf("%d", arr[size-1]);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int cmp_int(const void* a, const void* b){
return *(int*)a - *(int*)b;
}
int cmp_char(const void* a, const void* b){
return *(char*)a - *(char *)b;
}
int cmp_double(const void* a, const void* b) {
if(fabs(*(double*) a - *(double*) b) < 0.0001)
return 0;
if (*(double*) a - *(double*) b > 0)
return 1;
return -1;
}
int universalMax(void* arr, int arrsize, int arrtype, int (*cmp)(const void*, const void*)){
char* bytes = (char*)arr;
char* max = (char*)arr;
int indx = 0;
for(int i=0; i < arrsize * arrtype; i+=arrtype){
if(cmp(bytes+i, max)>0){
indx = i/arrtype;
max = bytes+i;
}
}
return indx;
}
int main(){
int arrsize = 20;
int arrtype;
int res;
scanf("%d", &arrtype);
getchar();
int* arr = malloc(arrtype * arrsize);
switch (arrtype) {
case 1:
for(int i=0; i < arrsize; i++){
scanf("%c", (char*)arr+i);
getchar();
}
res = universalMax(arr, arrsize, arrtype, cmp_char);
break;
case 4:
for(int i=0; i < arrsize; i++){
scanf("%d", (int*)arr+i);
}
res = universalMax(arr, arrsize, arrtype, cmp_int);
break;
case 8:
for(int i=0; i < arrsize; i++){
scanf("%lf", (double*)arr+i);
}
res = universalMax(arr, arrsize, arrtype, cmp_double);
break;
}
printf("%d", res);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int my_compare(const void *a, const void *b) {
char *sent1 = *(char **) (a);
char *sent2 = *(char **) (b);
return strlen(sent1) - strlen(sent2);
}
int main() {
char **sentences = malloc(sizeof(char *) * 100);
int i = 0;
int s_cnt = 0;
sentences[s_cnt] = calloc(1000, sizeof(char));
for (int j = 0; j < 1001; j++) {
char c = getchar();
if (c == '\n')
break;
else
sentences[s_cnt][i++] = c;
if (c == '.') {
char c = getchar();
sentences[++s_cnt] = calloc(1000, sizeof(char));
i = 0;
if (c == '\n')
break;
}
}
qsort(sentences, s_cnt, sizeof(char *), my_compare);
for (int k = 0; k < s_cnt; k++) {
printf("%s", sentences[k]);
if (k != s_cnt-1)
printf(" ");
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Book {
char name[30];
char author[30];
unsigned int pages;
char isbn[20];
};
int compare (const void* a, const void* b){
struct Book* book1 = (struct Book*)(a);
struct Book* book2 = (struct Book*)(b);
return strcmp(book2->author, book1->author);
}
int main(){
int cnt;
scanf("%d", &cnt);
struct Book* books = malloc(sizeof(struct Book)*cnt);
for (int i = 0; i<cnt; i++){
struct Book book;
scanf("%s %s %d %s", book.name, book.author, &book.pages, book.isbn);
books[i] = book;
}
qsort(books, cnt, sizeof(struct Book), compare);
for (int j = 0; j < cnt; j++){
printf("%s %s %d %s", books[j].name, books[j].author, books[j].pages, books[j].isbn);
if (j != cnt - 1)
printf(" ");
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Country {
char name[30];
unsigned int population;
char capital[30];
};
int compare (const void* a, const void* b){
struct Country* c1 = (struct Country*)(a);
struct Country* c2 = (struct Country*)(b);
int res = strcmp(c1->name, c2->name);
if (res == 0)
return c1->population - c2->population;
else
return res;
}
int main(){
int cnt;
scanf("%d", &cnt);
struct Country* array = malloc(sizeof(struct Country)*cnt);
for (int i = 0; i<cnt; i++){
struct Country country;
scanf("%s %u %s", country.name, &country.population, country.capital);
array[i] = country;
}
qsort(array, cnt, sizeof(struct Country), compare);
for (int j = 0; j < cnt; j++){
printf("%s %u %s", array[j].name, array[j].population, array[j].capital);
if (j != cnt - 1)
printf(" ");
}
return 0;
}
Editor is loading...