1.
2.
#include <stdio.h>
struct {
char *name;
double price;
double rate;
}stock;
int main(){
for(int i=0;i<5;i++){
char name[100];
double price,rate;
printf("enter a stock name:");
scanf("%s",name);
//char *p = gets(name);
printf("enter a series of numbers:");
scanf("%lf %lf",&price,&rate);
stock.name = name;
stock.price = price;
stock.rate = rate;
double income = stock.rate*stock.price;
printf("stock name is %s,the estimated price is %.2lf\n",stock.name,income);
}
return 0;
}
3.
#include <stdio.h>
struct {
int hour;
int minute;
} time;
int main(){
printf("enter a time:");
int hour,minute;
scanf("%d:%d",&hour,&minute);
if(minute==59){
minute = 00;
if(hour==23){
hour = 00;
}else{
hour +=1;
}
}else{
minute +=1;
}
time.hour = hour;
time.minute = minute;
printf("%02d:%02d",time.hour,time.minute);
return 0;
}
2.
#include <stdio.h>
struct car {
int license;
int distance;
int gallon;
};
int main(){
struct car part[5];
int license,distance,gallon;
double sum_dis,sum_gal;
for(int i=0;i<5;i++){
printf("enter three numbers:");
scanf("%d %d %d",&license,&distance,&gallon);
part[i].license = license;
part[i].distance = distance;
part[i].gallon = gallon;
sum_dis +=distance;
sum_gal +=gallon;
}
printf("汽车车牌号 行驶的英里 消耗的加仑数\n");
for(int i=0;i<5;i++){
printf("%d\t\t %d\t\t %d\t\t\n",part[i].license,part[i].distance,part[i].gallon);
}
printf("miles per gallon on average:%.2lf",sum_dis/sum_gal);
return 0;
}
3.
1.
#include <stdio.h>
struct Date{
int month;
int day;
int year;
};
int days(struct Date);
int main(){
int month,day,year;
printf("enter three numbers:");
scanf("%d %d %d",&month,&day,&year);
struct Date date = {month,day,year};
printf("the days is %d",days(date));
return 0;
}
int days(struct Date date){
int time = (date.year-1900)*360+(date.month-1)*30+date.day;
return time;
}
2.
#include <stdio.h>
#include <math.h>
struct Date{
int month;
int day;
int year;
};
int days(struct Date);
int difdays(struct Date date1,struct Date date2);
int main(){
int month,day,year;
printf("enter three numbers:");
scanf("%d %d %d",&month,&day,&year);
printf("enter three numbers:");
struct Date date1 = {month,day,year};
scanf("%d %d %d",&month,&day,&year);
struct Date date2 = {month,day,year};
printf("the difdays is %d",difdays(date1,date2));
return 0;
}
int days(struct Date date){
int time = (date.year-1900)*360+(date.month-1)*30+date.day;
return time;
}
int difdays(struct Date date1,struct Date date2){
int difdays = abs(days(date1) - days(date2));
return difdays;
}
4.
#include <stdio.h>
struct Date{
int month;
int day;
int year;
};
struct Date larger(struct Date,struct Date);
int main(){
int month1,day1,year1;
int month2,day2,year2;
printf("enter two groups of numbers:");
scanf("%d/%d/%d",&month1,&day1,&year1);
scanf("%d/%d/%d",&month2,&day2,&year2);
struct Date date1 = {month1,day1,year1};
struct Date date2 = {month2,day2,year2};
printf("return date is %d/%d/%d",larger(date1,date2).month,larger(date1,date2).day,larger(date1,date2).year);
}
struct Date larger(struct Date date1,struct Date date2){
if(date1.year>date2.year){
return date1;
}else if(date1.year<date2.year){
return date2;
}else{
if(date1.month>date2.month){
return date1;
}else if(date1.month<date2.month){
return date2;
}else{
if(date1.day>date2.day){
return date1;
}else{
return date2;
}
}
}
}
4.
8.
(a)
#include <stdio.h>
struct color
{
int red;
int green;
int blue;
}rgb={255,0,255};
int main(){
//const struct color MAGENTA={.red=255,.blue=255};
//printf("%d %d %d",MAGENTA.blue,MAGENTA.green,MAGENTA.blue);
printf("%d %d %d",rgb.red,rgb.green,rgb.blue);
return 0;
}
(b)
#include <stdio.h>
struct color
{
int red;
int green;
int blue;
};
int main(){
const struct color MAGENTA={.red=255,.blue=255};
printf("%d %d %d",MAGENTA.blue,MAGENTA.green,MAGENTA.blue);
return 0;
}
9.
(a)
struct color make_color(int red,int green,int blue){
struct color rgb = {red,green,blue};
if(rgb.red<0){
rgb.red = 0;
}else if(rgb.red>255){
rgb.red = 255;
}
if(rgb.green<0){
rgb.green = 0;
}else if(rgb.green>255){
rgb.green = 255;
}
if(rgb.blue<0){
rgb.blue = 0;
}else if(rgb.blue>255){
rgb.blue = 255;
}
return rgb;
};
(b)
int getRed(struct color c){
return c.red;
}
(c )
bool equal_color(struct color color1,struct color color2){
if((color1.red==color2.red)&&(color1.green==color2.green)&&(color1.blue==color2.blue)){
return 1;
}
}
(d)
struct color brighter(struct color c){
if(c.blue==c.red==c.green==0){
struct color cbr = {0,0,0};
}else if(c.red>0&&c.red<3){
c.red = 3;
}else if(c.green>0&&c.green<3){
c.green = 3;
}else if(c.blue>0&&c.blue<3){
c.blue = 3;
}
struct color cbr = {(int)c.red/0.7,(int)c.green/0.7,(int)c.blue/0.7};
if(cbr.red>255){
cbr.red = 255;
}else if(cbr.green>255){
cbr.green = 255;
}else if(cbr.blue>255){
cbr.blue = 255;
}
}
(e)
struct color darker(struct color c){
struct color cda ={(int)0.7*c.red,(int)0.7*c.blue,(int)0.7*c.blue};
}
10.
//返回r的面积
int area(struct rectangle r){
int area = (r.lower_right.x - r.upper_left.x)*(r.upper_left.y - r.lower_right.y);
return area;
}
//返回r的中心
struct point core(struct rectangle r){
struct point p;
struct point core ={(int)(r.lower_right.x+r.upper_left.x)/2,(int)(r.upper_left.y+r.lower_right.y)/2};
return core;
}
//返回移动之后的r
struct rectangle shift(struct rectangle r,int x,int y){
r.lower_right.x +=x;
r.lower_right.y +=y;
r.upper_left.x +=x;
r.upper_left.y +=y;
return r;
}
//判断点p是否在r内部
bool compare(struct rectangle r,struct point p){
if((r.upper_left.x<=p.x&&r.upper_left.y>=p.y)&&(r.lower_right.x>=p.x&&r.lower_right.y<=p.y)){
return true;
}else{
return false;
}
}
14.
//(a)
double area(struct shape s)
{
if (s.shape_kind == RECTANGLE)
return s.u.rectangle.height * s.u.rectangle.width;
else
return 3.14159 * s.u.circle.radius * s.u.circle.radius;
}
//(b)
struct shape move(struct shape s, int x, int y){
struct shape new_shape = s;
new_shape.center.x += x;
new_shape.center.y += y;
return new_shape;
}
//(c)
struct shape scale(struct shape s, double c){
struct shape new_shape = s;
if(new_shape.shape_kind == RECTANGLE){
new_shape.u.rectangle.height *= c;
new_shape.u.rectangle.width *= c;
}else{
new_shape.u.circle.radius *= c;
return new_shape;
}
5.
1.
#include <stdio.h>
#define COUNTRY_COUNT (int)(sizeof(country_codes)/sizeof(country_codes[0]))
struct dialing_code{
char *country;
int code;
};
const struct dialing_code country_codes[] ={
{"Argentina", 54}, {"Bangladesh", 880},
{"Brazil", 55}, {"Burma (Myanmar)", 95},
{"China", 86}, {"Colombia", 57},
{"Congo, Dem. Rep. of", 243}, {"Egypt", 20},
{"Ethiopia", 251}, {"France", 33},
{"Germany", 49}, {"India", 91},
{"Indonesia", 62}, {"Iran", 98},
{"Italy", 39}, {"Japan", 81},
{"Mexico", 52}, {"Nigeria", 234},
{"Pakistan", 92}, {"Philippines", 63},
{"Poland", 48}, {"Russia", 7},
{"South Africa", 27}, {"South Korea", 82},
{"Spain", 34}, {"Sudan", 249},
{"Thailand", 66}, {"Turkey", 90},
{"Ukraine", 380}, {"United Kingdom", 44},
{"United States", 1}, {"Vietnam", 84}
};
int main(void)
{
int code, i;
printf("Enter dialing code:");
scanf("%d", &code);
for (i = 0; i < COUNTRY_COUNT; i++){
if(code == country_codes[i].code){
printf("The country with dialing code %d is %s\n",code,country_codes[i].country);
return 0;
}
}
printf("No corresponding country found\n");
return 0;
}
5.
#include <stdio.h>
#include <math.h>
#define NUMBER 8//(int)(sizeof(flight)/sizeof(flight[0]))
struct time{
int fly;
int launch;
};
const struct time flight[] = {
{800,1016},{943,1152},
{1119,1331},{1247,1500},
{1400,1608},{1545,1755},
{1900,2120},{2145,2358}
};
int main(){
int hour,minute;
printf("Enter a 24-hour time:");
scanf("%d:%d",&hour,&minute);
int flag = 100*hour+minute;
int j,des = 100000;
for(int i=0;i<NUMBER;i++){
if(abs(flag-flight[i].fly)<des){
des = abs(flag - flight[i].fly);
printf("%d\n",des);
j = i;
}
}
printf("Closest departure time is %d:%d,arriving at %d:%d",\
flight[j].fly/100,flight[j].fly%100,flight[j].launch/100,flight[j].launch%100);
return 0;
}
网友评论