#include <stdio.h>
int main( void )
{
char myString[] = "1, 1.0, 1.000, 1.0000";
struct myStruct
{
int I1;
double D1;
double D2; double D3;
} myStruct;
sscanf( myString, "%d ,%lf ,%lf ,%lf",
&myStruct.I1,&myStruct.D1, &myStruct.D2, &myStruct.D3 );
printf( "%d, %.1f, %.2f, %.3f\n", myStruct.I1, myStruct.D1, myStruct.D2, myStruct.D2 );
return 0;
}
/*
#include <iostream>
#include <utility>
template <typename UnaryPredicate>
void conditional_bubble_sort( int a[], size_t n,
UnaryPredicate &&unary_predicate,
bool order = false )
{
while ( n && !unary_predicate( *a ) )
{
++a;
--n;
}
for ( size_t i = 0, last = 0; !( n < 2 ); n = last )
{
last = 0;
for ( size_t current = 0, next = 0; next < n; current = next )
{
while ( ++next < n && !unary_predicate( a[next] ) );
if ( next != n )
{
if ( !order ? a[next] < a[current] : a[current] < a[next] )
{
std::swap( a[current], a[next] );
last = next;
}
}
}
}
}
int main()
{
// int a[] = { 5, 2, 4, 3, 1 };
int a[] = { 9, 0, 7, 2, 5, 4, 3, 6, 1, 8 };
const size_t N = sizeof( a ) / sizeof( *a );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
conditional_bubble_sort( a, N, []( const auto &item ) { return item % 2 != 0;} );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
conditional_bubble_sort( a, N, []( const auto &item ) { return item % 2 == 0;}, true );
for ( const auto &item : a )
{
std::cout << item << ' ';
}
std::cout << '\n';
return 0;
}
*/
/*
#include <iostream>
#include <iomanip>
#include <cstring>
int stringcompare( const char ( * *( int * s1, const char *s2, sin )
{
while ( n && *s1 && *s1 == *s2 )
{
++s1;
++s2;
--n;
}
return n == 0 ? 0 : *s1 - *s2;
}
int main()
{
const char *s1 = "Hello";
const char *s2 = "Hello World";
std::cout << std::strncmp( s1, s2, 100 ) << '\n';
std::cout << std::strncmp( s1, s2, 0 ) << '\n';
std::cout << std::strncmp( s1, s2, 5 ) << '\n';
std::cout << std::strncmp( s1, s2, 6 ) << '\n';
std::cout << '\n';
std::cout << stringcompare( s1, s2, 100 ) << '\n';
std::cout << stringcompare( s1, s2, 0 ) << '\n';
std::cout << stringcompare( s1, s2, 5 ) << '\n';
std::cout << stringcompare( s1, s2, 6 ) << '\n';
return 0;
}
*/
/*
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
template <typename T>
std::list<T> merge_lists( std::list<T> &list1, std::list<T> &list2 )
{
std::list<T> list3;
typename std::list<T>::iterator first1 = std::begin( list1 ),
first2 = std::begin( list2 );
typename std::list<T>::iterator last1 = std::end( list1 ),
last2 = std::end( list2 );
while ( first1 != last1 && first2 != last2 )
{
if ( *first2 < *first1 )
{
list3.push_back( *first2 );
first2 = list2.erase( first2 );
}
else
{
list3.push_back( *first1 );
first1 = list1.erase( first1 );
}
}
list3.insert( std::end( list3 ), first1, last1 );
list1.erase( first1, last1 );
list3.insert( std::end( list3 ), first2, last2 );
list2.erase( first2, last2 );
return list3;
}
int main()
{
std::list<int> list1 = { 1, 3, 5 };
std::list<int> list2 = { 0, 2, 4 };
std::list<int> list3;
std::merge( std::begin( list1 ), std::end( list1 ),
std::begin( list2 ), std::end( list2 ),
std::back_inserter( list3 ) );
// std::list<int> list3 = merge_lists( list1, list2 );
for ( const auto &item : list3 )
{
std::cout << item << ' ';
}
std::cout << '\n';
std::cout << list1.empty() << '\n';
std::cout << list2.empty() << '\n';
}
#include <iostream>
#include <utility>
using cell = int;
int main()
{
using cell = std::pair<void *, cell *>;
return 0;
}
*/
/*
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
char * partition( char *s )
{
while ( *s && isdigit( ( unsigned char )*s ) ) ++s;
char *p = s;
while ( *s++ )
{
if ( isdigit( ( unsigned char )*s ) )
{
char c = *p;
*p++ = *s;
*s = c;
}
}
return p;
}
int cmp( const void *a, const void *b )
{
return *( const unsigned char * )a - *( const unsigned char * )b;
}
int main(void)
{
char s[] = "38ch50ej16af49di27bg";
puts( s );
char *p = partition( s );
qsort( s, p - s, sizeof( char ), cmp );
qsort( p, strlen( p ), sizeof( char ), cmp );
puts( s );
return 0;
}
*/
/*
#include <stdio.h>
int even( unsigned int );
int odd( unsigned int x )
{
if ( x ) return even( x - 1 );
return 0;
}
int even( unsigned int x )
{
if ( x ) return odd( x - 1 );
return 1;
}
int main(void)
{
printf( "odd( 1 ) = %d , even( 1 ) = %d\n", odd( 1 ), even( 1 ) );
printf( "odd( 2 ) = %d , even( 2 ) = %d\n", odd( 2 ), even( 2 ) );
return 0;
}
*/
/*
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::vector<int> datas = { 10, 5, 6, 8 };
std::vector<int> index = { 0, 2 };
std::vector<int>::size_type i = 0;
for ( const auto &pos : index )
{
if ( pos - i < datas.size() )
{
datas.erase( std::next( std::begin( datas ), pos - i++ ) );
}
else
{
break;
}
}
for ( const auto &item : datas )
{
std::cout << item << ' ';
}
std::cout << '\n';
return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct list
{
struct node *head;
struct node *tail;
};
int append( struct list *list, int data )
{
struct node *node = malloc( sizeof( *node ) );
int success = node != NULL;
if ( success )
{
node->data = data;
node->prev = list->tail;
node->next = NULL;
if ( list->tail == NULL )
{
list->head = node;
}
else
{
list->tail->next = node;
}
list->tail = node;
}
return success;
}
FILE * display( const struct list *list, FILE *fp )
{
for ( const struct node *current = list->head; current != NULL; current = current->next )
{
fprintf( fp, "%d -> ", current->data );
}
fputs( "null", fp );
return fp;
}
FILE * reverse_display( const struct list *list, FILE *fp )
{
for ( const struct node *current = list->tail; current != NULL; current = current->prev )
{
fprintf( fp, "%d -> ", current->data );
}
fputs( "null", fp );
return fp;
}
int check_prime( int n )
{
int prime = n % 2 == 0 ? n == 2 : n != 1;
for ( int i = 3; prime && i <= n / i; i += 2 )
{
prime = n % i != 0;
}
return prime;
}
void delete_prime_number( struct list *list )
{
struct node **head = &list->head;
while ( *head != NULL )
{
if ( check_prime( ( *head )->data ) )
{
struct node *tmp = *head;
if ( ( *head )->next != NULL )
{
( *head )->next->prev = ( *head )->prev;
}
else
{
list->tail = ( *head )->prev;
}
*head = ( *head )->next;
free( tmp );
}
else
{
head = &( *head )->next;
}
}
}
int main(void)
{
struct list list = { .head = NULL, .tail = NULL };
const int N = 14;
for ( int i = 0; i < N; i++ )
{
append( &list, i );
}
fputc( '\n', display( &list, stdout ) );
fputc( '\n', reverse_display( &list, stdout ) );
delete_prime_number( &list );
fputc( '\n', display( &list, stdout ) );
fputc( '\n', reverse_display( &list, stdout ) );
return 0;
}
*/
/*
#include <iostream>
#include <utility>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>
std::pair<std::vector<char>::const_iterator, std::vector<char>::size_type>
getMostFrequentElement( const std::vector<char> &v )
{
std::pair<std::vector<char>::const_iterator, std::vector<int>::size_type>
result( std::end( v ), 0 );
std::map<char, size_t> m;
for ( const auto &c : v ) ++m[c];
auto it = std::max_element( std::begin( m ), std::end( m ),
[]( const auto &p1, const auto &p2 )
{
return p1.second < p2.second;
} );
if ( it != std::end( m ) )
{
result.first = std::find( std::begin( v ), std::end( v ), it->first );
result.second = it->second;
}
return result;
}
std::vector<char> getMostFrequentElement( const std::vector<char> &v )
{
std::vector<char> result;
std::map<char, size_t> m;
for ( const auto &c : v ) ++m[c];
auto it = std::max_element( std::begin( m ), std::end( m ),
[]( const auto &p1, const auto &p2 )
{
return p1.second < p2.second;
} );
if ( it != std::end( m ) )
{
for ( const auto &p : m )
{
if ( p.second == it->second ) result.push_back( p.first );
}
}
return result;
}
int main()
{
std::vector<char> v = { 'a', 'a', 'b', 'b', 'c' };
auto p = getMostFrequentElement( v );
std::cout << *p.first << ": " << p.second<< '\n';
auto result = getMostFrequentElement( v );
for ( const auto &c : result ) std::cout << c << ' ';
std::cout << '\n';
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
int data;
struct Node *next;
} typedef Node;
FILE * display( const Node *head, FILE *fp )
{
for ( ; head != NULL; head = head->next )
{
fprintf( fp, "%d -> ", head->data );
}
fputs( "null", fp );
return fp;
}
void adjacent_swap( Node **node )
{
Node *tmp = *node;
*node = ( *node )->next;
tmp->next = ( *node )->next;
( *node )->next = tmp;
}
void bubble_sort( Node **head )
{
for ( Node *last = NULL, *sorted = NULL; *head && ( *head )->next != last; last = sorted )
{
sorted = ( *head )->next;
for ( Node **current = head; ( *current )->next != last; current = &( *current )->next )
{
if ( ( *current )->next->data < ( *current )->data )
{
adjacent_swap( current );
sorted = ( *current )->next;
}
}
}
}
int main(void)
{
Node *head = malloc( sizeof( Node ) );
head->data = 2;
head->next = malloc( sizeof( Node ) );
head->next->data = 1;
head->next->next = NULL;
fputc( '\n', display( head, stdout ) );
bubble_sort( &head );
fputc( '\n', display( head, stdout ) );
int num1 = 1, num2 = 2, num3 = 3;
if ( !( num1 < num2 ) && !( num1 < num3 ) )
{
puts( "First Number Is The Largest" );
}
if ( !( num2 < num3 ) )
{
puts( "Second Number Is The Largest" );
}
else
{
puts( "Third Number Is The Largest" );
}
return 0;
}
*/