当前位置:首页 » 编程语言 » c语言程序设计实例

c语言程序设计实例

发布时间: 2022-09-04 19:29:38

c语言程序设计实例(关于成绩输入问题)

同学你好!
这是我自己写的,肯定不是最好的,但是已经能够满足你的要求了,后面附有注释,若有不懂的话请留言。
#include<stdio.h>
#include<stdlib.h>

#define N 1024

typedef struct node
{
int sno;/*这里分别是学号(便于最后列表时统计),四门课的分数,总分,平均分*/
int course1;
int course2;
int course3;
int course4;
int aver;
}student;

void main()
{
int i,j,number;
int total1,total2,total3,total4;
total1=total2=total3=total4=0;/*total指的是四门课程的平均分*/
student st[N],temp;
printf("Please input the student's number:");/*确定学生人数*/
scanf("%d",&number);
if(number>1024)
{
printf("The number is too large!\n");
exit(0);
}
for(i=0;i<number;i++)/*进行成绩输入*/
{
printf("Please input the %dth student's sno:",i+1);
scanf("%d",&st[i].sno);
printf("Please input the %d's student's course1:",st[i].sno);
scanf("%d",&st[i].course1);
printf("Please input the %d's student's course2:",st[i].sno);
scanf("%d",&st[i].course2);
printf("Please input the %d's student's course3:",st[i].sno);
scanf("%d",&st[i].course3);
printf("Please input the %d's student's course4:",st[i].sno);
scanf("%d",&st[i].course4);
st[i].aver=(st[i].course1+st[i].course2+st[i].course3+st[i].course4)/4;
}
for(i=0;i<number;i++)/*输出每个学生的平均分*/
printf("The %d's student's aver-score is:%d\n",st[i].sno,st[i].aver);
for(i=0;i<number;i++)/*输出每科目平均分*/
{
total1+=st[i].course1;
total2+=st[i].course2;
total3+=st[i].course3;
total4+=st[i].course4;
}
total1/=number;
total2/=number;
total3/=number;
total4/=number;
printf("The averscore of course1 is:%d\n",total1);
printf("The averscore of course2 is:%d\n",total2);
printf("The averscore of course3 is:%d\n",total3);
printf("The averscore of course4 is:%d\n",total4);
for(i=0;i<number-1;i++)/*进行冒泡排序*/
for(j=i+1;j<number;j++)
if(st[i].aver<st[j].aver)
{
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
printf("The list of the student's course:\n");
printf("==========\n");/*列表公布成绩*/
for(i=0;i<number;i++)
{
printf("%d.",i+1);
printf("%dth student:%d\n",st[i].sno,st[i].aver);
}

}

上级运行成功,希望对你有所帮助!
祝你的成绩步步高升!

② 求C语言程序设计实例,要100行以上,求高手解答,不要太难

/* Note:Your choice is C IDE */
#include "stdio.h"
#include "string.h"
#define MAX 370000
typedef struct
{
char string[12];
int state;
int x;
int y;
}string;
typedef struct Queue
{
string queue[MAX];
int low;
int top;
}queue;
queue s1,s2;
char start[12];
char goal[9]={"123456789"};
char flag[MAX];
char step[2][MAX];
int xd[4]={0,-1,0,1};
int yd[4]={1,0,-1,0};
int fact[9]={1,1,2,6,24,120,720,5040,40320};
int cantor(char *s){
int i,j,count;
int sum=0;
for(i=0;i<8;i++)
{
count=0;
for(j=i+1;j<=8;j++)
if(s[i]>s[j])
count++;
sum+=fact[8-i]*count;
}
return sum;
}
int judge(int x,int y)
{
if(x>=0&&x<3&&y>=0&&y<3)
return 1;
return 0;
}
int ckey(char *s){
int i,j;
int sum=0,count;
for(i=8;i>=0;i--)
{
count=0;
if(s[i]=='9')
continue;
for(j=i-1;j>=0;j--)
{
if(s[j]=='9')
continue;
if(s[i]<s[j])
count++;
}
sum+=count;
}
if(sum%2)
return 0;
return 1;
}
int BFS()
{
string t1,t2;
int i;
while(s1.low<s1.top&&s2.low<s2.top)
{
t1=s1.queue[++s1.low];
for(i=0;i<4;i++)
{
if(judge(t1.x+xd[i],t1.y+yd[i]))
{
int currentstate,z,nz;
char t[12];
strcpy(t,t1.string);
z=3*t1.x+t1.y;
nz=3*(t1.x+xd[i])+t1.y+yd[i];
t[z]^=t[nz];
t[nz]^=t[z];
t[z]^=t[nz];
currentstate=cantor(t);
if(!flag[currentstate])
{
flag[currentstate]=1;
step[0][currentstate]=step[0][t1.state]+1;
strcpy(s1.queue[++s1.top].string,t);
s1.queue[s1.top].x=t1.x+xd[i];
s1.queue[s1.top].y=t1.y+yd[i];
s1.queue[s1.top].state=currentstate;
}
else if(flag[currentstate]==2)
return step[0][t1.state]+step[1][currentstate]+1;
}
}
t2=s2.queue[++s2.low];
for(i=0;i<4;i++)
{
if(judge(t2.x+xd[i],t2.y+yd[i]))
{

int currentstate,z,nz;
char t[12];
strcpy(t,t2.string);
z=3*t2.x+t2.y;
nz=3*(t2.x+xd[i])+t2.y+yd[i];
t[z]^=t[nz];
t[nz]^=t[z];
t[z]^=t[nz];
currentstate=cantor(t);
if(!flag[currentstate])
{
flag[currentstate]=2;
strcpy(s2.queue[++s2.top].string,t);
step[1][currentstate]=step[1][t2.state]+1;
s2.queue[s2.top].x=t2.x+xd[i];
s2.queue[s2.top].y=t2.y+yd[i];
s2.queue[s2.top].state=currentstate;
}
if(flag[currentstate]==1)
return step[1][t2.state]+step[0][currentstate]+1;
}
}

}
return -1;
}
void main()
{
int i,result;
char hb[9];
while(scanf("%s",hb)!=EOF)
{
result=0;
start[0]=hb[0];
for(i=1;i<9;i++)
{
scanf("%s",hb);
start[i]=hb[0];
}
for(i=0;i<9;i++)
if(start[i]=='x')
{
start[i]='9';
break;
}
if(!ckey(start))
result=-1;
else if(!strcmp(start,goal))
result=0;
else
{
memset(flag,0,sizeof(flag));
memset(step,0,sizeof(step));
s1.low=s1.top=-1;
s2.low=s2.top=-1;
flag[s1.queue[++s1.top].state=cantor(start)]=1;
flag[s2.queue[++s2.top].state=cantor(goal)]=2;
strcpy(s1.queue[s1.top].string,start);
strcpy(s2.queue[s2.top].string,goal);
s1.queue[s1.top].x=i/3;
s1.queue[s1.top].y=i%3;
s2.queue[s2.top].x=2;
s2.queue[s2.top].y=2;
result=BFS();
}
if(result<0)
printf("unsolvable\n");
else printf("%d\n",result);
}
}
这是经典的八数码问题的求解。。。双向BFS,。。。问题是给出一个初始状态。。求到达目标状态最小需要多少步。。。。其中目标状态固定。。。为1 2 3 4 5 6 7 8 x。。。x代表空格。。这九个数代表3*3的矩阵。。说白了就是小时候玩的拼图游戏。。。我把注释都消了。。。你慢慢看。。。。

③ c语言程序设计实例

// 电话本Dlg.cpp : 实现文件
//

#include stdafx.h
#include 电话本.h
#include 电话本Dlg.h

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// C电话本Dlg 对话框

C电话本Dlg::C电话本Dlg(CWnd* pParent /*=NULL*/)
: CDialog(C电话本Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);
}

