int main()
{
int a,b,c
printf("请输入旅客托运行李的重量、经济舱全额票价:")
scanf("%d%d",&a,&b)
if(a>50)
printf("不能托运\n")
else
{
if(a<=20)
printf("免费\n")
else
{
c = (a - 20)*0.015*b
printf("超重费用为%d",c)
}
}
return 0
}
7. 编写程序打印出如下杨辉三角形。解答: (1)源程序代码:(2)运行结果截图:(3)运行结果分析:
#include<stdio.h>
#define N 10
void main()
{
int s[N][N]
for(int i=0i<Ni++)
{
S[i][0]=s[i][i]=1
}
for(i=2i<Ni++)
for(int j=1j<Ij++)
s[i][j]=s[i-1][j-1]+s[i-1][j]
for(i=0i<Ni++)
{ for(int j=0j<=ij++)
printf(“\t%d”,s[i][j])
printf(“\n”)
}
}
8. 分析下列程序,查错并修改。
#include <stdio.h>
#include <string.h>
void main()
{ char a[10],b[10]
int c,d,kchar ch
scanf("%s",&a)---à scanf("%s",a)
scanf("%s",&b)--à scanf("%s",b)
printf("a=%s,b=%s\n",a,b)
c=strlen(a)
d=strlen(b)
if(c>d)
for(k=0k<dk++)
{ch=a[k]a[k]=b[k]b[k]=ch}
printf("a=%s\n",a)
printf("b=%s\n",b)
}
解答:(1) 源程序代码:(2)运行结果截图:(3)运行结果分析:
9.预习下面的程序的功能,写出预习结果。并上机验证。
#include <stdio.h>
#include <string.h>
void main( )
{ char s1[80], s2[40]
int i = 0, j = 0
printf(" \n Please inputstring1:")
scanf("%s", s1)
printf(" \n Please inputstring2:")
scanf("%s", s2)
while (s1[i]!= '\0' )
i++
while (s2[j]!= '\0' )
s1[i++]=s2[j ++]
s1[i]= '\0'
printf("\n New string: %s",s1)
}
解答:(1)运行结果截图:(2)运行结果分析:
S2附加到S1的最后
10. 补充以下程序,使其功能是对从键盘上输入的两个字符串进行比较,然后输出两个字符串中第一个不相同字符的ASCII码之差。例如:输入的两个字符串分别为abcdefg和abceef,则输出为-1。程序如下:
#include <stdio.h>
void main ( )
{ char str1[100],str2[100]
int i,s
printf("\n Input string1:\n")gets(str1)
printf(“\n Input string2:\n”)gets(str2)
i= 0 (1)
while((str1[i]==str2[i])&&(str1[i]!= ‘\0’ (2) ))
i++
s= str1[i]=str2[i] (3)
printf("%d\n",s)
}
解答:(1) 源程序代码:(2)运行结果截图:(3)运行结果分析:
11. 补充以下程序,功能是统计从终端输入字符串中每个大写字母的个数。用#号作为输入结束标志。
#include <stdio.h>
#include <ctype.h>
void main ( )
{ int num[26],ichar c
for (i=0i<26i++)num[i]=0
while ( (c=getchar())(1) != '#') /* 统计从终端输入的大写字母个数*/
if (isupper(c))num[c-65]++ (2)
for (i=0i<26i++) /* 输出大写字母和该字母的个数*/
if (num[i])printf("%c:%d\n",i+65 (3),num[i])
}
解答: (1)源程序代码:(2)运行结果截图:(3)运行结果分析:
12.编写程序,功能是输入若干个字符串,求出每个字符串的长度,并打印最长的字符串的内容。以“stop”作为输入的最后一个字符串。
解答:(1)源程序代码:(2)运行结果截图:(3)运行结果分析:
13.编写程序,输入字符串s1和s2以及插入位置f,在字符串s1中的指定位置f处插入字符串s2。如:输入"BEIJING"、"123"和位置3,则输出:"BEI123JING"。解答: (1)源程序代码:(2)运行结果截图:(3)运行结果分析:
这个程序比较长, 就写一下你的实验四吧, 有了实验四来做试验五不难, 只是打开文件, 读取数据而已实验四:结果是: |1|2|2|-->|4||4|1|-->|7|10|4|-->|12|14|3| 附上运行截图:#include<stdio.h>#include<malloc.h>
struct nodes{
int head,tail,num
struct nodes *next
}
typedef struct nodes node
typedef struct nodes* link
int main()
{
int buff[] = {1,2,4,7,8,9,10,12,13,14}
int len, i
link hp,p,newp
len = sizeof(buff)/sizeof(int)
//申请一个头节点,并将第一个数字填入头节点的head和tail,并将num设为1,next归为NULL
hp = (link)malloc(sizeof(node))
p = hp
p->head = buff[0]
p->tail = buff[0]
p->num = 1
p->next = NULL
for(i = 1i <leni++)
{
if(buff[i] == p->tail + 1)
{
//如果当前数字与当前节点的tail连续,则用该数字覆盖tail并将num++
p->tail = buff[i]
p->num ++
}
else
{
//如果当前节点与当前节点tail不连续,则新建一个节点接在链表尾部并将数字填入head和tail,num设为1,next归为NULL
newp = (link)malloc(sizeof(node))
p->next = newp
p = p->next
p->head = buff[i]
p->tail = buff[i]
p->num = 1
p->next = NULL
}
}
//遍历并输出每个节点的元素
p = hp
while(len &&p)
{
printf("|%d|%d|%d|",p->head,p->tail,p->num)
if(p->next)
{
printf("-->")
p = p->next
}
else
{
printf("")
break
}
}
//释放之前所申请的空间
p = hp
while(p)
{
newp = p->next
free(p)
p = newp
}
hp = NULL
newp = NULL
p = NULL
}