求助!C语言ungetc()的问题!

图片说明
如图 把c=getchar()和c!='#'写成一句话的时候,第一个字符就会出现乱码的情况
图片说明
把这两句话拆开写,一切恢复正常
s_gets 是我自定义的一个IO函数,问题应该不是出在这里,实际上,换成gets_s等其他IO函数,一样会出现这样子的问题。
书翻遍了,网上也找了很久,得不到答案
跪求大神解答,谢谢~~~~~~

c

1个回答

已经发现问题了。。。。运算符优先级问题

应该写成(c=getchar())!='#';
话说C prime plus 关于ungetc的函数原型写错了
应该是“int ungetc(char c,FILE *fp)”
而不是
应该是“int ungetc(int c,FILE *fp)”

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
关于ungetc函数的问题
我假设ch[10]='a','b','c','d','e','f','g','h','i','j';,然后用rn for(i=0;i<10;i++)rn rn putchar(ch[i]);rn ungetc(ch[i],stdin);rn putchar(getchar());rn putchar('\n');rn rn的出来的结果是:aarn bbrn ccrn .....rn但是用 for(i=0;i<10;i++)rn rn putchar(ch[i]);rn ungetc(ch[i],stdin);rn rn putchar('\n');rn for(i=0;i<10;i++)rn rn putchar(getchar());rn rn的出来的记过却是:abcdefghijrn arn这是怎么回事啊,一直搞不明白ungetc具体是怎么工作的?rn
C语言问题求助
names.txt 其中装有10个英文名。读入names.txt文本里的内容,按从小到大的顺序排列,然后输出到另外一个文件。 rn提示:在main函数中声明一个指针数组*name[10]。读入names.txt文件里的一个名字到buff[90]中(别忘了用‘\0‘结尾)。用malloc分配足够大的空间,并用一个指针数组的元素指向这个空间的地址。然后把buff[90]里的字符串copy到这个分配的空间中。rn(用C语言编写,请大家帮助,谢谢..)
求助C语言问题
#include rnfloat plus(int n);rnint main()rnint n,i;rnprintf("请输入n\n");rnscanf("%d",&n);rnprintf("%f\n",plus(n));rnsystem("pause");rnreturn 0;rn rnfloat plus(int n)rnint i;rnfloat *p;rnif (n%2==0)rnrnfor(i=1;i<=(n/2);i++)rnrn *p+=1/(2*i) ; rn rn return (*p);rn rn elsern rn for(i=1;i<=((n+1)/2);i++)rn rn *p+=1/(2*i-1);rn rn return (*p);rn rn rn[img=https://img-bbs.csdn.net/upload/201507/02/1435835182_934190.png][/img][img=https://img-bbs.csdn.net/upload/201507/02/1435835192_148627.png][/img]
求助c语言问题
如题,写一个计算程序,输出结果不是想要的rnrn#include rn#include rnrnint main()rnrn int a,b;rn char sign,quit = 'y' ;rn while((quit!='n')&&(quit!='N'))rn rn printf("请输入需要计算的数据\n");rn scanf("%d%s%d",&a,&sign,&b);rn switch(sign)rn rn case '+':printf("%d%c%d=%d\n",a,sign,b,a+b);break;rn case '-':printf("%d%c%d=%d\n",a,sign,b,a-b);break;rn case '*':printf("%d%c%d=%d\n",a,sign,b,a*b);break;rn case '/':rn if(b == 0)rn rn printf("请重新输入,分母不能为“0”\n");rn break;rn elsern rn printf("%d%c%d=%d\n",a,sign,b,a/b);rn break;rn rn default:printf("error");break;rn rn printf("请问是否继续计算y/n?\n");rn scanf("%c",&quit);rnrnrnrnrnrnrn预期结果为:比如输入 2 - 3 = -1 继续打印是否继续,然后接受scanf的值,我运行的结果为:输出计算结果后直接打印请输入需要计算的数据,没有暂停等待scanfrnrn请问是哪里出现问题了呢
c语言问题求助
c语言,用的vc,想要的是做个顺序表,现在插入信息出问题了,之后输入的信息会盖掉之前所有的,用的结构体,这之中有int的name和字符串的sno,int型的没问题,但是字符串类型的先插入a,再插入b,b会盖掉a,显示的为b b,但是int的显示的就正常,算法写的结构都是一样的,求大神帮助,代码如下,红字部分为插入的算法,写的一样但是类型不同结构不同,小白求回答的详细点rnrnrnrnrn#include"stdio.h"rn#include "stdlib.h"rn#include rnrnrnstruct listrnrn int max;rn int n;rn int name[100];rn char *sno[100];rn;rntypedef struct list *plist;rn//创建一个空的顺序表,最大值为m。n为0rnplist create(int m)rnrn plist a=(plist)malloc(sizeof(struct list));rn if(a!=NULL)rn rn a->name[100]=(int )malloc(sizeof(int)*m);rn a->sno[10]=(char *)malloc(sizeof(char)*m);rn if(a->name&&a->sno)rn rn a->max=m;rn a->n=0;rn printf("success to create\n");rn return a;rn rn else rn rn free(a);rn printf("free!");rn rn rn elsern printf("failed to create!");rn return NULL;rn rnrnrn//查询一个表是否为空表rnint check(plist pl)rnrn if(pl->n==0)rn rn printf("empty\n");rn return 0;rn rnprintf("not empty\n");rnreturn 1;rnrnrn//插入一个信息到顺序表,插入到第i个元素之前rnvoid insert(plist pl,int i,int x,char sno[10])rnrnrn int q;rn if(pl->nn-1;q>=i;q--)rn rn [color=#FF0000] pl->name[q+1]=pl->name[q];rn pl->sno[q+1]=pl->sno[q];[/color]rn rn pl->n++;rn pl->name[i]=x;rn pl->sno[i]=sno;rn rn rn//return pl;rnrnrnrnint visit(plist pl)rnrn int flag=-1;rn int i;rn flag=check(pl);rn if(!flag)rn return 0;rn elsern rn for(i=0;in;i++) rn rn printf("%d:%d\n",i,pl->name[i]);rn printf("%d:%s\n\n",i,pl->sno[i]);rn rn rnrnrnrnvoid main()rnrnlist l;rnplist pl;rnchar x[10];//存放姓名,方便输入,字符串rnint loc=-1;//选择插入位置,插入在该参数之前rnint id=-1;//id信息rnint temp=0;rnpl=create(10);rncheck(pl);rnrnrnwhile(1)rnrn printf("please press your name:\n");rn scanf("%s",&x);rn printf("press you id:");rn scanf("%d",&id);rn printf("please press which number you want put before:");rn scanf("%d",&loc);rn insert(pl,loc,id,x);rn visit(pl);rnrnrnrnrn/* list l;rn int i=0;rn plist pl; pl=create(10);rn pl->sno[i]="tao";rn i++;rn pl->sno[i]="xin";rn i++;rn pl->sno[i]="cool";rn pl->n=3;rn visit(pl);*/rnrnrnrnrnrn
c语言问题。求助!
#includernmain()rn int a[3][3]=2,4,6,i,*p=&a[0][0];rn for(i=0;i<2;i++)rn if(i==0) a[i][i+1]=*p+1;rn else ++p;rn printf("%d",*p);rn rn printf("\n");rn rn
C语言问题,求助!
void main()rnrn int a;rn float b;rn a=5/4;rn b=5/4;rn printf("%d\n",5/4); //1rn printf("%f\n",5/4); //0.000000rn printf("%f\n",b); //1.000000rn printf("%d\n",b); //0rn printf("%f\n",1.0/3*3); //1.000000rn printf("%f\n",1/3); //0.000000rn printf("%f\n",1/3*3); //0.000000rnrn这个main函数中第二个至第四个printf函数的结果为什么分别是0.000000 1.000000 0
求助C语言问题、、、
#includernmain()rnrn int a[5]=1,9,16,18,20;rn int *p=a;rn printf("%d\n",*p);rn printf("%d\n",*p++);//9rnrnrn程序运行出来的结果是1,1。不应该是1,9吗?
求助!c语言问题,
随机出十道100以内计算题,要求加减乘除概率相等rn出现的问题:有的时候运行到一半就不行了。rn希望大神帮帮忙rn#include rn#include rn#include rn#include rn#include rn#define IDNO_LENGTH 6rn#define CALC_KIND 4rn#define CALC_NUM 10rnrnbool checkNo(char idNo[]);rnchar getSignal();rnint getResult(int,int,char);rnvoid takeTest();rnrn//main函数rnint main()rnrn printf("Please input your four digit ID no(begin with two letters):");rn char idNo[IDNO_LENGTH];rn dorn rn scanf("%s",&idNo);rn fflush(stdin);rn while(checkNo(idNo)==false);rn rn printf("1.Start a test\t2.Check score\t3.Exitb\n");rn int choose;rn dorn rn printf("选择:");rn scanf("%d",&choose);rn fflush(stdin);rn while(choose!=1 && choose!=2&& choose!=3);rn rn if(choose==1)rnrn takeTest();rnelse if(choose==2)rnrnFILE *fp; rnchar ch; rnif((fp=fopen("score.txt","w+"))==NULL) rn rnprintf("\n你还没做过题目测试"); rngetchar(); rnexit(1); rn rnch=fgetc(fp); rnwhile(ch!=EOF) rn rnputchar(ch); rnch=fgetc(fp); rn rnfclose(fp);rnrn elsernrn exit(0);rnrnrnrn//获取随机运算符rnchar getSignal()rnrn char signal[CALC_KIND]='+','-','*','/';rn srand((unsigned)time(NULL));rn return signal[rand()%4];rnrnrn//获取随机数rnint random(void) rn rnint i; rni=rand() % 100;rnreturn (i);rn rnrnrn//运算rnint getResult(int num1,int num2,char signal)rnrn float res;rn switch(signal)rn rn case '+':rn res=num1+num2;break;rn case '-':rn res=num1-num2;break;rn case '*':rn res=num1*num2;break;rn case '/':rn res=num1/num2;break;rn default:rn res=0;rn printf("运算符不符合规则\n");rn rn return res;rnrnrn//int checkresult(int h)rn//rn// float res;rn// res=getresult(num1,num2,signal);rn// if (int(res)!=res|res>=100) h=0;rn// else h=1;rn// return h; rn//rnrn//列出10道题rnvoid takeTest()rnrnprintf("开始答题,每道题10分,总共100分\n");rn clock_t startTime;rn startTime=clock();rn int index;rn int res;rn int giveRes;rn int l,k,h;rn int num1;rn int num2;rn int score=0;rn char signal;rn for(index=0;index100||res<0) k=0;rn else k=1;rn if(signal=='*')rn rn num1=random();rn num2=rand()%(100/num1);rn rn if(signal=='/')rn rn do rn rn num1=random();rn num2=rand() % num1;rn int c;rn c=num1/num2;rn if(num2*c==num1) h=1;rn else h=0;rn while(h==0);rn rn rn printf("%d%c%d=",num1,signal,num2);rn scanf("%d",&giveRes);rn fflush(stdin);rn if(getResult(num1,num2,signal)==giveRes)rn rn printf("correct\n");rn score+=10;rn rn elsern rn printf("wrong\n");rn rn rnFILE *fp=fopen("score.txt","w");rn fprintf(fp,"分数为%d",score);rn fclose(fp);rnprintf("解10道题用了%lf秒\n",(double)(clock()- startTime)/CLOCKS_PER_SEC);rnrnrn//检测idNo是否合规则,合规则返回truernbool checkNo(char idNo[])rnrn int index;rn bool flag;rn flag=true;rn for(index=0;index
ungetc函数
ungetc函数是将输出流中的废弃数据退入流中去。 ungetc(ch,stdin); 像这样就可以把ch中的数据吐出来。 可以用于需要字符判断的地方。
C语言新手问题,求助
照着书排的码,运行结果是这样[img=https://img-bbs.csdn.net/upload/201512/31/1451496077_830989.png][/img]rn这是怎么回事,哪里错了。
求助一道C语言问题
题目是:求两个任意整数的最大公约数,用的是欧几里得算法rnrn[code=C/C++][/code]rn#includernvoid main()rnrn int a,b,r;rn printf("please input two integer:\n");rn scanf("%d%d",&a,&b);rn while(b!=0)rn rn r=a%b;rn a=b;rn r=b;rn rn printf("%d\n",a);rnrn getchar();rn getchar();rnrnrn程序运行的时候为什么输入两个整数后没显示最大公约数呢,求高手指点!
[求助]c语言有关?:的问题。
输入a,b,c 三个整数,求最大者,用 k = a>b?a:b>c?a>b?a:b:c,如果三个数是 2 6 9 ,程序对各种顺序都能找出最大的数 9,唯独输入顺序为6 2 9 时,输出却是 k = 6。当然,将 a>b?a:b 加上括号就全部正确了。有哪位能详细解释一下其中的原因吗?主要是c的编译对于不加括号时的分析过程是怎样的?包括运算顺序等等,请详解。
C语言, 菜鸟问题, 求助!!~~
这段程序的运行结果是这样的;rnrn input?: wrn Small letterrnrn为什么到这里就结束了, 我还想要继续输入别的数字怎么办 ,如何让它回到开头, 再继续判断啊, 哪位大虾能帮下忙, 谢谢了!!rn rn#include rnmain()rn rn char c; rn rnwhile(c != 32) rn goto loop;rnrnrn rnloop: rn printf("input? :");rn scanf("%c",&c);rn rn if(c>=97 && c<=122)rn printf("Small letter\n");rn else if(c>=65 && c<=90)rn printf("Capital letter\n");rn else if(c>=48 && c<=57)rn printf("Number\n");rn else printf("Others\n"); rnrn rn
一个c语言问题求助
#include rnrnint main()rnrn float a=0;rn scanf("%f",&a);rn printf("%f",a);rn return 1;rn rnrn编译后执行,当我输入226.26(或者其他小树后保留2位的实数)时发现输出的结果却是226.25995。请问怎么样才能使得变量a的值是226.26(或者226.2600也行),即将小数2位后的数清零?rnrn注:不是说仅仅显示到小数后2位,而是在以后使用变量a时它不会是226.25995
C语言递归问题求助
先列代码……rnint main(void)rnrn int a, b, c, n;rn a = 1;rn n = 3;rn b = fact(n);rn c = 2;rnrnrnint fact(int n)rnrn if(n < 1) return 1;rn else return (n * fact(n - 1));rnrnrn在调用函数前,需要把调用该函数的后面那条指令的地址入栈(我记得以前是这么学的,应该没错吧……)rn那以我的理解,在第一次调用fact之前,就应该把c = 2;这条指令对应在地址入栈。rnrn问题是:rn在第二次或第三次循环调用fact函数的时候,应该被入栈的地址对应的指令又是哪一条呢?rn想了好久感觉已经绕进去出不来了,望各位大神能指点一下,谢谢!
求助,一道c语言的问题
#include "stdio.h"rn swap(int *a,int *b)rn int *t;rn t=a; a=b; b=t;rn rnmain()rnint x=3,y=5,*p=&x,*q=&y;rnswap(p,q);rnprintf("%d%d\n",*p,*q);rnrnrnrnrn请问答案为什么是3 5,不是5 3啊?rn
c语言代码问题~~求助~~~~~
设甲乙两人同年出生,但月日不同。请计算甲乙的年龄差的绝对值。例如输入1997 2 27 3 10(表明甲乙出生在1997年,甲出生在2月27日,乙出生在3 月10日),则输出11(甲乙相差11天)nn思路:nn1、分别计算甲乙的生日是该年的第多少天(分别用n1,n2表示)。计算该年的多少天,可通过函数调用实现;nn2、年龄差绝对值计算nnn```n #includenint fun(int y,int m,int d)nint s=0;nfor(int i=1;i<=m-1;i++)nswitch(i)ncase 1:ncase 3:ncase 5:ncase 7:ncase 8:ncase 10:ncase 12:s+=31;ncase 4:ncase 6:ncase 9:ncase 11:s+=30;ncase 2:if(y%4==0&&y%100!=0||y%400==0) s+=29;n else s+=28;nnns+=d;nreturn s;nn/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */nnvoid main() n n int y,m1,d1,m2,d2,n1,n2; n scanf("%d%d%d",&y,&m1,&d1);n scanf("%d%d",&m2,&d2);n n1=fun(y,m1,d1);n n2=fun(y,m2,d2);n if(n1>=n2)n printf("%d\n",n1-n2);n elsen printf("%d\n",n2-n1);nnn/* PRESET CODE END - NEVER TOUCH CODE ABOVE */n```n这个代码是我写的 总会有一个隐藏的测试用例结果错误 不知道代码哪有缺陷 请大神赐教!n
C语言的简单排序问题求助
![图片说明](https://img-ask.csdn.net/upload/201705/03/1493823376_358366.png)nn题目见图nn我的代码是nn#includennvoid main()nn int a[5];n int i,j,c;n for (i = 0; i < 5; i++)n scanf("%d",&a[i]);n for (j = 0; j < 5; j++)n n for (i = 0; i < 5-j; i++)n n if (a[i]
C语言问题,新人求助
C语言的一个问题:rn 问题要求一次性输入多组参数进行处理,但是事先没有不输入循环次数。然后一次性输出多组结果。rn 示例如下:rn 输入:rn 1 2 3rn 8 9 3rn 输出:rn4rn5rn题中说文件以end of file结束,我不知道具体怎么实现输入控制,请教各位。
求助!一个C语言的问题.
请教 比如说我现在有一个.xml个是的文件文件内容比较特殊:rn  例 AAAAA|BBBBBBB|CC|DDDDDDDD|EEEEEEEEEE|...rn 谢谢!但是我不希望这样做。我是想通过指针依次读取这个文件中的各个字符串而且每个字符串都要存到相应的自己定义的数组里去。。。
[求助]:一个C语言问题
想把字符串前面都加上 "Ox" (format函数),但怎么将改变的值传回去?rnrn#include rn#include rnrnchar format(char *str)rnrn char *ch="0x";rn char buf[10];rn str=strcat(buf,ch);rn printf(str); // 这里输出都是 0xffffrnrnrn rnint main(void)rnrn char *p="ffff";rn format(p);rn printf(p); // 输出还是ffff没变rn rn getch();rn return 0;rn
[求助]C语言深度搜索问题
深度搜索应用在什么地方?!最好能给出些源代码~谢了rn新手上。
求助 C语言递归问题
#includernmain()rnrn void binary_to_ascii(unsigned int value);rn binary_to_ascii(4123);rnrnvoid binary_to_ascii(unsigned int value)rnrn unsigned int quotient;rn quotient = value/10;rn [color=#FF0000]while[/color](quotient != 0)rn binary_to_ascii(quotient);rn putchar(value%10 + '0');rnrnrn此程序执行后是死循环,一直打印 4 ,while改成if,可正常打印4123,请问各位大侠程序是怎么执行的
c语言数组问题——求助
[code=C/C++]rn#includernrn#define N 10rnrnint main()rnrn int a[N] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;rn int sum = 0;rn int *p = NULL;rnrn for (p = &a[0]; p < &a[N]; ++p)rn rn sum += *p;rn rnrn printf("sum = %d. \n", sum);rnrn return 0;rnrn[/code]rnrn元素a[N]并不存在,for循环中的“p < &a[N]; ”安全吗?rn
求助:c语言字符串问题
[code=c]rn#includern#includernvoid mystrcat(char *s, char *t);rnint main()rn char *fir= "abc";rn char *sec= "123";rn printf("%s", fir);rn printf("%s\n", sec);rn mystrcat(fir,sec);rn printf("%s", fir);rn return 0;rnrnrnvoid mystrcat(char *s, char *t)rn while(*s)rn s++;rn while(*s++ = *t++)rn ;rn[/code]rnrn将两个字符串相连,编译通过,却出不来结果。rn而如果不用rn[code=c]rn char *fir= "abc";rn char *sec= "123";rn[/code]rn而是改成数组rn[code=c]rn char fir[]= "abc";rn char sec[]= "123";rn[/code]rnrn就可以出来结果。rn请问下是什么原因?
求助C语言程序暂停的问题...
我想用DOS运行程序后,显视结果后,等用户按任意键结束程序.或双击C序程后有一样的结果.请各位帮帮忙呀...
c语言赋值问题求助
请看下面的程序:rnrn #include rn rn int main()rn rn int a, b, z;rn rn printf("a= ");rn scanf("%i", &a);rn printf("b= "); rn scanf("%i", &b);rn rn (z=a, a=b, b=z); // 问题在这里rn rn printf("a= %i\nb= %i\n", a, b);rn rn return 0;rn rn本来我是直接写 z=a; a=b; b=z;的,偶然间写成了程序里那样,居然一样可以运行。翻书找不到答案,请帮忙解释,谢谢
简单c语言问题求助
学生成绩报告单rn1,2,3,4四门课,写一程序,任给一学生学号即可打印出该学生的成绩报告单.rn成绩报告单rn姓名:*** 学号:*** 1:*** 2:*** 3:*** 4:*** 总分:*** 名次:***rn题目要求为输入一个学号,就能打印出其如上的成绩报告单,其中包括学号、姓名、四门成绩、总分和名次。要求计算出总分,并根据总分给出名次。rnrn#define NULL 0rnstruct RECORDrnrnchar ID[12];rnchar name[30];rnint record1,record2,record3,record4;rnint sum;rnstruct RECORD *next;rn;rnrntypedef struct RECORD RECORD_STRUCT;rnint main()rnrnchar option;rnRECORD_STRUCT* student = NULL;rnrnprintf("Usage: input (i name ID value1 value2 value3 value4 )\n");rnprintf("Usage: output (o) \n");rnprintf("Usage: find and print (f ID) \n");rnprintf("Usage: exit (x) \n");rnwhile(scanf("%c",&option) == 1)rnrnswitch(option)rnrncase 'i':rnrnint record[4];rnint sum;rnchar s[12];rnchar nam[30];rnif(scanf("%s %s %d %d %d %d",nam,s,&record[0],&record[1],&record[2],&record[3]) == 6)rnrnRECORD_STRUCT* pLeft = student;rnRECORD_STRUCT* pTmp = pLeft;rnRECORD_STRUCT* pRight = (RECORD_STRUCT*)malloc(sizeof(RECORD_STRUCT));rnrnsum = record[0]+record[1]+record[2]+record[3];rnstrcpy(pRight->name,nam);rnstrcpy(pRight->ID,s);rnpRight->record1 = record[0];rnpRight->record2 = record[1];rnpRight->record3 = record[2];rnpRight->record4 = record[3];rnpRight->sum = sum;rnpRight->next = NULL;rnwhile(pTmp != NULL && pTmp->sum > sum)rnrnpLeft = pTmp;rnpTmp = pTmp->next;rnrnpRight->next = pTmp;rnif(pTmp == student)rnstudent = pRight;rnelse if(pLeft != NULL)rnpLeft->next = pRight;rnpRight = NULL;rnrnbreak;rnrncase 'f':rnrnchar s[12];rnchar nam[30];rnRECORD_STRUCT* pLeft = student;rnint index = 1;rnscanf("%s",s);rnwhile(pLeft != NULL)rnrnif(strcmp(pLeft->ID,s) == 0)rnrnprintf("Found successfully!\n");rnprintf(" name ID 1 2 3 4 sum index\n");rnprintf("%s %s %d %d %d %d %d %d\n",rnpLeft->name,pLeft->ID,pLeft->record1,pLeft->record2,pLeft->record3,pLeft->record4,pLeft->sum,index);rnbreak;rnrnpLeft = pLeft ->next;rnindex++;rnrnif(pLeft == NULL)rnprintf("Sorry,can't find the result!\n");rnbreak;rnrncase 'o':rnrnint index = 1;rnRECORD_STRUCT* pLeft = student;rnprintf("name ID 1 2 3 4 sum index\n");rnwhile(pLeft != NULL)rnrnprintf("%s %s %d %d %d %d %d %d\n",rnpLeft->name,pLeft->ID,pLeft->record1,pLeft->record2,pLeft->record3,pLeft->record4,pLeft->sum,index);rnpLeft = pLeft->next;rnindex++;rnrnbreak;rnrncase 'x':rnrnprintf("Thank you for using this system,bye-bye!");rnreturn;rnrnrnrnrn只要解释一下算法rn
新人求助,c语言问题。
程序的功能是输入字符串存入pS指针,进行大小对比后按从小到大输出。发现当输入字符串超过5个时程序会停止运行,恳请哪位大神帮我看下代码有什么问题。n代码如下:n#includen#includen#includen#define BUF_LEN 100n#define COUNT 5nint main(void)nnchar buf[BUF_LEN];nsize_t str_count=0;nsize_t capacity=COUNT;nchar **pS=(char**)calloc(capacity,sizeof(char*));nchar** psTemp=NULL;nchar* pTemp=NULL;nsize_t str_len=0;nbool sorted=false;nprintf("Enter strings to be sorted,one per line.Press Enter to end:\n");nchar *ptr=NULL;nwhile(true)nn ptr=fgets(buf,BUF_LEN,stdin);n if(!ptr)n n printf("Error reading string.\n");n free(pS);n pS=NULL;n return 1;n n if(*ptr=='\n') break;n if(str_count==capacity)n n capacity+=capacity/4;n if(!(psTemp=(char**)realloc(pS,capacity))) return 1;n pS=psTemp;n n str_len=strlen(buf)+1;n if(!(pS[str_count]=(char*)malloc(str_len))) return 2;n strcpy(pS[str_count++],buf);nnwhile(!sorted)nn sorted=true;n for(size_t i=0;i0)n n sorted=false;n pTemp=pS[i];n pS[i]=pS[i+1];n pS[i+1]=pTemp;n n nnprintf("Your input sorted in ascending sequence is:\n\n");nfor(size_t k=0;k
c语言链表问题求助
输入过程可以执行,执行后直接退出.rn调试了很多遍,甚至按照 c primer plus 这本书抄了一遍代码还是不行rn求大神指点.rn平台是vs2017.rn 头文件rn#ifndef 头文件_H_rn#define 头文件_H_rn#include rnrn//特定程序的声明rnrn#define TSIZE 45 //储存电影名的数组大小rnstruct filmrnrn char title[TSIZE];rn int rating;rn;rnrn//一般类型定义rnrntypedef struct film Item;rntypedef struct nodernrn Item item;rn struct node * next;rn Node;rnrntypedef struct listrnrn Node * head;rn Node * end;rnList;rn rn//初始化链表为空rnvoid InitializeList(list plist);rnrn//链表为空,返回1rnbool ListIsFull(const List plist);rnrn//链表为满,返回1rnbool ListIsEmpty(const List plist);rnrn//为链表添加项,成功返回1rnbool AddItem(Item item, List plist);rnrn//确定链表的项数rnunsigned int ListItemCount(List plist);rnrn//遍历链表rnvoid Traverse(const List plist, void(*pfun)(Item item));rnrn//清空链表rnvoid EmptyTheList(List plist);rn#endifrn源文件1:rn#include rn#include rn#include rn#include "头文件.h"rnrn//函数声明rnvoid show_list(Item item);rnchar * s_gets(char * st, int n);rnrnint main(void)rnrn List movies = NULL,NULL;rn Item temp;rnrnrn //初始化rn InitializeList(movies);rn if (ListIsFull(movies)==false)rn rn fprintf(stderr, "No memory available.\n");rn exit(1);rn rnrn //储存数据rn printf("Please input the title of movie:\n");rn while (s_gets(temp.title, TSIZE) != NULL &&rn temp.title[0] != '\0')rn rn printf("Enter your rating <0-10>:\n");rn scanf_s("%d", &temp.rating);rn while (getchar() != '\n')rn continue;rn if (AddItem(temp,movies) == false)rn rn fprintf(stderr, "Allocting memory error.\n");rn break;rn rn if (ListIsFull(movies)==false)rn rn fprintf(stderr, "Sorry, the list is full.\n");rn break;rn rn printf("Input the next title (Empty line to stop):\n");rn rn rn getchar();rn //显示数据rn if (ListIsEmpty(movies) == false)rn rn printf("Sorry, the list is empty.\n");rn return 0;rn rnrn printf("Here is the movie list:\n");rn Traverse(movies, show_list);rn printf("You entered %u movies.\n", ListItemCount(movies));rnrn //清空数据rn EmptyTheList(movies);rnrn printf("Bye.\n");rn getchar();rn return 0;rnrnrnvoid show_list(Item item)rnrn printf("Movie: %s Rating: %d\n", item.title, item.rating);rnrnrnchar * s_gets(char * st, int n)rnrn char * ret_val;rn char * find;rnrn ret_val = fgets(st, n, stdin);rn if (ret_val)rn rn find = strchr(st, '\n');rn if (find)rn *find = '\0';rn elsern while (getchar() != '\n')rn continue;rn rnrn return ret_val;rnrnrn源文件2:rn#include rn#include rn#include "头文件.h"rnrn//局部函数原型rnstatic void CopyToNode(Item item, Node * pnode);rnrn//接口函数rn//把链表设置成空rnvoid InitializeList(list plist)rnrn plist.head = NULL;rn plist.head = NULL;rnrnrn//如果链表为空,则返回truernbool ListIsEmpty(const List plist) rnrn puts("进入了检查空的函数");rn if (plist.head == NULL)rn return false;rn elsern return true;rnrnrn rn//如果链表已满,返回truernbool ListIsFull(const List plist)rnrn bool full;rn Node *pt;rnrn pt = (Node*)malloc(sizeof(Node));rn if (pt == NULL)rn full = false;rn elsern full = true;rn free(pt);rnrn return full;rnrnrn//返回节点的个数rnunsigned int ListItemCount(const List plist)rnrn unsigned int count = 0;rn Node * process = plist.head;rnrn while (plist.head != plist.end)rn rn count++;rn process = process->next;rn rnrn return count + 1;rnrnrnrn//创建储存项的节点,并将其添加至由plist指向的链表末尾rnbool AddItem(Item item, List plist)rnrn Node * pnew;rnrn pnew = (Node*)malloc(sizeof(Node));rn if (pnew == NULL)rn return false;rnrn CopyToNode(item, pnew);rn pnew->next = NULL;rnrn if (plist.head == NULL)rn rn plist.head = pnew;rn rn elsern rn (plist.end)->next = pnew;rn rn rn plist.end = pnew;rn return true;rnrnrnrn//访问每个节点并执行pfun指向的函数rnvoid Traverse(const List plist, void(*pfun)(Item item))rnrn Node * pnode = plist.head;rnrn while (pnode != NULL)rn rn (*pfun)(pnode->item);rn pnode = pnode->next;rn rn rnrnrn//释放由malloc()释放的内存rn//设置链表指针为NULLrnvoid EmptyTheList(List plist)rnrnrn Node * example;rn rn while (plist.head != NULL)rn rn example = plist.head;rn plist.head = example->next;rn free(example);rn rnrnrnrnrn//局部函数定义rn//把一个项拷贝到节点中rnstatic void CopyToNode(Item item, Node * pnode)rnrn pnode->item = item;//拷贝结构rn
一个C语言的问题,求助
请看下面的代码:rnrn#include rn#include rn#include rnvoid main()rnrn rn FILE *fp;rn char str[80];rn if((fp=fopen("jak.txt","w+"))==NULL)rn rn printf("Cannot open file \n");rn exit(1);rn rn dorn rn printf("Enter a string (CR to quit): \n");rn gets(str);rn if(*str!='\n') //1rn rn strcat(str,"\n");rn fputs(str,fp);rn rn while(*str!='\n');rn .rn .rn .rnrnrn如果把//1处的if语句块注销,则程序变成了死循环,可为什么在str字符串后连接一个换行符却可以退出循环?gets()函数不是只接收回车以前的字符么?刚刚学习C语言没多久,实在想不明白,请高手指点,谢谢
求助c语言递归问题
for循环调用递归函数,为什么for循环体从递归函数余下部分没有执行
求助C语言数组的问题.
要做一个函数,在一个字符串中查找和输入字符串(可以在main函数中输入)相同的字符串,比如原字符串是abcgdefk,那么输入kabcde的话,那么输出abc,de,k,一个也算,高手帮忙呢...发分
求助C语言双精度问题,
#include rn#include rndouble hypotenuse (double x,double y);rnrnmain ()rnrndouble a,b;rnprintf ("Enter two numbers a&&b:");rnscanf ("%f,%f",&a,&b);rnprintf ("%f%13.1f%13.1f",a,b,hypotenuse(a,b));rnreturn 0;rnrndouble hypotenuse (double x,double y)rnrndouble c;rnc=sqrt(x*x+y*y);rnreturn (c);rnZrnrn为何我输入:3.0,4.0rn老提示溢出???rnrn
菜鸟求助,C语言 if 的问题。
输入一个数,判断是否为素数rn#include rn#include rnrnint IsPrime(x)rnrn int i;rn for (i = 2; i < x; i++)rn rn if (x % i == 0)rn break;rn rnrnrnint main()rnrn int x;rn printf("input : ");rn scanf("%d", &x);rn if (IsPrime(x))rn printf("%d is a prime number\n", x);rn elsern printf("%d is not a prime number\n", x);rn system("pause");rn return 0;rnrn函数 IsPrime() 中没有返回值,但是运行的时候可以出正确的结果,这是为什么。rn
求助!!!!一个c语言问题
#include rnvoid main()rnrn char k; rn int i;rn for(i=1;i<3;i++)rn rn scanf ("%c",&k);rn switch(k)rn rn case'0': printf("another\n");rn case'1': printf("number\n");rn rn rnrnrnrn输入01rn为什么会输出rnanotherrnnumberrnnumber
求助c语言回文数问题~~~
![图片说明](https://img-ask.csdn.net/upload/201602/15/1455518301_973945.png)n![图片说明](https://img-ask.csdn.net/upload/201602/15/1455518311_869841.png)n```n #includenint main()nn int i,t=0,s=0;n for(i=1000;i<10000;i++,s=0,t=0)n t=i;n while(t)n s=s*10+t%10;n t/=10;n n if(s==i) printf("%d",i);n n return 0;n n```nn结果是0分......n请问哪里错了n
C语言多线程问题求助!!!
[code=c]rn#includern#includern#includernint i=0;rnrnvoid doth(void *p);rnvoid jtime(void *p);rnvoid main()rnrn _beginthread(jtime,0,NULL);rn _beginthread(doth,0,NULL);rn getchar();rnrnvoid jtime(void *p)rnrnrn char str[30];rn while(1)rn rn sprintf(str,"title %d秒",i);rn system(str);rn i++;rn Sleep(1000);rn rnrnvoid doth(void *p)rnrn while(1)rn rn if(i==2)rn rn system("calc");rn rn else if(i==5)rn rn system("notepad");rn rn else if(i==7)rn rn system("tasklist & pause");rn rn Sleep(500);rn rn[/code]rn请问doth()里面的if-else语句的else if里面的东西都不执行是怎么回事?谢谢。
[求助]C语言“=”和“==”的区别问题
[求助]C语言“=”和“==”的区别问题rnrn我看了一段时间书,以前看书前都用= 在表达式上。 ==好像是用在计算式上。rnrn但是现在晕了。。。rnrn谁能简单的概括一下“=”“==”之间的区别,看书东西太多,更晕~~rnrn谢谢rn
相关热词 c# login 居中 c# 考试软件 c# 自然语言分析 c# 分段读取文件 c# 泛型反射 c#打断点 c# 时间转多少秒 c# 线程函数加参数 c# modbus 读取 c#查询集合表