195080410李荪玮
_

  1. 上机运行如下程序,给出程序运行的结果(包括输入的数据)。

    1. void exchange(int a, int b)
    2. {
    3. int temp;
    4. temp=a; a=b; b=temp;
    5. }
    6. void main()
    7. {
    8. int n1,n2;
    9. printf("\nPlease input 2 intgers: ");
    10. scanf("%d %d",&n1,&n2);
    11. exchange(n1,n2);
    12. printf("\nn1=%d,n2=%d\n",n1,n2);
    13. }

    答:输入3 6,输出n1=3,n2=6

  2. 上机运行如下程序,给出程序运行的结果(包括输入的数据),并说明和上例程序的区别。

    1. void exchange(int * a, int * b)
    2. {
    3. int temp;
    4. temp=*a; *a=*b; *b=temp;
    5. }
    6. void main()
    7. {
    8. int n1,n2;
    9. printf("\nPlease input 2 intgers: ");
    10. scanf("%d %d",&n1,&n2);
    11. exchange(&n1,&n2);
    12. printf("\nn1=%d,n2=%d\n",n1,n2);
    13. }

    答:
    输入3 6,输出n1=6,n2=3
    区别:第一题调用函数的时候是直接传递了局部变量的值,函数运行结束销毁变量,没有影响main函数中的变量值。第二题传递了main函数中两个变量的地址,可以修改main函数中变量的值。

  3. 编写函数len,其功能是求字符串的长度,主函数调用如下:

    1. #define N 20
    2. void main()
    3. {
    4. char str[N];
    5. printf("\nPlease input a string: ");
    6. scanf("%s",str);
    7. printf("\nThe length of this string is : %d\n",len(str));
    8. }

    答:

    1. int len(char* str){
    2. int count=0;
    3. while (*str!='\0'){
    4. count++;
    5. str++;
    6. }
    7. return count;
    8. }
  4. 编写函数void inverse (char *s ),对字符串进行逆置。编写main函数,并在其中调用inverse函数。(比如输入串“abcdef”,输出“fedcba”。)

答:

  1. #include <stdio.h>
  2. #define N 20
  3. void reverse(char *str) {
  4. int count = 0, temp = 0;
  5. char origin[N];
  6. while (str[count] != '\0') {
  7. origin[count]=str[count];
  8. count++;
  9. }
  10. origin[count]='\0';
  11. count--;
  12. while (count >= 0) {
  13. str[temp] = origin[count];
  14. temp++;
  15. count--;
  16. }
  17. str[temp]='\0';
  18. }
  19. void main() {
  20. char str[N];
  21. char rev[N];
  22. printf("\nPlease input a string: ");
  23. scanf("%s", str);
  24. reverse(str);
  25. printf("%s", str);
  26. }
  1. 编写函数void sort ( int *a, int n ),对数组中的n个数利用选择法进行降序排序。编写main函数,并在其中调用sort函数。

答:

  1. #include <stdio.h>
  2. #define N 20
  3. void sort(int *a, int n) {
  4. int maxNum,temp;
  5. for (int i = 0; i < n-1; ++i) {
  6. maxNum=i;
  7. for (int j = i+1; j < n; ++j) {
  8. if (a[j]>a[maxNum]){
  9. maxNum=j;
  10. temp=a[i];
  11. a[i]=a[maxNum];
  12. a[maxNum]=temp;
  13. }
  14. }
  15. }
  16. }
  17. int main() {
  18. int a[N], n;
  19. printf("\n你想输入几个数字? ");
  20. scanf("%d", &n);
  21. printf("\n请输入数字:\n");
  22. for (int i = 0; i < n; i++) {
  23. scanf("%d", &a[i]);
  24. }
  25. sort(a,n);
  26. for (int i = 0; i < n; i++) {
  27. printf("%d ", a[i]);
  28. }
  29. }
  1. 编程判断所输入的字符串是否为“回文”。“回文”是指该串的逆序串与原串相同,如串“dad”就是回文串。要求回文串的判定由一个用户自定义函数来实现,并在主函数中调用该用户自定义函数。

