當前位置:首頁 » 編程語言 » 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