<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>c程序设计课后答案</title><link>http://www.wb98.com/c/</link><description>c程序设计第四版谭浩强</description><generator>RainbowSoft Studio Z-Blog 2.2 Prism Build 140101</generator><language>zh-CN</language><pubDate>Tue, 30 Jun 2020 19:03:35 +0800</pubDate><item><title>从键盘输入若干行字符（每行长度不等），输入后把它们存储到一磁盘文件中，再从该文件中读入这些数据，将其中小写字母转换成大写字母后显示在屏幕上输出。</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.11.html</link><pubDate>Mon, 20 Jun 2016 13:05:13 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.11.html</guid><description><![CDATA[<p>&nbsp;从键盘输入若干行字符（每行长度不等），输入后把它们存储到一磁盘文件中，再从该文件中读入这些数据，将其中小写字母转换成大写字母后显示在屏幕上输出。<br/><br/>【c源程序】<br/>#include &lt;stdio.h&gt;<br/>int main()<br/>&nbsp;{ int i,flag;<br/>&nbsp;&nbsp; char str[80],c;<br/>&nbsp;&nbsp; FILE *fp;<br/>&nbsp;&nbsp; fp=fopen(&quot;text&quot;,&quot;w&quot;);<br/>&nbsp;&nbsp; flag=1;<br/>&nbsp;&nbsp; while(flag==1)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;input string:\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gets(str);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(fp,&quot;%s &quot;,str);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;continue?&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c=getchar();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((c==&#39;N&#39;)||(c==&#39;n&#39;))<br/>&nbsp;&nbsp;&nbsp; &nbsp;flag=0;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getchar();<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; fclose(fp);<br/>&nbsp;&nbsp; fp=fopen(&quot;text&quot;,&quot;r&quot;);<br/>&nbsp;&nbsp; while(fscanf(fp,&quot;%s&quot;,str)!=EOF)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {for (i=0;str[i]!=&#39;\0&#39;;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; if ((str[i]&gt;=&#39;a&#39;) &amp;&amp; (str[i]&lt;=&#39;z&#39;))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; str[i]-=32;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%s\n&quot;,str);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; fclose(fp);<br/>&nbsp;&nbsp; return 0;<br/>&nbsp;}<br/></p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.11.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=110</wfw:commentRss></item><item><title>从第9题（上一题）的职工工资文件中删去一个职工的数据，再存回原文件。</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.10.html</link><pubDate>Mon, 20 Jun 2016 13:03:31 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.10.html</guid><description><![CDATA[<p>&nbsp;从第9题（上一题）的职工工资文件中删去一个职工的数据，再存回原文件。<br/><br/>谭浩强c语言程序设计第四版答案<br/>【c源程序】<br/>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>#include &lt;string.h&gt;<br/>struct emploee<br/>{char&nbsp; name[10];<br/>&nbsp;int&nbsp;&nbsp; salary;<br/>}emp[20];</p><p>int main()<br/>&nbsp;{ FILE *fp;<br/>&nbsp;&nbsp; int i,j,n,flag;<br/>&nbsp;&nbsp; char name[10];<br/>&nbsp;&nbsp; if ((fp=fopen(&quot;emp_salary&quot;,&quot;rb&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;can not open file.\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; printf(&quot;\noriginal data:\n&quot;);<br/>&nbsp;&nbsp; for (i=0;fread(&amp;emp[i],sizeof(struct emploee),1,fp)!=0;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n&nbsp; %8s&nbsp;&nbsp; %7d&quot;,emp[i].name,emp[i].salary);<br/>&nbsp;&nbsp; fclose(fp);<br/>&nbsp;&nbsp; n=i;<br/>&nbsp;&nbsp; printf(&quot;\ninput name deleted:\n&quot;);<br/>&nbsp;&nbsp; scanf(&quot;%s&quot;,name);<br/>&nbsp;&nbsp; for (flag=1,i=0;flag &amp;&amp; i&lt;n;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {if (strcmp(name,emp[i].name)==0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {for (j=i;j&lt;n-1;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {strcpy(emp[j].name,emp[j+1].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emp[j].salary=emp[j+1].salary;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flag=0;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; if(!flag)<br/>&nbsp;&nbsp;&nbsp;&nbsp; n=n-1;<br/>&nbsp;&nbsp; else<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\nnot found!&quot;);<br/>&nbsp;&nbsp; printf(&quot;\nNow,The content of file:\n&quot;);<br/>&nbsp;&nbsp; if((fp=fopen(&quot;emp_salary&quot;,&quot;wb&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;can not open file\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; for (i=0;i&lt;n;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fwrite(&amp;emp[i],sizeof(struct emploee),1,fp);<br/>&nbsp;&nbsp; fclose(fp);<br/>&nbsp;&nbsp; fp=fopen(&quot;emp_salary&quot;,&quot;r&quot;);<br/>&nbsp;&nbsp; for (i=0;fread(&amp;emp[i],sizeof(struct emploee),1,fp)!=0;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n%8s&nbsp;&nbsp; %7d&quot;,emp[i].name,emp[i].salary);<br/>&nbsp;&nbsp; printf(&quot;\n&quot;);<br/>&nbsp;&nbsp; fclose(fp);<br/>&nbsp;&nbsp; return 0;<br/>&nbsp;}</p><p><br/></p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.10.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=109</wfw:commentRss></item><item><title>有一磁盘文件“employee”，内存放职工的数据。每个职工数据包括职工姓名、职工号、性别、年龄……</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.9.html</link><pubDate>Mon, 20 Jun 2016 11:59:55 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.9.html</guid><description><![CDATA[<p>&nbsp;有一磁盘文件“employee”，内存放职工的数据。每个职工数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另件一个简明的职工工资文件。<br/><br/>【c源程序】<br/>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>#include &lt;string.h&gt;<br/>struct emploee<br/>{char&nbsp;&nbsp;&nbsp; num[6];<br/>&nbsp;char&nbsp;&nbsp;&nbsp; name[10];<br/>&nbsp;char&nbsp;&nbsp;&nbsp; sex[2];<br/>&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp; age;<br/>&nbsp;char&nbsp;&nbsp;&nbsp; addr[20];<br/>&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp; salary;<br/>&nbsp;char&nbsp;&nbsp;&nbsp; health[8];<br/>&nbsp;char&nbsp;&nbsp;&nbsp; class[10];<br/>&nbsp;}em[10];</p><p>&nbsp;struct emp<br/>&nbsp;{char name[10];<br/>&nbsp; int&nbsp; salary;<br/>&nbsp;}em_case[10];</p><p>int main()<br/>&nbsp;{FILE *fp1,*fp2;<br/>&nbsp; int i,j;<br/>&nbsp; if ((fp1=fopen(&quot;emploee&quot;,&quot;r&quot;))==NULL)<br/>&nbsp;&nbsp; {printf(&quot;can not open file.\n&quot;);<br/>&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp; }<br/>&nbsp; printf(&quot;\n&nbsp; NO.&nbsp;&nbsp; name&nbsp; sex&nbsp;&nbsp; age&nbsp;&nbsp;&nbsp; addr&nbsp;&nbsp; salary&nbsp;&nbsp; health&nbsp; class\n&quot;);<br/>&nbsp; for (i=0;fread(&amp;em[i],sizeof(struct emploee),1,fp1)!=0;i++)<br/>&nbsp;&nbsp;&nbsp; {printf(&quot;\n%4s%8s%4s%6d%10s%6d%10s%8s&quot;,em[i].num,em[i].name,em[i].sex,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; em[i].age,em[i].addr,em[i].salary,em[i].health,em[i].class);<br/>&nbsp;&nbsp;&nbsp;&nbsp; strcpy(em_case[i].name,em[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp; em_case[i].salary=em[i].salary;<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; printf(&quot;\n\n&nbsp; ********************************&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;);<br/>&nbsp;&nbsp; if((fp2=fopen(&quot;emp_salary&quot;,&quot;wb&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;can not open file\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; for (j=0;j&lt;i;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {if(fwrite(&amp;em_case[j],sizeof(struct emp),1,fp2)!=1)<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;error!&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n&nbsp; %12s%10d&quot;,em_case[j].name,em_case[j].salary);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; printf(&quot;\n&nbsp;&nbsp; *******************************&nbsp;&nbsp;&nbsp;&nbsp; &quot;);<br/>&nbsp;&nbsp; fclose(fp1);<br/>&nbsp;&nbsp; fclose(fp2);<br/>&nbsp;&nbsp; return 0;<br/>&nbsp;}</p><p>&nbsp;</p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.9.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=108</wfw:commentRss></item><item><title>将第7题结果仍存入原有的”stu_sort&amp;quot;文件而不另建新文件。</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.8.html</link><pubDate>Mon, 20 Jun 2016 11:52:43 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.8.html</guid><description><![CDATA[<p>将第7题结果仍存入原有的”stu_sort&quot;文件而不另建新文件。<br/>第7题内容：将第6题已排序的学生成绩文件进入插入处理。插入一个学生的3门课程成绩，程序先计算新插入学生的平均成绩，然后将它按成绩高低顺序插入，插入后建立一个新文件。</p><p>第6题内容：将第5题“stud”文件中的学生数据，按平均分进行排序处理，将已排序的学生数据存入一个新文件“stu_sort“中。</p><p>第5题内容：有5个学生，每个学生有3门课程的成绩，从键盘输入学生数据（包括学号、姓名、三门课的成绩），计算出平均成绩，将原有数据和计算出平均分数存放在磁盘文件“stud”中 。</p><p><br/>c程序设计(第四版)学习辅导 谭浩强 编著<br/>【c源程序】<br/><br/></p><p>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>struct student<br/>{char num[10];<br/>&nbsp;char name[8];<br/>&nbsp;int score[3];<br/>&nbsp;float ave;<br/>&nbsp;}&nbsp; st[10],s;</p><p>&nbsp;int main()<br/>&nbsp;{FILE *fp,*fp1;<br/>&nbsp; int i,j,t,n;<br/>&nbsp; printf(&quot;\nNO.:&quot;);<br/>&nbsp; scanf(&quot;%s&quot;,s.num);<br/>&nbsp; printf(&quot;name:&quot;);<br/>&nbsp; scanf(&quot;%s&quot;,s.name);<br/>&nbsp; printf(&quot;score1,score2,score3:&quot;);<br/>&nbsp; scanf(&quot;%d,%d,%d&quot;,&amp;s.score[0],&amp;s.score[1],&amp;s.score[2]);<br/>&nbsp; s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*从文件读数据*/<br/>&nbsp; if((fp=fopen(&quot;stu_sort&quot;,&quot;r&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp; {printf(&quot;can not open file.&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; printf(&quot;original data:\n&quot;);<br/>&nbsp;&nbsp;&nbsp; for (i=0;fread(&amp;st[i],sizeof(struct student),1,fp)!=0;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;\n%8s%8s&quot;,st[i].num,st[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%8d&quot;,st[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%10.2f&quot;,st[i].ave);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p>&nbsp; n=i;<br/>&nbsp; for (t=0;st[t].ave&gt;s.ave &amp;&amp; t&lt;n;t++);</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*向文件写数据*/<br/>&nbsp; printf(&quot;\nNow:\n&quot;);<br/>&nbsp; fp1=fopen(&quot;sort1.dat&quot;,&quot;w&quot;);<br/>&nbsp; for (i=0;i&lt;t;i++)<br/>&nbsp;&nbsp;&nbsp; {fwrite(&amp;st[i],sizeof(struct student),1,fp1);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n %8s%8s&quot;,st[i].num,st[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp; for (j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%8d&quot;,st[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%10.2f&quot;,st[i].ave);<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; fwrite(&amp;s,sizeof(struct student),1,fp1);<br/>&nbsp; printf(&quot;\n %8s %7s %7d %7d %7d%10.2f&quot;,s.num,s.name,s.score[0],<br/>&nbsp;&nbsp;s.score[1],s.score[2],s.ave);</p><p>&nbsp; for (i=t;i&lt;n;i++)<br/>&nbsp;&nbsp;&nbsp; {fwrite(&amp;st[i],sizeof(struct student),1,fp1);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n %8s%8s&quot;,st[i].num,st[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp; for(j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%8d&quot;,st[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%10.2f&quot;,st[i].ave);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp; printf(&quot;\n&quot;);<br/>&nbsp; fclose(fp);<br/>&nbsp; fclose(fp1);<br/>&nbsp; return 0;<br/>&nbsp;}<br/></p><p></p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.8.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=107</wfw:commentRss></item><item><title>将第6题已排序的学生成绩文件进入插入处理。插入一个学生的3门课程成绩</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.7.html</link><pubDate>Mon, 20 Jun 2016 11:50:03 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.7.html</guid><description><![CDATA[<p>&nbsp;<span style="font: 16px/30.4px &quot;Microsoft YaHei&quot;, Verdana, small, Arial, Helvetica, sans-serif; text-align: justify; color: rgb(42, 42, 42); text-transform: none; text-indent: 32px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; widows: 1; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">&nbsp;将第6题已排序的学生成绩文件进入插入处理。插入一个学生的3门课程成绩，程序先计算新插入学生的平均成绩，然后将它按成绩高低顺序插入，插入后建立一个新文件。</span><br/><br/>第6题内容：将第5题“stud”文件中的学生数据，按平均分进行排序处理，将已排序的学生数据存入一个新文件“stu_sort“中。<br/><br/>第5题内容：有5个学生，每个学生有3门课程的成绩，从键盘输入学生数据（包括学号、姓名、三门课的成绩），计算出平均成绩，将原有数据和计算出平均分数存放在磁盘文件“stud”中 。<br/></p><p style="font: 16px/30.4px &quot;Microsoft YaHei&quot;, Verdana, small, Arial, Helvetica, sans-serif; margin: 0px; padding: 8px 0px; text-align: justify; color: rgb(42, 42, 42); text-transform: none; text-indent: 2em; letter-spacing: normal; word-spacing: 0px; white-space: normal; widows: 1; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">【c源程序】<br/><br/></p><p>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>struct student<br/>{char num[10];<br/>&nbsp;char name[8];<br/>&nbsp;int score[3];<br/>&nbsp;float ave;<br/>&nbsp;}&nbsp; st[10],s;</p><p>&nbsp;int main()<br/>&nbsp;{FILE *fp,*fp1;<br/>&nbsp; int i,j,t,n;<br/>&nbsp; printf(&quot;\nNO.:&quot;);<br/>&nbsp; scanf(&quot;%s&quot;,s.num);<br/>&nbsp; printf(&quot;name:&quot;);<br/>&nbsp; scanf(&quot;%s&quot;,s.name);<br/>&nbsp; printf(&quot;score1,score2,score3:&quot;);<br/>&nbsp; scanf(&quot;%d,%d,%d&quot;,&amp;s.score[0],&amp;s.score[1],&amp;s.score[2]);<br/>&nbsp; s.ave=(s.score[0]+s.score[1]+s.score[2])/3.0;</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*从文件读数据*/<br/>&nbsp; if((fp=fopen(&quot;stu_sort&quot;,&quot;r&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp; {printf(&quot;can not open file.&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; printf(&quot;original data:\n&quot;);<br/>&nbsp;&nbsp;&nbsp; for (i=0;fread(&amp;st[i],sizeof(struct student),1,fp)!=0;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;\n%8s%8s&quot;,st[i].num,st[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%8d&quot;,st[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%10.2f&quot;,st[i].ave);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p><p>&nbsp; n=i;<br/>&nbsp; for (t=0;st[t].ave&gt;s.ave &amp;&amp; t&lt;n;t++);</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*向文件写数据*/<br/>&nbsp; printf(&quot;\nNow:\n&quot;);<br/>&nbsp; fp1=fopen(&quot;sort1.dat&quot;,&quot;w&quot;);<br/>&nbsp; for (i=0;i&lt;t;i++)<br/>&nbsp;&nbsp;&nbsp; {fwrite(&amp;st[i],sizeof(struct student),1,fp1);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n %8s%8s&quot;,st[i].num,st[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp; for (j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%8d&quot;,st[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%10.2f&quot;,st[i].ave);<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; fwrite(&amp;s,sizeof(struct student),1,fp1);<br/>&nbsp; printf(&quot;\n %8s %7s %7d %7d %7d%10.2f&quot;,s.num,s.name,s.score[0],<br/>&nbsp;&nbsp;s.score[1],s.score[2],s.ave);</p><p>&nbsp; for (i=t;i&lt;n;i++)<br/>&nbsp;&nbsp;&nbsp; {fwrite(&amp;st[i],sizeof(struct student),1,fp1);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n %8s%8s&quot;,st[i].num,st[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp; for(j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%8d&quot;,st[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%10.2f&quot;,st[i].ave);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp; printf(&quot;\n&quot;);<br/>&nbsp; fclose(fp);<br/>&nbsp; fclose(fp1);<br/>&nbsp; return 0;<br/>&nbsp;}<br/>&nbsp;</p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.7.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=106</wfw:commentRss></item><item><title>将第5题stud文件中的学生数据，按平均分进行排序处理</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.6.html</link><pubDate>Mon, 20 Jun 2016 11:05:07 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.6.html</guid><description><![CDATA[<p>将第5题“stud”文件中的学生数据，按平均分进行排序处理，将已排序的学生数据存入一个新文件“stu_sort“中。<br/><br/>第5题内容：有5个学生，每个学生有3门课程的成绩，从键盘输入学生数据（包括学号、姓名、三门课的成绩），计算出平均成绩，将原有数据和计算出平均分数存放在磁盘文件“stud”中 。<br/><br/>解法1【c源程序】<br/><br/></p><p>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>#define N 10<br/>struct student<br/>{char num[10];<br/>&nbsp;char name[8];<br/>&nbsp;int score[3];<br/>&nbsp;float ave;<br/>&nbsp;} st[N],temp;</p><p>int main()<br/>&nbsp;{FILE *fp;<br/>&nbsp; int i,j,n;</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*读文件*/<br/>&nbsp; if ((fp=fopen(&quot;stud&quot;,&quot;r&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp; {printf(&quot;can not open.\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; printf(&quot;File &#39;stud&#39;: &quot;);<br/>&nbsp; for (i=0;fread(&amp;st[i],sizeof(struct student),1,fp)!=0;i++)<br/>&nbsp;&nbsp;&nbsp; {printf(&quot;\n%8s%8s&quot;,st[i].num,st[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp; for (j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp; printf(&quot;%8d&quot;,st[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%10.2f&quot;,st[i].ave);<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; printf(&quot;\n&quot;);<br/>&nbsp; fclose(fp);<br/>&nbsp; n=i;</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*排序*/<br/>&nbsp; for (i=0;i&lt;n;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; for (j=i+1;j&lt;n;j++)<br/>&nbsp;if (st[i].ave &lt; st[j].ave)<br/>&nbsp;&nbsp; {temp=st[i];<br/>&nbsp;&nbsp;&nbsp; st[i]=st[j];<br/>&nbsp;&nbsp;&nbsp; st[j]=temp;<br/>&nbsp;&nbsp; }</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*输出*/<br/>&nbsp; printf(&quot;\nNow:&quot;);<br/>&nbsp; fp=fopen(&quot;stu_sort&quot;,&quot;w&quot;);<br/>&nbsp; for (i=0;i&lt;n;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {fwrite(&amp;st[i],sizeof(struct student),1,fp);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n%8s%8s&quot;,st[i].num,st[i].name);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf (&quot;%8d&quot;,st[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%10.2f&quot;,st[i].ave);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp; printf(&quot;\n&quot;);<br/>&nbsp; fclose(fp);<br/>&nbsp; return 0;<br/>&nbsp;}</p><p><br/>解法2【c源程序】<br/>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>#define SIZE 5<br/>struct student<br/>{<br/>&nbsp;char name[10];<br/>&nbsp;int num;<br/>&nbsp;int score[3];<br/>&nbsp;float ave;<br/>&nbsp;}&nbsp; stud[SIZE],work;<br/>int main()<br/>&nbsp;{<br/>&nbsp;&nbsp; void sort(void);<br/>&nbsp;&nbsp; int i;<br/>&nbsp;&nbsp; FILE *fp;<br/>&nbsp;&nbsp; sort();<br/>&nbsp;&nbsp; fp=fopen(&quot;stud_sort.dat&quot;,&quot;rb&quot;);<br/>&nbsp;&nbsp; printf(&quot;sorted student&#39;s scores list as follow\n&quot;);<br/>&nbsp;&nbsp; printf(&quot;----------------------------------------------------\n&quot;);<br/>&nbsp;&nbsp; printf(&quot; NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; N0.&nbsp;&nbsp;&nbsp;&nbsp; SCORE1&nbsp;&nbsp; SCORE2&nbsp;&nbsp; SCORE3&nbsp;&nbsp;&nbsp; AVE&nbsp;&nbsp;&nbsp; \n&quot;);<br/>&nbsp;&nbsp; printf(&quot;----------------------------------------------------\n&quot;);<br/>&nbsp;&nbsp; for (i=0;i&lt;SIZE;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp; fread(&amp;stud[i],sizeof(struct student),1,fp);<br/>&nbsp;&nbsp; printf(&quot;%-10s %3d %8d %8d %8d %9.2f\n&quot;,stud[i].name,stud[i].num,<br/>&nbsp;&nbsp; stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; fclose(fp);<br/>&nbsp;&nbsp; return 0;<br/>&nbsp;}</p><p>&nbsp;void sort(void)<br/>&nbsp; {FILE *fp1,*fp2;<br/>&nbsp;&nbsp; int i,j;<br/>&nbsp;&nbsp; if ((fp1=fopen(&quot;stu.dat&quot;,&quot;rb&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;The file can not open\n\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; if ((fp2=fopen(&quot;stud_sort.dat&quot;,&quot;wb&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;The file write error\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; for (i=0;i&lt;SIZE;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (fread(&amp;stud[i],sizeof(struct student),1,fp1)!=1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;file read error\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; for (i=0;i&lt;SIZE;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {for (j=i+1;j&lt;SIZE;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (stud[i].ave&lt;stud[j].ave)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {work=stud[i];<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stud[i]=stud[j];<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stud[j]=work;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fwrite(&amp;stud[i],sizeof(struct student),1,fp2);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; fclose(fp1);<br/>&nbsp;&nbsp; fclose(fp2);<br/>&nbsp;}</p><p><br/></p><p>&nbsp;</p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.6.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=105</wfw:commentRss></item><item><title>有5个学生，每个学生有3门课程的成绩，从键盘输入学生数据（包括学号、姓名、三门课的成绩），计算出平均成绩，将原有数据和计算出平均分数存放在磁盘文件“stud”中 。</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.5.html</link><pubDate>Mon, 20 Jun 2016 11:00:57 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.5.html</guid><description><![CDATA[<p>&nbsp;有5个学生，每个学生有3门课程的成绩，从键盘输入学生数据（包括学号、姓名、三门课的成绩），计算出平均成绩，将原有数据和计算出平均分数存放在磁盘文件“stud”中 。<br/><br/>解法1【c源程序】<br/></p><p>#include &lt;stdio.h&gt;<br/>struct student<br/>{char num[10];<br/>&nbsp;char name[8];<br/>&nbsp;int score[3];<br/>&nbsp;float ave;<br/>&nbsp;}&nbsp; stu[5];</p><p>int main()<br/>&nbsp;{ int i,j,sum;<br/>&nbsp;&nbsp; FILE *fp;<br/>&nbsp;&nbsp; for(i=0;i&lt;5;i++)<br/>&nbsp;&nbsp; {printf(&quot;\ninput score of student %d:\n&quot;,i+1);<br/>&nbsp;&nbsp; printf(&quot;NO.:&quot;);<br/>&nbsp;&nbsp; scanf(&quot;%s&quot;,stu[i].num);<br/>&nbsp;&nbsp; printf(&quot;name:&quot;);<br/>&nbsp;&nbsp; scanf(&quot;%s&quot;,stu[i].name);<br/>&nbsp;&nbsp; sum=0;<br/>&nbsp;&nbsp; for (j=0;j&lt;3;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;score %d:&quot;,j+1);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf(&quot;%d&quot;,&amp;stu[i].score[j]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sum+=stu[i].score[j];<br/>&nbsp;&nbsp; }<br/>&nbsp;&nbsp; stu[i].ave=sum/3.0;<br/>&nbsp;&nbsp; }</p><p>&nbsp;&nbsp;&nbsp; /*将数据写入文件*/<br/>&nbsp; fp=fopen(&quot;stud&quot;,&quot;w&quot;);<br/>&nbsp; for (i=0;i&lt;5;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (fwrite(&amp;stu[i],sizeof(struct student),1,fp)!=1)<br/>&nbsp;printf(&quot;file write error\n&quot;);<br/>&nbsp; fclose(fp);</p><p>&nbsp; fp=fopen(&quot;stud&quot;,&quot;r&quot;);<br/>&nbsp; for (i=0;i&lt;5;i++)<br/>&nbsp;&nbsp;&nbsp; {fread(&amp;stu[i],sizeof(struct student),1,fp);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n%s,%s,%d,%d,%d,%6.2f\n&quot;,stu[i].num,stu[i].name,stu[i].score[0],<br/>&nbsp;&nbsp;&nbsp; stu[i].score[1],stu[i].score[2],stu[i].ave);}<br/>&nbsp; return 0;<br/>&nbsp; }</p><p><br/>解法2【c源程序】<br/><br/></p><p>#include &lt;stdio.h&gt;<br/>#define SIZE 5<br/>struct student<br/>{char name[10];<br/>&nbsp;int num;<br/>&nbsp;int score[3];<br/>&nbsp;float ave;<br/>&nbsp;}&nbsp; stud[SIZE];</p><p>int main()<br/>&nbsp;{ void save(void);<br/>&nbsp;&nbsp; int i;<br/>&nbsp;&nbsp; float sum[SIZE];<br/>&nbsp;&nbsp; FILE *fp1;<br/>&nbsp;&nbsp; for (i=0;i&lt;SIZE;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; { scanf(&quot;%s %d %d %d %d&quot;,stud[i].name,&amp;stud[i].num,&amp;stud[i].score[0],<br/>&nbsp;&nbsp;&amp;stud[i].score[1],&amp;stud[i].score[2]);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sum[i]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2];<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stud[i].ave=sum[i]/3;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;save();<br/>&nbsp;fp1=fopen(&quot;stu.dat&quot;,&quot;rb&quot;);<br/>&nbsp;printf(&quot;\n name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NO.&nbsp;&nbsp;&nbsp; score1&nbsp; score2&nbsp; score3&nbsp;&nbsp; ave\n&quot;);<br/>&nbsp;printf(&quot;-----------------------------------------------\n&quot;);<br/>&nbsp;for (i=0;i&lt;SIZE;i++)<br/>&nbsp;&nbsp; {fread(&amp;stud[i],sizeof(struct student),1,fp1);<br/>&nbsp;&nbsp;&nbsp; printf(&quot;%-10s %3d %7d %7d %7d %8.2f\n&quot;,stud[i].name,stud[i].num,<br/>&nbsp;&nbsp;&nbsp; stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave);<br/>&nbsp;&nbsp; }<br/>&nbsp;fclose (fp1);<br/>&nbsp;return 0;<br/>&nbsp;}</p><p>&nbsp;void save(void)<br/>&nbsp;{<br/>&nbsp;&nbsp; FILE *fp;<br/>&nbsp;&nbsp; int i;<br/>&nbsp;&nbsp; if ((fp=fopen(&quot;stu.dat&quot;,&quot;wb&quot;))==NULL)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;The file can not open\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; for(i=0;i&lt;SIZE;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (fwrite(&amp;stud[i],sizeof(struct student),1,fp)!=1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;file write error\n&quot;);<br/>&nbsp;&nbsp; return;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp; fclose(fp);<br/>&nbsp;}<br/></p><p>&nbsp;</p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.5.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=104</wfw:commentRss></item><item><title>有两个磁盘文件a和b，各存放一行字母，今要求把这两个文件中的信息合并</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.4.html</link><pubDate>Mon, 20 Jun 2016 10:42:03 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.4.html</guid><description><![CDATA[<p>&nbsp;有两个磁盘文件“A”和“B”，各存放一行字母，今要求把这两个文件中的信息合并（按字母顺序排列），输出到一个新文件&quot;<a style="color: rgb(0, 0, 0); text-decoration: underline;" href="http://www.wb86.com/cjia" target="_self"><span style="color: rgb(0, 0, 0);">C</span></a>&quot;中去。<br/><br/>【c源程序】<br/>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>int main ()<br/>{<br/>&nbsp;FILE *fp;<br/>&nbsp;int i,j,n,i1;<br/>&nbsp;char c[100],t,ch;<br/>&nbsp;if ((fp=fopen(&quot;a1&quot;,&quot;r&quot;))==NULL)<br/>&nbsp;&nbsp; { printf(&quot;\ncan not open file\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp; }<br/>&nbsp;printf(&quot;file A :\n&quot;);<br/>&nbsp;for (i=0;(ch=fgetc(fp))!=EOF;i++)<br/>&nbsp; {<br/>&nbsp;&nbsp; c[i]=ch;<br/>&nbsp;&nbsp; putchar(c[i]);<br/>&nbsp; }<br/>&nbsp;fclose(fp);</p><p>&nbsp;i1=i;<br/>&nbsp;if ((fp=fopen(&quot;b1&quot;,&quot;r&quot;))==NULL)<br/>&nbsp; {printf(&quot;\ncan not open file\n&quot;);<br/>&nbsp;&nbsp; exit(0);<br/>&nbsp; }<br/>&nbsp;printf(&quot;\nfile B:\n&quot;);<br/>&nbsp;for (i=i1;(ch=fgetc(fp))!=EOF;i++)<br/>&nbsp;&nbsp; {c[i]=ch;<br/>&nbsp;&nbsp;&nbsp; putchar(c[i]);<br/>&nbsp;&nbsp; }<br/>&nbsp;fclose(fp);</p><p>&nbsp;n=i;<br/>&nbsp;for (i=0;i&lt;n;i++)<br/>&nbsp;&nbsp; for (j=i+1;j&lt;n;j++)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (c[i]&gt;c[j])<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {t=c[i];<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c[i]=c[j];<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c[j]=t;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;printf(&quot;\nfile C :\n&quot;);<br/>&nbsp;fp=fopen(&quot;c1&quot;,&quot;w&quot;);<br/>&nbsp;for (i=0;i&lt;n;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {putc(c[i],fp);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(c[i]);<br/>&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;printf(&quot;\n&quot;);<br/>&nbsp;fclose(fp);<br/>&nbsp;return 0;<br/>}&nbsp;</p><p></p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.4.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=103</wfw:commentRss></item><item><title>从键盘输入一个字符串 将其中的小写字母全部转换成大写字母，然后输出到一个磁盘文件</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_10.3.html</link><pubDate>Mon, 20 Jun 2016 10:38:29 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_10.3.html</guid><description><![CDATA[<p>&nbsp;从键盘输入一个字符串 将其中的小写字母全部转换成大写字母，然后输出到一个磁盘文件“test&quot;中保存，输入的字符串以“！”结束。<br/><br/>c程序设计(第四版)学习辅导 谭浩强 编著<br/>【c源程序】<br/>#include &lt;stdio.h&gt;<br/>#include &lt;string.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>int main ()<br/>{<br/>&nbsp;FILE *fp;<br/>&nbsp;char str[100];<br/>&nbsp;int i=0;<br/>&nbsp;if ((fp=fopen(&quot;a1&quot;,&quot;w&quot;))==NULL)<br/>&nbsp;&nbsp; { printf(&quot;can not open file\n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; exit(0);<br/>&nbsp;&nbsp; }<br/>&nbsp;printf(&quot;input a string:\n&quot;);<br/>&nbsp;gets(str);<br/>&nbsp;while (str[i]!=&#39;!&#39;)<br/>&nbsp; {if (str[i]&gt;=&#39;a&#39;&amp;&amp; str[i]&lt;=&#39;z&#39;)<br/>&nbsp;&nbsp;&nbsp;&nbsp; str[i]=str[i]-32;<br/>&nbsp;&nbsp; fputc(str[i],fp);<br/>&nbsp;&nbsp; i++;<br/>&nbsp; }<br/>&nbsp;fclose(fp);<br/>&nbsp;fp=fopen(&quot;a1&quot;,&quot;r&quot;);<br/>&nbsp;fgets(str,strlen(str)+1,fp);<br/>&nbsp;printf(&quot;%s\n&quot;,str);<br/>&nbsp;fclose(fp);<br/>&nbsp;return 0;<br/>}</p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_10.3.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=102</wfw:commentRss></item><item><title>建立一个链表,每个结点数据包括:学号、姓名，性别、年龄。</title><author>null@null.com (hyf64)</author><link>http://www.wb98.com/c/post/tanhaoqiang_9.12.html</link><pubDate>Mon, 20 Jun 2016 10:27:12 +0800</pubDate><guid>http://www.wb98.com/c/post/tanhaoqiang_9.12.html</guid><description><![CDATA[<p>&nbsp;建立一个链表,每个结点数据包括:学号、姓名，性别、年龄。输入一个年龄，如果链表中的结点所包含的年龄等于此年龄，则将此结点删去。<br/><br/>【<a style="color: rgb(0, 0, 0); text-decoration: underline;" href="http://www.wb86.com/cjia" target="_self"><span style="color: rgb(0, 0, 0);">c</span></a>源程序】<br/><br/>#include &lt;stdio.h&gt;<br/>#include &lt;malloc.h&gt;<br/>#define LEN sizeof(struct&nbsp; student)<br/>struct&nbsp; student<br/>{&nbsp; char num[6];<br/>&nbsp;&nbsp; char name[8];<br/>&nbsp;&nbsp; char sex[2];<br/>&nbsp;&nbsp; int age;<br/>&nbsp;&nbsp; struct student *next;<br/>} stu[10];</p><p>int main()<br/>{ struct student *p,*pt,*head;<br/>&nbsp; int i,length,iage,flag=1;<br/>&nbsp; int find=0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //找到待删除元素 find=1,否则find=0<br/>&nbsp; while (flag==1)<br/>&nbsp;&nbsp; {printf(&quot;input length of list(&lt;10):&quot;);<br/>&nbsp;&nbsp;&nbsp; scanf(&quot;%d&quot;,&amp;length);<br/>&nbsp;&nbsp;&nbsp; if (length&lt;10)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; flag=0;<br/>&nbsp;&nbsp; }</p><p>&nbsp;&nbsp;&nbsp; //建立链表 <br/>&nbsp; for (i=0;i&lt;length;i++)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {p=(struct student *) malloc(LEN);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i==0)<br/>&nbsp;&nbsp;&nbsp;&nbsp; head=pt=p;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br/>&nbsp;&nbsp;&nbsp;&nbsp; pt-&gt;next=p;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pt=p;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;NO.:&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf(&quot;%s&quot;,p-&gt;num);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;name:&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf(&quot;%s&quot;,p-&gt;name);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;sex:&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf(&quot;%s&quot;,p-&gt;sex);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;age:&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf(&quot;%d&quot;,&amp;p-&gt;age);<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; p-&gt;next=NULL;<br/>&nbsp; p=head;<br/>&nbsp; printf(&quot;\n NO.&nbsp;&nbsp;&nbsp; name&nbsp;&nbsp;&nbsp; sex&nbsp; age\n&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //显示&nbsp; <br/>&nbsp; while(p!=NULL)<br/>&nbsp;&nbsp;&nbsp;&nbsp; {printf(&quot;%4s%8s%6s%6d\n&quot;,p-&gt;num,p-&gt;name,p-&gt;sex,p-&gt;age);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p=p-&gt;next;<br/>&nbsp;&nbsp;&nbsp;&nbsp; }</p><p>&nbsp;&nbsp;&nbsp; //&nbsp; 删除&nbsp; <br/>&nbsp; printf(&quot;input age:&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //输入待删年龄&nbsp; <br/>&nbsp; scanf(&quot;%d&quot;,&amp;iage);<br/>&nbsp; pt=head;<br/>&nbsp; p=pt;<br/>&nbsp; if (pt-&gt;age==iage)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //链头是待删元素<br/>&nbsp;&nbsp;&nbsp; {p=pt-&gt;next;<br/>&nbsp;&nbsp;&nbsp;&nbsp; head=pt=p;<br/>&nbsp;&nbsp;&nbsp;&nbsp; find=1;<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //链头不是待删元素<br/>&nbsp;&nbsp;&nbsp; pt=pt-&gt;next;<br/>&nbsp; while (pt!=NULL)<br/>&nbsp;&nbsp;&nbsp; {if (pt-&gt;age==iage)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {p-&gt;next=pt-&gt;next;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; find=1;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 中间结点不是待删元素<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p=pt;<br/>&nbsp;&nbsp;&nbsp;&nbsp; pt=pt-&gt;next;<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; if (!find)<br/>&nbsp;&nbsp;&nbsp; printf(&quot; not found&nbsp; %d.&quot;,iage);</p><p>&nbsp; p=head;<br/>&nbsp; printf(&quot;\n NO.&nbsp;&nbsp;&nbsp; name&nbsp;&nbsp;&nbsp; sex&nbsp; age\n&quot;); //显示结果<br/>&nbsp; while (p!=NULL)<br/>&nbsp;&nbsp;&nbsp; {printf(&quot;%4s%8s&quot;,p-&gt;num,p-&gt;name);<br/>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%6s%6d\n&quot;,p-&gt;sex,p-&gt;age);<br/>&nbsp;&nbsp;&nbsp;&nbsp; p=p-&gt;next;<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; return 0;<br/>&nbsp;}</p><p></p>]]></description><category>c程序设计第四版谭浩强课后答案</category><comments>http://www.wb98.com/c/post/tanhaoqiang_9.12.html#comment</comments><wfw:commentRss>http://www.wb98.com/c/feed.asp?cmt=101</wfw:commentRss></item></channel></rss>
