C程序设计实验报告
实验名称:结构程序设计 学时安排:2课时
实验类别:上机操作型 实验要求:1人1组
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
一、实验目的
1. 掌握结构变量的基本使用方法;
2. 掌握结构数组的基本使用方法;
3. 掌握结构的简单嵌套应用;
4. 掌握结构指针的概念,以及结构指针作为函数参数的编程方法。
二、实验设备介绍
软件需求: Visual C++ 6.0或CFree3.5以上版本
硬件需求: 对于硬件方面的要求,建议配置是Pentium III 450以上的CPU处理器,64MB以上的内存,200MB的自由硬盘空间、CD-ROM驱动器、能支持24位真彩色的显示卡、彩色显示器、打印机。
三、实验内容
1. 定义一个关于年、月、日的结构,并编写函数计算某日是该年中的第几天?要求用结构指针作为函数参数,注意闰年问题。以下部分程序供参考,可自行改动:
struct date{ /*日期结构体:包含年月日*/
int year;
int month;
int day;
};
void dayofyear(struct date *pday);
int main(void)
{
struct date temp,*pday;
pday=&temp;
/*请添加*/
}
运行结果示例如下:
Input year、month and day: 2006 3 5
64
2. 改正程序error09_2.cpp中的错误。建立一个有n(3到10之间)个学生成绩的结构记录,包括学号、姓名和3门成绩,输出总分最高的学生姓名和总分。输入输出结果如下所示:
n=3
1 zhang 78 83 75
2 wang 76 80 77
3 shen 92 88 78
总分最高的学生是:shen,258分
要求把正确的代码(按规范缩进)写入报告,并以注释的方式说明错误原因。
注释示例如下:
int i; /*循环控制变量不能定义为double类型*/
3. 编写一程序,计算两个时刻之间的时间差,并将其值返回。时间以时、分、秒表示,假设两个时刻的差小于24 小时。(此题选做)
提示:先输入两个时间,然后比较两个时间的大小,用大的减去小的。时间相减从秒开始,如果负数则需位,再减分,最后减小时。
四、程序清单
第一题
#include<stdio.h>
int main()
{
int result;
struct riqi{
int year;
int month;
int day;
}date;
printf("Input year、month and day:");
scanf("%d%d%d",&date.year,&date.month,&date.day);
int day_of_year(int year,int month,int day);
result=day_of_year(date.year,date.month,date.day);
printf("%d\n",result);
return 0;
}
int day_of_year(int year,int month,int day)
{
int k,leap,yearday;
int tab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30},{0,31,29,31,30,31,30,31,31,30,31,30}};
leap=(year%4==0&&year%100!=0)||year%400==0;
yearday=0;
for(k=1;k<month;k++)
yearday=yearday+tab[leap][k];
yearday=yearday+day;
return yearday;
}
第二题
#include <stdio.h>
int main (void)
{
struct student{
int number;
char name[20];
int score[3];
int sum;
};
struct student student[20];/*定义记录20个学生信息的结构数组*/
int i, j, k, n, max=0;
printf("输入n:");
scanf("%d",&n);
for (i = 0; i < n; i++){
printf("输入第%d个学生的学号、姓名和3门成绩:",i+1);/*输入第i+1个学生*/
student[i].sum=0;/*将成绩和初始化*/
scanf("%d%s", &student[i].number, &student[i].name);
for(j = 0; j < 3; j++){
scanf("%d", &student[i].score[j]);
student[i].sum += student[i].score[j];
}
}
max = student[0].sum;
for(i = 0; i < n; i++)
if(max < student[i].sum)
{
max = student[i].sum;
k = i;
}
printf("总分最高的学生是: %s,%d分\n", student[k].name, student[k].sum);
return 0;
}
第三题
#include <stdio.h>
struct time
{
int hour;
int minute;
int second;
};
void main()
{
struct time time1,time2;
int timestart,timeend,hours,minutes,seconds;
printf("输入起始时刻:");
scanf("%d:%d:%d",&time1.hour,&time1.minute,&time1.second);
printf("输入结束时刻:");
scanf("%d:%d:%d",&time2.hour,&time2.minute,&time2.second);
timestart=time1.hour*3600+time1.minute*60+time1.second;
timeend=time2.hour*3600+time2.minute*60+time2.second;
hours=(timeend-timestart)/3600;
minutes=(timeend-timestart-3600*hours)/60;
seconds=timeend-timestart-3600*hours-60*minutes;
printf("两时刻相差:%d小时%d分%d秒\n",hours,minutes,seconds);
return 0;
}
五、运行结果
第一题
第二题
第三题
六、实验心得
写结构的程序题时,注意结构定义以及结构名。注意程序要条理清晰。
第二篇:C程序设计上机实验报告11
C程序设计实验报告
实验名称:指针与字符串 学时安排:2课时
实验类别:上机操作型 实验要求:1人1组
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
一、实验目的
1. 掌握通过指针操作字符串的方法;
二、实验设备介绍
软件需求: Visual C++ 6.0或CFree3.5以上版本
硬件需求: 对于硬件方面的要求,建议配置是Pentium III 450以上的CPU处理器,64MB以上的内存,200MB的自由硬盘空间、CD-ROM驱动器、能支持24位真彩色的显示卡、彩色显示器、打印机。
三、实验内容
1.输入一个字符串t和一个正整数m,讲t中从第m个(m小于字符串长度)字符开始的全部字符复制的字符串s中,再输出字符串s。要求用字符指针定义并调用函数strmcpy(s, t, m),它的功能是将字符串t中从第m个字符开始的全部字符复制到字符串s中。示例如下:
Input a string: happy new year!
Input an integers: 7
Output is: new year!
2. 编写一个函数delchar(s, c),该函数将字符串s中出现的所有c字符删除。自己定义main函数,并在其中调用delchar(s,c)函数。D此大锅饭
3. 改正程序error08_4.cpp中的错误。函数strc的作用是将字符串s连接到t的尾部。在主函数中输入两个字符串s和t,调用strc完成字符串的连接。输入输出结果如下所示:
World!
Hello
HelloWorld!
要求把正确的代码(按规范缩进)写入报告,并以注释的方式说明错误原因。
注释示例如下:
int i; /*循环控制变量不能定义为double类型*/
4. 编程判断输入的一串字符是否为“回文”,如果是输出“Yes”,否则输出“No”。所谓“回文”,是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是回文。
四、程序清单
第一题
#include<stdio.h>
void strmcpy(char str[],char t[],int m){
int i=0,n; char *s; s=str;
for(s+=m-1;*s!=0;s++)
{
t[i]=*s;
i++; }
printf("Output is:");
for(n=0;n<i;n++)
puts(t);
printf("\n");
}
int main(){
char s[80],t[80];
int m;
printf("Input a string:");
gets(s);
printf("Input an integer:");
scanf("%d",&m);
strmcpy(s,t,m);
return 0;
}
第二题
#include<stdio.h>
int i;
char str[80];
void delchar(char str[],char c)
{
char *s;
for(s=str;*s!='\0';s++)
{
if(*s==c)
*s='\0';
i++;
}
}
int main()
{
int n;
char a,str[80];
printf("Input a string:");
gets(str);
printf("Input a char:");
a=getchar();
printf("After deleted,the string is:");
delchar(str,a);
for(n=0;n<=i;n++)
{
if(str[n]!='\0')
putchar(str[n]);
}
printf("\n");
return 0;
}
第三题
# include <stdio.h>
void strc(char *s, char *t);/*形参应该是指针或数组*/
int main()
{
char s[80], t[80];
gets(s);
gets(t);
strc(s, t);
puts(t);
return 0;
}
void strc(char *s, char *t) /*形参应该是指针或数组*/
{
while (*t!='\0')
t++;
while (*t++=*s++); /*判断条件不对,While的结束标志就是*s = NULL的时候,应当改成这样*/
}
第四题
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[100];
int i,j;
gets(a);
i=0;
j=strlen(a)-1;
while(i<j)
{
if(a[i]==a[j])
{i++;j--;}
else break;
}
if(i>=j)
printf("Yes!\n");
else printf("No!\n");
return 0;
}
五、运行结果
第一题
第二题
第三题
第四题
六、实验心得
1.数组名作为实参,形参是指针变量或数组
2.变量地址做实参,对应的形参为指针。
3.编写程序时,应简洁。