void C电话本Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(C电话本Dlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT1, C电话本Dlg::OnName)
ON_EN_CHANGE(IDC_EDIT2, C电话本Dlg::OnPhoto)
ON_EN_CHANGE(IDC_EDIT3, C电话本Dlg::OnAddress)
ON_EN_CHANGE(IDC_EDIT4, C电话本Dlg::OnShow)
ON_BN_CLICKED(IDC_BUTTON1, C电话本Dlg::OnFill)
ON_BN_CLICKED(IDC_BUTTON2, C电话本Dlg::OnInsert)
ON_BN_CLICKED(IDC_BUTTON3, C电话本Dlg::OnFind)
ON_BN_CLICKED(IDC_BUTTON4, C电话本Dlg::OnDelete)
ON_BN_CLICKED(IDC_BUTTON5, C电话本Dlg::OnEdit)
ON_BN_CLICKED(IDC_BUTTON7, C电话本Dlg::OnSave)
END_MESSAGE_MAP()

// C电话本Dlg 消息处理程序

BOOL C电话本Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标

// TODO: 在此添加额外的初始化代码
ifstream InFile(Wab.fsd,ios::in);
listWabList::iterator ItWab;
char Tmp1[8],Tmp2[16],Tmp3[32];
Wab.clear();
while(InFile Tmp1 Tmp2 Tmp3)
{
OutWab.Name=Tmp1;
OutWab.Photo=Tmp2;
OutWab.Address=Tmp3;
Wab.push_front(OutWab);
}
C电话本Dlg::OnFill();

