C语言数据结构
main函数
# include <stdio.h>
# include "hahahaha.h"
int main ( )
{
int loop;
int arr[ ] = { 3 , 2 , 5 , 8 , 4 , 7 , 6 , 9 } ;
scanf ( "%d" , & loop) ;
}
h文件
# ifndef _HAHAHAHA_H_
# define _HAHAHAHA_H_
# include <stdio.h>
# include <stdlib.h>
int find_sum_array ( int arr[ ] , int length)
{
int num, loop1, loop2, flag;
flag= 1 ;
printf ( "请输入您的数字:\n" ) ;
scanf ( "%d" , & num) ;
for ( loop1= 0 ; loop1!= length&& flag; loop1= loop1+ 1 )
{
for ( loop2= loop1+ 1 ; loop2!= length&& flag; loop2= loop2+ 1 )
{
if ( arr[ loop1] + arr[ loop2] == num)
{
printf ( "索引为%d的数%d ,%d的数%d 之和为%d\n" , loop1, arr[ loop1] , loop2, arr[ loop2] , num) ;
flag= 0 ;
}
}
}
if ( flag== 1 )
{
printf ( "没有找到和为%d的两个数" , num) ;
}
return 0 ;
}
void print_array ( int arr[ ] , int length, int mod)
{
int k;
printf ( "请输入您的输出圈数" ) ;
scanf ( "%d" , & k) ;
if ( mod== 1 )
{
for ( int loop1= 0 ; loop1!= k; loop1= loop1+ 1 )
{
for ( int loop2= 0 ; loop2!= length; loop2= loop2+ 1 )
{
printf ( "%d " , arr[ loop2] ) ;
}
printf ( "\n" ) ;
}
}
if ( mod== 2 )
{
for ( int loop3= 0 ; loop3!= k* length; loop3= loop3+ 1 )
{
printf ( "%d" , arr[ loop3% length] ) ;
if ( ( loop3+ 1 ) % length== 0 )
{
printf ( "\n" ) ;
}
}
}
}
void find_max_min ( int arr[ ] , int length)
{
int index_max, index_min;
index_max= index_min= 0 ;
for ( int loop= 0 ; loop!= length; loop= loop+ 1 )
{
if ( arr[ loop] > arr[ index_max] )
{
index_max= loop;
}
if ( arr[ loop] < arr[ index_min] )
{
index_min= loop;
}
}
printf ( "最大值,最小值分别为%d,%d" , arr[ index_max] , arr[ index_min] ) ;
}
void find_max_num ( int arr[ ] , int length)
{
int index_max= 0 ;
int num_max= 0 ;
for ( int loop= 0 ; loop!= length; loop= loop+ 1 )
{
if ( arr[ loop] == arr[ index_max] )
{
num_max= num_max+ 1 ;
}
else if ( arr[ loop] > arr[ index_max] )
{
index_max= loop;
num_max= 1 ;
}
}
printf ( "最大值为%d,最大值出现了%d次" , arr[ index_max] , num_max) ;
}
void find_max_nextmax ( int arr[ ] , int length)
{
int max, max_next;
max= 0 , max_next= 1 ;
if ( arr[ 0 ] < arr[ 1 ] )
{
max= 1 ;
max_next= 0 ;
}
for ( int loop= 2 ; loop< length; loop= loop+ 1 )
{
if ( arr[ loop] > arr[ max] )
{
max_next= max;
max= loop;
}
else if ( arr[ loop] > arr[ max_next] )
{
max_next= loop;
}
}
printf ( "最大值是%d,次大值是%d\n" , arr[ max] , arr[ max_next] ) ;
}
void char_string ( )
{
char str1[ 8 ] ;
gets ( str1) ;
printf ( "%s\n\n" , str1) ;
puts ( str1) ;
}
# define N 88
void a_A_invert ( )
{
char str1[ N] ;
printf ( "请输入您的字符串:\n" ) ;
gets ( str1) ;
for ( int loop= 0 ; str1[ loop] ; loop++ )
{
if ( str1[ loop] >= 'a' && str1[ loop] <= 'z' )
{
str1[ loop] = str1[ loop] - 32 ;
}
}
printf ( "输出字符大小写转换后的字符串:\n" ) ;
puts ( str1) ;
}
# define M 10
void str_num ( )
{
char str[ N] ;
printf ( "请输入您的数字字符串:\n" ) ;
gets ( str) ;
int num= 0 ;
for ( int loop= 0 ; str[ loop] ; loop++ )
{
num= num* M+ str[ loop] - '0' ;
}
printf ( "这个%d进制数字的值是%d" , M, num) ;
}
# define length1 20
# define length2 30
void str1_add_str2 ( )
{
char str1[ length1] ;
char str2[ length2] ;
char str3[ 666 ] ;
printf ( "请分别输入您的字符串1和字符串2\n" ) ;
gets ( str1) ;
gets ( str2) ;
int len1, len2;
len1= len2= 0 ;
for ( int loop= 0 ; str1[ loop] ; loop++ )
{
len1++ ;
}
for ( int loop= 0 ; str2[ loop] ; loop++ )
{
len2++ ;
}
int max_length= len2;
if ( len1> len2)
{
max_length= len1;
}
int end_= max_length;
int flag= 0 ;
int num1, num2;
while ( len1>= 0 || len2>= 0 )
{
if ( str1[ len1- 1 ] - '0' && len1> 0 )
{
num1= str1[ len1- 1 ] - '0' ;
}
else
{
num1= 0 ;
}
if ( str2[ len2- 1 ] - '0' && len2> 0 )
{
num2= str2[ len2- 1 ] - '0' ;
}
else
{
num2= 0 ;
}
str3[ max_length] = ( num1+ num2) % 10 + flag+ '0' ;
flag= ( num1+ num2) / 10 ;
len1-- , len2-- , max_length-- ;
}
str3[ ++ end_] = '\0' ;
printf ( "输出相加后的字符串%s\n" , str3) ;
printf ( "将这个字符串转化为数字\n" ) ;
int sum_num= 0 ;
for ( int loop= 0 ; str3[ loop] ; loop++ )
{
sum_num= sum_num* 10 + ( str3[ loop] - '0' ) ;
}
printf ( "\n%d\n" , sum_num) ;
}
# define N 6666
void find_num_bin_1 ( )
{
int num;
printf ( "请输入您的数字\n" ) ;
scanf ( "%d" , & num) ;
char str[ N] ;
int loop= 0 ;
int cnt= 0 ;
while ( num)
{
if ( num% 2 )
{
cnt++ ;
}
str[ loop++ ] = num% 2 + '0' ;
num= num/ 2 ;
}
str[ loop] = '\0' ;
printf ( "没有进行字符串逆序的字符串是%s\n" , str) ;
int length= 0 , invert;
for ( int loop= 0 ; str[ loop] ; loop++ )
{
length++ ;
}
for ( int loop= 0 ; loop< length/ 2 ; loop++ )
{
invert= str[ loop] ;
str[ loop] = str[ length- loop- 1 ] ;
str[ length- loop- 1 ] = invert;
}
printf ( "这个数字转化为二进制是%s\n" , str) ;
printf ( "其中1出现的次数是%d次\n" , cnt) ;
}
# endif