答:

  1. #include <stdio.h>
  2. #define N 20
  3. int ifHW(char *str) {
  4. int count = 0, temp = 0;
  5. while (str[count] != '\0') {
  6. count++;
  7. }
  8. count--;
  9. while (count > temp) {
  10. if (str[count]!=str[temp])return 0;
  11. temp++;
  12. count--;
  13. }
  14. return 1;
  15. }
  16. void main() {
  17. char str[N];
  18. char rev[N];
  19. printf("\nPlease input a string: ");
  20. scanf("%s", str);
  21. if (ifHW(str))printf("是回文序列");
  22. else printf("不是回文");
  23. }
  1. 编写函数void del_ch(char * s, char c),删除s串中的所有字符c。编写main函数,并在其中调用del_ch函数。(比如s串为“madam”, 删除字符‘m’后s串为“ada”。)

答:

  1. #include <stdio.h>
  2. #define N 20
  3. void del_ch(char *s, char c) {
  4. int i = 0;
  5. while (s[i] != '\0') {
  6. if (s[i] == c) {
  7. int temp = i;
  8. while (s[temp] != '\0') {
  9. s[temp] = s[temp + 1];
  10. temp++;
  11. }
  12. i--;
  13. }
  14. i++;
  15. }
  16. }
  17. void main() {
  18. char str[N];
  19. char c;
  20. printf("\nPlease input a string: ");
  21. scanf("%s", str);
  22. c = getc(stdin);
  23. printf("\nPlease input a letter: ");
  24. scanf("%c", &c);
  25. del_ch(str, c);
  26. printf("%s", str);
  27. }
  1. 编写函数void sub_str (char s, char sub, int pos, int len ),从指针s所指向串的第pos个位置取出长度为len的子串存放到sub串中。编写main函数,并在其中调用sub_str函数。

答:

  1. #include <stdio.h>
  2. #define N 20
  3. void sub_str(char *s, char *sub, int pos, int len) {
  4. int i;
  5. for (i = 0; i < len; ++i) {
  6. sub[i] = s[i + pos-1];
  7. }
  8. sub[i]='\0';
  9. }
  10. void main() {
  11. char s[N];
  12. char sub[N];
  13. int pos, len;
  14. printf("\nPlease input a string: ");
  15. scanf("%s", s);
  16. printf("\nPlease input position and length: ");
  17. scanf("%d%d", &pos, &len);
  18. sub_str(s,sub,pos,len);
  19. printf("%s", sub);
  20. }
  1. 编写函数int index_str (char s, char t ),返回在s串中第一次出现的子串t的位置,如果s串中不存在子串t,则返回0。编写main函数,并在其中调用index_str函数。(比如s串为“abcdef”, t串为“de”,index_str函数返回值为4。)

答:

  1. #include <stdio.h>
  2. #define N 20
  3. int index_str (char *s, char *t ){
  4. int pos=0,temp=0;
  5. while (*s!='\0' && *t!='\0'){
  6. pos++;
  7. while(*(s+temp)==*(t+temp)){
  8. if(*(t+temp+1)=='\0')return pos;
  9. temp++;
  10. }
  11. temp=0;
  12. s++;
  13. }
  14. return 0;
  15. }
  16. void main() {
  17. char s[N];
  18. char t[N];
  19. printf("\nPlease input a string: ");
  20. scanf("%s", s);
  21. printf("\nPlease input another string: ");
  22. scanf("%s", t);
  23. printf("%d",index_str(s,t));
  24. }
  1. 编写一程序,输入整数月份号,输出该月份对应的英文字符串。例如输入“3”,则输出“March”。要求利用指针数组实现。 ```c

    include

void main() { int n; char* days[12]={“January”,”February”,”March”,”April”,”May”,”June”,”July”,”August”,”September”,”October”,”November”,”December”}; scanf(“%d”,&n); printf(“%s”,days[n-1]); } ```