return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。

void C电话本Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

SendMessage(WM_ICONERASEBKGND, reinterpret_castWPARAM(dc.GetSafeHdc()), 0);

// 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(rect);
int x = (rect.Width() - cxIcon 1) / 2;
int y = (rect.Height() - cyIcon 1) / 2;

// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR C电话本Dlg::OnQueryDragIcon()
{
return static_castHCURSOR(m_hIcon);
}

void C电话本Dlg::OnName()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT1);
pMaxLen-SetLimitText(6);
GetDlgItemText(IDC_EDIT1,InWab.Name);
}

void C电话本Dlg::OnPhoto()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT2);
pMaxLen-SetLimitText(11);
GetDlgItemText(IDC_EDIT2,InWab.Photo);
}

void C电话本Dlg::OnAddress()
{
CEdit *pMaxLen=(CEdit*)GetDlgItem(IDC_EDIT3);
pMaxLen-SetLimitText(30);
GetDlgItemText(IDC_EDIT3,InWab.Address);
}

void C电话本Dlg::OnShow()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

// TODO: 在此添加控件通知处理程序代码
}

void C电话本Dlg::OnFill()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
OutWab=*ItWab;
OutTemp=OutWab.Name Space OutWab.Photo Space OutWab.Address;
OutAll=OutAll OutTemp Enter;
}
SetDlgItemText(IDC_EDIT4,OutAll);
}

void C电话本Dlg::OnInsert()
{
Wab.push_front(InWab);
C电话本Dlg::OnFill();
}

void C电话本Dlg::OnFind()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
}

void C电话本Dlg::OnDelete()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
break;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
if(ItWab!=Wab.end()) Wab.erase(ItWab);
}

void C电话本Dlg::OnEdit()
{
CString OutAll,OutTemp,Space,Enter;
Space=_T( );
Enter=_T(\r\n);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
if(ItWab-Name==InWab.Name)
{
OutTemp=ItWab-Name Space ItWab-Photo Space ItWab-Address;
OutAll=OutAll OutTemp Enter;
break;
}
}
SetDlgItemText(IDC_EDIT4,OutAll);
if(ItWab!=Wab.end())
{
Wab.erase(ItWab);
Wab.push_front(InWab);
}
}

void C电话本Dlg::OnSave()
{
CString Tmp1,Tmp2,Tmp3;
ofstream OutFile(Wab.fsd,ios::out);
listWabList::iterator ItWab;
for(ItWab=Wab.begin();ItWab!=Wab.end();ItWab )
{
Tmp1=ItWab-Name;
Tmp2=ItWab-Photo;
Tmp3=ItWab-Address;
OutFile Tmp1 ' ' Tmp2 ' ' Tmp3 endl;
}
}
只是个用C语言编写的电话本程序 很有实用意义

