當前位置:編程學習大全網 - 編程語言 - 基礎編程題

基礎編程題

LZ想要的是這種答案吧。。。。

//-------------------------------第壹題

#include <stdio.h>

#include "e:\myc\zylib\zylib.h"

STRING GetString(STRING prompt);

double GetReal(STRING prompt);

int main()

{

double bookprice;

STRING bookname;

bookname=GetString("請輸入字符串:");

bookprice=GetReal("請輸入實數:");

printf("字符串為:%s\n",bookname);

printf("實數為:%.2f\n",bookprice);

}

STRING GetString(STRING prompt)

{

STRING name;

printf("%s",prompt);

name=GetStringFromKeyboard();

return name;

}

double GetReal(STRING prompt)

{

double price;

printf("%s",prompt);

price=GetRealFromKeyboard();

return price;

}

//-------------------------------------第二題

#include <stdio.h>

#include "e:\myc\zylib\zylib.h"

BOOL IsPrime(int n);

int main()

{

int n;

printf("請輸入壹個整數:");

scanf("%d",&n);

if(n>2)

if(IsPrime(n))printf("%d是素數\n",n);

else printf("%d不是素數\n",n);

else printf("數據非法\n");

return 0;

}

BOOL IsPrime(int n)

{

int i;

for(i=2;i<n;i++)

if(n%i= =0) return FALSE;

return TRUE;

}

//--------------------------------第三題

#include <stdio.h>

#define TRUE 1

int gcd(int x,int y);

int main()

{

int m,n,max;

printf("請輸入兩個正整數:");

scanf("%d %d",&m,&n);

max=gcd(m,n);

printf("最大公約數為:%d\n",max);

return 0;

}

int gcd(int x,int y)

{

int r;

while(TRUE)

{

r=x%y;

if(r==0)break;

x=y;

y=r;

}

return y;

}

//--------------------------------第四題

#include <stdio.h>

#include "e:\myc\zylib\zylib.h"

typedef enum{sun,mon,tue,thi,wen,fri,sat}WEEKDAY;//定義枚舉類型

int GetInteger(STRING prompt);//輸入壹下整數

int Count(int year,int month);//計算某年某月之前到2007年1月1日的天數

BOOL IsLeapYear(int n);//判斷某年是否是閏年

int month_day(int year,int month);//計算某個月的天數

void print(int year,int month,int total);//打印某年某月的日歷

void print1(WEEKDAY weekday);//打印某月的第1天

int main()

{

int year,month,total;

year=GetInteger("please input year:");

if(year<2007)

PrintErrorMessage(FALSE,"年份小於2007,錯誤\n");

month=GetInteger("please input month:");

total=Count(year,month);

print(year,month,total);

}

int GetInteger(STRING prompt)

{

int t;

printf("%s",prompt);

t=GetIntegerFromKeyboard();

return t;

}

int Count(int year,int month)

{

int s,i;

s=0;

for(i=2007;i<year;i++)

if(IsLeapYear(i))s+=366;

else s+=365;

for(i=1;i<month;i++)

s+=month_day(year,i);

return s;

}

BOOL IsLeapYear(int n)

{

return n%4==0&&n%100!=0||n%400==0;

}

int month_day(int year,int month)

{

int day;

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 9:

case 10:

case 12:day=31;break;

case 2:day=28+IsLeapYear(year);break;

default:day=30;

}

return day;

}

void print(int year,int month,int total)

{

WEEKDAY weekday;

const WEEKDAY first=mon;

int i,day;

printf("%d-%d canlendar\n",year,month);

printf("-----------------------------------\n");

printf(" sun mon tue thi wen fri sat\n");

printf("-----------------------------------\n");

day=month_day(year,month);

for(i=1;i<=day;i++)

{

weekday=(WEEKDAY)((total+i+first-1)%7);

if(i==1)print1(weekday);

else if(weekday==sat)

printf("%4d\n",i);

else printf("%4d",i);

}

printf("\n------------------------------------\n");

}

void print1(WEEKDAY weekday)

{

if(weekday==0)printf("%4d",1);

else if(weekday==1)printf("%8d",1);

else if(weekday==2)printf("%12d",1);

else if(weekday==3)printf("%16d",1);

else if(weekday==4)printf("%20d",1);

else if(weekday==5)printf("%24d",1);

else if(weekday==6)printf("%28d\n",1);

}

//---------------------------------------

上面的壹些文件路徑妳自己改了,唉,其實我自己給妳寫的那些算法更好,。

  • 上一篇:管理模塊的這個錯誤日誌是啥意思?應該怎麽解決
  • 下一篇:python裝飾器是什麽意思
  • copyright 2024編程學習大全網