Tagged with: C + +
1. Using the parameters of the main function, implement an integer calculator, the program can accept three parameters, the first parameter "-a" option to perform the addition, "-S" option to perform the subtraction, "-M" option to perform the multiplication, "-d" option to perform the division, the following two parameters are the operands.
Example: Input test.exe-a 1 2
Performing 1+2 Output 3
#include <stdio.h> #include <stdlib.h> #include <string.h>int main (int argc,char* argv[]) { int num1 = atoi (argv[2]); int num2 = atoi (argv[3]); int ret = 0; if (strcmp (argv[1], "-a") == 0) { ret = num1 + num2; } if (strcmp (argv[1], "-S") == 0) { ret = num1 - num2; } if (strcmp (argv[1], "-M") == 0) { ret = num1 * num2; } if (strcmp (Argv[1],&nbsP; " -L ") == 0) { ret = num1 / num2; }printf ("%d\n", ret); system ("pause"); return 0;}
2. Adjust the array so that all odd numbers are in front of even numbers.
Topic:
Enter an array of integers to implement a function that adjusts the order of the numbers in the array so that all the odd digits in the array are in the first half of the array, and all the even digits are in the second half of the array.
/* The odd digit is in the first half of the array, and all the even digits are in the second half of the array. */
int * Sort_arr (int arr[], int len) {int left = 0;int right = Len-1;while (left < right) {int is_left = Arr[left]% 2; T is_right = arr[right]% 2; if (Is_left = = 0 && Is_right = = 1)//left even right odd {int temp = Arr[left];arr[left] = Arr[right] ; Arr[right] = temp;left++;right--;continue;} if (Is_left = = 1) {left++;} if (Is_right = = 0) {right--;}} return arr;}
==================================================
Reference
void swap (INT&NBSP;*PA,&NBSP;INT&NBSP;*PB) {int tmp = *pa;*pa = *pb;*pb = tmp;} Int main () {int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };int sz = sizeof (arr) / sizeof (arr[0]); int i = 0 ;int start = 0;int end = sz - 1;while (start < end) { while ((start < end) && arr[start] % 2 != 0) {start++;} while ((start < end) && arr[end] % 2 == 0) {end--;} if (start < end) {swap (arr + start, arr + end); start++;end--;}} for (i = 0; i < sz; i++) {printf ("%d ", arr[i]);} printf ("\ n"); system ("pause"); return 0;}
==================================================
3. Write bubble sort to sort multiple strings.
/* Write bubble sort to sort multiple strings. */
void Swap_str (char *c1, char *c2) {Char temp = *C1;*C1 = *C2;*C2 = temp;} void S_paixu (char a[][30],int line) {int i = 0;for (; i < line-1;i++) {int j = 0;for (; j < line-i-1;j++) {if (strcmp ( A[J],A[J+1]) > 0) {int k = 0;while (K <) {swap_str (&a[j][k],&a[j+1][k]); k++;}}}}
----------------------------------------has a return value type----------
-----------------------
Char (*s_paixu (char a[][30],int line)) [30]//returns two-dimensional pointer address {int i = 0;for (; i < line-1;i++) {int j = 0;for (; j < line-i- 1;j++) {if (strcmp (a[j],a[j+1]) > 0) {int k = 0;while (K <) {swap_str (&a[j][k],&a[j+1][k]); k++;}}} return A;} int main () {char arr[][30] = {"Abfgg", "ABCDF", "abcda"};int i = 0;char (*p) [+] = S_paixu (arr,3);//s_paixu (arr,3); for (; I &L T 3;i++) {//printf ("%s", Arr[i]);p rintf ("%s", P[i]),//p[i] corresponds to the row address of each row for the two-dimensional array}system ("pause"); return 0;}
----------------------------------------------------
Reference
void Bubble (char *arr[], int sz) {int i = 0;int j = 0;for (; i < sz-1; i++) {for (; J < sz-1-I; j + +) {if (strcmp ( ARR[J], Arr[j + 1]) >0) {Char *tmp = arr[j];//Interchange address arr[j] = arr[j + 1];arr[j + 1] = tmp;}}} int main () {char *arr[] = {"ABCdef", "AAAAA", "Cbdef", "bbbbb"};int sz = sizeof (arr)/sizeof (arr[0]); int i = 0;bubble (ar R, SZ); for (i = 0; i < sz; i++) {printf ("%s\n", Arr[i]);} System ("pause"); return 0;}
===================================================
4. Write the function to determine the current small end of the machine.
void Check_big_little () {int i = 1;if (* (char *) (&i)) {printf ("little\n");} else{printf ("big\n");}}
5. Get a good understanding of array pointers and pointer arrays.
This article is from "Love Technology Love Life" blog, please be sure to keep this source http://alick.blog.51cto.com/10786574/1720142
20151202 C Language Small code