④ 求c语言程序设计实例80到100行

#include <stdio.h> /*头函数*/
int main(void) /*主函数*/
{
char c; /*指定c成为字符变量*/
printf("请输入0到9的任意一个数字\n"); /*输出请输入0到9的任意一个数字*/
c=getchar(); /*输入一个字符*/
while(c>=48&&c<=57) /*c的取值范围*/
{
getchar();
switch(c) /*根据c的值转换*/
{
case '0': printf("你喜欢奋斗吗?\n"); /*假如c=0,输出你喜欢奋斗吗?*/
break; /*中断跳出,执行c=getchar()*/
case '1': printf("你喜欢当模特吗?\n"); /*假如c=1,输出你喜欢当模特吗?*/
break; /*中断跳出,执行c=getchar()*/
case '2': printf("你喜欢和谐吗?\n"); /*假如c=2,输出你喜欢和谐吗?*/
break; /*中断跳出,执行c=getchar()*/
case '3': printf("你喜欢道家吗?\n"); /*假如c=3,输出你喜欢道家吗?*/
break; /*中断跳出,执行c=getchar()*/
case '4': printf("你是广东人吗?\n"); /*假如c=4,输出你是广东人吗?*/
break; /*中断跳出,执行c=getchar()*/
case '5': printf("你喜欢武术吗?\n"); /*假如c=5,输出你喜欢武术吗?*/
break; /*中断跳出,执行c=getchar()*/
case '6': printf("祝你一帆风顺!\n"); /*假如c=6,输出祝你一帆风顺!*/
break; /*中断跳出,执行c=getchar()*/
case '7': printf("观察等待好机会!\n"); /*假如c=7,输出观察等待好机会!*/
break; /*中断跳出,执行c=getchar()*/
case '8': printf("你喜欢交际吗?\n"); /*假如c=8,输出你喜欢交际吗?*/
break; /*中断跳出,执行c=getchar()*/
case '9': printf("追求完美吧!\n"); /*假如c=9,输出追求完美吧!*/
break; /*中断跳出,执行c=getchar()*/
}
c=getchar(); /*再输入一个字符*/
}
return 0; /*返回值*/
}

⑤ c语言程序设计例题

题目1
#include "stdio.h"
void main(void)
{
int n=5,m=10,i=1;
long sum=1;
for(;i<=n;i++)
{
sum*=i;
}
printf("\n5!=%d",sum);
for(i=1;i<=10;i++)
{
sum*=i;
}
printf("\n10!=%d",sum);
}
题目2
#include "stdio.h"
#include "string.h"
struct Student
{
char s_Name[25];
long n_Code;
int n_English;
int n_Math;
int n_Computer;
}student_1,student_2;
void main(void)
{
printf("\nStudent1:\nName:");
scanf("%s",&student_1.s_Name);
printf("StudentNum:");
scanf("%d",&student_1.n_Code);
printf("English Score:");
scanf("%d",&student_1.n_English);
printf("Math Score:");
scanf("%d",&student_1.n_Math);
printf("Computer Score:");
scanf("%d",&student_1.n_Computer);

printf("\nStudent2:\nName:");
scanf("%s",&student_2.s_Name);
printf("StudentNum:");
scanf("%d",&student_2.n_Code);
printf("English Score:");
scanf("%d",&student_2.n_English);
printf("Math Score:");
scanf("%d",&student_2.n_Math);
printf("Computer Score:");
scanf("%d",&student_2.n_Computer);

printf("\nStudent1:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n",student_1.s_Name,student_1.n_Code,student_1.n_English,student_1.n_Math,student_1.n_Computer);
printf("\nStudent2:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n",student_2.s_Name,student_2.n_Code,student_2.n_English,student_2.n_Math,student_2.n_Computer);

}

⑥ 经典C语言程序例子

题目01:在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,空格用来分隔不同的单词。

