當前位置:首頁 » 編程語言 » c語言前驅字元怎麼算
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言前驅字元怎麼算

發布時間: 2022-09-18 16:09:22

Ⅰ 用c語言設計線性順序表,求前驅,後進的問題。

下面是我該完的程序。請運行一下,我改的地方都加了注釋」//這里修改了「
#include <stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int status;
typedef int elemtype;
typedef struct {
elemtype elem[MAXSIZE];
int length;
}sqlist;
/*初始化*/
status initlist(sqlist *l){
l->length=0;
return OK;
}
/*銷毀*/
status destroylist(sqlist *l){
l->length=0;
return OK;
}
/*順序表置空*/
status clearlist(sqlist *l){
l->length=0;
return OK;
}
/*求順序表長度*/
int listlength(sqlist l){
return l.length;
}
/*順序表空否*/
status listempty(sqlist l){
if( l.length) return FALSE;
else return TRUE;
}
/*查找*/
int locateelem(sqlist l,elemtype e){
int i=0;
while(i<l.length)
if (l.elem[i++]==e) return i;
return 0;
}
/*讀第i個元素*/
status getelem(sqlist l,int i,elemtype *e){
if((i<1)||(i>l.length)) return ERROR;
*e=l.elem[i-1];
return OK;
}
/*求前驅*/
status priorelem(sqlist l,elemtype cur_e,elemtype *pre_e){
int i;
i=locateelem(l,cur_e);
if((i==0)||(i==1)) {*pre_e='no';printf(" 沒有前驅 ");;return ERROR;} //這里修改了
else //這里修改了
{
*pre_e=l.elem[i-2];
return OK;
}
}
/*求後繼*/
status nextelem(sqlist l,elemtype cur_e,elemtype *next_e){
int i;
i=locateelem(l,cur_e);
if((i==0)||(i==l.length)){ *next_e='no';printf(" 沒有後繼 ");return ERROR;} //這里修改了
else //這里修改了
{
*next_e=l.elem[i];
return OK;
}
}
/*在第i個位置插入元素e*/
status listinsert(sqlist *l,int i,elemtype e){
int j;
if((i<1)||(i>l->length+1)) return ERROR;
for(j=l->length;j>i-1;j--) l->elem[j]=l->elem[j-1];
l->elem[i-1]=e;
l->length++;
return OK;
}
/*刪除第i個元素*/
status listdelete(sqlist *l,int i,elemtype *e){
int j;
if((i<1)||(i>l->length)) return ERROR;
*e=l->elem[i-1];
for(j= i;j< l->length;j++) l->elem[j-1]=l->elem[j];
l->length--;
return OK;
}
/*遍歷*/
status listtraverse(sqlist l){
int i;
for(i=1;i<=l.length;i++)
printf("%d ",l.elem[i-1]);
printf("\n");
return OK;
}
/*建立順序表*/
status listcreate(sqlist *l){
int i=0;
elemtype e;
printf("input data(end by -1):");
scanf("%d",&e);
while(e!=-1){
l->elem[i++]=e;
scanf("%d",&e);
}
l->length=i;
return OK;
}
/*主函數*/
main(){
sqlist la,lb,lc;
int i;
elemtype e,pre_e,next_e;
listcreate(&la);
listtraverse(la);
printf("the length of the list is %d\n",listlength(la));
printf("input i and e for insert:");
scanf("%d %d",&i,&e);
if(listinsert(&la,i,e)) listtraverse(la);
else printf("i is error!\n");
printf("input i :");
scanf("%d",&i);
getelem(la,i,&e);
printf("the element is %d ,",e);
priorelem(la,e,&pre_e);
nextelem(la,e,&next_e);
if(pre_e!='no')printf("the priore is %d\n",pre_e); //這里修改了
if(next_e!='no')printf("the next is %d\n",next_e); //這里修改了
listdelete(&la,i,&e);
listtraverse(la);

}

Ⅱ 輸入一個字元,然後順序輸出該字元的前驅字元,該字元本身和後續字元 c語言

程序運行如下:

#include<stdio.h>
intmain()
{
printf("請輸入一個字元: ");
charmiddle;
scanf("%c",&middle);
printf("前驅字元[%c],本身[%c],後續字元[%c] ",middle-1,middle,middle+1);
return0;
}


PS: 裡面沒有做異常判斷,比如是否確實是可見字元,是否有溢出等。

有問題請追問,謝謝!

Ⅲ C語言:求前驅和後繼字母。輸入一個大寫字母,求對應的小寫字母及它的前驅和後繼

樓主你好。
#include<stdio.h>
int main()
{
char ch,ch1,ch2;
scanf("%c",&ch);
if(ch>='B'&&ch<='Y'){
ch1=ch+31;
ch2=ch+33;
}else if(ch=='A'){
ch1='-';
ch2=ch+33;
}else {
ch1=ch+31;
ch2='-';
}
printf("%c %c\n",ch1,ch2);
return 0;
}
ch,ch1,ch2應該聲明為char類型。
你原先定義成為int類型會導致條件判斷的時候總是進入最後一個else中。所以只有前驅,沒有後繼。

Ⅳ 在c語言中,求一個字元的前驅字元和後驅字元。這個程序怎麼編寫,大一問題。求解答

定義一個字元串,然後輸入一個字元,再判斷出它在字元串中的位置i,輸出a[i-1],a[i+1]就ok

Ⅳ 怎樣用c語言輸出前驅字元和後繼字元

如樓上所說
常見於連續存儲的數據結構
數組,鏈表,棧,隊列
以字元數組為例
"abcde",其中b的前驅是a,後繼是c

Ⅵ C語言:輸入一個大寫字母,求其對應的小寫字母及其它的前驅與後繼

#include <stdio.h>
int main()
{
int ch;
ch=getchar();
putchar(ch-'a'+'A') ;
putchar(ch-1);
putchar(ch+1);
return 0;
}
如果沒有太多要求,這樣應該是可以的。