typedef struct product{
const char * name;
float price;
struct product *next;
} product;
void printLinkedList(product *pProduct){
while(pProduct != NULL){
printf("A %s costs %.2f\n\n", (*pProduct).name, pProduct->price);
pProduct = pProduct->next;
}
}
int main()
{
product tomato = {"Tomato", .51, NULL};
product potato = {"Potato", 1.21, NULL};
product lemon = {"Lemon", 1.69, NULL};
product milk = {"Milk", 3.09, NULL};
product turkey = {"Turkey", 1.86, NULL};
tomato.next = &potato;
potato.next = &lemon;
lemon.next = &milk;
milk.next = &turkey;
product apple = {"Apple", 1.58, NULL};
lemon.next = &apple;
apple.next = &milk;
printLinkedList(&tomato);
return 0;
}
Tutorial 7 Strings and Functions
void makeLower(char * theString){
int i = 0;
while(theString[i]){
theString[i]= tolower(theString[i]);
i++;
}
}
int main()
{
_Bool isaNum = 1;
int num;
int sumofnum=0;
do{
printf("Enter a number:");
isaNum = (scanf("%d", &num)==1);
if(isaNum) sumofnum += num;
} while( isaNum );
printf("The Sum is: %d\n\n", sumofnum);
char theChar;
while((theChar = getchar())!='~'){
putchar(theChar);
}
char name[50];
printf("What is your name?");
fgets(name, 50, stdin);
fputs("Hi ", stdout);
fputs(name, stdout);
return 0;
}
Tutorial 8 Malloc()
struct product{
float price;
char productname[20];
};
int main(){
struct product * pProducts;
int i, j;
int numofPro;
printf("Enter the number of products to store:");
scanf("%d", &numofPro);
pProducts = (struct product*) malloc(numofPro *sizeof(struct product));
for(i = 0; i<numofPro; i++){
printf("Enter a product name:");
scanf("%s", &(pProducts+i)->productname);
printf("Enter a product price:");
scanf("%f", &(pProducts+i)->price);
}
printf("Products Stored\n");
for(i = 0; i<numofPro; ++i){
printf("%s\t%.2f\n", (pProducts+i)->productname, (pProducts+i)->price);
}
free(pProducts);
return 0;
}
No comments:
Post a Comment