(6)c语言程序设计实例扩展阅读:

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

⑦ 求C语言程序设计实例(200行)

时间函数举例程序分析
2.程序源代码:

#include "stdio.h"
#include "time.h"
void main()
{
time_t lt; /*define a longint time varible*/
lt=time(NULL);/*system time and date*/
printf(ctime(<)); /*english format output*/
printf(asctime(localtime(<)));/*tranfer to tm*/
printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
}
【程序92】

题目:时间函数举例2

1.程序分析:

2.程序源代码:

/*calculate time*/
#include "time.h"
#include "stdio.h"
main()
{
time_t start,end;
int i;
start=time(NULL);
for(i=0;i<3000;i++)
{
printf("\1\1\1\1\1\1\1\1\1\1\n");
}
end=time(NULL);
printf("\1: The different is %6.3f\n",difftime(end,start));
}
【程序93】

题目:时间函数举例3

1.程序分析:

2.程序源代码:

/*calculate time*/
#include "time.h"
#include "stdio.h"
main()
{
clock_t start,end;
int i;
double var;
start=clock();
for(i=0;i<10000;i++)
{
printf("\1\1\1\1\1\1\1\1\1\1\n");
}
end=clock();
printf("\1: The different is %6.3f\n",(double)(end-start));
}
【程序94】

题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)

1.程序分析:

2.程序源代码:

