课外闲谈2.常用操作总结

简介: 随机输入字符串,最常见的操作就是用while,定义一个特殊的条件,例如while((ch=getchar())!=‘\n’);这样的话,结束标志就是回车键,并且,回车键不会被算入在内。

1.有序表插入删除操作


//定位,找到待插入的位置,即退出循环时i的位置
  for (i = 0; i < count; i++) {
    if (value < a[i]) {
      break;
    }
  }//升序排列
  //腾位:将a[i]~a[count-1]向后顺移一位
  for (j = count - 1; j >= i; j--) {
    a[j + 1] = a[j];
  }
  a[i] = value;//将value的值赋给a[i]
  count++;//数组待处理的元素数量加1


//定位:如果找到待删除的元素,用index记录其下标
  for (i = 0; i < count; i++) {
    if (value == a[i]) {
      index = i;
      break;
    } 
  }
  //没找到
  if (index == -1) {
    printf("Failed to find the date,deletion failed.");
  }
  //找到,删除a[index],并且将之后的所有元素向前挪一位
  else {
    for (i = index; i < count - 1; i++) {
      a[i] = a[i + 1];
    }
  }
  //待处理的元素数量减1
  count--;


2.随机输入字符串,最常见的操作就是用while,定义一个特殊的条件,例如while((ch=getchar())!=‘\n’);这样的话,结束标志就是回车键,并且,回车键不会被算入在内。


3.直接插入排序中,需要定义一个工作单元,就类似于交换两个元素的值里面要定义一个temp的作用类似。


#define _CRT_SECURE_NO_WARNINGS 1
//在有序数组中插入一个元素,并且插入过后同样是有序的。
#include<stdio.h>
int main() {
  //a[0]为工作单元,从a[1]开始存放数据。
  int a[10] = { 0,1,2,3,4,6,7};
  //j代表元素个数
  int x, i, j = 6;
  //输入插入的值
  printf("Enter a number:");
  scanf("%d", &x);
  //把x要放在数组中,避免数据的丢失。
  a[0] = x;
  //可以选择从大往小,也可以选择从小往大。我这里是从大往小。
  i = j;
  while (a[i] > x) {
    a[i + 1] = a[i];
    i--;
  }
  //把x放到比他小的值后面一位,因为上面已经把它的后面一位空出来了,所以,这个操作不会对数组内的数据有影响
  a[++i] = x;
  //插入x后,元素总个数增加。
  j++;
  //输出数组,从1开始,因为0是工作单元
  for (i = 1; i <= j; i++)
    printf("%8d", a[i]);
  printf("\n");
  return 0;
}


4.取地址字符串的操作,取出来的是字符串首元素的地址,解引用得到的就是首字符。


5.如果程序中有除法操作,要专门写一个if语句,用来判断分母为0的情况。


6.一个数每除以10,就会少一位,每%10,就会得到最后一位的数。


7.数学问题求答案时,可以写一个循环,将判断条件置为真,然后根据遍历所有的整数,来得到答案


#include<stdio.h>
int main(){
  int n;
  for(n=1;;n++)//本题要的就是n,故对n无限制条件{
  if((n%5==0)&&(n%6==5)&&(n%7==4)&&(n%11==10))
  {
    printf("%d",n);
    break;
  }
} 
  return 0;
} 


8.字符移动


  for(i=0;i<3;i++){
    a[i]=s[i];
  }
  for(i=3;s[i];i++){
    s[i-3]=s[i];
  }
  for(j=i-3,i=0;i<3;i++){
    s[j++]=a[i];
  }


for(int i=0;i<len;i++)
  s[i]=temp[(i+3) % len];//数组左移都能用这套。


9.含静态变量的函数反复调用(这个是比较重要的,但是见到的机会可能会比较少)


#include<stdio.h>
double fact_s(int n);
int main(){
  int i,n;
  printf("Input n:");
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    printf("%3d!=%.0f\n",i,fact_s(i));//1.
  }
  return 0;
}
double fact_s(int n){
  static double f=1;
  f*=n;
  return f;
}
/*1处,在循环处,反复调用函数,第一次调用后,第二次调用时,第一次中的f空间被释放,第二次的f会重新赋值为一
但此处加了static,f放到了静态区,f的值在main函数结束前,并未被释放*/ 


10.要学会使用递归


