c语言经典算法例程

c语言经典算法例程

 

----------------------------------------------------------------------------------------

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 //==================================================================================
  5 
  6 
  7 //==================================================================================
  8 void test1()
  9 {
 10     char str1[] = "abcdefg";
 11     char str2[] = "abc";
 12     if (strlen(str2) < strlen(str1))
 13     {
 14         printf("str2 is longer than str1 \n");
 15     }
 16 }
 17 void test2()
 18 {
 19     struct record
 20     {
 21         char name[10];
 22         int age;
 23     };
 24     struct record array[2] = { {"ken",24},{"knuth",28}};
 25 
 26     FILE *fp = fopen("d:/work/code_c/Project0622/file0622/test2.txt", "w");
 27     if (fp == NULL)
 28     {
 29         perror("open file recfile");
 30         exit(1);
 31     }
 32     fwrite(array, sizeof(struct record), 2, fp);
 33     fclose(fp);
 34     printf("test2 finish\n");
 35 }
 36 //-----------------------------------------------------
 37 void Algorithm_Test1()
 38 {
 39     printf("hello world!");
 40 }
 41 void Algorithm_Test2()
 42 {
 43     printf("2输出整数");
 44     int number;
 45 
 46     printf("请输入一个整数:");
 47     scanf("%d",&number);
 48     printf("you input number:%d \n",number);
 49 }
 50 void Algorithm_Test3()
 51 {
 52     printf("3.输出单字符\n");
 53     char c;
 54     c = 'A';
 55     printf("c of value %c \n",c);
 56 }
 57 void Algorithm_Test4()
 58 {
 59     printf("4.输出浮点数\n");
 60     float f;
 61     f = 3.141512f;
 62     printf("f of value %f \n", f);
 63 }
 64 void Algorithm_Test5()
 65 {
 66     printf("5.输出双精度 \n");
 67     float d;
 68     d = 3.141512f;
 69     printf("d of value %le \n", d);
 70 }
 71 void Algorithm_Test6()
 72 {
 73     printf("6.二个整数相加 \n");
 74     int FirstNumber, SecondNumber, SumOfTwoNumber;
 75 
 76     scanf("%d %d", &FirstNumber, &SecondNumber);
 77     SumOfTwoNumber = FirstNumber + SecondNumber;
 78     printf("输出结果:%d + %d = %d \n", FirstNumber, SecondNumber, SumOfTwoNumber);
 79 }
 80 void Algorithm_Test7()
 81 {
 82     printf("7.二个浮点相乘 \n");
 83     double FirstNumber, SecondNumber, product;
 84 
 85     scanf("%lf %lf", &FirstNumber, &SecondNumber);
 86     product = FirstNumber * SecondNumber;
 87     printf("输出结果: %.2lf \n", product);
 88 }
 89 void Algorithm_Test8()
 90 {
 91     printf("8.字符转ASCII \n");
 92     char c;
 93     printf("输入一个字符:");
 94     scanf("%c",&c);
 95     printf("%c 的ASIIC %d\n", c,c);
 96 
 97 
 98     char s[65536];
 99     printf("请输入长度小于 %d 的任意字符:", 65536);
100     scanf("%s", s);    // 读取字符串。
101     for (int i = 0; s[i]; i++) 
102     {
103         printf("%c的ASCII:%d\t\n", s[i], s[i]);
104     }
105 
106     char num, enter;
107     int temp = 1;
108     for (; temp > 0;)
109     {
110         printf("----------------------------\n");
111         printf("|**      开始            **|\n");
112         printf("|**ASCII  转  字符  按:1 **|\n");
113         printf("|**字符   转  ASCII 按:2 **|\n");
114         printf("|**      结束       按:0 **|\n");
115         printf("----------------------------\n");
116         scanf("%d", &temp);
117         if (temp == 1)
118         {
119             printf("请输入数值小于 %d 的任意字符:", 127);
120             scanf("%d", &num);
121             printf("ASCII为 %d ,对应的字符为 %c \n", num, num);
122         }
123         if (temp == 2)
124         {
125             printf("输入一个字符: \n");
126             scanf("%c", &enter);      //回车键也算字符,所以这里使用其他变量替之.
127             scanf("%c", &num);
128             printf("     %c 的 ASCII 为 %d    \n", num, num);
129         }
130     }
131 }
132 void Algorithm_Test9()
133 {
134     printf("9.二个数相除 \n");
135     int dividend, divisor, quotient, remainder;
136 
137     printf("输入除数:");
138     scanf("%d", &dividend);
139 
140     printf("输入除数: ");
141     scanf("%d", &divisor);
142 
143     // 计算商
144     quotient = dividend / divisor;
145 
146     // 计算余数
147     remainder = dividend % divisor;
148 
149     printf("商 = %d\n", quotient);
150     printf("余数 = %d\n", remainder);
151 }
152 //==================================================================================
153 // main
154 //==================================================================================
155 int main()
156 {
157     printf("测试开始 Alex \n");
158     //-------------------------------------------------------
159     //test1();
160     //test2();
161     //Algorithm_Test1();
162     //Algorithm_Test2();
163     //Algorithm_Test3();
164     //Algorithm_Test4();
165     //Algorithm_Test5();
166     //Algorithm_Test6();
167     //Algorithm_Test7();
168     //Algorithm_Test8();
169     Algorithm_Test9();
170     //-------------------------------------------------------
171     system("pause");
172     return 0;
173 }

 

 

---------------------------------------------------------------------------------------

posted @ 2021-06-24 17:01  楚格  阅读(111)  评论(0编辑  收藏  举报