#include "time.h"
#include "stdlib.h"
#include "stdio.h"
main()
{
char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
srand(time(NULL));
printf("do you want to play it.('y' or 'n') \n");
loop:
while((c=getchar())=='y')
{
i=rand()%100;
printf("\nplease input number you guess:\n");
start=clock();
a=time(NULL);
scanf("%d",&guess);
while(guess!=i)
{
if(guess>i)
{
printf("please input a little smaller.\n");
scanf("%d",&guess);
}
else
{
printf("please input a little bigger.\n");
scanf("%d",&guess);
}
}
end=clock();
b=time(NULL);
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25)
printf("\1\1 you are normal! \1\1\n\n");
else
printf("\1\1 you are stupid! \1\1\n\n");
printf("\1\1 Congralations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
if((c=getch())=='y')
goto loop;
}
【程序95】

题目:家庭财务管理小程序

1.程序分析:

2.程序源代码:

/*money management system*/
#include "stdio.h"
#include "dos.h"
main()
{
FILE *fp;
struct date d;
float sum,chm=0.0;
int len,i,j=0;
int c;
char ch[4]="",ch1[16]="",chtime[12]="",chshop[16],chmoney[8];
pp: clrscr();
sum=0.0;
gotoxy(1,1);printf("|----------------------------------------------------|");
gotoxy(1,2);printf("| money management system(C1.0) 2000.03 |");
gotoxy(1,3);printf("|----------------------------------------------------|");
gotoxy(1,4);printf("| -- money records -- | -- today cost list -- |");
gotoxy(1,5);printf("| ------------------------ |-----------------------------|");
gotoxy(1,6);printf("| date: -------------- | |");
gotoxy(1,7);printf("| | | | |");
gotoxy(1,8);printf("| -------------- | |");
gotoxy(1,9);printf("| thgs: ------------------ | |");
gotoxy(1,10);printf("| | | | |");
gotoxy(1,11);printf("| ------------------ | |");
gotoxy(1,12);printf("| cost: ---------- | |");
gotoxy(1,13);printf("| | | | |");
gotoxy(1,14);printf("| ---------- | |");
gotoxy(1,15);printf("| | |");
gotoxy(1,16);printf("| | |");
gotoxy(1,17);printf("| | |");
gotoxy(1,18);printf("| | |");
gotoxy(1,19);printf("| | |");
gotoxy(1,20);printf("| | |");
gotoxy(1,21);printf("| | |");
gotoxy(1,22);printf("| | |");
gotoxy(1,23);printf("|--------------------------------------------------|");
i=0;
getdate(&d);
sprintf(chtime,"%4d.%02d.%02d",d.da_year,d.da_mon,d.da_day);
for(;;)
{
gotoxy(3,24);printf(" Tab __browse cost list Esc __quit");
gotoxy(13,10);printf(" ");
gotoxy(13,13);printf(" ");
gotoxy(13,7);printf("%s",chtime);
j=18;
ch[0 ]=getch();
if(ch[0]==27)
break;
strcpy (chshop,"");
strcpy(chmoney,"");
if(ch[0]==9)
{
mm:i=0;
fp=fopen("home.dat","r+");
gotoxy(3,24);printf(" ");
gotoxy(6,4);printf(" list records ");
gotoxy(1,5);printf("|-------------------------------------|");
gotoxy(41,4);printf(" ");
gotoxy(41,5);printf(" |");
while(fscanf(fp,"%10s%14s%f\n",chtime,chshop,&chm)!=EOF)
{
if(i==36)
{
getch();
i=0;
}
if ((i%36)<17)
{
gotoxy(4,6+i);
printf(" ");
gotoxy(4,6+i);
}
else
if((i%36)>16)
{
gotoxy(41,4+i-17);
printf(" ");
gotoxy(42,4+i-17);
}
i++;
sum=sum+chm;
printf("%10s %-14s %6.1f\n",chtime,chshop,chm);}
gotoxy(1,23);printf("|----------------------------------------------|");
gotoxy(1,24);printf("| |");
gotoxy(1,25);printf("|----------------------------------------------|");
gotoxy(10,24);printf("total is %8.1f$",sum);
fclose(fp);
gotoxy(49,24);printf("press any key to.....");getch();goto pp;
}
else
{
while(ch[0]!='\r')
{
if(j<10)
{
strncat(chtime,ch,1);
j++;
}
if(ch[0]==8)
{
len=strlen(chtime)-1;
if(j>15)
{
len=len+1;
j=11;
}
strcpy(ch1,"");
j=j-2;
strncat(ch1,chtime,len);
strcpy(chtime,"");
strncat(chtime,ch1,len-1);
gotoxy(13,7);printf(" ");
}
gotoxy(13,7);printf("%s",chtime);ch[0]=getch();
if(ch[0]==9)
goto mm;
if(ch[0]==27)
exit(1);
}
gotoxy(3,24);printf(" ");
gotoxy(13,10);
j=0;
ch[0]=getch();
while(ch[0]!='\r')
{
if (j<14)
{
strncat(chshop,ch,1);
j++;
}
if(ch[0]==8)
{
len=strlen(chshop)-1;
strcpy(ch1,"");
j=j-2;
strncat(ch1,chshop,len);
strcpy(chshop,"");
strncat(chshop,ch1,len-1);
gotoxy(13,10);printf(" ");
}
gotoxy(13,10);printf("%s",chshop);ch[0]=getch();}
gotoxy(13,13);
j=0;
ch[0]=getch();
while(ch[0]!='\r')
{
if (j<6)
{
strncat(chmoney,ch,1);
j++;
}
if(ch[0]==8)
{
len=strlen(chmoney)-1;
strcpy(ch1,"");
j=j-2;
strncat(ch1,chmoney,len);
strcpy(chmoney,"");
strncat(chmoney,ch1,len-1);
gotoxy(13,13);printf(" ");
}
gotoxy(13,13);printf("%s",chmoney);ch[0]=getch();
}
if((strlen(chshop)==0)||(strlen(chmoney)==0))
continue;
if((fp=fopen("home.dat","a+"))!=NULL);
fprintf(fp,"%10s%14s%6s",chtime,chshop,chmoney);
fputc('\n',fp);
fclose(fp);
i++;
gotoxy(41,5+i);
printf("%10s %-14s %-6s",chtime,chshop,chmoney);
}
}
}

【程序96】

题目:计算字符串中子串出现的次数

1.程序分析:

2.程序源代码:

#include "string.h"
#include "stdio.h"
main()
{
char str1[20],str2[20],*p1,*p2;
int sum=0;
printf("please input two strings\n");
scanf("%s%s",str1,str2);
p1=str1;p2=str2;
while(*p1!='\0')
{
if(*p1==*p2)
{
while(*p1==*p2&&*p2!='\0')
{
p1++;
p2++;
}
}
else
p1++ ;
if(*p2=='\0')
sum++;
p2=str2;
}
printf("%d",sum);
getch();
}
【程序97】

题目:从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个#为止。

1.程序分析:

2.程序源代码:

#include "stdio.h"
main()
{
FILE *fp;
char ch,filename[10];
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=getchar();
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);putchar(ch);
ch=getchar();
}
fclose(fp);
}