//递归 
#include<stdio.h>
#include<string.h>
int judge(int low,int high,char *arr,int len){
  //最终条件。
  if(len==0 || len==1) 
    return 1;
  if(arr[low]!=arr[high])
    return 0;
  return judge(low+1,high-1,arr,len-2);
}
int main(){
  char arr[10]="aaabbaaa";
  int len=strlen(arr);
  if(judge(0,len-1,arr,len))
  printf("是!");
  else
  printf("不是!"); 
  return 0;
} 
/*递归的作用在于把问题的规模不断缩少,直到问题缩少到简单地解决 
通过观察可以知道,一个回文字符串其中内部也是回文。所以,我们只需要以去掉两端的字符的形式一层层检查,
每一次的检查都去掉了两个字符,这样就达到了缩少问题规模的目的。
1. 字符串长度可能会奇数或偶数:
如果字符串长度是奇数,字符串会剩下最中间那位字符,但其不影响回文。当检查到长度为1的时候即代表此字符串是回文
如果字符串长度是偶数,当两端的字符串两两比较检查后不会剩下字符。即检查到长度为0的时候即代表此字符串是回文
2. 如果检查到两端两个字符不相同。则说明此字符串不是回文,直接返回0,不需要继续检查


void print(unsigned int num ){
  //递归 
  if(n>9){
    print(n/10);
  }//反复调用此自定义函数,直到n<=9,首先输出n<=9时的数,因为n<=9时函数最先调用完,其次就是它的上一层调用,妙呀 
  printf("%d",n%10);
  //数组 
}
#include<stdio.h>
int main(){
  unsigned int num=0; 
  scanf("%u",&num);
  print(num);
  return 0;
} 


#include<stdio.h>
#include<math.h>
int main(){
  double fact(double x,int n);
  int n;
  float x;
  printf("请输入x,n:");
  scanf("%f%d",&x,&n);
  printf("%.2f",fact(x,n));
  return 0;
}
double fact(double x,int n){
  if(n==0)
  return 1;
  else
  return fact(x,n-1)*x;
}


11.大小写英文字母转换


#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main() {
  char ch;
  printf("Input characters:");
  ch = getchar();
  while (ch != '\n') {
    if (ch >= 65 && ch <= 90)
      ch = ch - 'A' + 'a';
    else if (ch >= 97 && ch <= 122) {
      ch = ch - 'a' + 'A';
    }
    putchar(ch);
    ch = getchar();
  }
  return 0;
}


12.结构体排序(选择排序)


//这里与已知长度和数据的数组的选择法排序不同,这里数据都是未知的。
  for (i = 1; i <= n; i++) {
    printf("NO.%d:",i);
    scanf("%d%s%d%d%d",&stu.num,stu.name,&stu.math,&stu.english,&stu.computer);
    stu.average = (stu.math + stu.english + stu.computer) / 3.0;
    if (i == 1) {
      max = stu;
    }
    else if (max.average < stu.average)
      max = stu;
  }


13.链表的增删查操作


//insert
struct node *insert(struct node* head,struct node* p,int x){
  struct node *k;
  k=(struct node*)calloc(1,sizeof(struct node));
  k->date=x;  
  //判断head指向的是不是空链表 
  if(!head){
    //既是第一个结点,也是最后一个结点 
    head=k;
    head->next=NULL;
  }
  else{
    //在中间的某个位置 
    k->next=p->next;
    p->next=k;
  }
  return head;
} 


//delete
struct node *delete(struct node* head,struct node* p){
  struct node *q;
  //如果p为空,则不做处理 
  if(!p)
    return head;
  //如果p是第一个结点,删除第一个结点 
  if(p==head)
    head=head->next;//就是把第二个结点的地址当成第一个 
  else{
    //如果p不是第一个结点,令q指向第一个结点 
    q=head;
    //向后一直找,找到后删除。 
    while(q->next!=p)
      q=q->next;
    q->next=p->next;
  }
  //结点删除的流程
  //1.删去地址
  //2.释放空间 
  free(p);
  return head;
} 


//find
struct node *find(struct node* head,int m){
  struct node *p=head;
  while(p && p->date!=m)
  p=p->next;
  if(!p)
    return NULL;//没找到 
  else
    return p;//找到了就返回地址 
} 


14.


#define _CRT_SECURE_NO_WARNINGS 1
//建立链表,查找值为x的结点并删除这个结点,x的值从键盘输入。
#include<stdio.h>
#include<stdlib.h>
//先定义结构体类型
struct node {
  int date;
  struct node* next;
};
//函数声明
struct node* create_number(int);
struct node* find(struct node *, int);
struct node* Delete(struct node *, struct node * );
void out_list(struct node*);
int main() {
  //建议还是初始化一下
  struct node* head=NULL, * p=NULL;
  int n, x;
  n = x = 0;
  printf("Create List,Enter n:");
  scanf("%d", &n);
  head = create_number(n);
  printf("List:");
    out_list(head);
  printf("x:");
    scanf("%d", &x);
    p = find(head, x);
    head = Delete(head, p);
    printf("Result:");
    out_list(head);
  return 0;
}
//创造结点的函数
struct node* create_number(int n) {//n代表准备创造结点的个数
  //创造结点时,至少需要三个指针变量,head固定在首结点,p和k用于创造新结点
  int i;
  struct node* head=NULL, * k=NULL, * p=NULL;
  if (n < 1) {
    return NULL;
  }
  else {
    k = (struct node*)malloc(sizeof(struct node));
    //判断是否申请成功
    if (!k)
      exit(1);
    k->date = 1;//初始化第一个数据
    //第一个结点,也是最后一个,这个操作相当于给指针域初始化
    k->next = NULL;
    head = k;
    p = k;
    for (i = 2; i <= n; i++) {
      k = (struct node*)malloc(sizeof(struct node));
      if (!k)
        exit(1);
      k->date = i;
      //初始化
      k->next = NULL;
      p->next = k;
      p = k;
    }
    return head;
  }
}
//查找函数
struct node* find(struct node* head, int x) {//头指针和待查找的元素
  //另外定义一个指针去遍历,从而保证head指针不被改变
  struct node* p = head;
  while (p && p->date != x) {
    p = p->next;
  }
  if (!p)
    return NULL;
  else
    return p;
}
//删除函数
struct node* Delete(struct node* head, struct node* p) {
  struct node* q;
  if (!p)
    return head;
  //如果p不为空,判断p指向结点的位置,从而进行删除操作
  if (p = head)
    head = head->next;
  else {
    q = head;
    while (q->next != p)
      q = q->next;
    q->next = p->next;
  }
  //别忘了释放空间
  free(p);
  return head;
}
//输出函数
void out_list(struct node* head) {
  //另外定义变量去遍历
  struct node* p;
  if (!head) {
    p = head;
    while (!p) {
      printf("%d", p->date);
      p = p->next;
    }
  }
  else
    printf("不存在\n");
  putchar('\n');
}


15.


//getchar调出换行符
#include<stdio.h>
int main(){
  int digit,i,letter,n,other;
  char ch;
  digit=letter=other=0;
  printf("请输入n:");
  scanf("%d",&n);
  getchar();
  printf("请输入%d个字符:",n);
  for(i=1;i<=n;i++)
  {
  ch=getchar();
  if(ch>=48 && ch<=57)
  digit++;
  else if((ch>=65 && ch<=80)||(ch>=97 && ch<=122))
  letter++;
  else
  other++;}
  printf("digit=%d,letter=%d,other=%d",digit,letter,other);
  return 0;
}


先就这些吧,以后有了再补。

目录
相关文章
|
2月前
|
存储 Python
【编程神技】Python函数修炼秘籍:从菜鸟到高手只需这几个案例!
【8月更文挑战第22天】Python函数是组织与重用代码的关键,通过定义与调用函数可以使程序更加清晰。例如,定义`add_numbers(a, b)`来计算两数之和,或使用递归来定义`factorial(n)`计算阶乘。Python还支持可变参数列表如`sum_numbers(*args)`以及带有默认值的关键字参数如`greet(name, greeting=&quot;Hello&quot;)`,这些特性让函数更加灵活强大。掌握这些技巧能显著提升编程效率与代码质量。
15 2
|
2月前
|
缓存 NoSQL 物联网
这些年背过的面试题——个人项目篇
本文是技术人面试系列个人项目篇,作者总结了一些自己的实战项目经验,一文带你详细了解,欢迎收藏!
|
4月前
|
测试技术 虚拟化 云计算
GitHub高赞!速通Python编程基础手册,被玩出花了!
随着云时代的来临,Python 语言越来越被程序开发人员喜欢和使用,因为其不仅简单易学,而且还有丰富的第三方程序库和相应完善的管理工具。 从命令行脚本程序到 GUI程序,从图形技术到科学计算,从软件开发到自动化测试,从云计算到虚拟化,所有这些领域都有 Python 的身影。 今天给小伙伴们分享的这份手册采用以任务为导向的编写模式,全面地介绍了 Python 编程基础及其相关知识的应用,讲解了如何利用 Python 的知识解决部分实际问题。
GitHub高赞!速通Python编程基础手册,被玩出花了!
|
11月前
|
人工智能 算法 数据挖掘
Python技术自学的方式
Python技术自学的方式
|
缓存 Dart JavaScript
编程时光小册上新啦
持续推出 《linwu的算法笔记》,《现代Javascrip高级教程》,《现代TypeScript高级教程》,《深入浅出Dart》等等精品小册
410 0
编程时光小册上新啦
|
存储 程序员 数据安全/隐私保护
|
SQL 存储
MySqI——常用语法技巧(刷文虽然枯燥,但受益匪浅 )
MySqI——常用语法技巧(刷文虽然枯燥,但受益匪浅 )
65 0
|
存储 算法
课外闲谈9.谈一谈分治法和在线处理等常见方法
将整个问题分解成若干个小问题后再分而治之。如果觉得得到的子问题的规模还是太大,那就继续分解,直到得到的子问题规模达到要求。必要时逐步合并这些子问题的解,从而得到问题的解。
86 0
|
JavaScript 前端开发 Python
带你读书之“红宝书”:第三章 语法基础(上)
带你读书之“红宝书”:第三章 语法基础(上)
73 0
带你读书之“红宝书”:第三章 语法基础(上)
|
算法 数据可视化 小程序
为学弟学妹精心整理的 python 系统入门学习
为学弟学妹精心整理的 python 系统入门学习
243 0
为学弟学妹精心整理的 python 系统入门学习