【程序98】

题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存。输入的字符串以!结束。

1.程序分析:

2.程序源代码:

#include "stdio.h"
main()
{
FILE *fp;
char str[100],filename[10];
int i=0;
if((fp=fopen("test","w"))==NULL)
{
printf("cannot open the file\n");
exit(0);
}
printf("please input a string:\n");
gets(str);
while(str[i]!='!')
{
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
fputc(str[i],fp);
i++;
}
fclose(fp);
fp=fopen("test","r");
fgets(str,strlen(str)+1,fp);
printf("%s\n",str);
fclose(fp);
}
【程序99】

题目:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),
输出到一个新文件C中。

1.程序分析:

2.程序源代码:

#include "stdio.h"
main()
{
FILE *fp;
int i,j,n,ni;
char c[160],t,ch;
if((fp=fopen("A","r"))==NULL)
{
printf("file A cannot be opened\n");
exit(0);
}
printf("\n A contents are :\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
ni=i;
if((fp=fopen("B","r"))==NULL)
{
printf("file B cannot be opened\n");
exit(0);
}
printf("\n B contents are :\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
n=i;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(c[i]>c[j])
{
t=c[i];c[i]=c[j];c[j]=t;
}
printf("\n C file is:\n");
fp=fopen("C","w");
for(i=0;i<n;i++)
{
putc(c[i],fp);
putchar(c[i]);
}
fclose(fp);
}
【程序100】

题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。

1.程序分析:

2.程序源代码:

#include "stdio.h"
struct student
{
char num[6];
char name[8];
int score[3];
float avr;
} stu[5];
main()
{
int i,j,sum;
FILE *fp;
/*input*/
for(i=0;i<5;i++)
{
printf("\n please input No. %d score:\n",i);
printf("stuNo:");
scanf("%s",stu[i].num);
printf("name:");
scanf("%s",stu[i].name);
sum=0;
for(j=0;j<3;j++)
{
printf("score %d.",j+1);
scanf("%d",&stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].avr=sum/3.0;
}
fp=fopen("stud","w");
for(i=0;i<5;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}

⑧ C语言程序设计,比较数字大小的实例

下面是一个比较三个数的例子:

#include <stdio.h>
int main()
{
int t,a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a<b)
{
t=a,a=b,b=t;
}
if(a<c)
{
t=a,a=c,c=t;
}
if(b<c)
{
t=b, b=c, c=t;
}
printf("%d %d %d\n",c,b,a);
return 0;
}
原理就是运用冒泡算法,把最大的数浮在最上面,而小的数就下沉,最后就输出。

⑨ C语言程序设计实例

你去如鹏网看“C语言也能干大事吧”,很不错,免费的。

热点内容
yy图片文件夹 发布:2025-01-15 08:01:00 浏览:841
可缓存影视 发布:2025-01-15 07:42:50 浏览:799
php函数默认值 发布:2025-01-15 07:34:31 浏览:238
编译应用后apk无法打开 发布:2025-01-15 07:33:45 浏览:438
lc脚本编辑器 发布:2025-01-15 07:18:59 浏览:529
追剧脚本 发布:2025-01-15 07:00:39 浏览:446
c语言字符串库函数 发布:2025-01-15 06:54:49 浏览:526
c语言的工作 发布:2025-01-15 06:50:50 浏览:522
口语交际访问 发布:2025-01-15 06:44:13 浏览:329
编程少儿学习 发布:2025-01-15 06:39:03 浏览:504